diff options
author | Nathan Youngman <[email protected]> | 2015-10-14 15:10:50 -0600 |
---|---|---|
committer | Bjørn Erik Pedersen <[email protected]> | 2015-10-15 17:59:28 +0200 |
commit | ec9c6912163f9ca3c10ad75aba939a76ec96e932 (patch) | |
tree | fc0499b6d57d51ceac1a3a336af1b326ce0b05f9 /helpers/pygments.go | |
parent | 6a3aced15a402183b7c92817a259c44d890cf7be (diff) | |
download | hugo-ec9c6912163f9ca3c10ad75aba939a76ec96e932.tar.gz hugo-ec9c6912163f9ca3c10ad75aba939a76ec96e932.zip |
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
Diffstat (limited to 'helpers/pygments.go')
-rw-r--r-- | helpers/pygments.go | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/helpers/pygments.go b/helpers/pygments.go index 29a5ec54f..17c30ee0a 100644 --- a/helpers/pygments.go +++ b/helpers/pygments.go @@ -111,14 +111,23 @@ func Highlight(code, lang, optsStr string) string { 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 != "" { // 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()) } } - return out.String() + return str } var pygmentsKeywords = make(map[string]bool) |