diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-10-18 10:21:23 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-10-22 20:46:13 +0200 |
commit | 1e3e34002dae3d4a980141efcc86886e7de5bef8 (patch) | |
tree | 1c94049787d5e1076c5044662846ae3a586c5722 /hugolib/shortcode.go | |
parent | 1b7ecfc2e176315b69914756c70b46306561e4d1 (diff) |
hugolib: Integrate new page parser
See #5324
Diffstat (limited to 'hugolib/shortcode.go')
-rw-r--r-- | hugolib/shortcode.go | 88 |
1 files changed, 15 insertions, 73 deletions
diff --git a/hugolib/shortcode.go b/hugolib/shortcode.go index a21a10ad2..749730236 100644 --- a/hugolib/shortcode.go +++ b/hugolib/shortcode.go @@ -222,20 +222,28 @@ func (s *shortcodeHandler) nextPlaceholderID() int { } func (s *shortcodeHandler) createShortcodePlaceholder() string { - if s.placeholderFunc != nil { - return s.placeholderFunc() - } - return fmt.Sprintf("HAHA%s-%p-%d-HBHB", shortcodePlaceholderPrefix, s.p.Page, s.nextPlaceholderID()) + return s.placeholderFunc() } func newShortcodeHandler(p *Page) *shortcodeHandler { - return &shortcodeHandler{ + + s := &shortcodeHandler{ p: p.withoutContent(), contentShortcodes: newOrderedMap(), shortcodes: newOrderedMap(), nameSet: make(map[string]bool), renderedShortcodes: make(map[string]string), } + + placeholderFunc := p.s.shortcodePlaceholderFunc + if placeholderFunc == nil { + placeholderFunc = func() string { + return fmt.Sprintf("HAHA%s-%p-%d-HBHB", shortcodePlaceholderPrefix, p, s.nextPlaceholderID()) + } + + } + s.placeholderFunc = placeholderFunc + return s } // TODO(bep) make it non-global @@ -480,7 +488,7 @@ var errShortCodeIllegalState = errors.New("Illegal shortcode state") // pageTokens state: // - before: positioned just before the shortcode start // - after: shortcode(s) consumed (plural when they are nested) -func (s *shortcodeHandler) extractShortcode(ordinal int, pt *pageparser.Tokens, p *PageWithoutContent) (*shortcode, error) { +func (s *shortcodeHandler) extractShortcode(ordinal int, pt *pageparser.Iterator, p *Page) (*shortcode, error) { sc := &shortcode{ordinal: ordinal} var isInner = false @@ -510,7 +518,7 @@ Loop: if cnt > 0 { // nested shortcode; append it to inner content - pt.Backup3(currItem, next) + pt.Backup() nested, err := s.extractShortcode(nestedOrdinal, pt, p) nestedOrdinal++ if nested.name != "" { @@ -615,72 +623,6 @@ Loop: var shortCodeStart = []byte("{{") -func (s *shortcodeHandler) extractShortcodes(input []byte, p *PageWithoutContent) (string, error) { - - startIdx := bytes.Index(input, shortCodeStart) - - // short cut for docs with no shortcodes - if startIdx < 0 { - return string(input), nil - } - - // the parser takes a string; - // since this is an internal API, it could make sense to use the mutable []byte all the way, but - // it seems that the time isn't really spent in the byte copy operations, and the impl. gets a lot cleaner - pt := pageparser.ParseFrom(input, startIdx) - - result := bp.GetBuffer() - defer bp.PutBuffer(result) - //var result bytes.Buffer - - // the parser is guaranteed to return items in proper order or fail, so … - // … it's safe to keep some "global" state - var currShortcode shortcode - var ordinal int - -Loop: - for { - currItem := pt.Next() - - switch { - case currItem.IsText(): - result.WriteString(currItem.ValStr()) - case currItem.IsLeftShortcodeDelim(): - // let extractShortcode handle left delim (will do so recursively) - pt.Backup() - - currShortcode, err := s.extractShortcode(ordinal, pt, p) - - if currShortcode.name != "" { - s.nameSet[currShortcode.name] = true - } - - if err != nil { - return result.String(), err - } - - if currShortcode.params == nil { - currShortcode.params = make([]string, 0) - } - - placeHolder := s.createShortcodePlaceholder() - result.WriteString(placeHolder) - ordinal++ - s.shortcodes.Add(placeHolder, currShortcode) - case currItem.IsEOF(): - break Loop - case currItem.IsError(): - err := fmt.Errorf("%s:shortcode:%d: %s", - p.pathOrTitle(), (p.lineNumRawContentStart() + pt.LineNumber() - 1), currItem) - currShortcode.err = err - return result.String(), err - } - } - - return result.String(), nil - -} - // Replace prefixed shortcode tokens (HUGOSHORTCODE-1, HUGOSHORTCODE-2) with the real content. // Note: This function will rewrite the input slice. func replaceShortcodeTokens(source []byte, prefix string, replacements map[string]string) ([]byte, error) { |