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