From b2a676f5f09a3eea360887b099b9d5fc25a88492 Mon Sep 17 00:00:00 2001 From: Bjørn Erik Pedersen Date: Tue, 30 Oct 2018 20:24:34 +0100 Subject: hugolib: Fix broken manual summary handling Fixes #5381 --- parser/pageparser/pagelexer.go | 12 ++++++++++++ parser/pageparser/pageparser_intro_test.go | 7 ++++--- 2 files changed, 16 insertions(+), 3 deletions(-) (limited to 'parser') diff --git a/parser/pageparser/pagelexer.go b/parser/pageparser/pagelexer.go index ddf109b3d..565be2994 100644 --- a/parser/pageparser/pagelexer.go +++ b/parser/pageparser/pagelexer.go @@ -194,6 +194,16 @@ func (l *pageLexer) consumeCRLF() bool { return consumed } +func (l *pageLexer) consumeSpace() { + for { + r := l.next() + if r == eof || !unicode.IsSpace(r) { + l.backup() + return + } + } +} + func lexMainSection(l *pageLexer) stateFunc { // Fast forward as far as possible. var l1, l2 int @@ -234,6 +244,8 @@ func lexMainSection(l *pageLexer) stateFunc { } l.summaryDividerChecked = true l.pos += len(l.summaryDivider) + // This makes it a little easier to reason about later. + l.consumeSpace() l.emit(TypeLeadSummaryDivider) } } diff --git a/parser/pageparser/pageparser_intro_test.go b/parser/pageparser/pageparser_intro_test.go index ba4a2c84b..60c431c10 100644 --- a/parser/pageparser/pageparser_intro_test.go +++ b/parser/pageparser/pageparser_intro_test.go @@ -37,7 +37,7 @@ var ( tstFrontMatterYAMLCRLF = nti(TypeFrontMatterYAML, "foo: \"bar\"\r\n") tstFrontMatterJSON = nti(TypeFrontMatterJSON, tstJSON+"\r\n") tstSomeText = nti(tText, "\nSome text.\n") - tstSummaryDivider = nti(TypeLeadSummaryDivider, "") + tstSummaryDivider = nti(TypeLeadSummaryDivider, "\n") tstHtmlStart = nti(TypeHTMLStart, "<") tstORG = ` @@ -65,8 +65,9 @@ var frontMatterTests = []lexerTest{ {"TOML front matter", "+++\nfoo = \"bar\"\n+++\n\nSome text.\n", []Item{tstFrontMatterTOML, tstSomeText, tstEOF}}, {"JSON front matter", tstJSON + "\r\n\nSome text.\n", []Item{tstFrontMatterJSON, tstSomeText, tstEOF}}, {"ORG front matter", tstORG + "\nSome text.\n", []Item{tstFrontMatterORG, tstSomeText, tstEOF}}, - {"Summary divider ORG", tstORG + "\nSome text.\n# more\nSome text.\n", []Item{tstFrontMatterORG, tstSomeText, nti(TypeLeadSummaryDivider, "# more"), tstSomeText, tstEOF}}, - {"Summary divider", "+++\nfoo = \"bar\"\n+++\n\nSome text.\n\nSome text.\n", []Item{tstFrontMatterTOML, tstSomeText, tstSummaryDivider, tstSomeText, tstEOF}}, + {"Summary divider ORG", tstORG + "\nSome text.\n# more\nSome text.\n", []Item{tstFrontMatterORG, tstSomeText, nti(TypeLeadSummaryDivider, "# more\n"), nti(tText, "Some text.\n"), tstEOF}}, + {"Summary divider", "+++\nfoo = \"bar\"\n+++\n\nSome text.\n\nSome text.\n", []Item{tstFrontMatterTOML, tstSomeText, tstSummaryDivider, nti(tText, "Some text.\n"), tstEOF}}, + {"Summary divider same line", "+++\nfoo = \"bar\"\n+++\n\nSome text.Some text.\n", []Item{tstFrontMatterTOML, nti(tText, "\nSome text."), nti(TypeLeadSummaryDivider, ""), nti(tText, "Some text.\n"), tstEOF}}, } func TestFrontMatter(t *testing.T) { -- cgit v1.2.3