hugolib: Redo the summary delimiter logic

Now that we have a proper page parse tree, this can be greatly simplified.

See #5324
This commit is contained in:
Bjørn Erik Pedersen
2018-10-19 11:30:57 +02:00
parent 1e3e34002d
commit 44da60d869
12 changed files with 75 additions and 142 deletions

View File

@@ -20,7 +20,7 @@ import (
"github.com/chaseadamsio/goorgeous"
"github.com/gohugoio/hugo/parser/pageparser"
"github.com/pkg/errors"
yaml "gopkg.in/yaml.v1"
yaml "gopkg.in/yaml.v2"
)
type Format string

View File

@@ -19,7 +19,7 @@ import (
"fmt"
"github.com/spf13/cast"
yaml "gopkg.in/yaml.v1"
yaml "gopkg.in/yaml.v2"
)
// HandleYAMLData unmarshals YAML-encoded datum and returns a Go interface

View File

@@ -13,10 +13,13 @@
package pageparser
import "fmt"
import (
"bytes"
"fmt"
)
type Item struct {
Typ ItemType
Type ItemType
pos pos
Val []byte
}
@@ -28,65 +31,69 @@ func (i Item) ValStr() string {
}
func (i Item) IsText() bool {
return i.Typ == tText
return i.Type == tText
}
func (i Item) IsNonWhitespace() bool {
return len(bytes.TrimSpace(i.Val)) > 0
}
func (i Item) IsShortcodeName() bool {
return i.Typ == tScName
return i.Type == tScName
}
func (i Item) IsLeftShortcodeDelim() bool {
return i.Typ == tLeftDelimScWithMarkup || i.Typ == tLeftDelimScNoMarkup
return i.Type == tLeftDelimScWithMarkup || i.Type == tLeftDelimScNoMarkup
}
func (i Item) IsRightShortcodeDelim() bool {
return i.Typ == tRightDelimScWithMarkup || i.Typ == tRightDelimScNoMarkup
return i.Type == tRightDelimScWithMarkup || i.Type == tRightDelimScNoMarkup
}
func (i Item) IsShortcodeClose() bool {
return i.Typ == tScClose
return i.Type == tScClose
}
func (i Item) IsShortcodeParam() bool {
return i.Typ == tScParam
return i.Type == tScParam
}
func (i Item) IsShortcodeParamVal() bool {
return i.Typ == tScParamVal
return i.Type == tScParamVal
}
func (i Item) IsShortcodeMarkupDelimiter() bool {
return i.Typ == tLeftDelimScWithMarkup || i.Typ == tRightDelimScWithMarkup
return i.Type == tLeftDelimScWithMarkup || i.Type == tRightDelimScWithMarkup
}
func (i Item) IsFrontMatter() bool {
return i.Typ >= TypeFrontMatterYAML && i.Typ <= TypeFrontMatterORG
return i.Type >= TypeFrontMatterYAML && i.Type <= TypeFrontMatterORG
}
func (i Item) IsDone() bool {
return i.Typ == tError || i.Typ == tEOF
return i.Type == tError || i.Type == tEOF
}
func (i Item) IsEOF() bool {
return i.Typ == tEOF
return i.Type == tEOF
}
func (i Item) IsError() bool {
return i.Typ == tError
return i.Type == tError
}
func (i Item) String() string {
switch {
case i.Typ == tEOF:
case i.Type == tEOF:
return "EOF"
case i.Typ == tError:
case i.Type == tError:
return string(i.Val)
case i.Typ > tKeywordMarker:
case i.Type > tKeywordMarker:
return fmt.Sprintf("<%s>", i.Val)
case len(i.Val) > 50:
return fmt.Sprintf("%v:%.20q...", i.Typ, i.Val)
return fmt.Sprintf("%v:%.20q...", i.Type, i.Val)
}
return fmt.Sprintf("%v:[%s]", i.Typ, i.Val)
return fmt.Sprintf("%v:[%s]", i.Type, i.Val)
}
type ItemType int

View File

@@ -235,6 +235,7 @@ func lexMainSection(l *pageLexer) stateFunc {
}
l.summaryDividerChecked = true
l.pos += pos(len(summaryDivider))
//l.consumeCRLF()
l.emit(TypeLeadSummaryDivider)
} else if l.hasPrefix(summaryDividerOrg) {
if l.pos > l.start {
@@ -242,6 +243,7 @@ func lexMainSection(l *pageLexer) stateFunc {
}
l.summaryDividerChecked = true
l.pos += pos(len(summaryDividerOrg))
//l.consumeCRLF()
l.emit(TypeSummaryDividerOrg)
}
}

View File

@@ -86,7 +86,7 @@ func (t *Iterator) Backup() {
// check for non-error and non-EOF types coming next
func (t *Iterator) IsValueNext() bool {
i := t.Peek()
return i.Typ != tError && i.Typ != tEOF
return i.Type != tError && i.Type != tEOF
}
// look at, but do not consume, the next item
@@ -95,12 +95,23 @@ func (t *Iterator) Peek() Item {
return t.l.items[t.lastPos+1]
}
// PeekWalk will feed the next items in the iterator to walkFn
// until it returns false.
func (t *Iterator) PeekWalk(walkFn func(item Item) bool) {
for i := t.lastPos + 1; i < pos(len(t.l.items)); i++ {
item := t.l.items[i]
if !walkFn(item) {
break
}
}
}
// Consume is a convencience method to consume the next n tokens,
// but back off Errors and EOF.
func (t *Iterator) Consume(cnt int) {
for i := 0; i < cnt; i++ {
token := t.Next()
if token.Typ == tError || token.Typ == tEOF {
if token.Type == tError || token.Type == tEOF {
t.Backup()
break
}

View File

@@ -91,7 +91,7 @@ func collect(input []byte, skipFrontMatter bool, stateStart stateFunc) (items []
for {
item := t.Next()
items = append(items, item)
if item.Typ == tEOF || item.Typ == tError {
if item.Type == tEOF || item.Type == tError {
break
}
}
@@ -104,7 +104,7 @@ func equal(i1, i2 []Item) bool {
return false
}
for k := range i1 {
if i1[k].Typ != i2[k].Typ {
if i1[k].Type != i2[k].Type {
return false
}
if !reflect.DeepEqual(i1[k].Val, i2[k].Val) {