| 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 |
import ( |
| 17 |
"fmt" |
| 18 |
"strings" |
| 19 |
"testing" |
| 20 |
|
| 21 |
qt "github.com/frankban/quicktest" |
| 22 |
) |
| 23 |
|
| 24 |
type lexerTest struct { |
| 25 |
name string |
| 26 |
input string |
| 27 |
items []typeText |
| 28 |
err error |
| 29 |
} |
| 30 |
|
| 31 |
type typeText struct { |
| 32 |
typ ItemType |
| 33 |
text string |
| 34 |
} |
| 35 |
|
| 36 |
func nti(tp ItemType, val string) typeText { |
| 37 |
return typeText{typ: tp, text: val} |
| 38 |
} |
| 39 |
|
| 40 |
var ( |
| 41 |
tstJSON = `{ "a": { "b": "\"Hugo\"}" } }` |
| 42 |
tstFrontMatterTOML = nti(TypeFrontMatterTOML, "foo = \"bar\"\n") |
| 43 |
tstFrontMatterYAML = nti(TypeFrontMatterYAML, "foo: \"bar\"\n") |
| 44 |
tstFrontMatterYAMLCRLF = nti(TypeFrontMatterYAML, "foo: \"bar\"\r\n") |
| 45 |
tstFrontMatterJSON = nti(TypeFrontMatterJSON, tstJSON+"\r\n") |
| 46 |
tstSomeText = nti(tText, "\nSome text.\n") |
| 47 |
tstSummaryDivider = nti(TypeLeadSummaryDivider, "<!--more-->\n") |
| 48 |
tstNewline = nti(tText, "\n") |
| 49 |
|
| 50 |
tstORG = ` |
| 51 |
#+TITLE: T1 |
| 52 |
#+AUTHOR: A1 |
| 53 |
#+DESCRIPTION: D1 |
| 54 |
` |
| 55 |
tstFrontMatterORG = nti(TypeFrontMatterORG, tstORG) |
| 56 |
) |
| 57 |
|
| 58 |
var crLfReplacer = strings.NewReplacer("\r", "#", "\n", "$") |
| 59 |
|
| 60 |
// TODO(bep) a way to toggle ORG mode vs the rest. |
| 61 |
var frontMatterTests = []lexerTest{ |
| 62 |
{"empty", "", []typeText{tstEOF}, nil}, |
| 63 |
{"Byte order mark", "\ufeff\nSome text.\n", []typeText{nti(TypeIgnore, "\ufeff"), tstSomeText, tstEOF}, nil}, |
| 64 |
{"No front matter", "\nSome text.\n", []typeText{tstSomeText, tstEOF}, nil}, |
| 65 |
{"YAML front matter", "---\nfoo: \"bar\"\n---\n\nSome text.\n", []typeText{tstFrontMatterYAML, tstSomeText, tstEOF}, nil}, |
| 66 |
{"YAML empty front matter", "---\n---\n\nSome text.\n", []typeText{nti(TypeFrontMatterYAML, ""), tstSomeText, tstEOF}, nil}, |
| 67 |
// Note that we keep all bytes as they are, but we need to handle CRLF |
| 68 |
{"YAML front matter CRLF", "---\r\nfoo: \"bar\"\r\n---\n\nSome text.\n", []typeText{tstFrontMatterYAMLCRLF, tstSomeText, tstEOF}, nil}, |
| 69 |
{"TOML front matter", "+++\nfoo = \"bar\"\n+++\n\nSome text.\n", []typeText{tstFrontMatterTOML, tstSomeText, tstEOF}, nil}, |
| 70 |
{"JSON front matter", tstJSON + "\r\n\nSome text.\n", []typeText{tstFrontMatterJSON, tstSomeText, tstEOF}, nil}, |
| 71 |
{"ORG front matter", tstORG + "\nSome text.\n", []typeText{tstFrontMatterORG, tstSomeText, tstEOF}, nil}, |
| 72 |
{"Summary divider ORG", tstORG + "\nSome text.\n# more\nSome text.\n", []typeText{tstFrontMatterORG, tstSomeText, nti(TypeLeadSummaryDivider, "# more\n"), nti(tText, "Some text.\n"), tstEOF}, nil}, |
| 73 |
{"Summary divider", "+++\nfoo = \"bar\"\n+++\n\nSome text.\n<!--more-->\nSome text.\n", []typeText{tstFrontMatterTOML, tstSomeText, tstSummaryDivider, nti(tText, "Some text.\n"), tstEOF}, nil}, |
| 74 |
{"Summary divider same line", "+++\nfoo = \"bar\"\n+++\n\nSome text.<!--more-->Some text.\n", []typeText{tstFrontMatterTOML, nti(tText, "\nSome text."), nti(TypeLeadSummaryDivider, "<!--more-->"), nti(tText, "Some text.\n"), tstEOF}, nil}, |
| 75 |
// https://github.com/gohugoio/hugo/issues/5402 |
| 76 |
{"Summary and shortcode, no space", "+++\nfoo = \"bar\"\n+++\n\nSome text.\n<!--more-->{{< sc1 >}}\nSome text.\n", []typeText{tstFrontMatterTOML, tstSomeText, nti(TypeLeadSummaryDivider, "<!--more-->"), tstLeftNoMD, tstSC1, tstRightNoMD, tstSomeText, tstEOF}, nil}, |
| 77 |
// https://github.com/gohugoio/hugo/issues/5464 |
| 78 |
{"Summary and shortcode only", "+++\nfoo = \"bar\"\n+++\n{{< sc1 >}}\n<!--more-->\n{{< sc2 >}}", []typeText{tstFrontMatterTOML, tstLeftNoMD, tstSC1, tstRightNoMD, tstNewline, tstSummaryDivider, tstLeftNoMD, tstSC2, tstRightNoMD, tstEOF}, nil}, |
| 79 |
} |
| 80 |
|
| 81 |
func TestFrontMatter(t *testing.T) { |
| 82 |
t.Parallel() |
| 83 |
c := qt.New(t) |
| 84 |
for i, test := range frontMatterTests { |
| 85 |
items, err := collect([]byte(test.input), false, lexIntroSection) |
| 86 |
if err != nil { |
| 87 |
c.Assert(err, qt.Equals, test.err) |
| 88 |
continue |
| 89 |
} else { |
| 90 |
c.Assert(test.err, qt.IsNil) |
| 91 |
} |
| 92 |
if !equal(test.input, items, test.items) { |
| 93 |
got := itemsToString(items, []byte(test.input)) |
| 94 |
expected := testItemsToString(test.items) |
| 95 |
c.Assert(got, qt.Equals, expected, qt.Commentf("Test %d: %s", i, test.name)) |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
func itemsToString(items []Item, source []byte) string { |
| 101 |
var sb strings.Builder |
| 102 |
for i, item := range items { |
| 103 |
var s string |
| 104 |
if item.Err != nil { |
| 105 |
s = item.Err.Error() |
| 106 |
} else { |
| 107 |
s = string(item.Val(source)) |
| 108 |
} |
| 109 |
sb.WriteString(fmt.Sprintf("%s: %s\n", item.Type, s)) |
| 110 |
|
| 111 |
if i < len(items)-1 { |
| 112 |
sb.WriteString("\n") |
| 113 |
} |
| 114 |
} |
| 115 |
return crLfReplacer.Replace(sb.String()) |
| 116 |
} |
| 117 |
|
| 118 |
func testItemsToString(items []typeText) string { |
| 119 |
var sb strings.Builder |
| 120 |
for i, item := range items { |
| 121 |
sb.WriteString(fmt.Sprintf("%s: %s\n", item.typ, item.text)) |
| 122 |
|
| 123 |
if i < len(items)-1 { |
| 124 |
sb.WriteString("\n") |
| 125 |
} |
| 126 |
} |
| 127 |
return crLfReplacer.Replace(sb.String()) |
| 128 |
} |
| 129 |
|
| 130 |
func collectWithConfig(input []byte, skipFrontMatter bool, stateStart stateFunc, cfg Config) (items []Item, err error) { |
| 131 |
l := newPageLexer(input, stateStart, cfg) |
| 132 |
l.run() |
| 133 |
iter := NewIterator(l.items) |
| 134 |
|
| 135 |
for { |
| 136 |
if l.err != nil { |
| 137 |
return nil, l.err |
| 138 |
} |
| 139 |
item := iter.Next() |
| 140 |
items = append(items, item) |
| 141 |
if item.Type == tEOF || item.Type == tError { |
| 142 |
break |
| 143 |
} |
| 144 |
} |
| 145 |
return |
| 146 |
} |
| 147 |
|
| 148 |
func collect(input []byte, skipFrontMatter bool, stateStart stateFunc) (items []Item, err error) { |
| 149 |
var cfg Config |
| 150 |
|
| 151 |
return collectWithConfig(input, skipFrontMatter, stateStart, cfg) |
| 152 |
} |
| 153 |
|
| 154 |
func collectStringMain(input string) ([]Item, error) { |
| 155 |
return collect([]byte(input), true, lexMainSection) |
| 156 |
} |
| 157 |
|
| 158 |
// no positional checking, for now ... |
| 159 |
func equal(source string, got []Item, expect []typeText) bool { |
| 160 |
if len(got) != len(expect) { |
| 161 |
return false |
| 162 |
} |
| 163 |
sourceb := []byte(source) |
| 164 |
for k := range got { |
| 165 |
g := got[k] |
| 166 |
e := expect[k] |
| 167 |
if g.Type != e.typ { |
| 168 |
return false |
| 169 |
} |
| 170 |
|
| 171 |
var s string |
| 172 |
if g.Err != nil { |
| 173 |
s = g.Err.Error() |
| 174 |
} else { |
| 175 |
s = string(g.Val(sourceb)) |
| 176 |
} |
| 177 |
|
| 178 |
if s != e.text { |
| 179 |
return false |
| 180 |
} |
| 181 |
|
| 182 |
} |
| 183 |
return true |
| 184 |
} |