mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-21 20:46:30 -05:00
Insert code tag for server-side syntax highlighting
Inserts a code tag into Pygments output with the language-info that is present when using client-side highlighting (useful for CSS hooks) ```html <code class="language-go" data-lang="go"> ``` closes #1490
This commit is contained in:
parent
6a3aced15a
commit
ec9c691216
2 changed files with 12 additions and 3 deletions
|
@ -38,7 +38,7 @@ func TestCodeFence(t *testing.T) {
|
||||||
input, expected string
|
input, expected string
|
||||||
}
|
}
|
||||||
data := []test{
|
data := []test{
|
||||||
{true, "<html></html>", "<div class=\"highlight\"><pre><span class=\"nt\"><html></html></span>\n</pre></div>\n"},
|
{true, "<html></html>", "<div class=\"highlight\"><pre><code class=\"language-html\" data-lang=\"html\"><span class=\"nt\"><html></html></span>\n</code></pre></div>\n"},
|
||||||
{false, "<html></html>", "<pre><code class=\"language-html\"><html></html></code></pre>\n"},
|
{false, "<html></html>", "<pre><code class=\"language-html\"><html></html></code></pre>\n"},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -111,14 +111,23 @@ func Highlight(code, lang, optsStr string) string {
|
||||||
return code
|
return code
|
||||||
}
|
}
|
||||||
|
|
||||||
|
str := out.String()
|
||||||
|
|
||||||
|
// inject code tag into Pygments output
|
||||||
|
if lang != "" && strings.Contains(str, "<pre>") {
|
||||||
|
codeTag := fmt.Sprintf(`<pre><code class="language-%s" data-lang="%s">`, lang, lang)
|
||||||
|
str = strings.Replace(str, "<pre>", codeTag, 1)
|
||||||
|
str = strings.Replace(str, "</pre>", "</code></pre>", 1)
|
||||||
|
}
|
||||||
|
|
||||||
if cachefile != "" {
|
if cachefile != "" {
|
||||||
// Write cache file
|
// Write cache file
|
||||||
if err := WriteToDisk(cachefile, bytes.NewReader(out.Bytes()), fs); err != nil {
|
if err := WriteToDisk(cachefile, strings.NewReader(str), fs); err != nil {
|
||||||
jww.ERROR.Print(stderr.String())
|
jww.ERROR.Print(stderr.String())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return out.String()
|
return str
|
||||||
}
|
}
|
||||||
|
|
||||||
var pygmentsKeywords = make(map[string]bool)
|
var pygmentsKeywords = make(map[string]bool)
|
||||||
|
|
Loading…
Reference in a new issue