Debian Codegraph @

parser/pageparser/pagelexer_intro.go

raw
1 // Copyright 2018 The Hugo Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package pageparser
15
16 func lexIntroSection(l *pageLexer) stateFunc {
17 LOOP:
18 for {
19 r := l.next()
20 if r == eof {
21 break
22 }
23
24 switch {
25 case r == '+':
26 return l.lexFrontMatterSection(TypeFrontMatterTOML, r, "TOML", delimTOML)
27 case r == '-':
28 return l.lexFrontMatterSection(TypeFrontMatterYAML, r, "YAML", delimYAML)
29 case r == '{':
30 return lexFrontMatterJSON
31 case r == '#':
32 return lexFrontMatterOrgMode
33 case !isASCIISpace(r) && !isEndOfLine(r):
34 break LOOP
35 }
36 }
37
38 // Now move on to the shortcodes.
39 return lexMainSection
40 }
41
42 func lexFrontMatterJSON(l *pageLexer) stateFunc {
43 // Include the left delimiter
44 l.backup()
45
46 var (
47 inQuote bool
48 level int
49 )
50
51 for {
52
53 r := l.next()
54
55 switch {
56 case r == eof:
57 return l.errorf("unexpected EOF parsing JSON front matter")
58 case r == '{':
59 if !inQuote {
60 level++
61 }
62 case r == '}':
63 if !inQuote {
64 level--
65 }
66 case r == '"':
67 inQuote = !inQuote
68 case r == '\\':
69 // This may be an escaped quote. Make sure it's not marked as a
70 // real one.
71 l.next()
72 }
73
74 if level == 0 {
75 break
76 }
77 }
78
79 l.consumeCRLF()
80 l.emit(TypeFrontMatterJSON)
81
82 return lexMainSection
83 }
84
85 func lexFrontMatterOrgMode(l *pageLexer) stateFunc {
86 /*
87 #+TITLE: Test File For chaseadamsio/goorgeous
88 #+AUTHOR: Chase Adams
89 #+DESCRIPTION: Just another golang parser for org content!
90 */
91
92 l.backup()
93
94 if !l.hasPrefix(delimOrg) {
95 return lexMainSection
96 }
97
98 l.summaryDivider = summaryDividerOrg
99
100 // Read lines until we no longer see a #+ prefix
101 LOOP:
102 for {
103
104 r := l.next()
105
106 switch {
107 case r == '\n':
108 if !l.hasPrefix(delimOrg) {
109 break LOOP
110 }
111 case r == eof:
112 break LOOP
113
114 }
115 }
116
117 l.emit(TypeFrontMatterORG)
118
119 return lexMainSection
120 }
121
122 // Handle YAML or TOML front matter.
123 func (l *pageLexer) lexFrontMatterSection(tp ItemType, delimr rune, name string, delim []byte) stateFunc {
124 for range 2 {
125 if r := l.next(); r != delimr {
126 return l.errorf("invalid %s delimiter", name)
127 }
128 }
129
130 // Let front matter start at line 1
131 wasEndOfLine := l.consumeCRLF()
132 // We don't care about the delimiters.
133 l.ignore()
134
135 var r rune
136
137 for {
138 if !wasEndOfLine {
139 r = l.next()
140 if r == eof {
141 return l.errorf("EOF looking for end %s front matter delimiter", name)
142 }
143 }
144
145 if wasEndOfLine || isEndOfLine(r) {
146 if l.hasPrefix(delim) {
147 l.emit(tp)
148 l.pos += 3
149 l.consumeCRLF()
150 l.ignore()
151 break
152 }
153 }
154
155 wasEndOfLine = false
156 }
157
158 return lexMainSection
159 }