-
Notifications
You must be signed in to change notification settings - Fork 33
/
parser_frontmatter.go
87 lines (74 loc) · 1.55 KB
/
parser_frontmatter.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
package document
import (
"bytes"
"errors"
)
var errParseRawFrontmatter = errors.New("failed to extract frontmatter")
// parseRawFrontmatter is a parser state function that extracts the frontmatter with
// "---" or "+++" delimiters. According to the spec, there must be three delimiter characters.
func parseRawFrontmatter(l *itemParser, delimiter byte) parserStateFunc {
for i := 0; i < 2; i++ {
if r := l.next(); r != rune(delimiter) {
l.error(errParseRawFrontmatter)
return nil
}
}
wasEndOfLine := l.consume(crlf)
var r rune
for {
if !wasEndOfLine {
r = l.next()
if r == eof {
l.errorf("got EOF while looking for the end of the front matter delimiter")
return nil
}
}
if wasEndOfLine || isEOL(r) {
if l.hasPrefix(bytes.Repeat([]byte{delimiter}, 3)) {
l.pos += 3
l.emit(parsedItemFrontMatter)
l.consume(crlf)
l.ignore()
break
}
}
wasEndOfLine = false
}
return parseContent
}
func parseRawFrontmatterJSON(l *itemParser) parserStateFunc {
l.backup()
var (
inQuote bool
level int
)
for {
r := l.next()
switch {
case r == eof:
l.errorf("got EOF while looking for the end of the JSON front matter")
return nil
case r == '{':
if !inQuote {
level++
}
case r == '}':
if !inQuote {
level--
}
case r == '"':
inQuote = !inQuote
case r == '\\':
// This may be an escaped quote. Make sure it's not marked as a
// real one.
l.next()
}
if level == 0 {
break
}
}
l.emit(parsedItemFrontMatter)
l.consume(crlf)
l.ignore()
return parseContent
}