Fix shortcode vs pygments

This is the nth attempt to fix an issue by changing the placeholder token pattern, but
now we actually have tests for all the historic trouble cases.

Fixes #2223
This commit is contained in:
Bjørn Erik Pedersen 2016-09-08 21:23:01 +02:00 committed by GitHub
parent ccb3040ad4
commit 76bf2dcdd2
3 changed files with 48 additions and 41 deletions

View file

@ -44,7 +44,7 @@ func (renderer *HugoHTMLRenderer) BlockCode(out *bytes.Buffer, text []byte, lang
}
func (renderer *HugoHTMLRenderer) Link(out *bytes.Buffer, link []byte, title []byte, content []byte) {
if renderer.LinkResolver == nil || bytes.HasPrefix(link, []byte("{-{-HUGOSHORTCODE")) {
if renderer.LinkResolver == nil || bytes.HasPrefix(link, []byte("HAHAHUGOSHORTCODE")) {
// Use the blackfriday built in Link handler
renderer.Renderer.Link(out, link, title, content)
} else {
@ -58,7 +58,7 @@ func (renderer *HugoHTMLRenderer) Link(out *bytes.Buffer, link []byte, title []b
}
}
func (renderer *HugoHTMLRenderer) Image(out *bytes.Buffer, link []byte, title []byte, alt []byte) {
if renderer.FileResolver == nil || bytes.HasPrefix(link, []byte("{-{-HUGOSHORTCODE")) {
if renderer.FileResolver == nil || bytes.HasPrefix(link, []byte("HAHAHUGOSHORTCODE")) {
// Use the blackfriday built in Image handler
renderer.Renderer.Image(out, link, title, alt)
} else {

View file

@ -204,7 +204,7 @@ func isInnerShortcode(t *template.Template) (bool, error) {
}
func createShortcodePlaceholder(id int) string {
return fmt.Sprintf("{-{-%s-%d-}-}", shortcodePlaceholderPrefix, id)
return fmt.Sprintf("HAHA%s-%dHBHB", shortcodePlaceholderPrefix, id)
}
const innerNewlineRegexp = "\n"
@ -528,8 +528,8 @@ func replaceShortcodeTokens(source []byte, prefix string, replacements map[strin
sourceLen := len(source)
start := 0
pre := []byte("{-{-" + prefix)
post := []byte("-}-}")
pre := []byte("HAHA" + prefix)
post := []byte("HBHB")
pStart := []byte("<p>")
pEnd := []byte("</p>")

View file

@ -332,7 +332,7 @@ void do();
}
}
const testScPlaceholderRegexp = "{-{-HUGOSHORTCODE-\\d+-}-}"
const testScPlaceholderRegexp = "HAHAHUGOSHORTCODE-\\d+HBHB"
func TestExtractShortcodes(t *testing.T) {
for i, this := range []struct {
@ -368,18 +368,18 @@ func TestExtractShortcodes(t *testing.T) {
`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["{-{-HUGOSHORTCODE-1-}-}:inner([], true){[First **Inner** Content]}" "{-{-HUGOSHORTCODE-2-}-}:inner([], false){[Inner **Content**]}"]`,
`map["HAHAHUGOSHORTCODE-1HBHB:inner([], true){[First **Inner** Content]}" "HAHAHUGOSHORTCODE-2HBHB: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["{-{-HUGOSHORTCODE-1-}-}:sc1([], false){[]}" "{-{-HUGOSHORTCODE-2-}-}:sc2([], false){[]}"]`,
`map["HAHAHUGOSHORTCODE-1HBHB:sc1([], false){[]}" "HAHAHUGOSHORTCODE-2HBHB:sc2([], false){[]}"]`,
testScPlaceholderRegexp + testScPlaceholderRegexp, ""},
{"mix of shortcodes", `Hello {{< sc1 >}}world{{% sc2 p2="2"%}}. And that's it.`,
`map["{-{-HUGOSHORTCODE-1-}-}:sc1([], false){[]}" "{-{-HUGOSHORTCODE-2-}-}:sc2([\"p2:2\"]`,
`map["HAHAHUGOSHORTCODE-1HBHB:sc1([], false){[]}" "HAHAHUGOSHORTCODE-2HBHB: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["{-{-HUGOSHORTCODE-1-}-}:sc1([], false){[]}" "{-{-HUGOSHORTCODE-2-}-}:inner([\"p2:2\"], true){[Inner]}"]`,
`map["HAHAHUGOSHORTCODE-1HBHB:sc1([], false){[]}" "HAHAHUGOSHORTCODE-2HBHB:inner([\"p2:2\"], true){[Inner]}"]`,
fmt.Sprintf("Hello %sworld%s. And that's it.", testScPlaceholderRegexp, testScPlaceholderRegexp), ""},
} {
@ -461,6 +461,9 @@ func TestShortcodesInSite(t *testing.T) {
viper.Set("UglyURLs", false)
viper.Set("verbose", true)
viper.Set("pygmentsuseclasses", true)
viper.Set("pygmentscodefences", true)
tests := []struct {
contentPath string
content string
@ -512,10 +515,11 @@ e`,
// #2192 #2209: Shortcodes in markdown headers
{"sect/doc5.md", `# {{< b >}}
## {{% c %}}`,
filepath.FromSlash("sect/doc5/index.html"), "\n\n<h1 id=\"hugoshortcode-1\">b</h1>\n\n<h2 id=\"hugoshortcode-2\">c</h2>\n"},
{"sect/doc6.md", "\n```bash\n{{< b >}}\n{{% c %}}\n```\n",
filepath.FromSlash("sect/doc5/index.html"), "\n\n<h1 id=\"hahahugoshortcode-1hbhb\">b</h1>\n\n<h2 id=\"hahahugoshortcode-2hbhb\">c</h2>\n"},
// #2223 pygments
{"sect/doc6.md", "\n```bash\nb: {{< b >}} c: {{% c %}}\n```\n",
filepath.FromSlash("sect/doc6/index.html"),
"<pre><code class=\"language-bash\">b\nc\n</code></pre>\n"},
"<div class=\"highlight\"><pre><code class=\"language-bash\" data-lang=\"bash\"><span></span>b: b c: c\n</code></pre></div>\n"},
// #2249
{"sect/doc7.ad", `_Shortcodes:_ *b: {{< b >}} c: {{% c %}}*`,
filepath.FromSlash("sect/doc7/index.html"),
@ -595,6 +599,9 @@ tags:
} else if strings.HasSuffix(test.contentPath, ".rst") && !helpers.HasRst() {
fmt.Println("Skip Rst test case as no rst2html present.")
continue
} else if strings.Contains(test.expected, "code class") && !helpers.HasPygments() {
fmt.Println("Skip Pygments test case as no pygments present.")
continue
}
file, err := hugofs.Destination().Open(test.outFile)
@ -637,11 +644,11 @@ func BenchmarkReplaceShortcodeTokens(b *testing.B) {
replacements map[string]string
expect []byte
}{
{"Hello {-{-HUGOSHORTCODE-1-}-}.", map[string]string{"{-{-HUGOSHORTCODE-1-}-}": "World"}, []byte("Hello World.")},
{strings.Repeat("A", 100) + " {-{-HUGOSHORTCODE-1-}-}.", map[string]string{"{-{-HUGOSHORTCODE-1-}-}": "Hello World"}, []byte(strings.Repeat("A", 100) + " Hello World.")},
{strings.Repeat("A", 500) + " {-{-HUGOSHORTCODE-1-}-}.", map[string]string{"{-{-HUGOSHORTCODE-1-}-}": "Hello World"}, []byte(strings.Repeat("A", 500) + " Hello World.")},
{strings.Repeat("ABCD ", 500) + " {-{-HUGOSHORTCODE-1-}-}.", map[string]string{"{-{-HUGOSHORTCODE-1-}-}": "Hello World"}, []byte(strings.Repeat("ABCD ", 500) + " Hello World.")},
{strings.Repeat("A ", 3000) + " {-{-HUGOSHORTCODE-1-}-}." + strings.Repeat("BC ", 1000) + " {-{-HUGOSHORTCODE-1-}-}.", map[string]string{"{-{-HUGOSHORTCODE-1-}-}": "Hello World"}, []byte(strings.Repeat("A ", 3000) + " Hello World." + strings.Repeat("BC ", 1000) + " Hello World.")},
{"Hello HAHAHUGOSHORTCODE-1HBHB.", map[string]string{"HAHAHUGOSHORTCODE-1HBHB": "World"}, []byte("Hello World.")},
{strings.Repeat("A", 100) + " HAHAHUGOSHORTCODE-1HBHB.", map[string]string{"HAHAHUGOSHORTCODE-1HBHB": "Hello World"}, []byte(strings.Repeat("A", 100) + " Hello World.")},
{strings.Repeat("A", 500) + " HAHAHUGOSHORTCODE-1HBHB.", map[string]string{"HAHAHUGOSHORTCODE-1HBHB": "Hello World"}, []byte(strings.Repeat("A", 500) + " Hello World.")},
{strings.Repeat("ABCD ", 500) + " HAHAHUGOSHORTCODE-1HBHB.", map[string]string{"HAHAHUGOSHORTCODE-1HBHB": "Hello World"}, []byte(strings.Repeat("ABCD ", 500) + " Hello World.")},
{strings.Repeat("A ", 3000) + " HAHAHUGOSHORTCODE-1HBHB." + strings.Repeat("BC ", 1000) + " HAHAHUGOSHORTCODE-1HBHB.", map[string]string{"HAHAHUGOSHORTCODE-1HBHB": "Hello World"}, []byte(strings.Repeat("A ", 3000) + " Hello World." + strings.Repeat("BC ", 1000) + " Hello World.")},
}
var in = make([]input, b.N*len(data))
@ -681,31 +688,31 @@ func TestReplaceShortcodeTokens(t *testing.T) {
replacements map[string]string
expect interface{}
}{
{"Hello {-{-PREFIX-1-}-}.", "PREFIX", map[string]string{"{-{-PREFIX-1-}-}": "World"}, "Hello World."},
{"Hello {-{-PREFIX-1@}@.", "PREFIX", map[string]string{"{-{-PREFIX-1-}-}": "World"}, false},
{"{-{-PREFIX2-1-}-}", "PREFIX2", map[string]string{"{-{-PREFIX2-1-}-}": "World"}, "World"},
{"Hello HAHAPREFIX-1HBHB.", "PREFIX", map[string]string{"HAHAPREFIX-1HBHB": "World"}, "Hello World."},
{"Hello HAHAPREFIX-1@}@.", "PREFIX", map[string]string{"HAHAPREFIX-1HBHB": "World"}, false},
{"HAHAPREFIX2-1HBHB", "PREFIX2", map[string]string{"HAHAPREFIX2-1HBHB": "World"}, "World"},
{"Hello World!", "PREFIX2", map[string]string{}, "Hello World!"},
{"!{-{-PREFIX-1-}-}", "PREFIX", map[string]string{"{-{-PREFIX-1-}-}": "World"}, "!World"},
{"{-{-PREFIX-1-}-}!", "PREFIX", map[string]string{"{-{-PREFIX-1-}-}": "World"}, "World!"},
{"!{-{-PREFIX-1-}-}!", "PREFIX", map[string]string{"{-{-PREFIX-1-}-}": "World"}, "!World!"},
{"_{_PREFIX-1-}-}", "PREFIX", map[string]string{"{-{-PREFIX-1-}-}": "World"}, "_{_PREFIX-1-}-}"},
{"Hello {-{-PREFIX-1-}-}.", "PREFIX", map[string]string{"{-{-PREFIX-1-}-}": "To You My Old Friend Who Told Me This Fantastic Story"}, "Hello To You My Old Friend Who Told Me This Fantastic Story."},
{"A {-{-A-1-}-} asdf {-{-A-2-}-}.", "A", map[string]string{"{-{-A-1-}-}": "v1", "{-{-A-2-}-}": "v2"}, "A v1 asdf v2."},
{"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"}, "Hello Europe. Go Jonny, Go, Go Johnny Go Go!."},
{"A {-{-PREFIX-2-}-} {-{-PREFIX-1-}-}.", "PREFIX", map[string]string{"{-{-PREFIX-1-}-}": "A", "{-{-PREFIX-2-}-}": "B"}, "A B A."},
{"A {-{-PREFIX-1-}-} {-{-PREFIX-2", "PREFIX", map[string]string{"{-{-PREFIX-1-}-}": "A"}, false},
{"A {-{-PREFIX-1-}-} but not the second.", "PREFIX", map[string]string{"{-{-PREFIX-1-}-}": "A", "{-{-PREFIX-2-}-}": "B"}, "A A but not the second."},
{"An {-{-PREFIX-1-}-}.", "PREFIX", map[string]string{"{-{-PREFIX-1-}-}": "A", "{-{-PREFIX-2-}-}": "B"}, "An A."},
{"An {-{-PREFIX-1-}-} {-{-PREFIX-2-}-}.", "PREFIX", map[string]string{"{-{-PREFIX-1-}-}": "A", "{-{-PREFIX-2-}-}": "B"}, "An A B."},
{"A {-{-PREFIX-1-}-} {-{-PREFIX-2-}-} {-{-PREFIX-3-}-} {-{-PREFIX-1-}-} {-{-PREFIX-3-}-}.", "PREFIX", map[string]string{"{-{-PREFIX-1-}-}": "A", "{-{-PREFIX-2-}-}": "B", "{-{-PREFIX-3-}-}": "C"}, "A A B C A C."},
{"A {-{-PREFIX-1-}-} {-{-PREFIX-2-}-} {-{-PREFIX-3-}-} {-{-PREFIX-1-}-} {-{-PREFIX-3-}-}.", "PREFIX", map[string]string{"{-{-PREFIX-1-}-}": "A", "{-{-PREFIX-2-}-}": "B", "{-{-PREFIX-3-}-}": "C"}, "A A B C A C."},
{"!HAHAPREFIX-1HBHB", "PREFIX", map[string]string{"HAHAPREFIX-1HBHB": "World"}, "!World"},
{"HAHAPREFIX-1HBHB!", "PREFIX", map[string]string{"HAHAPREFIX-1HBHB": "World"}, "World!"},
{"!HAHAPREFIX-1HBHB!", "PREFIX", map[string]string{"HAHAPREFIX-1HBHB": "World"}, "!World!"},
{"_{_PREFIX-1HBHB", "PREFIX", map[string]string{"HAHAPREFIX-1HBHB": "World"}, "_{_PREFIX-1HBHB"},
{"Hello HAHAPREFIX-1HBHB.", "PREFIX", map[string]string{"HAHAPREFIX-1HBHB": "To You My Old Friend Who Told Me This Fantastic Story"}, "Hello To You My Old Friend Who Told Me This Fantastic Story."},
{"A HAHAA-1HBHB asdf HAHAA-2HBHB.", "A", map[string]string{"HAHAA-1HBHB": "v1", "HAHAA-2HBHB": "v2"}, "A v1 asdf v2."},
{"Hello HAHAPREFIX2-1HBHB. Go HAHAPREFIX2-2HBHB, Go, Go HAHAPREFIX2-3HBHB Go Go!.", "PREFIX2", map[string]string{"HAHAPREFIX2-1HBHB": "Europe", "HAHAPREFIX2-2HBHB": "Jonny", "HAHAPREFIX2-3HBHB": "Johnny"}, "Hello Europe. Go Jonny, Go, Go Johnny Go Go!."},
{"A HAHAPREFIX-2HBHB HAHAPREFIX-1HBHB.", "PREFIX", map[string]string{"HAHAPREFIX-1HBHB": "A", "HAHAPREFIX-2HBHB": "B"}, "A B A."},
{"A HAHAPREFIX-1HBHB HAHAPREFIX-2", "PREFIX", map[string]string{"HAHAPREFIX-1HBHB": "A"}, false},
{"A HAHAPREFIX-1HBHB but not the second.", "PREFIX", map[string]string{"HAHAPREFIX-1HBHB": "A", "HAHAPREFIX-2HBHB": "B"}, "A A but not the second."},
{"An HAHAPREFIX-1HBHB.", "PREFIX", map[string]string{"HAHAPREFIX-1HBHB": "A", "HAHAPREFIX-2HBHB": "B"}, "An A."},
{"An HAHAPREFIX-1HBHB HAHAPREFIX-2HBHB.", "PREFIX", map[string]string{"HAHAPREFIX-1HBHB": "A", "HAHAPREFIX-2HBHB": "B"}, "An A B."},
{"A HAHAPREFIX-1HBHB HAHAPREFIX-2HBHB HAHAPREFIX-3HBHB HAHAPREFIX-1HBHB HAHAPREFIX-3HBHB.", "PREFIX", map[string]string{"HAHAPREFIX-1HBHB": "A", "HAHAPREFIX-2HBHB": "B", "HAHAPREFIX-3HBHB": "C"}, "A A B C A C."},
{"A HAHAPREFIX-1HBHB HAHAPREFIX-2HBHB HAHAPREFIX-3HBHB HAHAPREFIX-1HBHB HAHAPREFIX-3HBHB.", "PREFIX", map[string]string{"HAHAPREFIX-1HBHB": "A", "HAHAPREFIX-2HBHB": "B", "HAHAPREFIX-3HBHB": "C"}, "A A B C A C."},
// Issue #1148 remove p-tags 10 =>
{"Hello <p>{-{-PREFIX-1-}-}</p>. END.", "PREFIX", map[string]string{"{-{-PREFIX-1-}-}": "World"}, "Hello World. END."},
{"Hello <p>{-{-PREFIX-1-}-}</p>. <p>{-{-PREFIX-2-}-}</p> END.", "PREFIX", map[string]string{"{-{-PREFIX-1-}-}": "World", "{-{-PREFIX-2-}-}": "THE"}, "Hello World. THE END."},
{"Hello <p>{-{-PREFIX-1-}-}. END</p>.", "PREFIX", map[string]string{"{-{-PREFIX-1-}-}": "World"}, "Hello <p>World. END</p>."},
{"<p>Hello {-{-PREFIX-1-}-}</p>. END.", "PREFIX", map[string]string{"{-{-PREFIX-1-}-}": "World"}, "<p>Hello World</p>. END."},
{"Hello <p>{-{-PREFIX-1-}-}12", "PREFIX", map[string]string{"{-{-PREFIX-1-}-}": "World"}, "Hello <p>World12"},
{"Hello {-{-P-1-}-}. {-{-P-1-}-}-{-{-P-1-}-} {-{-P-1-}-} {-{-P-1-}-} {-{-P-1-}-} END", "P", map[string]string{"{-{-P-1-}-}": strings.Repeat("BC", 100)},
{"Hello <p>HAHAPREFIX-1HBHB</p>. END.", "PREFIX", map[string]string{"HAHAPREFIX-1HBHB": "World"}, "Hello World. END."},
{"Hello <p>HAHAPREFIX-1HBHB</p>. <p>HAHAPREFIX-2HBHB</p> END.", "PREFIX", map[string]string{"HAHAPREFIX-1HBHB": "World", "HAHAPREFIX-2HBHB": "THE"}, "Hello World. THE END."},
{"Hello <p>HAHAPREFIX-1HBHB. END</p>.", "PREFIX", map[string]string{"HAHAPREFIX-1HBHB": "World"}, "Hello <p>World. END</p>."},
{"<p>Hello HAHAPREFIX-1HBHB</p>. END.", "PREFIX", map[string]string{"HAHAPREFIX-1HBHB": "World"}, "<p>Hello World</p>. END."},
{"Hello <p>HAHAPREFIX-1HBHB12", "PREFIX", map[string]string{"HAHAPREFIX-1HBHB": "World"}, "Hello <p>World12"},
{"Hello HAHAP-1HBHB. HAHAP-1HBHB-HAHAP-1HBHB HAHAP-1HBHB HAHAP-1HBHB HAHAP-1HBHB END", "P", map[string]string{"HAHAP-1HBHB": strings.Repeat("BC", 100)},
fmt.Sprintf("Hello %s. %s-%s %s %s %s END",
strings.Repeat("BC", 100), strings.Repeat("BC", 100), strings.Repeat("BC", 100), strings.Repeat("BC", 100), strings.Repeat("BC", 100), strings.Repeat("BC", 100))},
} {