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