/
docs_parser.go
178 lines (145 loc) 路 4.46 KB
/
docs_parser.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
package schema
import (
"bytes"
"io"
"unicode"
"unicode/utf8"
"github.com/pgavlin/goldmark"
"github.com/pgavlin/goldmark/ast"
"github.com/pgavlin/goldmark/parser"
"github.com/pgavlin/goldmark/text"
"github.com/pgavlin/goldmark/util"
)
const (
// ExamplesShortcode is the name for the `{{% examples %}}` shortcode, which demarcates a set of example sections.
ExamplesShortcode = "examples"
// ExampleShortcode is the name for the `{{% example %}}` shortcode, which demarcates the content for a single
// example.
ExampleShortcode = "example"
)
// Shortcode represents a shortcode element and its contents, e.g. `{{% examples %}}`.
type Shortcode struct {
ast.BaseBlock
// Name is the name of the shortcode.
Name []byte
}
func (s *Shortcode) Dump(w io.Writer, source []byte, level int) {
m := map[string]string{
"Name": string(s.Name),
}
ast.DumpHelper(w, s, source, level, m, nil)
}
// KindShortcode is an ast.NodeKind for the Shortcode node.
var KindShortcode = ast.NewNodeKind("Shortcode")
// Kind implements ast.Node.Kind.
func (*Shortcode) Kind() ast.NodeKind {
return KindShortcode
}
// NewShortcode creates a new shortcode with the given name.
func NewShortcode(name []byte) *Shortcode {
return &Shortcode{Name: name}
}
type shortcodeParser int
// NewShortcodeParser returns a BlockParser that parses shortcode (e.g. `{{% examples %}}`).
func NewShortcodeParser() parser.BlockParser {
return shortcodeParser(0)
}
func (shortcodeParser) Trigger() []byte {
return []byte{'{'}
}
func (shortcodeParser) parseShortcode(line []byte, pos int) (int, int, int, bool, bool) {
// Look for `{{%` to open the shortcode.
text := line[pos:]
if len(text) < 3 || text[0] != '{' || text[1] != '{' || text[2] != '%' {
return 0, 0, 0, false, false
}
text, pos = text[3:], pos+3
// Scan through whitespace.
for {
if len(text) == 0 {
return 0, 0, 0, false, false
}
r, sz := utf8.DecodeRune(text)
if !unicode.IsSpace(r) {
break
}
text, pos = text[sz:], pos+sz
}
// Check for a '/' to indicate that this is a closing shortcode.
isClose := false
if text[0] == '/' {
isClose = true
text, pos = text[1:], pos+1
}
// Find the end of the name and the closing delimiter (`%}}`) for this shortcode.
nameStart, nameEnd, inName := pos, pos, true
for {
if len(text) == 0 {
return 0, 0, 0, false, false
}
if len(text) >= 3 && text[0] == '%' && text[1] == '}' && text[2] == '}' {
if inName {
nameEnd = pos
}
pos = pos + 3
// We don't need to update text
// because we return after this break.
break
}
r, sz := utf8.DecodeRune(text)
if inName && unicode.IsSpace(r) {
nameEnd, inName = pos, false
}
text, pos = text[sz:], pos+sz
}
return nameStart, nameEnd, pos, isClose, true
}
func (p shortcodeParser) Open(parent ast.Node, reader text.Reader, pc parser.Context) (ast.Node, parser.State) {
line, _ := reader.PeekLine()
pos := pc.BlockOffset()
if pos < 0 {
return nil, parser.NoChildren
}
nameStart, nameEnd, shortcodeEnd, isClose, ok := p.parseShortcode(line, pos)
if !ok || isClose {
return nil, parser.NoChildren
}
name := line[nameStart:nameEnd]
reader.Advance(shortcodeEnd)
return NewShortcode(name), parser.HasChildren
}
func (p shortcodeParser) Continue(node ast.Node, reader text.Reader, pc parser.Context) parser.State {
line, seg := reader.PeekLine()
pos := pc.BlockOffset()
if pos < 0 {
return parser.Continue | parser.HasChildren
} else if pos > seg.Len() {
return parser.Continue | parser.HasChildren
}
nameStart, nameEnd, shortcodeEnd, isClose, ok := p.parseShortcode(line, pos)
if !ok || !isClose {
return parser.Continue | parser.HasChildren
}
shortcode := node.(*Shortcode)
if !bytes.Equal(line[nameStart:nameEnd], shortcode.Name) {
return parser.Continue | parser.HasChildren
}
reader.Advance(shortcodeEnd)
return parser.Close
}
func (shortcodeParser) Close(node ast.Node, reader text.Reader, pc parser.Context) {
}
// CanInterruptParagraph returns true for shortcodes.
func (shortcodeParser) CanInterruptParagraph() bool {
return true
}
// CanAcceptIndentedLine returns false for shortcodes; all shortcodes must start at the first column.
func (shortcodeParser) CanAcceptIndentedLine() bool {
return false
}
// ParseDocs parses the given documentation text as Markdown with shortcodes and returns the AST.
func ParseDocs(docs []byte) ast.Node {
p := goldmark.DefaultParser()
p.AddOptions(parser.WithBlockParsers(util.Prioritized(shortcodeParser(0), 50)))
return p.Parse(text.NewReader(docs))
}