diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-04-23 08:09:56 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-04-23 08:09:56 +0200 |
commit | 3decf4a327157e98d3da3502b6d777de63437c39 (patch) | |
tree | 012aaee3827a32eb13921bdc3e51c7ab82a7e2bf /hugolib/shortcode.go | |
parent | 24c662ce6bea029b0b24160376eaceaed1481d1c (diff) |
hugolib: Add zero-based Ordinal to shortcode
The count starts at 0 relative to the shortcode's parent: Either the page or the surrounding shortcode.
Access it in a shortcode like this:
```bash
Ordinal is {{ .Ordinal }}
```
Note that this is a shared ordinal for all shortcodes in the relevant context, so, as an example, you have this in a content page:
```markdown
This is a shortcode:
{{< hello >}}
This is another shortcode:
{{< hugo >}}
The `.Ordinal` you get in the two shortcodes above is 0 and 1.
```
See #3359
Diffstat (limited to 'hugolib/shortcode.go')
-rw-r--r-- | hugolib/shortcode.go | 21 |
1 files changed, 15 insertions, 6 deletions
diff --git a/hugolib/shortcode.go b/hugolib/shortcode.go index 933bbe44e..4792b7f61 100644 --- a/hugolib/shortcode.go +++ b/hugolib/shortcode.go @@ -40,7 +40,11 @@ type ShortcodeWithPage struct { Page *PageWithoutContent Parent *ShortcodeWithPage IsNamedParams bool - scratch *Scratch + + // Zero-based oridinal in relation to its parent. + Ordinal int + + scratch *Scratch } // Site returns information about the current site. @@ -122,6 +126,7 @@ type shortcode struct { name string inner []interface{} // string or nested shortcode params interface{} // map or array + ordinal int err error doMarkup bool } @@ -287,7 +292,7 @@ func renderShortcode( return "" } - data := &ShortcodeWithPage{Params: sc.params, Page: p, Parent: parent} + data := &ShortcodeWithPage{Ordinal: sc.ordinal, Params: sc.params, Page: p, Parent: parent} if sc.params != nil { data.IsNamedParams = reflect.TypeOf(sc.params).Kind() == reflect.Map } @@ -449,12 +454,13 @@ 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(pt *pageTokens, p *PageWithoutContent) (*shortcode, error) { - sc := &shortcode{} +func (s *shortcodeHandler) extractShortcode(ordinal int, pt *pageTokens, p *PageWithoutContent) (*shortcode, error) { + sc := &shortcode{ordinal: ordinal} var isInner = false var currItem item var cnt = 0 + var nestedOrdinal = 0 Loop: for { @@ -470,7 +476,8 @@ Loop: if cnt > 0 { // nested shortcode; append it to inner content pt.backup3(currItem, next) - nested, err := s.extractShortcode(pt, p) + nested, err := s.extractShortcode(nestedOrdinal, pt, p) + nestedOrdinal++ if nested.name != "" { s.nameSet[nested.name] = true } @@ -593,6 +600,7 @@ func (s *shortcodeHandler) extractShortcodes(stringToParse string, p *PageWithou // … it's safe to keep some "global" state var currItem item var currShortcode shortcode + var ordinal int Loop: for { @@ -605,7 +613,7 @@ Loop: // let extractShortcode handle left delim (will do so recursively) pt.backup() - currShortcode, err := s.extractShortcode(pt, p) + currShortcode, err := s.extractShortcode(ordinal, pt, p) if currShortcode.name != "" { s.nameSet[currShortcode.name] = true @@ -621,6 +629,7 @@ Loop: placeHolder := s.createShortcodePlaceholder() result.WriteString(placeHolder) + ordinal++ s.shortcodes.Add(placeHolder, currShortcode) case tEOF: break Loop |