-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcollect.go
459 lines (390 loc) · 11.3 KB
/
collect.go
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
package main
import (
"errors"
"fmt"
"io/fs"
"net/url"
"path"
"path/filepath"
"strings"
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/parser"
"go.abhg.dev/goldmark/frontmatter"
"go.abhg.dev/goldmark/toc"
"go.abhg.dev/stitchmd/internal/goldast"
"go.abhg.dev/stitchmd/internal/header"
"go.abhg.dev/stitchmd/internal/rawhtml"
"go.abhg.dev/stitchmd/internal/stitch"
"go.abhg.dev/stitchmd/internal/tree"
)
// collector loads all Markdown files in a TOC
// and builds an alternative, parsed representation of the files.
type collector struct {
Parser parser.Parser // required
FS fs.FS // required
// Paths relative to root of fs.FS,
// representing the stack of summary file embeds.
// Used to detect cycles.
Stack []string
// Directory under FS to resolve relative paths from.
// Must use '/' as the path separator.
Dir string
idGen *header.IDGen
files map[string]*markdownFileItem
}
type markdownCollection struct {
Sections []*markdownSection
// FilesByPath maps a Markdown file path to its parsed representation.
// The path is /-separated, regardless of the OS.
FilesByPath map[string]*markdownFileItem
}
func (c *collector) Collect(info goldast.Positioner, toc *stitch.Summary) (*markdownCollection, error) {
c.files = make(map[string]*markdownFileItem)
if c.idGen == nil {
c.idGen = header.NewIDGen()
}
errs := goldast.NewErrorList(info)
sections := make([]*markdownSection, len(toc.Sections))
for i, sec := range toc.Sections {
sections[i] = c.collectSection(errs, sec)
}
return &markdownCollection{
Sections: sections,
FilesByPath: c.files,
}, errs.Err()
}
type markdownSection struct {
Title *ast.Heading
TOCItems *ast.List
Items tree.List[markdownItem]
}
func (c *collector) collectSection(errs *goldast.ErrorList, sec *stitch.Section) *markdownSection {
items := tree.TransformList(sec.Items, func(cursor tree.Cursor[stitch.Item]) markdownItem {
i, err := c.collectItem(cursor)
if err != nil {
errs.Pushf(cursor.Value().Node(), "%v", err)
return nil
}
return i
})
var title *ast.Heading
if sec.Title != nil {
title = sec.Title.AST
}
return &markdownSection{
Title: title,
TOCItems: sec.AST,
Items: items,
}
}
// markdownItem unifies nodes of the following kinds:
//
// - markdownFileItem: an included Markdown file
// - markdownGroupItem: a title without any files, grouping other items
// - markdownExternalLinkItem: an external link
// - markdownEmbedItem: a request to embed another summary file
type markdownItem interface {
markdownItem()
}
func (c *collector) collectItem(cursor tree.Cursor[stitch.Item]) (markdownItem, error) {
item := cursor.Value()
switch item := item.(type) {
case *stitch.LinkItem:
return c.collectLinkItem(item, cursor)
case *stitch.EmbedItem:
return c.collectEmbedItem(item, cursor)
case *stitch.TextItem:
return c.collectGroupItem(item), nil
default:
panic(fmt.Sprintf("unhandled item type %T", item))
}
}
func (c *collector) collectLinkItem(item *stitch.LinkItem, cursor tree.Cursor[stitch.Item]) (markdownItem, error) {
u, err := url.Parse(item.Target)
if err == nil && u.Host != "" {
if cursor.ChildCount() > 0 {
return nil, errors.New("external link cannot have children")
}
return &markdownExternalLinkItem{
Item: item,
}, nil
}
return c.collectFileItem(item)
}
// markdownExternalLinkItem is a marker for external links
// in the summary.
type markdownExternalLinkItem struct {
Item *stitch.LinkItem
}
func (*markdownExternalLinkItem) markdownItem() {}
type markdownFileItem struct {
// Path is the /-separated path to the Markdown file.
Path string
// Item is the original link in the TOC
// that referenced the Markdown file.
Item *stitch.LinkItem
// Title is the title of the Markdown file, if any.
Title *markdownHeading
// File is the parsed Markdown file
// that the link points to.
File *goldast.File
// Links holds all links that were found in the Markdown file.
Links []*ast.Link
// Images holds all images that were found in the Markdown file.
Images []*ast.Image
// Headings holds all headings that were found in the Markdown file.
Headings []*markdownHeading
// HeadingsByOldID maps IDs of headings, as interpreted in isolation.
// The IDs will change once interpreted as part of the combined document.
HeadingsByOldID map[string]*markdownHeading
HTMLPairs rawhtml.Pairs
RawHTMLs []*ast.RawHTML
HTMLBlocks []*ast.HTMLBlock
// Absorb indicates that the headings in this file
// should be included in the parent TOC.
Absorb bool
TOC *toc.TOC
}
func (*markdownFileItem) markdownItem() {}
func (c *collector) collectFileItem(item *stitch.LinkItem) (*markdownFileItem, error) {
src, err := c.readFile(item.Target)
if err != nil {
return nil, err
}
ctx := parser.NewContext()
f := goldast.Parse(c.Parser, item.Target, src, parser.WithContext(ctx))
fidgen := header.NewIDGen()
var options struct {
// Headings included in the file
// should be absorbed into the parent TOC.
Absorb bool `yaml:"absorb"`
}
if data := frontmatter.Get(ctx); data != nil {
if err := data.Decode(&options); err != nil {
return nil, fmt.Errorf("bad frontmatter: %v", err)
}
}
var (
links []*ast.Link
images []*ast.Image
headings []*markdownHeading
h1s []*markdownHeading
rawHTMLs []*ast.RawHTML
htmlBlocks []*ast.HTMLBlock
)
headingsByOldID := make(map[string]*markdownHeading)
// Error ignored because walker doesn't return errors.
_ = goldast.Walk(f.AST, func(n ast.Node) error {
switch n := n.(type) {
case *ast.Link:
links = append(links, n)
case *ast.Image:
images = append(images, n)
case *ast.Heading:
mh := c.newHeading(f, fidgen, n)
headings = append(headings, mh)
if mh.Level() == 1 {
h1s = append(h1s, mh)
}
headingsByOldID[mh.OldID] = mh
case *ast.RawHTML:
rawHTMLs = append(rawHTMLs, n)
case *ast.HTMLBlock:
htmlBlocks = append(htmlBlocks, n)
}
return nil
})
mf := &markdownFileItem{
Path: item.Target,
Item: item,
File: f,
Links: links,
Images: images,
Headings: headings,
HeadingsByOldID: headingsByOldID,
HTMLPairs: rawhtml.GetPairs(ctx),
RawHTMLs: rawHTMLs,
HTMLBlocks: htmlBlocks,
Absorb: options.Absorb,
}
// If the page has only one level 1 heading,
// and it's the first element in the page,
// then use it as the title.
if len(h1s) == 1 && h1s[0].AST.PreviousSibling() == nil {
mf.Title = h1s[0]
f.AST.RemoveChild(f.AST, h1s[0].AST)
} else {
// The included file does not have a title.
// Generate one from the TOC link.
heading := ast.NewHeading(1)
heading.AppendChild(
heading,
ast.NewString([]byte(item.Text)),
)
heading.SetBlankPreviousLines(true)
mf.Title = c.newHeading(f, fidgen, heading)
// Push all existing headers down one level
// to make room for the new title
// if any of them is a level 1 header.
if len(h1s) > 0 {
for _, h := range mf.Headings {
h.Lvl++
}
}
mf.Headings = append([]*markdownHeading{mf.Title}, mf.Headings...)
}
// If we're being absorbed, we'll need a TOC.
if mf.Absorb {
fileTOC, err := toc.Inspect(mf.File.AST, mf.File.Source, toc.Compact(true))
if err != nil {
return nil, err
}
mf.TOC = fileTOC
}
c.files[item.Target] = mf
return mf, nil
}
type markdownGroupItem struct {
Item *stitch.TextItem
Heading *markdownHeading
src []byte
}
func (*markdownGroupItem) markdownItem() {}
func (c *collector) collectGroupItem(item *stitch.TextItem) *markdownGroupItem {
h := ast.NewHeading(1) // will be transformed
h.AppendChild(h, ast.NewString([]byte(item.Text)))
h.SetBlankPreviousLines(true)
id, _ := c.idGen.GenerateID(item.Text)
return &markdownGroupItem{
Item: item,
Heading: &markdownHeading{
AST: h,
ID: id,
Lvl: h.Level,
},
}
}
type markdownEmbedItem struct {
Item *stitch.EmbedItem
Section *markdownSection
FilesByPath map[string]*markdownFileItem
Heading *markdownHeading
SummaryFile *goldast.File
src []byte
}
var _ markdownItem = (*markdownEmbedItem)(nil)
func (c *collector) collectEmbedItem(item *stitch.EmbedItem, cursor tree.Cursor[stitch.Item]) (*markdownEmbedItem, error) {
if cursor.ChildCount() > 0 {
return nil, errors.New("embed cannot have children")
}
embedPath := path.Join(c.Dir, item.Target)
for _, p := range c.Stack {
if p == embedPath {
return nil, fmt.Errorf("embed cycle: %v", strings.Join(append(c.Stack, embedPath), " -> "))
}
}
summaryStack := append(c.Stack, embedPath)
src, err := c.readFile(item.Target)
if err != nil {
return nil, err
}
summaryFile := goldast.Parse(c.Parser, embedPath, src)
summary, err := stitch.ParseSummary(summaryFile)
if err != nil {
return nil, err
}
coll, err := (&collector{
Dir: path.Join(c.Dir, path.Dir(item.Target)),
Parser: c.Parser,
FS: c.FS,
idGen: c.idGen,
Stack: summaryStack,
}).Collect(summaryFile.Info, summary)
if err != nil {
return nil, err
}
switch len(coll.Sections) {
case 0:
// Unreachable: ParseSummary always returns at least one section.
return nil, errors.New("no sections found")
case 1:
// ok
default:
pos := summaryFile.Position(goldast.OffsetOf(coll.Sections[1].Title))
return nil, fmt.Errorf("%v:unexpected section; expected only one section", pos)
}
section := coll.Sections[0]
var heading *markdownHeading
if h := section.Title; h != nil {
// Ignore the heading level in the summary file.
// It'll get whatever the depth of the embed is.
h.Level = 1
id, _ := c.idGen.GenerateID(string(goldast.Text(summaryFile.Source, h)))
heading = &markdownHeading{
AST: h,
ID: id,
Lvl: h.Level,
}
// Unset the section title so it doesn't transform
// heading levels.
section.Title = nil
} else {
// The included file does not have a title.
// Generate one from the TOC link.
h := ast.NewHeading(1) // will be transformed
h.AppendChild(h, ast.NewString([]byte(item.Text)))
h.SetBlankPreviousLines(true)
id, _ := c.idGen.GenerateID(item.Text)
heading = &markdownHeading{
AST: h,
ID: id,
Lvl: h.Level,
}
}
return &markdownEmbedItem{
Item: item,
Section: section,
FilesByPath: coll.FilesByPath,
SummaryFile: summaryFile,
Heading: heading,
}, nil
}
func (*markdownEmbedItem) markdownItem() {}
type markdownHeading struct {
AST ast.Node
ID string
Lvl int
// ID of the heading in the original file.
OldID string
}
func (c *collector) newHeading(f *goldast.File, fgen *header.IDGen, h *ast.Heading) *markdownHeading {
text := string(goldast.Text(f.Source, h))
id, _ := c.idGen.GenerateID(text)
oldID, _ := fgen.GenerateID(text)
h.SetAttributeString("id", []byte(id)) // needed for toc.Inspect
return &markdownHeading{
AST: h,
ID: id,
OldID: oldID,
Lvl: h.Level,
}
}
func (h *markdownHeading) Level() int {
return h.Lvl
}
// readFile reads a file from the underlying filesystem.
func (c *collector) readFile(p string) ([]byte, error) {
p = path.Join(c.Dir, filepath.ToSlash(p))
src, err := fs.ReadFile(c.FS, p)
if err != nil {
// If the error is because the path name was not valid,
// it likely contains "." or ".." components,
// or has a "/" at the start or end of the path.
// Provide a hint to the user.
if errors.Is(err, fs.ErrInvalid) {
return nil, fmt.Errorf("invalid path %q; did you mean to use -unsafe?", p)
}
return nil, err
}
return src, nil
}