-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathattr.go
158 lines (131 loc) · 4.33 KB
/
attr.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
// Package attributes is a extension for the goldmark
// (http://github.com/yuin/goldmark).
//
// This extension adds support for block attributes in markdowns.
// paragraph text with attributes
// {#id .class option="value"}
package attributes
import (
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/renderer"
"github.com/yuin/goldmark/text"
"github.com/yuin/goldmark/util"
)
// block are parsed attributes block.
type block struct {
ast.BaseBlock
}
// Dump implements Node.Dump.
func (a *block) Dump(source []byte, level int) {
attrs := a.Attributes()
list := make(map[string]string, len(attrs))
for _, attr := range attrs {
name := util.BytesToReadOnlyString(attr.Name)
value := util.BytesToReadOnlyString(util.EscapeHTML(attr.Value.([]byte)))
list[name] = value
}
ast.DumpHelper(a, source, level, list, nil)
}
// KindAttributes is a NodeKind of the attributes block node.
var KindAttributes = ast.NewNodeKind("BlockAttributes")
// Kind implements Node.Kind.
func (a *block) Kind() ast.NodeKind {
return KindAttributes
}
type attrParser struct{}
// Trigger implement parser.BlockParser interface.
func (a *attrParser) Trigger() []byte {
return []byte{'{'}
}
// Open implement parser.BlockParser interface.
func (a *attrParser) Open(parent ast.Node, reader text.Reader, pc parser.Context) (ast.Node, parser.State) {
// add attributes if defined
if attrs, ok := parser.ParseAttributes(reader); ok {
node := &block{BaseBlock: ast.BaseBlock{}}
for _, attr := range attrs {
node.SetAttribute(attr.Name, attr.Value)
}
return node, parser.NoChildren
}
return nil, parser.RequireParagraph
}
// Continue implement parser.BlockParser interface.
func (a *attrParser) Continue(node ast.Node, reader text.Reader, pc parser.Context) parser.State {
return parser.Close
}
// Close implement parser.BlockParser interface.
func (a *attrParser) Close(node ast.Node, reader text.Reader, pc parser.Context) {
// nothing to do
}
// CanInterruptParagraph implement parser.BlockParser interface.
func (a *attrParser) CanInterruptParagraph() bool {
return true
}
// CanAcceptIndentedLine implement parser.BlockParser interface.
func (a *attrParser) CanAcceptIndentedLine() bool {
return false
}
type transformer struct{}
// Transform implement parser.Transformer interface.
func (a *transformer) Transform(node *ast.Document, reader text.Reader, pc parser.Context) {
// collect all attributes block
var attributes = make([]ast.Node, 0, 1000)
_ = ast.Walk(node, func(node ast.Node, entering bool) (ast.WalkStatus, error) {
if entering && node.Kind() == KindAttributes {
attributes = append(attributes, node)
return ast.WalkSkipChildren, nil
}
return ast.WalkContinue, nil
})
// set attributes to next block sibling
for _, attr := range attributes {
prev := attr.PreviousSibling()
if prev != nil && prev.Type() == ast.TypeBlock &&
!attr.HasBlankPreviousLines() {
for _, attr := range attr.Attributes() {
if _, exist := prev.Attribute(attr.Name); !exist {
prev.SetAttribute(attr.Name, attr.Value)
}
}
}
// remove attributes node
attr.Parent().RemoveChild(attr.Parent(), attr)
}
}
type attrRender struct{}
// RegisterFuncs implement renderer.NodeRenderer interface.
func (a *attrRender) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer) {
// not render
reg.Register(KindAttributes,
func(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
return ast.WalkSkipChildren, nil
})
}
// extension defines a goldmark.Extender for markdown block attributes.
type extension struct{}
var (
defaultParser = new(attrParser)
defaultTransformer = new(transformer)
defaultRenderer = new(attrRender)
)
// Extend implement goldmark.Extender interface.
func (a *extension) Extend(m goldmark.Markdown) {
m.Parser().AddOptions(
parser.WithBlockParsers(
util.Prioritized(defaultParser, 100)),
parser.WithASTTransformers(
util.Prioritized(defaultTransformer, 100),
),
)
m.Renderer().AddOptions(
renderer.WithNodeRenderers(
util.Prioritized(defaultRenderer, 100),
),
)
}
// Extension is a goldmark.Extender with markdown block attributes support.
var Extension goldmark.Extender = new(extension)
// Enable is a goldmark.Option with block attributes support.
var Enable = goldmark.WithExtensions(Extension)