-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathsection.go
471 lines (380 loc) · 10.6 KB
/
section.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
460
461
462
463
464
465
466
467
468
469
470
471
package booklit
import (
"fmt"
"regexp"
"strings"
"github.com/agext/levenshtein"
"github.com/vito/booklit/ast"
)
// Section is a Booklit document, typically loaded from a .lit file.
type Section struct {
// The file path that the section was loaded from.
Path string
// The section title and body.
Title Content
Body Content
// The the file source location where the title was set.
TitleLocation ast.Location
// The primary tag and additional tags for the section.
PrimaryTag Tag
Tags []Tag
// The section's parent and child sections, if any.
Parent *Section
Children []*Section
// The section's style. Used at the rendering stage to e.g. use different
// templates.
//
// Set with \style{foo}.
Style string
// Arbitrary named content.
//
// Partials are typically rendered in templates via {{.Partial "Foo" | render}}.
//
// Set with \set-partial{name}{content}.
Partials Partials
// Instructs the renderer to render child sections on their own pages.
//
// Set with \split-sections.
SplitSections bool
// Instructs the renderer to never render child sections on their own pages,
// even if they set SplitSections.
//
// Typically used to have a single-page view or printout of the content.
//
// Set with \single-page.
PreventSplitSections bool
// Instructs .PageDepth to pretend the section is the lowest point.
//
// Set with \split-sections, even if PreventSplitSections is true, to ensure
// section numbers remain consistent.
ResetDepth bool
// Omit child sections from table-of-contents lists.
//
// Set with \omit-children-from-table-of-contents.
OmitChildrenFromTableOfContents bool
// Plugins used by the section.
PluginFactories []PluginFactory
Plugins []Plugin
// Location is the source location where the section was defined, if it was
// defined as an inline section.
Location ast.Location
// InvokeLocation is set prior to each function call so that the plugin's
// method can assign it on content that it produces, e.g. Reference and
// Target, so that error messages can be annotated with the source of the
// error.
//
// XXX: make this an optional argument instead?
InvokeLocation ast.Location
// Processor is used for evaluating additional child sections.
Processor SectionProcessor
}
// Partials is a map of named snippets of content. By convention, partial names
// are camel-cased like FooBar.
type Partials map[string]Content
// SectionProcessor evaluates a file or an inline syntax node to produce a
// child section.
type SectionProcessor interface {
EvaluateFile(*Section, string, []PluginFactory) (*Section, error)
EvaluateNode(*Section, ast.Node, []PluginFactory) (*Section, error)
}
// Tag is something which can be referenced (by its name) from elsewhere in the
// section tree to produce a link.
type Tag struct {
// The name of the tag, to be referenced with \reference.
Name string
// The title of the tag to display when no display is given by \reference.
Title Content
// The section the tag resides in.
Section *Section
// An optional anchor for the tag.
//
// Anchored tags correspond to page anchors and are typically displayed in
// the section body, as opposed to tags corresponding to a section.
Anchor string
// Content that the tag corresponds to. For example, the section body or the
// text that an anchored tag is for.
//
// Typically used for showing content previews in search results.
Content Content
// The source location of the tag's definition.
Location ast.Location
}
// String summarizes the content for debugging purposes.
func (con *Section) String() string {
return fmt.Sprintf("{section (%s): %s}", con.Path, con.Title)
}
// FilePath is the file that the section is contained in.
func (con *Section) FilePath() string {
if con.Path != "" {
return con.Path
}
if con.Parent != nil {
return con.Parent.FilePath()
}
return ""
}
// IsFlow returns false.
func (con *Section) IsFlow() bool {
return false
}
// Visit calls VisitSection on visitor.
func (con *Section) Visit(visitor Visitor) error {
return visitor.VisitSection(con)
}
// SetTitle sets the section title and tags, setting a default tag based on the
// title if no tags are specified.
func (con *Section) SetTitle(title Content, loc ast.Location, tags ...string) {
if len(tags) == 0 {
tags = []string{con.defaultTag(title)}
}
con.Tags = []Tag{}
for _, name := range tags {
con.SetTag(name, title, loc)
}
con.Title = title
con.TitleLocation = loc
con.PrimaryTag = con.Tags[0]
}
// SetTag adds a tag to the section.
func (con *Section) SetTag(name string, title Content, loc ast.Location, optionalAnchor ...string) {
anchor := ""
if len(optionalAnchor) > 0 {
anchor = optionalAnchor[0]
}
con.Tags = append(con.Tags, Tag{
Section: con,
Location: loc,
Name: name,
Title: title,
Anchor: anchor,
})
}
// SetTagAnchored adds an anchored tag to the section, along with content
// associated to the anchor.
func (con *Section) SetTagAnchored(name string, title Content, loc ast.Location, content Content, anchor string) {
con.Tags = append(con.Tags, Tag{
Section: con,
Location: loc,
Name: name,
Title: title,
Anchor: anchor,
Content: content,
})
}
// Number returns a string denoting the section's unique numbering for use in
// titles and tables of contents, e.g. "3.2".
func (con *Section) Number() string {
if con.Parent == nil {
return ""
}
parentNumber := con.Parent.Number()
selfIndex := 1
for _, child := range con.Parent.Children {
if child == con {
break
}
selfIndex++
}
if parentNumber == "" {
return fmt.Sprintf("%d", selfIndex)
}
return fmt.Sprintf("%s.%d", parentNumber, selfIndex)
}
// HasAnchors returns true if the section has any anchored tags or if any
// inline child sections have anchors.
func (con *Section) HasAnchors() bool {
for _, tag := range con.Tags {
if tag.Anchor != "" {
return true
}
}
if con.SplitSections {
return false
}
for _, child := range con.Children {
if child.HasAnchors() {
return true
}
}
return false
}
// AnchorTags returns the section's tags which have anchors.
func (con *Section) AnchorTags() []Tag {
tags := []Tag{}
for _, tag := range con.Tags {
if tag.Anchor == "" {
continue
}
tags = append(tags, tag)
}
return tags
}
// Top returns the top-level section.
func (con *Section) Top() *Section {
if con.Parent != nil {
return con.Parent.Top()
}
return con
}
// Contains returns true if the section is sub or if any of the children
// Contains sub.
func (con *Section) Contains(sub *Section) bool {
if con == sub {
return true
}
for _, child := range con.Children {
if child.Contains(sub) {
return true
}
}
return false
}
// IsOrHasChild returns true if the section is sub or has sub as a direct
// child.
func (con *Section) IsOrHasChild(sub *Section) bool {
if con == sub {
return true
}
for _, child := range con.Children {
if child == sub {
return true
}
}
return false
}
// Prev returns the previous section, i.e. the previous sibling section or the
// parent section if it is the first child.
//
// If the section has no parent, Prev returns nil.
func (con *Section) Prev() *Section {
if con.Parent == nil {
return nil
}
var lastChild *Section
for _, child := range con.Parent.Children {
if lastChild != nil && child == con {
return lastChild
}
lastChild = child
}
return con.Parent
}
// Next returns the next section, i.e. if SplitSections return the first child,
// otherwise return the NextSibling.
func (con *Section) Next() *Section {
if con.SplitSections {
if len(con.Children) > 0 {
return con.Children[0]
}
}
return con.NextSibling()
}
// NextSibling returns the section after the current section in the parent's
// children.
//
// If there is no section after this section, the parent's NextSibling is
// returned.
func (con *Section) NextSibling() *Section {
if con.Parent == nil {
return nil
}
var sawSelf bool
for _, child := range con.Parent.Children {
if sawSelf {
return child
}
if child == con {
sawSelf = true
}
}
return con.Parent.NextSibling()
}
// FindTag searches for a given tag name and returns all matching tags.
func (con *Section) FindTag(tagName string) []Tag {
return con.filterTags(true, nil, func(other string) bool {
return other == tagName
})
}
// SimilarTags searches for a given tag name and returns all similar tags, i.e.
// tags with a levenshtein distance > 0.5.
func (con *Section) SimilarTags(tagName string) []Tag {
return con.filterTags(true, nil, func(other string) bool {
return levenshtein.Match(tagName, other, nil) > 0.5
})
}
// SetPartial assigns a partial within the section.
func (con *Section) SetPartial(name string, value Content) {
if con.Partials == nil {
con.Partials = Partials{}
}
con.Partials[name] = value
}
// Partial returns the given partial, or nil if it does not exist.
func (con *Section) Partial(name string) Content {
return con.Partials[name]
}
// UsePlugin registers the plugin within the section.
func (con *Section) UsePlugin(pf PluginFactory) {
con.PluginFactories = append(con.PluginFactories, pf)
con.Plugins = append(con.Plugins, pf(con))
}
// Depth returns the absolute depth of the section.
func (con *Section) Depth() int {
if con.Parent == nil {
return 0
}
return con.Parent.Depth() + 1
}
// PageDepth returns the depth within the page that the section will be
// rendered on, i.e. accounting for ResetDepth being set on the parent section.
func (con *Section) PageDepth() int {
if con.Parent == nil || con.Parent.ResetDepth {
return 0
}
return con.Parent.PageDepth() + 1
}
// SplitSectionsPrevented returns true if PreventSplitSections is true or if
// the parent has SplitSectionsPrevented.
func (con *Section) SplitSectionsPrevented() bool {
if con.PreventSplitSections {
return true
}
if con.Parent != nil && con.Parent.SplitSectionsPrevented() {
return true
}
return false
}
func (con *Section) filterTags(up bool, exclude *Section, match func(string) bool) []Tag {
tags := []Tag{}
for _, t := range con.Tags {
if match(t.Name) {
tags = append(tags, t)
}
}
for _, sub := range con.Children {
if sub != exclude {
tags = append(tags, sub.filterTags(false, nil, match)...)
}
}
if up && con.Parent != nil {
tags = append(tags, con.Parent.filterTags(true, con, match)...)
}
return tags
}
var whitespaceRegexp = regexp.MustCompile(`\s+`)
var specialCharsRegexp = regexp.MustCompile(`[^[:alnum:]_\-]`)
func (con *Section) defaultTag(title Content) string {
return strings.ToLower(
specialCharsRegexp.ReplaceAllString(
whitespaceRegexp.ReplaceAllString(
strings.ReplaceAll(
StripAux(title).String(),
" & ",
" and ",
),
"-",
),
"",
),
)
}