-
Notifications
You must be signed in to change notification settings - Fork 9
/
meta.go
320 lines (273 loc) · 6.79 KB
/
meta.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
// package meta is a extension for the goldmark(http://github.com/yuin/goldmark).
//
// This extension parses YAML metadata blocks and store metadata to a
// parser.Context.
package meta
import (
"bytes"
"fmt"
"github.com/yuin/goldmark"
gast "github.com/yuin/goldmark/ast"
east "github.com/yuin/goldmark/extension/ast"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/text"
"github.com/yuin/goldmark/util"
"gopkg.in/yaml.v2"
)
type data struct {
Map map[string]interface{}
Items yaml.MapSlice
Error error
Node gast.Node
}
var contextKey = parser.NewContextKey()
// Option interface sets options for this extension.
type Option interface {
metaOption()
}
// Get returns a YAML metadata.
func Get(pc parser.Context) map[string]interface{} {
v := pc.Get(contextKey)
if v == nil {
return nil
}
d := v.(*data)
return d.Map
}
// TryGet tries to get a YAML metadata.
// If there are YAML parsing errors, then nil and error are returned
func TryGet(pc parser.Context) (map[string]interface{}, error) {
dtmp := pc.Get(contextKey)
if dtmp == nil {
return nil, nil
}
d := dtmp.(*data)
if d.Error != nil {
return nil, d.Error
}
return d.Map, nil
}
// GetItems returns a YAML metadata.
// GetItems preserves defined key order.
func GetItems(pc parser.Context) yaml.MapSlice {
v := pc.Get(contextKey)
if v == nil {
return nil
}
d := v.(*data)
return d.Items
}
// TryGetItems returns a YAML metadata.
// TryGetItems preserves defined key order.
// If there are YAML parsing errors, then nil and erro are returned.
func TryGetItems(pc parser.Context) (yaml.MapSlice, error) {
dtmp := pc.Get(contextKey)
if dtmp == nil {
return nil, nil
}
d := dtmp.(*data)
if d.Error != nil {
return nil, d.Error
}
return d.Items, nil
}
type metaParser struct {
}
var defaultParser = &metaParser{}
// NewParser returns a BlockParser that can parse YAML metadata blocks.
func NewParser() parser.BlockParser {
return defaultParser
}
func isSeparator(line []byte) bool {
line = util.TrimRightSpace(util.TrimLeftSpace(line))
for i := 0; i < len(line); i++ {
if line[i] != '-' {
return false
}
}
return true
}
func (b *metaParser) Trigger() []byte {
return []byte{'-'}
}
func (b *metaParser) Open(parent gast.Node, reader text.Reader, pc parser.Context) (gast.Node, parser.State) {
linenum, _ := reader.Position()
if linenum != 0 {
return nil, parser.NoChildren
}
line, _ := reader.PeekLine()
if isSeparator(line) {
return gast.NewTextBlock(), parser.NoChildren
}
return nil, parser.NoChildren
}
func (b *metaParser) Continue(node gast.Node, reader text.Reader, pc parser.Context) parser.State {
line, segment := reader.PeekLine()
if isSeparator(line) && !util.IsBlank(line) {
reader.Advance(segment.Len())
return parser.Close
}
node.Lines().Append(segment)
return parser.Continue | parser.NoChildren
}
func (b *metaParser) Close(node gast.Node, reader text.Reader, pc parser.Context) {
lines := node.Lines()
var buf bytes.Buffer
for i := 0; i < lines.Len(); i++ {
segment := lines.At(i)
buf.Write(segment.Value(reader.Source()))
}
d := &data{}
d.Node = node
meta := map[string]interface{}{}
if err := yaml.Unmarshal(buf.Bytes(), &meta); err != nil {
d.Error = err
} else {
d.Map = meta
}
metaMapSlice := yaml.MapSlice{}
if err := yaml.Unmarshal(buf.Bytes(), &metaMapSlice); err != nil {
d.Error = err
} else {
d.Items = metaMapSlice
}
pc.Set(contextKey, d)
if d.Error == nil {
node.Parent().RemoveChild(node.Parent(), node)
}
}
func (b *metaParser) CanInterruptParagraph() bool {
return false
}
func (b *metaParser) CanAcceptIndentedLine() bool {
return false
}
type astTransformer struct {
transformerConfig
}
type transformerConfig struct {
// Renders metadata as an html table.
Table bool
// Stores metadata in ast.Document.Meta().
StoresInDocument bool
}
type transformerOption interface {
Option
// SetMetaOption sets options for the metadata parser.
SetMetaOption(*transformerConfig)
}
var _ transformerOption = &withTable{}
type withTable struct {
value bool
}
func (o *withTable) metaOption() {}
func (o *withTable) SetMetaOption(m *transformerConfig) {
m.Table = o.value
}
// WithTable is a functional option that renders a YAML metadata as a table.
func WithTable() Option {
return &withTable{
value: true,
}
}
var _ transformerOption = &withStoresInDocument{}
type withStoresInDocument struct {
value bool
}
func (o *withStoresInDocument) metaOption() {}
func (o *withStoresInDocument) SetMetaOption(c *transformerConfig) {
c.StoresInDocument = o.value
}
// WithStoresInDocument is a functional option that parser will store YAML meta in ast.Document.Meta().
func WithStoresInDocument() Option {
return &withStoresInDocument{
value: true,
}
}
func newTransformer(opts ...transformerOption) parser.ASTTransformer {
p := &astTransformer{
transformerConfig: transformerConfig{
Table: false,
StoresInDocument: false,
},
}
for _, o := range opts {
o.SetMetaOption(&p.transformerConfig)
}
return p
}
func (a *astTransformer) Transform(node *gast.Document, reader text.Reader, pc parser.Context) {
dtmp := pc.Get(contextKey)
if dtmp == nil {
return
}
d := dtmp.(*data)
if d.Error != nil {
msg := gast.NewString([]byte(fmt.Sprintf("<!-- %s -->", d.Error)))
msg.SetCode(true)
d.Node.AppendChild(d.Node, msg)
return
}
if a.Table {
meta := GetItems(pc)
if meta == nil {
return
}
table := east.NewTable()
alignments := []east.Alignment{}
for range meta {
alignments = append(alignments, east.AlignNone)
}
row := east.NewTableRow(alignments)
for _, item := range meta {
cell := east.NewTableCell()
cell.AppendChild(cell, gast.NewString([]byte(fmt.Sprintf("%v", item.Key))))
row.AppendChild(row, cell)
}
table.AppendChild(table, east.NewTableHeader(row))
row = east.NewTableRow(alignments)
for _, item := range meta {
cell := east.NewTableCell()
cell.AppendChild(cell, gast.NewString([]byte(fmt.Sprintf("%v", item.Value))))
row.AppendChild(row, cell)
}
table.AppendChild(table, row)
node.InsertBefore(node, node.FirstChild(), table)
}
if a.StoresInDocument {
for k, v := range d.Map {
node.AddMeta(k, v)
}
}
}
type meta struct {
options []Option
}
// Meta is a extension for the goldmark.
var Meta = &meta{}
// New returns a new Meta extension.
func New(opts ...Option) goldmark.Extender {
e := &meta{
options: opts,
}
return e
}
// Extend implements goldmark.Extender.
func (e *meta) Extend(m goldmark.Markdown) {
topts := []transformerOption{}
for _, opt := range e.options {
if topt, ok := opt.(transformerOption); ok {
topts = append(topts, topt)
}
}
m.Parser().AddOptions(
parser.WithBlockParsers(
util.Prioritized(NewParser(), 0),
),
)
m.Parser().AddOptions(
parser.WithASTTransformers(
util.Prioritized(newTransformer(topts...), 0),
),
)
}