Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The highlight shortcode and template func unescapes the input #4179

Closed
pgundlach opened this issue Dec 21, 2017 · 10 comments
Closed

The highlight shortcode and template func unescapes the input #4179

pgundlach opened this issue Dec 21, 2017 · 10 comments
Milestone

Comments

@pgundlach
Copy link

I use this code to try out chroma:

package main

import (
	"log"
	"os"

	"github.com/alecthomas/chroma/quick"
)

func main() {
	someSourceCode := `<Foo attr=" &lt; "></Foo>`
	err := quick.Highlight(os.Stdout, someSourceCode, "xml", "html", "monokai")
	if err != nil {
		log.Fatal(err)
	}
}

which results in <Foo attr=" &lt; "></Foo> whreas this code in Hugo (using chroma)

{{< highlight xml >}}
<Foo attr=" &lt; "></Foo>
{{< /highlight >}}

which renders as <Foo attr=" < "></Foo>. The difference is the interpretation of &lt; into <, which would be fine if the contents is XML or HTML, but not verbose source code.

The same happens when using code fences.

Hugo generates attr=</span><span class="s">&#34; &lt; &#34;</span> whereas chroma (with the above code) generates attr=</span><span class="s">&#34; &amp;lt; &#34;</span>.

$ hugo version
Hugo Static Site Generator v0.31 darwin/amd64 BuildDate: 2017-11-27T11:14:58+01:00

Operating system: macOS 10.13.2

config.toml syntax highlighting setting:

pygmentsCodeFences = true
pygmentsUseClasses = true
pygmentsUseClassic = false

Chroma version (running the code above) from today:
alecthomas/chroma@c9f612c

@bep
Copy link
Member

bep commented Dec 21, 2017

It doesn't make much sense to test with a different version of Chroma than used in your Hugo version.

@pgundlach
Copy link
Author

I have now tried with alecthomas/chroma@9c81d25 which seems to be the version used in the 0.31 release (found in Gopkg.lock)

I have got the same result as above.

@kaushalmodi
Copy link
Contributor

kaushalmodi commented Dec 21, 2017

I just confirmed this issue on hugo 0.31.1:

@kaushalmodi
Copy link
Contributor

kaushalmodi commented Dec 21, 2017

I have a sinking feeling that this issue is with blackfriday, which IMO is very poorly maintained.. no one responds to the Issues opened there, and I haven't seen a commit on that project even on that v2 branch since last Sept.

Github Insights/Pulse of russross/blackfriday for the past month

@bep I hope this is something that can be fixed on the hugo side. Or if you know folks on the blackfriday dev team, then inspire them to start responding to issues and fixing bugs.

@bep
Copy link
Member

bep commented Dec 21, 2017

I have a sinking feeling that this issue is with blackfriday, which IMO is very poorly maintained..

Blackfriday isn't poorly maintained. And this isn't a BF issue.

@bep
Copy link
Member

bep commented Dec 21, 2017

I would guess it is the unescape thing here that is the culprit:

https://github.com/gohugoio/hugo/blob/master/tpl/transform/transform.go#L58

This is not my code, so I have no recollection as to why it was put there.

Maybe @moorereason remembers?

@bep bep added the Bug label Dec 21, 2017
@bep bep added this to the v0.33 milestone Dec 21, 2017
@kaushalmodi
Copy link
Contributor

That line has a loong history.. traced it back to template_funcs.go:

image

But looks like it lived in a template.go before that.

@bep bep modified the milestones: v0.33, v0.32 Dec 21, 2017
@kaushalmodi
Copy link
Contributor

kaushalmodi commented Dec 21, 2017

Alright, found the actual source.. db29f57c @spf13

commit db29f57cc485debcea76e48dcbb3a4a4f33a3f7f
Author: spf13 <steve.francia@gmail.com>
Date:   Thu Dec 5 09:43:49 2013 -0500

    Adding (source code) Highlight template helper

diff --git a/template/bundle/template.go b/template/bundle/template.go
--- a/template/bundle/template.go
+++ b/template/bundle/template.go
@@ -112,0 +114,14 @@
+	var str string
+	av := reflect.ValueOf(in)
+	switch av.Kind() {
+	case reflect.String:
+		str = av.String()
+	}
+
+	if strings.HasPrefix(strings.TrimSpace(str), "<pre><code>") {
+		str = str[strings.Index(str, "<pre><code>")+11:]
+	}
+	if strings.HasSuffix(strings.TrimSpace(str), "</code></pre>") {
+		str = str[:strings.LastIndex(str, "</code></pre>")]
+	}
+	return template.HTML(helpers.Highlight(html.UnescapeString(str), lang))

@bep
Copy link
Member

bep commented Dec 21, 2017

That code is old and pre-Chroma -- and it doesn't make sense in my head as a general rule. People can easily use the htmlUnescape template func if they really need it.

@bep bep modified the milestones: v0.32, v0.33 Dec 29, 2017
@bep bep changed the title Chroma has different results than Hugo The highlight shortcode and template func unescapes the result Dec 29, 2017
@bep bep changed the title The highlight shortcode and template func unescapes the result The highlight shortcode and template func unescapes the input Dec 29, 2017
@bep bep closed this as completed in c067f34 Dec 29, 2017
@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 10, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants