-
Notifications
You must be signed in to change notification settings - Fork 38
/
parser.go
199 lines (167 loc) · 3.48 KB
/
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package document
import (
"bytes"
"unicode"
"unicode/utf8"
"github.com/pkg/errors"
)
type ParsedSections struct {
FrontMatter []byte
Content []byte
ContentOffset int
}
func ParseSections(source []byte) (result ParsedSections, _ error) {
l := &itemParser{input: source}
runItemParser(l, parseInit)
for _, item := range l.items {
switch item.Type() {
case parsedItemFrontMatter:
result.FrontMatter = item.Value(source)
case parsedItemContent:
result.ContentOffset = item.start
result.Content = item.Value(source)
case parsedItemError:
if errors.Is(item.err, errParseRawFrontmatter) {
return ParsedSections{
Content: source,
}, nil
}
return result, item.err
}
}
return
}
const eof = -1
var crlf = []rune{'\r', '\n'}
func isEOL(r rune) bool {
return r == crlf[0] || r == crlf[1]
}
type parsedItemType int
const (
parsedItemFrontMatter parsedItemType = iota + 1
parsedItemContent
parsedItemError
)
type parsedItem struct {
typ parsedItemType
start int
end int
err error
}
func (i parsedItem) String(source []byte) string {
return string(source[i.start:i.end])
}
func (i parsedItem) Type() parsedItemType {
return i.typ
}
func (i parsedItem) Value(source []byte) []byte {
return source[i.start:i.end]
}
type itemParser struct {
input []byte
items []parsedItem
pos int
start int
width int
}
func (l *itemParser) backup() {
l.pos -= l.width
}
func (l *itemParser) consume(runes []rune) bool {
var consumed bool
for _, r := range runes {
if l.next() != r {
l.backup()
} else {
consumed = true
}
}
return consumed
}
func (l *itemParser) emit(t parsedItemType) {
l.items = append(l.items, parsedItem{
typ: t,
start: l.start,
end: l.pos,
})
l.start = l.pos
}
func (l *itemParser) errorf(format string, args ...interface{}) {
l.error(errors.Errorf(format, args...))
}
func (l *itemParser) error(err error) {
l.items = append(l.items, parsedItem{
typ: parsedItemError,
err: err,
})
}
func (l *itemParser) hasPrefix(prefix []byte) bool {
return bytes.HasPrefix(l.input[l.pos:], prefix)
}
func (l *itemParser) ignore() {
l.start = l.pos
}
func (l *itemParser) next() rune {
if l.pos >= len(l.input) {
l.width = 0
return eof
}
runeValue, runeWidth := utf8.DecodeRune(l.input[l.pos:])
l.width = runeWidth
l.pos += l.width
return runeValue
}
func runItemParser(l *itemParser, startState parserStateFunc) {
for stateFn := startState; stateFn != nil; {
stateFn = stateFn(l)
}
}
type parserStateFunc func(*itemParser) parserStateFunc
func parseInit(l *itemParser) parserStateFunc {
loop:
for {
r0 := l.next()
if r0 == eof {
break
}
r1 := l.next()
l.backup()
switch {
case r0 == '+':
return parseRawFrontmatter(l, byte(r0))
case r0 == '-':
return parseRawFrontmatter(l, byte(r0))
case r0 == '{' && r1 == '{':
// skip markdown templates
l.backup()
break loop
case r0 == '{' && r1 == '%':
// skip markdown preprocessor includes
l.backup()
break loop
case r0 == '{':
return parseRawFrontmatterJSON
case r0 == '\ufeff':
// skip
case !unicode.IsSpace(r0) && !isEOL(r0):
l.backup()
break loop
}
}
return parseContent
}
func parseContent(l *itemParser) parserStateFunc {
// Ignore any new lines at the beginning.
for consumed := true; consumed; {
consumed = l.consume(crlf)
}
l.ignore()
l.pos = len(l.input)
return parseFinish
}
func parseFinish(l *itemParser) parserStateFunc {
if l.pos > l.start {
l.emit(parsedItemContent)
}
return nil
}