Page *Page
}
+func (scp *ShortcodeWithPage) Ref(ref string) (string, error) {
+ return scp.Page.Ref(ref)
+}
+
+func (scp *ShortcodeWithPage) RelRef(ref string) (string, error) {
+ return scp.Page.RelRef(ref)
+}
+
func (scp *ShortcodeWithPage) Get(key interface{}) interface{} {
if reflect.ValueOf(scp.Params).Len() == 0 {
return nil
// all in one go: extract, render and replace
// only used for testing
func ShortcodesHandle(stringToParse string, page *Page, t tpl.Template) string {
-
tmpContent, tmpShortcodes := extractAndRenderShortcodes(stringToParse, page, t)
if len(tmpShortcodes) > 0 {
}
func createShortcodePlaceholder(id int) string {
- return fmt.Sprintf("<div>%s-%d</div>", shortcodePlaceholderPrefix, id)
+ return fmt.Sprintf("{@{@%s-%d@}@}", shortcodePlaceholderPrefix, id)
}
func renderShortcodes(sc shortcode, p *Page, t tpl.Template) string {
return shortcodes
}
+const innerNewlineRegexp = "\n"
+const innerCleanupRegexp = `\A<p>(.*)</p>\n\z`
+const innerCleanupExpand = "$1"
+
func renderShortcode(sc shortcode, tokenizedShortcodes map[string](string), cnt int, p *Page, t tpl.Template) string {
var data = &ShortcodeWithPage{Params: sc.params, Page: p}
tmpl := GetTemplate(sc.name, t)
}
if sc.doMarkup {
- data.Inner = template.HTML(helpers.RenderBytes([]byte(inner), p.guessMarkupType(), p.UniqueId()))
+ newInner := helpers.RenderBytes([]byte(inner), p.guessMarkupType(), p.UniqueId())
+
+ // If the type is “unknown” or “markdown”, we assume the markdown
+ // generation has been performed. Given the input: `a line`, markdown
+ // specifies the HTML `<p>a line</p>\n`. When dealing with documents as a
+ // whole, this is OK. When dealing with an `{{ .Inner }}` block in Hugo,
+ // this is not so good. This code does two things:
+ //
+ // 1. Check to see if inner has a newline in it. If so, the Inner data is
+ // unchanged.
+ // 2 If inner does not have a newline, strip the wrapping <p> block and
+ // the newline. This was previously tricked out by wrapping shortcode
+ // substitutions in <div>HUGOSHORTCODE-1</div> which prevents the
+ // generation, but means that you can’t use shortcodes inside of
+ // markdown structures itself (e.g., `[foo]({{% ref foo.md %}})`).
+ switch p.guessMarkupType() {
+ case "unknown", "markdown":
+ if match, _ := regexp.MatchString(innerNewlineRegexp, inner); !match {
+ cleaner, err := regexp.Compile(innerCleanupRegexp)
+
+ if err == nil {
+ newInner = cleaner.ReplaceAll(newInner, []byte(innerCleanupExpand))
+ }
+ }
+ }
+
+ data.Inner = template.HTML(newInner)
} else {
data.Inner = template.HTML(inner)
}
// Replace prefixed shortcode tokens (HUGOSHORTCODE-1, HUGOSHORTCODE-2) with the real content.
// This assumes that all tokens exist in the input string and that they are in order.
// numReplacements = -1 will do len(replacements), and it will always start from the beginning (1)
-// wrappendInDiv = true means that the token is wrapped in a <div></div>
-func replaceShortcodeTokens(source []byte, prefix string, numReplacements int, wrappedInDiv bool, replacements map[string]string) ([]byte, error) {
+// wrapped = true means that the token has been wrapped in {@{@/@}@}
+func replaceShortcodeTokens(source []byte, prefix string, numReplacements int, wrapped bool, replacements map[string]string) ([]byte, error) {
if numReplacements < 0 {
numReplacements = len(replacements)
for i := 1; i <= numReplacements; i++ {
key := prefix + "-" + strconv.Itoa(i)
- if wrappedInDiv {
- key = "<div>" + key + "</div>"
+ if wrapped {
+ key = "{@{@" + key + "@}@}"
}
val := []byte(replacements[key])
for i := 0; i < numReplacements; i++ {
tokenNum := i + 1
oldVal := prefix + "-" + strconv.Itoa(tokenNum)
- if wrappedInDiv {
- oldVal = "<div>" + oldVal + "</div>"
+ if wrapped {
+ oldVal = "{@{@" + oldVal + "@}@}"
}
newVal := []byte(replacements[oldVal])
j := start
k := bytes.Index(source[start:], []byte(oldVal))
+
if k < 0 {
// this should never happen, but let the caller decide to panic or not
- return nil, fmt.Errorf("illegal state in content; shortcode token #%d is missing or out of order", tokenNum)
+ return nil, fmt.Errorf("illegal state in content; shortcode token #%d is missing or out of order (%q)", tokenNum, source)
}
j += k
tem.AddInternalShortcode("scn1.html", `<div>Outer, inner is {{ .Inner }}</div>`)
tem.AddInternalShortcode("scn2.html", `<div>SC2</div>`)
- CheckShortCodeMatch(t, `{{% scn1 %}}{{% scn2 %}}{{% /scn1 %}}`, "<div>Outer, inner is <div>SC2</div>\n</div>", tem)
+ CheckShortCodeMatch(t, `{{% scn1 %}}{{% scn2 %}}{{% /scn1 %}}`, "<div>Outer, inner is <div>SC2</div></div>", tem)
+
+ CheckShortCodeMatch(t, `{{< scn1 >}}{{% scn2 %}}{{< /scn1 >}}`, "<div>Outer, inner is <div>SC2</div></div>", tem)
}
func TestNestedComplexSC(t *testing.T) {
tem.AddInternalShortcode("aside.html", `-aside-{{ .Inner }}-asideStop-`)
CheckShortCodeMatch(t, `{{< row >}}1-s{{% column %}}2-**s**{{< aside >}}3-**s**{{< /aside >}}4-s{{% /column %}}5-s{{< /row >}}6-s`,
- "-row-1-s-col-<p>2-<strong>s</strong>-aside-3-**s**-asideStop-4-s</p>\n-colStop-5-s-rowStop-6-s", tem)
+ "-row-1-s-col-2-<strong>s</strong>-aside-3-**s**-asideStop-4-s-colStop-5-s-rowStop-6-s", tem)
// turn around the markup flag
CheckShortCodeMatch(t, `{{% row %}}1-s{{< column >}}2-**s**{{% aside %}}3-**s**{{% /aside %}}4-s{{< /column >}}5-s{{% /row %}}6-s`,
- "-row-<p>1-s-col-2-**s**-aside-<p>3-<strong>s</strong></p>\n-asideStop-4-s-colStop-5-s</p>\n-rowStop-6-s", tem)
+ "-row-1-s-col-2-**s**-aside-3-<strong>s</strong>-asideStop-4-s-colStop-5-s-rowStop-6-s", tem)
}
func TestFigureImgWidth(t *testing.T) {
CheckShortCodeMatch(t, code, "\n<div class=\"highlight\" style=\"background: #ffffff\"><pre style=\"line-height: 125%\"><span style=\"font-weight: bold\">void</span> do();\n</pre></div>\n", tem)
}
-const testScPlaceholderRegexp = "<div>HUGOSHORTCODE-\\d+</div>"
+const testScPlaceholderRegexp = "{@{@HUGOSHORTCODE-\\d+@}@}"
func TestExtractShortcodes(t *testing.T) {
for i, this := range []struct {
`inner([], false){[inner2-> inner2([\"param1\"], true){[inner2txt->inner3 inner3(%!q(<nil>), false){[inner3txt]}]} final close->`,
fmt.Sprintf("Inner->%s<-done", testScPlaceholderRegexp), ""},
{"two inner", `Some text. {{% inner %}}First **Inner** Content{{% / inner %}} {{< inner >}}Inner **Content**{{< / inner >}}. Some more text.`,
- `map["<div>HUGOSHORTCODE-1</div>:inner([], true){[First **Inner** Content]}" "<div>HUGOSHORTCODE-2</div>:inner([], false){[Inner **Content**]}"]`,
+ `map["{@{@HUGOSHORTCODE-1@}@}:inner([], true){[First **Inner** Content]}" "{@{@HUGOSHORTCODE-2@}@}:inner([], false){[Inner **Content**]}"]`,
fmt.Sprintf("Some text. %s %s. Some more text.", testScPlaceholderRegexp, testScPlaceholderRegexp), ""},
{"closed without content", `Some text. {{< inner param1 >}}{{< / inner >}}. Some more text.`, `inner([\"param1\"], false){[]}`,
fmt.Sprintf("Some text. %s. Some more text.", testScPlaceholderRegexp), ""},
{"two shortcodes", "{{< sc1 >}}{{< sc2 >}}",
- `map["<div>HUGOSHORTCODE-1</div>:sc1([], false){[]}" "<div>HUGOSHORTCODE-2</div>:sc2([], false){[]}"]`,
+ `map["{@{@HUGOSHORTCODE-1@}@}:sc1([], false){[]}" "{@{@HUGOSHORTCODE-2@}@}:sc2([], false){[]}"]`,
testScPlaceholderRegexp + testScPlaceholderRegexp, ""},
{"mix of shortcodes", `Hello {{< sc1 >}}world{{% sc2 p2="2"%}}. And that's it.`,
- `map["<div>HUGOSHORTCODE-1</div>:sc1([], false){[]}" "<div>HUGOSHORTCODE-2</div>:sc2([\"p2:2\"]`,
+ `map["{@{@HUGOSHORTCODE-1@}@}:sc1([], false){[]}" "{@{@HUGOSHORTCODE-2@}@}:sc2([\"p2:2\"]`,
fmt.Sprintf("Hello %sworld%s. And that's it.", testScPlaceholderRegexp, testScPlaceholderRegexp), ""},
{"mix with inner", `Hello {{< sc1 >}}world{{% inner p2="2"%}}Inner{{%/ inner %}}. And that's it.`,
- `map["<div>HUGOSHORTCODE-1</div>:sc1([], false){[]}" "<div>HUGOSHORTCODE-2</div>:inner([\"p2:2\"], true){[Inner]}"]`,
+ `map["{@{@HUGOSHORTCODE-1@}@}:sc1([], false){[]}" "{@{@HUGOSHORTCODE-2@}@}:inner([\"p2:2\"], true){[Inner]}"]`,
fmt.Sprintf("Hello %sworld%s. And that's it.", testScPlaceholderRegexp, testScPlaceholderRegexp), ""},
} {
wrappedInDiv bool
expect interface{}
}{
- {[]byte("Hello PREFIX-1."), "PREFIX",
- map[string]string{"PREFIX-1": "World"}, -1, false, []byte("Hello World.")},
- {[]byte("A <div>A-1</div> asdf <div>A-2</div>."), "A",
- map[string]string{"<div>A-1</div>": "v1", "<div>A-2</div>": "v2"}, -1, true, []byte("A v1 asdf v2.")},
- {[]byte("Hello PREFIX2-1. Go PREFIX2-2, Go, Go PREFIX2-3 Go Go!."), "PREFIX2",
- map[string]string{"PREFIX2-1": "Europe", "PREFIX2-2": "Jonny", "PREFIX2-3": "Johnny"},
- -1, false, []byte("Hello Europe. Go Jonny, Go, Go Johnny Go Go!.")},
- {[]byte("A PREFIX-2 PREFIX-1."), "PREFIX",
- map[string]string{"PREFIX-1": "A", "PREFIX-2": "B"}, -1, false, false},
- {[]byte("A PREFIX-1 PREFIX-2"), "PREFIX",
- map[string]string{"PREFIX-1": "A"}, -1, false, []byte("A A PREFIX-2")},
- {[]byte("A PREFIX-1 but not the second."), "PREFIX",
- map[string]string{"PREFIX-1": "A", "PREFIX-2": "B"}, -1, false, false},
- {[]byte("An PREFIX-1."), "PREFIX",
- map[string]string{"PREFIX-1": "A", "PREFIX-2": "B"}, 1, false, []byte("An A.")},
- {[]byte("An PREFIX-1 PREFIX-2."), "PREFIX",
- map[string]string{"PREFIX-1": "A", "PREFIX-2": "B"}, 1, false, []byte("An A PREFIX-2.")},
+ {[]byte("Hello PREFIX-1."), "PREFIX", map[string]string{"PREFIX-1": "World"}, -1, false, []byte("Hello World.")},
+ {[]byte("A {@{@A-1@}@} asdf {@{@A-2@}@}."), "A", map[string]string{"{@{@A-1@}@}": "v1", "{@{@A-2@}@}": "v2"}, -1, true, []byte("A v1 asdf v2.")},
+ {[]byte("Hello PREFIX2-1. Go PREFIX2-2, Go, Go PREFIX2-3 Go Go!."), "PREFIX2", map[string]string{"PREFIX2-1": "Europe", "PREFIX2-2": "Jonny", "PREFIX2-3": "Johnny"}, -1, false, []byte("Hello Europe. Go Jonny, Go, Go Johnny Go Go!.")},
+ {[]byte("A PREFIX-2 PREFIX-1."), "PREFIX", map[string]string{"PREFIX-1": "A", "PREFIX-2": "B"}, -1, false, false},
+ {[]byte("A PREFIX-1 PREFIX-2"), "PREFIX", map[string]string{"PREFIX-1": "A"}, -1, false, []byte("A A PREFIX-2")},
+ {[]byte("A PREFIX-1 but not the second."), "PREFIX", map[string]string{"PREFIX-1": "A", "PREFIX-2": "B"}, -1, false, false},
+ {[]byte("An PREFIX-1."), "PREFIX", map[string]string{"PREFIX-1": "A", "PREFIX-2": "B"}, 1, false, []byte("An A.")},
+ {[]byte("An PREFIX-1 PREFIX-2."), "PREFIX", map[string]string{"PREFIX-1": "A", "PREFIX-2": "B"}, 1, false, []byte("An A PREFIX-2.")},
} {
+ fmt.Printf("this<%#v>", this)
results, err := replaceShortcodeTokens(this.input, this.prefix, this.numReplacements, this.wrappedInDiv, this.replacements)
if b, ok := this.expect.(bool); ok && !b {