pagelexer_intro.go - hugo - [fork] hugo port for 9front
HTML git clone https://git.drkhsh.at/hugo.git
DIR Log
DIR Files
DIR Refs
DIR Submodules
DIR README
DIR LICENSE
---
pagelexer_intro.go (3111B)
---
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 r == byteOrderMark:
34 l.emit(TypeIgnore)
35 case !isSpace(r) && !isEndOfLine(r):
36 break LOOP
37 }
38 }
39
40 // Now move on to the shortcodes.
41 return lexMainSection
42 }
43
44 func lexFrontMatterJSON(l *pageLexer) stateFunc {
45 // Include the left delimiter
46 l.backup()
47
48 var (
49 inQuote bool
50 level int
51 )
52
53 for {
54
55 r := l.next()
56
57 switch {
58 case r == eof:
59 return l.errorf("unexpected EOF parsing JSON front matter")
60 case r == '{':
61 if !inQuote {
62 level++
63 }
64 case r == '}':
65 if !inQuote {
66 level--
67 }
68 case r == '"':
69 inQuote = !inQuote
70 case r == '\\':
71 // This may be an escaped quote. Make sure it's not marked as a
72 // real one.
73 l.next()
74 }
75
76 if level == 0 {
77 break
78 }
79 }
80
81 l.consumeCRLF()
82 l.emit(TypeFrontMatterJSON)
83
84 return lexMainSection
85 }
86
87 func lexFrontMatterOrgMode(l *pageLexer) stateFunc {
88 /*
89 #+TITLE: Test File For chaseadamsio/goorgeous
90 #+AUTHOR: Chase Adams
91 #+DESCRIPTION: Just another golang parser for org content!
92 */
93
94 l.backup()
95
96 if !l.hasPrefix(delimOrg) {
97 return lexMainSection
98 }
99
100 l.summaryDivider = summaryDividerOrg
101
102 // Read lines until we no longer see a #+ prefix
103 LOOP:
104 for {
105
106 r := l.next()
107
108 switch {
109 case r == '\n':
110 if !l.hasPrefix(delimOrg) {
111 break LOOP
112 }
113 case r == eof:
114 break LOOP
115
116 }
117 }
118
119 l.emit(TypeFrontMatterORG)
120
121 return lexMainSection
122 }
123
124 // Handle YAML or TOML front matter.
125 func (l *pageLexer) lexFrontMatterSection(tp ItemType, delimr rune, name string, delim []byte) stateFunc {
126 for range 2 {
127 if r := l.next(); r != delimr {
128 return l.errorf("invalid %s delimiter", name)
129 }
130 }
131
132 // Let front matter start at line 1
133 wasEndOfLine := l.consumeCRLF()
134 // We don't care about the delimiters.
135 l.ignore()
136
137 var r rune
138
139 for {
140 if !wasEndOfLine {
141 r = l.next()
142 if r == eof {
143 return l.errorf("EOF looking for end %s front matter delimiter", name)
144 }
145 }
146
147 if wasEndOfLine || isEndOfLine(r) {
148 if l.hasPrefix(delim) {
149 l.emit(tp)
150 l.pos += 3
151 l.consumeCRLF()
152 l.ignore()
153 break
154 }
155 }
156
157 wasEndOfLine = false
158 }
159
160 return lexMainSection
161 }