-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathaux_.go
144 lines (115 loc) · 3.02 KB
/
aux_.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
package booklit
// Aux is auxiliary content, typically in section titles, which can be removed
// in certain contexts such as references or table-of-content lists.
//
// It is primarily used with the `stripAux` template function.
//
// See https://booklit.page/baselit.html#aux for more information.
type Aux struct {
Content
}
// StripAux recurses through Content and removes any Aux content from any
// nested sequences.
func StripAux(content Content) Content {
visitor := &stripAuxVisitor{}
_ = content.Visit(visitor)
return visitor.Result
}
type stripAuxVisitor struct {
Result Content
}
func (strip *stripAuxVisitor) VisitString(con String) error {
strip.Result = con
return nil
}
func (strip *stripAuxVisitor) VisitSequence(con Sequence) error {
strip.Result = Sequence(stripAuxSeq(con))
return nil
}
func (strip *stripAuxVisitor) VisitParagraph(con Paragraph) error {
strip.Result = Paragraph(stripAuxSeq(con))
return nil
}
func (strip *stripAuxVisitor) VisitPreformatted(con Preformatted) error {
strip.Result = Preformatted(stripAuxSeq(con))
return nil
}
func (strip *stripAuxVisitor) VisitReference(con *Reference) error {
ref := *con
ref.Content = StripAux(ref.Content)
strip.Result = &ref
return nil
}
func (strip *stripAuxVisitor) VisitSection(con *Section) error {
strip.Result = con
return nil
}
func (strip *stripAuxVisitor) VisitTableOfContents(con TableOfContents) error {
strip.Result = con
return nil
}
func (strip *stripAuxVisitor) VisitStyled(con Styled) error {
con.Content = StripAux(con.Content)
strippedPartials := Partials{}
for k, v := range con.Partials {
if v == nil {
continue
}
strippedPartials[k] = StripAux(v)
}
con.Partials = strippedPartials
strip.Result = con
return nil
}
func (strip *stripAuxVisitor) VisitTarget(con Target) error {
con.Title = StripAux(con.Title)
con.Content = StripAux(con.Content)
strip.Result = con
return nil
}
func (strip *stripAuxVisitor) VisitImage(con Image) error {
strip.Result = con
return nil
}
func (strip *stripAuxVisitor) VisitList(con List) error {
con.Items = stripAuxSeq(con.Items)
strip.Result = con
return nil
}
func (strip *stripAuxVisitor) VisitLink(con Link) error {
con.Content = StripAux(con.Content)
strip.Result = con
return nil
}
func (strip *stripAuxVisitor) VisitTable(con Table) error {
newTable := Table{}
for _, row := range con.Rows {
newTable.Rows = append(newTable.Rows, stripAuxSeq(row))
}
strip.Result = newTable
return nil
}
func (strip *stripAuxVisitor) VisitDefinitions(con Definitions) error {
stripped := Definitions{}
for _, def := range con {
stripped = append(stripped, Definition{
Subject: StripAux(def.Subject),
Definition: StripAux(def.Definition),
})
}
strip.Result = stripped
return nil
}
func (strip *stripAuxVisitor) VisitLazy(con *Lazy) error {
strip.Result = con
return nil
}
func stripAuxSeq(seq []Content) []Content {
stripped := []Content{}
for _, c := range seq {
if _, isAux := c.(Aux); !isAux {
stripped = append(stripped, StripAux(c))
}
}
return stripped
}