1package main
  2
  3import (
  4	"bytes"
  5	"context"
  6	"fmt"
  7	"html/template"
  8	"os"
  9	"path/filepath"
 10	"runtime"
 11	"strings"
 12	"sync"
 13
 14	"github.com/alecthomas/chroma/v2/formatters/html"
 15	"github.com/alecthomas/chroma/v2/lexers"
 16	"github.com/alecthomas/chroma/v2/styles"
 17
 18	"github.com/antonmedv/gitmal/pkg/git"
 19	"github.com/antonmedv/gitmal/pkg/links"
 20	"github.com/antonmedv/gitmal/pkg/progress_bar"
 21	"github.com/antonmedv/gitmal/pkg/templates"
 22)
 23
 24func generateBlobs(files []git.Blob, params Params) error {
 25	// Prepare shared, read-only resources
 26	lightStyle := styles.Get(params.LightStyle)
 27	if lightStyle == nil {
 28		return fmt.Errorf("unknown style: %s", params.LightStyle)
 29	}
 30
 31	formatterOptions := []html.Option{
 32		html.WithLineNumbers(true),
 33		html.WithLinkableLineNumbers(true, "L"),
 34		html.WithClasses(true),
 35		html.WithCSSComments(false),
 36	}
 37
 38	dirsSet := links.BuildDirSet(files)
 39	filesSet := links.BuildFileSet(files)
 40
 41	// Bounded worker pool
 42	workers := runtime.NumCPU()
 43	if workers < 1 {
 44		workers = 1
 45	}
 46
 47	ctx, cancel := context.WithCancel(context.Background())
 48	defer cancel()
 49
 50	jobs := make(chan git.Blob)
 51	errCh := make(chan error, 1)
 52	var wg sync.WaitGroup
 53
 54	p := progress_bar.NewProgressBar("blobs for "+params.Ref.String(), len(files))
 55
 56	workerFn := func() {
 57		defer wg.Done()
 58
 59		// Per-worker instances
 60		md := createMarkdown(params.LightStyle)
 61		formatter := html.New(formatterOptions...)
 62
 63		check := func(err error) bool {
 64			if err != nil {
 65				select {
 66				case errCh <- err:
 67					cancel()
 68				default:
 69				}
 70				return true
 71			}
 72			return false
 73		}
 74
 75		for {
 76			select {
 77			case <-ctx.Done():
 78				return
 79			case blob, ok := <-jobs:
 80				if !ok {
 81					return
 82				}
 83				func() {
 84					var content string
 85					data, isBin, err := git.BlobContent(params.Ref, blob.Path, params.RepoDir)
 86					if check(err) {
 87						return
 88					}
 89
 90					isImg := isImage(blob.Path)
 91					if !isBin {
 92						content = string(data)
 93					}
 94
 95					// Write raw file for every blob
 96					rawPath := filepath.Join(params.OutputDir, "raw", params.Ref.DirName(), blob.Path)
 97					if err := os.MkdirAll(filepath.Dir(rawPath), 0o755); check(err) {
 98						return
 99					}
100					rf, err := os.Create(rawPath)
101					if check(err) {
102						return
103					}
104					if _, err := rf.Write(data); check(err) {
105						_ = rf.Close()
106						return
107					}
108					_ = rf.Close()
109
110					outPath := filepath.Join(params.OutputDir, "blob", params.Ref.DirName(), blob.Path) + ".html"
111					if err := os.MkdirAll(filepath.Dir(outPath), 0o755); check(err) {
112						return
113					}
114
115					f, err := os.Create(outPath)
116					if check(err) {
117						return
118					}
119					defer func() {
120						_ = f.Close()
121					}()
122
123					depth := 0
124					if strings.Contains(blob.Path, "/") {
125						depth = len(strings.Split(blob.Path, "/")) - 1
126					}
127					rootHref := strings.Repeat("../", depth+2)
128					rawHref := filepath.Join(rootHref, "raw", params.Ref.DirName(), blob.Path)
129
130					if isMarkdown(blob.Path) {
131						var b bytes.Buffer
132						if err := md.Convert([]byte(content), &b); check(err) {
133							return
134						}
135
136						contentHTML := links.Resolve(
137							b.String(),
138							blob.Path,
139							rootHref,
140							params.Ref.DirName(),
141							dirsSet,
142							filesSet,
143						)
144
145						err = templates.MarkdownTemplate.ExecuteTemplate(f, "layout.gohtml", templates.MarkdownParams{
146							LayoutParams: templates.LayoutParams{
147								Title:         fmt.Sprintf("%s/%s at %s", params.Name, blob.Path, params.Ref),
148								Name:          params.Name,
149								RootHref:      rootHref,
150								CurrentRefDir: params.Ref.DirName(),
151								Selected:      "code",
152								AssetNames:    params.AssetNames,
153							},
154							HeaderParams: templates.HeaderParams{
155								Ref:         params.Ref,
156								Breadcrumbs: breadcrumbs(params.Name, blob.Path, true),
157								RawHref:     rawHref,
158							},
159							Blob:    blob,
160							Content: template.HTML(contentHTML),
161						})
162						if check(err) {
163							return
164						}
165
166					} else {
167
168						var contentHTML template.HTML
169						if !isBin {
170							var b bytes.Buffer
171							lx := lexers.Match(blob.Path)
172							if lx == nil {
173								lx = lexers.Fallback
174							}
175							iterator, _ := lx.Tokenise(nil, content)
176							if err := formatter.Format(&b, lightStyle, iterator); check(err) {
177								return
178							}
179							contentHTML = template.HTML(b.String())
180
181						} else if isImg {
182
183							relativeRawPath := filepath.Join(rootHref, "raw", params.Ref.DirName(), blob.Path)
184							contentHTML = template.HTML(fmt.Sprintf(`<img src="%s" alt="%s" />`, relativeRawPath, blob.FileName))
185						}
186
187						err = templates.BlobTemplate.ExecuteTemplate(f, "layout.gohtml", templates.BlobParams{
188							LayoutParams: templates.LayoutParams{
189								Title:         fmt.Sprintf("%s/%s at %s", params.Name, blob.Path, params.Ref),
190								Name:          params.Name,
191								RootHref:      rootHref,
192								CurrentRefDir: params.Ref.DirName(),
193								Selected:      "code",
194								AssetNames:    params.AssetNames,
195							},
196							HeaderParams: templates.HeaderParams{
197								Ref:         params.Ref,
198								Breadcrumbs: breadcrumbs(params.Name, blob.Path, true),
199								RawHref:     rawHref,
200							},
201							Blob:     blob,
202							IsBinary: isBin,
203							IsImage:  isImg,
204							Content:  contentHTML,
205						})
206						if check(err) {
207							return
208						}
209					}
210				}()
211
212				p.Inc()
213			}
214		}
215	}
216
217	// Start workers
218	wg.Add(workers)
219	for i := 0; i < workers; i++ {
220		go workerFn()
221	}
222
223	// Feed jobs
224	go func() {
225		defer close(jobs)
226		for _, b := range files {
227			select {
228			case <-ctx.Done():
229				return
230			case jobs <- b:
231			}
232		}
233	}()
234
235	// Wait for workers
236	doneCh := make(chan struct{})
237	go func() {
238		wg.Wait()
239		close(doneCh)
240	}()
241
242	var runErr error
243	select {
244	case runErr = <-errCh:
245		// error occurred, wait workers to finish
246		<-doneCh
247	case <-doneCh:
248	}
249
250	p.Done()
251	return runErr
252}