/
plugin.go
279 lines (221 loc) · 5.92 KB
/
plugin.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
package baselit
import (
"fmt"
"path/filepath"
"github.com/vito/booklit"
"github.com/vito/booklit/ast"
)
func init() {
booklit.RegisterPlugin("base", NewPlugin)
}
func NewPlugin(section *booklit.Section) booklit.Plugin {
return Plugin{
section: section,
}
}
type Plugin struct {
section *booklit.Section
}
func (plugin Plugin) UsePlugin(name string) error {
pluginFactory, found := booklit.LookupPlugin(name)
if !found {
return fmt.Errorf("unknown plugin '%s'", name)
}
plugin.section.UsePlugin(pluginFactory)
return nil
}
func (plugin Plugin) Styled(name string) {
plugin.section.Style = name
}
func (plugin Plugin) Title(title booklit.Content, tags ...string) {
plugin.section.SetTitle(title, plugin.section.InvokeLocation, tags...)
}
func (plugin Plugin) Aux(content booklit.Content) booklit.Content {
return booklit.Aux{
Content: content,
}
}
func (plugin Plugin) Section(node ast.Node) error {
section, err := plugin.section.Processor.EvaluateNode(plugin.section, node, plugin.section.PluginFactories)
if err != nil {
return err
}
section.Location = plugin.section.InvokeLocation
plugin.section.Children = append(plugin.section.Children, section)
return nil
}
func (plugin Plugin) IncludeSection(path string) error {
sectionPath := filepath.Join(filepath.Dir(plugin.section.FilePath()), path)
section, err := plugin.section.Processor.EvaluateFile(plugin.section, sectionPath, []booklit.PluginFactory{NewPlugin})
if err != nil {
return err
}
plugin.section.Children = append(plugin.section.Children, section)
return nil
}
func (plugin Plugin) SinglePage() {
plugin.section.PreventSplitSections = true
}
func (plugin Plugin) SplitSections() {
plugin.section.ResetDepth = true
if !plugin.section.SplitSectionsPrevented() {
plugin.section.SplitSections = true
}
}
func (plugin Plugin) OmitChildrenFromTableOfContents() {
plugin.section.OmitChildrenFromTableOfContents = true
}
func (plugin Plugin) TableOfContents() booklit.Content {
return booklit.TableOfContents{
Section: plugin.section,
}
}
func (plugin Plugin) Code(content booklit.Content) booklit.Content {
return booklit.Styled{
Content: content,
Style: booklit.StyleVerbatim,
}
}
func (plugin Plugin) Italic(content booklit.Content) booklit.Content {
return booklit.Styled{
Content: content,
Style: booklit.StyleItalic,
}
}
func (plugin Plugin) Bold(content booklit.Content) booklit.Content {
return booklit.Styled{
Content: content,
Style: booklit.StyleBold,
}
}
func (plugin Plugin) Larger(content booklit.Content) booklit.Content {
return booklit.Styled{
Content: content,
Style: booklit.StyleLarger,
}
}
func (plugin Plugin) Smaller(content booklit.Content) booklit.Content {
return booklit.Styled{
Content: content,
Style: booklit.StyleSmaller,
}
}
func (plugin Plugin) Strike(content booklit.Content) booklit.Content {
return booklit.Styled{
Content: content,
Style: booklit.StyleStrike,
}
}
func (plugin Plugin) Superscript(content booklit.Content) booklit.Content {
return booklit.Styled{
Content: content,
Style: booklit.StyleSuperscript,
}
}
func (plugin Plugin) Subscript(content booklit.Content) booklit.Content {
return booklit.Styled{
Content: content,
Style: booklit.StyleSubscript,
}
}
func (plugin Plugin) Inset(content booklit.Content) booklit.Content {
return booklit.Styled{
Content: content,
Style: booklit.StyleInset,
}
}
func (plugin Plugin) Aside(content booklit.Content) booklit.Content {
return booklit.Styled{
Content: content,
Style: booklit.StyleAside,
}
}
func (plugin Plugin) Link(content booklit.Content, target string) booklit.Content {
return booklit.Link{
Content: content,
Target: target,
}
}
func (plugin Plugin) Reference(tag string, content ...booklit.Content) booklit.Content {
ref := &booklit.Reference{
TagName: tag,
Location: plugin.section.InvokeLocation,
}
if len(content) > 0 {
ref.Content = content[0]
}
return ref
}
func (plugin Plugin) Target(tag string, titleAndContent ...booklit.Content) booklit.Content {
ref := &booklit.Target{
TagName: tag,
Location: plugin.section.InvokeLocation,
}
switch len(titleAndContent) {
case 2:
ref.Title = titleAndContent[0]
ref.Content = titleAndContent[1]
case 1:
ref.Title = titleAndContent[0]
default:
ref.Title = booklit.String(tag)
}
return ref
}
func (plugin Plugin) List(items ...booklit.Content) booklit.Content {
return booklit.List{
Items: items,
}
}
func (plugin Plugin) OrderedList(items ...booklit.Content) booklit.Content {
return booklit.List{
Items: items,
Ordered: true,
}
}
func (plugin Plugin) Image(path string, description ...string) booklit.Content {
img := booklit.Image{
Path: path,
}
if len(description) > 0 {
img.Description = description[0]
}
return img
}
func (plugin Plugin) SetPartial(name string, content booklit.Content) {
plugin.section.SetPartial(name, content)
}
func (plugin Plugin) Table(rows ...booklit.Content) (booklit.Content, error) {
table := booklit.Table{}
for _, row := range rows {
list, ok := row.(booklit.List)
if !ok {
return nil, fmt.Errorf("table row is not a list: %s", row)
}
table.Rows = append(table.Rows, list.Items)
}
return table, nil
}
func (plugin Plugin) TableRow(cols ...booklit.Content) booklit.Content {
return plugin.List(cols...)
}
func (plugin Plugin) Definitions(items ...booklit.Content) (booklit.Content, error) {
defs := booklit.Definitions{}
for _, item := range items {
list, ok := item.(booklit.List)
if !ok {
return nil, fmt.Errorf("definition item is not a list: %s", item)
}
if len(list.Items) != 2 {
return nil, fmt.Errorf("definition item must have two entries: %s", item)
}
defs = append(defs, booklit.Definition{
Subject: list.Items[0],
Definition: list.Items[1],
})
}
return defs, nil
}
func (plugin Plugin) Definition(subject booklit.Content, definition booklit.Content) booklit.Content {
return plugin.List(subject, definition)
}