From b296872ff8d44b08d3b2a66109eeab768f957667 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Tue, 23 Oct 2018 14:37:09 +0200 Subject: [PATCH] Resolve error handling/parser related TODOs See #5324 --- hugolib/hugo_sites_build_errors_test.go | 2 +- hugolib/hugo_sites_build_test.go | 8 ++- hugolib/page.go | 8 ++- hugolib/page_content.go | 21 +++++--- hugolib/page_test.go | 12 ++--- hugolib/site_render.go | 5 +- hugolib/site_test.go | 3 +- parser/pageparser/item.go | 5 +- parser/pageparser/pagelexer.go | 62 +++++++++++----------- parser/pageparser/pagelexer_test.go | 8 +-- parser/pageparser/pageparser.go | 2 +- parser/pageparser/pageparser_intro_test.go | 8 +-- tpl/template.go | 2 +- 13 files changed, 73 insertions(+), 73 deletions(-) diff --git a/hugolib/hugo_sites_build_errors_test.go b/hugolib/hugo_sites_build_errors_test.go index 1e53eb3c46b..8e913f061b2 100644 --- a/hugolib/hugo_sites_build_errors_test.go +++ b/hugolib/hugo_sites_build_errors_test.go @@ -191,7 +191,7 @@ func TestSiteBuildErrors(t *testing.T) { }, assertBuildError: func(a testSiteBuildErrorAsserter, err error) { assert.Error(err) - assert.Contains(err.Error(), `"content/mytoml.md": render of "page" failed: execute of template failed: panic in Execute`) + assert.Contains(err.Error(), `execute of template failed: panic in Execute`) }, }, } diff --git a/hugolib/hugo_sites_build_test.go b/hugolib/hugo_sites_build_test.go index 727cc6ed924..f1e317f5967 100644 --- a/hugolib/hugo_sites_build_test.go +++ b/hugolib/hugo_sites_build_test.go @@ -631,12 +631,10 @@ func assertShouldNotBuild(t *testing.T, sites *HugoSites) { for _, p := range s.rawAllPages { // No HTML when not processed require.Equal(t, p.shouldBuild(), bytes.Contains(p.workContent, []byte(" - TypeSummaryDividerOrg // # more + TypeLeadSummaryDivider // , # more TypeFrontMatterYAML TypeFrontMatterTOML TypeFrontMatterJSON diff --git a/parser/pageparser/pagelexer.go b/parser/pageparser/pagelexer.go index e02475d420e..201abf8c809 100644 --- a/parser/pageparser/pagelexer.go +++ b/parser/pageparser/pagelexer.go @@ -48,6 +48,8 @@ type pageLexer struct { start int // item start position width int // width of last element + // The summary divider to look for. + summaryDivider []byte // Set when we have parsed any summary divider summaryDividerChecked bool @@ -69,7 +71,6 @@ func (l *pageLexer) Input() []byte { // note: the input position here is normally 0 (start), but // can be set if position of first shortcode is known -// TODO(bep) 2errors byte func newPageLexer(input []byte, inputPosition int, stateStart stateFunc) *pageLexer { lexer := &pageLexer{ input: input, @@ -117,7 +118,7 @@ var ( delimTOML = []byte("+++") delimYAML = []byte("---") delimOrg = []byte("#+") - htmlCOmmentStart = []byte("") ) @@ -195,17 +196,18 @@ func (l *pageLexer) consumeCRLF() bool { func lexMainSection(l *pageLexer) stateFunc { // Fast forward as far as possible. - var l1, l2, l3 int - if !l.summaryDividerChecked { - // TODO(bep) 2errors make the summary divider per type - l1 = l.index(summaryDivider) - l2 = l.index(summaryDividerOrg) - if l1 == -1 && l2 == -1 { + var l1, l2 int + + if !l.summaryDividerChecked && l.summaryDivider != nil { + l1 = l.index(l.summaryDivider) + if l1 == -1 { l.summaryDividerChecked = true } } - l3 = l.index(leftDelimSc) - skip := minPositiveIndex(l1, l2, l3) + + l2 = l.index(leftDelimSc) + skip := minIndex(l1, l2) + if skip > 0 { l.pos += skip } @@ -225,23 +227,14 @@ func lexMainSection(l *pageLexer) stateFunc { return lexShortcodeLeftDelim } - if !l.summaryDividerChecked { - if l.hasPrefix(summaryDivider) { + if !l.summaryDividerChecked && l.summaryDivider != nil { + if l.hasPrefix(l.summaryDivider) { if l.pos > l.start { l.emit(tText) } l.summaryDividerChecked = true - l.pos += len(summaryDivider) - //l.consumeCRLF() + l.pos += len(l.summaryDivider) l.emit(TypeLeadSummaryDivider) - } else if l.hasPrefix(summaryDividerOrg) { - if l.pos > l.start { - l.emit(tText) - } - l.summaryDividerChecked = true - l.pos += len(summaryDividerOrg) - //l.consumeCRLF() - l.emit(TypeSummaryDividerOrg) } } @@ -261,6 +254,8 @@ func (l *pageLexer) isShortCodeStart() bool { } func lexIntroSection(l *pageLexer) stateFunc { + l.summaryDivider = summaryDivider + LOOP: for { r := l.next() @@ -283,7 +278,7 @@ LOOP: // No front matter. if r == '<' { l.backup() - if l.hasPrefix(htmlCOmmentStart) { + if l.hasPrefix(htmlCommentStart) { right := l.index(htmlCOmmentEnd) if right == -1 { return l.errorf("starting HTML comment with no end") @@ -291,10 +286,14 @@ LOOP: l.pos += right + len(htmlCOmmentEnd) l.emit(TypeHTMLComment) } else { - // Not need to look further. Hugo treats this as plain HTML, - // no front matter, no shortcodes, no nothing. - l.pos = len(l.input) - l.emit(TypeHTMLDocument) + if l.pos > l.start { + l.emit(tText) + } + l.next() + // This is the start of a plain HTML document with no + // front matter. I still can contain shortcodes, so we + // have to keep looking. + l.emit(TypeHTMLStart) } } break LOOP @@ -365,10 +364,11 @@ func lexFrontMatterOrgMode(l *pageLexer) stateFunc { #+DESCRIPTION: Just another golang parser for org content! */ + l.summaryDivider = summaryDividerOrg + l.backup() if !l.hasPrefix(delimOrg) { - // TODO(bep) consider error return lexMainSection } @@ -715,12 +715,12 @@ func (l *pageLexer) currentRightShortcodeDelim() []byte { // helper functions -// returns the min index > 0 -func minPositiveIndex(indices ...int) int { +// returns the min index >= 0 +func minIndex(indices ...int) int { min := -1 for _, j := range indices { - if j <= 0 { + if j < 0 { continue } if min == -1 { diff --git a/parser/pageparser/pagelexer_test.go b/parser/pageparser/pagelexer_test.go index 5c85df0176b..ff0e59e6a82 100644 --- a/parser/pageparser/pagelexer_test.go +++ b/parser/pageparser/pagelexer_test.go @@ -21,9 +21,9 @@ import ( func TestMinPositiveIndex(t *testing.T) { assert := require.New(t) - assert.Equal(1, minPositiveIndex(4, 1, 2, 3)) - assert.Equal(2, minPositiveIndex(4, 0, -2, 2, 5)) - assert.Equal(-1, minPositiveIndex()) - assert.Equal(-1, minPositiveIndex(-2, -3)) + assert.Equal(1, minIndex(4, 1, 2, 3)) + assert.Equal(0, minIndex(4, 0, -2, 2, 5)) + assert.Equal(-1, minIndex()) + assert.Equal(-1, minIndex(-2, -3)) } diff --git a/parser/pageparser/pageparser.go b/parser/pageparser/pageparser.go index 6e75f195ade..75439712166 100644 --- a/parser/pageparser/pageparser.go +++ b/parser/pageparser/pageparser.go @@ -48,7 +48,7 @@ func Parse(r io.Reader) (Result, error) { } func parseMainSection(input []byte, from int) Result { - lexer := newPageLexer(input, from, lexMainSection) // TODO(bep) 2errors + lexer := newPageLexer(input, from, lexMainSection) lexer.run() return lexer } diff --git a/parser/pageparser/pageparser_intro_test.go b/parser/pageparser/pageparser_intro_test.go index 32de6dc4446..ba4a2c84b76 100644 --- a/parser/pageparser/pageparser_intro_test.go +++ b/parser/pageparser/pageparser_intro_test.go @@ -38,7 +38,7 @@ var ( tstFrontMatterJSON = nti(TypeFrontMatterJSON, tstJSON+"\r\n") tstSomeText = nti(tText, "\nSome text.\n") tstSummaryDivider = nti(TypeLeadSummaryDivider, "") - tstSummaryDividerOrg = nti(TypeSummaryDividerOrg, "# more") + tstHtmlStart = nti(TypeHTMLStart, "<") tstORG = ` #+TITLE: T1 @@ -54,8 +54,8 @@ var crLfReplacer = strings.NewReplacer("\r", "#", "\n", "$") var frontMatterTests = []lexerTest{ {"empty", "", []Item{tstEOF}}, {"Byte order mark", "\ufeff\nSome text.\n", []Item{nti(TypeIgnore, "\ufeff"), tstSomeText, tstEOF}}, - {"HTML Document", ` `, []Item{nti(TypeHTMLDocument, " "), tstEOF}}, - {"HTML Document 2", `

Hugo Rocks

`, []Item{nti(TypeHTMLDocument, "

Hugo Rocks

"), tstEOF}}, + {"HTML Document", ` `, []Item{nti(tText, " "), tstHtmlStart, nti(tText, "html> "), tstEOF}}, + {"HTML Document with shortcode", `{{< sc1 >}}`, []Item{tstHtmlStart, nti(tText, "html>"), tstLeftNoMD, tstSC1, tstRightNoMD, nti(tText, ""), tstEOF}}, {"No front matter", "\nSome text.\n", []Item{tstSomeText, tstEOF}}, {"YAML front matter", "---\nfoo: \"bar\"\n---\n\nSome text.\n", []Item{tstFrontMatterYAML, tstSomeText, tstEOF}}, {"YAML empty front matter", "---\n---\n\nSome text.\n", []Item{nti(TypeFrontMatterYAML, ""), tstSomeText, tstEOF}}, @@ -65,7 +65,7 @@ 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, tstSummaryDividerOrg, 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}}, } diff --git a/tpl/template.go b/tpl/template.go index 12a4607fbf7..9687054934a 100644 --- a/tpl/template.go +++ b/tpl/template.go @@ -179,7 +179,7 @@ func (t *TemplateAdapter) addFileContext(name string, inerr error) error { } return false } - // TODO(bep) 2errors text vs HTML + fe, ok := herrors.WithFileContext(ferr, realFilename, f, lineMatcher) if ok || !hasMaster { return fe