Repository navigation
Expand file tree
/
Copy pathconverter.go
More file actions
411 lines (358 loc) · 11.2 KB
/
Copy pathconverter.go
File metadata and controls
411 lines (358 loc) · 11.2 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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
package internal
import (
"bytes"
"fmt"
"net/url"
"path"
"path/filepath"
"strings"
"github.com/gohugoio/hugo/common/hexec"
"github.com/gohugoio/hugo/common/loggers"
"github.com/gohugoio/hugo/config/security"
"github.com/gohugoio/hugo/identity"
"github.com/gohugoio/hugo/langs"
"github.com/gohugoio/hugo/markup/asciidocext/asciidocext_config"
"github.com/gohugoio/hugo/markup/converter"
"github.com/gohugoio/hugo/markup/internal"
"github.com/gohugoio/hugo/markup/tableofcontents"
"github.com/spf13/cast"
"golang.org/x/net/html"
)
type AsciiDocConverter struct {
Ctx converter.DocumentContext
Cfg converter.ProviderConfig
}
type asciiDocResult struct {
converter.ResultRender
toc *tableofcontents.Fragments
}
type pageSubset interface {
IsPage() bool
RelPermalink() string
Section() string
}
const (
// asciiDocBinaryName is name of the AsciiDoc converter CLI.
asciiDocBinaryName = "asciidoctor"
// asciiDocDiagramExtension is the name of the AsciiDoc converter diagram
// extension.
asciiDocDiagramExtension = "asciidoctor-diagram"
// asciiDocDiagramCacheDirKey is the AsciiDoc converter attribute key for
// setting the path to the diagram cache directory.
asciiDocDiagramCacheDirKey = "diagram-cachedir"
// asciiDocDiagramCacheImagesOptionKey is the AsciiDoc converter attribute
// key for determining whether to cache image files in addition to
// metadata files.
asciiDocDiagramCacheImagesOptionKey = "diagram-cache-images-option"
// gemBinaryName is the name of the RubyGems CLI.
gemBinaryName = "gem"
// goatBinaryName is the name of the GoAT CLI.
goatBinaryName = "goat"
)
func (r asciiDocResult) TableOfContents() *tableofcontents.Fragments {
return r.toc
}
func (a *AsciiDocConverter) Convert(ctx converter.RenderContext) (converter.ResultRender, error) {
b, err := a.GetAsciiDocContent(ctx.Src, a.Ctx)
if err != nil {
return nil, err
}
content, toc, err := a.extractTOC(b)
if err != nil {
return nil, err
}
return asciiDocResult{
ResultRender: converter.Bytes(content),
toc: toc,
}, nil
}
func (a *AsciiDocConverter) Supports(_ identity.Identity) bool {
return false
}
// GetAsciiDocContent calls asciidoctor as an external helper to convert
// AsciiDoc content to HTML.
func (a *AsciiDocConverter) GetAsciiDocContent(src []byte, ctx converter.DocumentContext) ([]byte, error) {
if !hexec.InPath(asciiDocBinaryName) {
return nil, fmt.Errorf("asciidoctor not found in $PATH, cannot render %q", ctx.DocumentName)
}
args, err := a.ParseArgs(ctx)
if err != nil {
return nil, err
}
args = append(args, "-") // read from stdin
a.Cfg.Logger.Infof("Rendering %s using Asciidoctor args %s ...", ctx.DocumentName, args)
return internal.ExternallyRenderContent(a.Cfg, ctx, src, asciiDocBinaryName, args)
}
func (a *AsciiDocConverter) ParseArgs(ctx converter.DocumentContext) ([]string, error) {
cfg := a.Cfg.MarkupConfig().AsciiDocExt
args := []string{}
args = a.AppendArg(args, "-b", cfg.Backend, asciidocext_config.CliDefault.Backend, asciidocext_config.AllowedBackend)
for _, extension := range cfg.Extensions {
if strings.LastIndexAny(extension, `\/.`) > -1 {
a.Cfg.Logger.Errorf(
"The %q Asciidoctor extension is unsupported and ignored. Only installed Asciidoctor extensions are allowed.",
extension,
)
continue
}
args = append(args, "-r", extension)
if extension == asciiDocDiagramExtension {
cacheDir := filepath.Clean(filepath.Join(a.Cfg.Conf.CacheDirMisc(), asciiDocDiagramExtension))
args = append(args, "-a", asciiDocDiagramCacheDirKey+"="+cacheDir)
args = append(args, "-a", asciiDocDiagramCacheImagesOptionKey)
}
}
for attributeKey, attributeValue := range cfg.Attributes {
if asciidocext_config.DisallowedAttributes[attributeKey] {
a.Cfg.Logger.Errorf(
"The %q Asciidoctor attribute is unsupported and ignored.",
attributeKey,
)
continue
}
if attributeKey == asciiDocDiagramCacheImagesOptionKey {
a.Cfg.Logger.Warnf(
"The %q Asciidoctor attribute is fixed and cannot be modified. To disable caching of both image and metadata files, set markup.asciidocext.attributes.diagram-nocache-option to true in your project configuration.",
attributeKey,
)
continue
}
if attributeKey == asciiDocDiagramCacheDirKey {
a.Cfg.Logger.Warnf(
"The %q Asciidoctor attribute is fixed and cannot be modified. To change the cache location, modify caches.misc.dir in your project configuration.",
attributeKey,
)
continue
}
// To set a document attribute to true: -a attributeKey
// To set a document attribute to false: -a '!attributeKey'
// For other types: -a attributeKey=attributeValue
if b, ok := attributeValue.(bool); ok {
arg := attributeKey
if !b {
arg = "'!" + attributeKey + "'"
}
args = append(args, "-a", arg)
} else {
args = append(args, "-a", attributeKey+"="+cast.ToString(attributeValue))
}
}
if cfg.WorkingFolderCurrent {
page, ok := ctx.Document.(pageSubset)
if !ok {
return nil, fmt.Errorf("expected pageSubset, got %T", ctx.Document)
}
// Derive the outdir document attribute from the relative permalink.
relPath := strings.TrimPrefix(page.RelPermalink(), a.Cfg.Conf.BaseURL().BasePathNoTrailingSlash)
relPath, err := url.PathUnescape(relPath)
if err != nil {
return nil, err
}
if a.Cfg.Conf.IsMultihost() {
// In a multi-host configuration, neither absolute nor relative
// permalinks include the language key; prepend it.
language, ok := a.Cfg.Conf.Language().(*langs.Language)
if !ok {
return nil, fmt.Errorf("expected *langs.Language, got %T", a.Cfg.Conf.Language())
}
relPath = path.Join(language.Lang, relPath)
}
if a.Cfg.Conf.IsUglyURLs(page.Section()) {
if page.IsPage() {
// Remove the extension.
relPath = strings.TrimSuffix(relPath, path.Ext(relPath))
} else {
// Remove the file name.
relPath = path.Dir(relPath)
}
// Set imagesoutdir and imagesdir attributes.
imagesoutdir, err := filepath.Abs(filepath.Join(a.Cfg.Conf.BaseConfig().PublishDir, relPath))
if err != nil {
return nil, err
}
imagesdir := filepath.Base(imagesoutdir)
if page.IsPage() {
args = append(args, "-a", "imagesoutdir="+imagesoutdir, "-a", "imagesdir="+imagesdir)
} else {
args = append(args, "-a", "imagesoutdir="+imagesoutdir)
}
}
// Prepend the publishDir.
outDir, err := filepath.Abs(filepath.Join(a.Cfg.Conf.BaseConfig().PublishDir, relPath))
if err != nil {
return nil, err
}
args = append(args, "--base-dir", filepath.Dir(ctx.Filename), "-a", "outdir="+outDir)
}
if cfg.NoHeaderOrFooter {
args = append(args, "--no-header-footer")
} else {
a.Cfg.Logger.Warnln("Asciidoctor parameter NoHeaderOrFooter is required for correct HTML rendering")
}
if cfg.SectionNumbers {
args = append(args, "--section-numbers")
}
if cfg.Verbose {
args = append(args, "--verbose")
}
if cfg.Trace {
args = append(args, "--trace")
}
args = a.AppendArg(args, "--failure-level", cfg.FailureLevel, asciidocext_config.CliDefault.FailureLevel, asciidocext_config.AllowedFailureLevel)
args = a.AppendArg(args, "--safe-mode", cfg.SafeMode, asciidocext_config.CliDefault.SafeMode, asciidocext_config.AllowedSafeMode)
return args, nil
}
func (a *AsciiDocConverter) AppendArg(args []string, option, value, defaultValue string, allowedValues map[string]bool) []string {
if value != defaultValue {
if allowedValues[value] {
args = append(args, option, value)
} else {
a.Cfg.Logger.Errorf(
"Unsupported Asciidoctor value %q for option %q was passed in and will be ignored.",
value,
option,
)
}
}
return args
}
// HasAsciiDoc reports whether the AsciiDoc converter is installed.
func HasAsciiDoc() (bool, error) {
if !hexec.InPath(asciiDocBinaryName) {
return false, fmt.Errorf("the AsciiDoc converter (%s) is not installed", asciiDocBinaryName)
}
return true, nil
}
// CanRenderGoATDiagrams reports whether the AsciiDoc converter can render
// GoAT diagrams. Only used in tests.
func CanRenderGoATDiagrams() (bool, error) {
// Verify that the AsciiDoc converter is installed.
if ok, err := HasAsciiDoc(); !ok {
return false, err
}
// Verify that the RubyGems CLI is installed.
if !hexec.InPath(gemBinaryName) {
return false, fmt.Errorf("the RubyGems CLI (%s) is not installed", gemBinaryName)
}
// Verify that the required AsciiDoc converter extension is installed.
sc := security.DefaultConfig
sc.Exec.Allow = security.MustNewWhitelist(gemBinaryName)
ex := hexec.New(sc, "", loggers.NewDefault())
args := []any{"list", asciiDocDiagramExtension, "--installed"}
cmd, err := ex.New(gemBinaryName, args...)
if err != nil {
return false, err
}
err = cmd.Run()
if err != nil {
return false, fmt.Errorf("the %s gem is not installed", asciiDocDiagramExtension)
}
// Verify that the GoAT CLI is installed.
if !hexec.InPath(goatBinaryName) {
return false, fmt.Errorf("the GoAT CLI (%s) is not installed", goatBinaryName)
}
return true, nil
}
// extractTOC extracts the toc from the given src html.
// It returns the html without the TOC, and the TOC data
func (a *AsciiDocConverter) extractTOC(src []byte) ([]byte, *tableofcontents.Fragments, error) {
var buf bytes.Buffer
buf.Write(src)
node, err := html.Parse(&buf)
if err != nil {
return nil, nil, err
}
var (
f func(*html.Node) bool
toc *tableofcontents.Fragments
toVisit []*html.Node
)
f = func(n *html.Node) bool {
if n.Type == html.ElementNode && (n.Data == "div" || n.Data == "nav") && attr(n, "id") == "toc" {
toc = parseTOC(n)
if !a.Cfg.MarkupConfig().AsciiDocExt.PreserveTOC {
n.Parent.RemoveChild(n)
}
return true
}
if n.FirstChild != nil {
toVisit = append(toVisit, n.FirstChild)
}
if n.NextSibling != nil && f(n.NextSibling) {
return true
}
for len(toVisit) > 0 {
nv := toVisit[0]
toVisit = toVisit[1:]
if f(nv) {
return true
}
}
return false
}
f(node)
if err != nil {
return nil, nil, err
}
buf.Reset()
err = html.Render(&buf, node)
if err != nil {
return nil, nil, err
}
// ltrim <html><head></head><body> and rtrim </body></html> which are added by html.Render
res := buf.Bytes()[25:]
res = res[:len(res)-14]
return res, toc, nil
}
// parseTOC returns a TOC root from the given toc Node
func parseTOC(doc *html.Node) *tableofcontents.Fragments {
var (
toc tableofcontents.Builder
f func(*html.Node, int, int)
)
f = func(n *html.Node, row, level int) {
if n.Type == html.ElementNode {
switch n.Data {
case "ul", "ol":
if level == 0 {
row++
}
level++
f(n.FirstChild, row, level)
case "li":
for c := n.FirstChild; c != nil; c = c.NextSibling {
if c.Type != html.ElementNode || c.Data != "a" {
continue
}
href := attr(c, "href")[1:]
toc.AddAt(&tableofcontents.Heading{
Title: nodeContent(c),
ID: href,
Level: level + 1,
}, row, level)
}
f(n.FirstChild, row, level)
}
}
if n.NextSibling != nil {
f(n.NextSibling, row, level)
}
}
f(doc.FirstChild, -1, 0)
return toc.Build()
}
func attr(node *html.Node, key string) string {
for _, a := range node.Attr {
if a.Key == key {
return a.Val
}
}
return ""
}
func nodeContent(node *html.Node) string {
var buf bytes.Buffer
for c := node.FirstChild; c != nil; c = c.NextSibling {
html.Render(&buf, c)
}
return buf.String()
}