summaryrefslogtreecommitdiffstats
path: root/helpers/pygments.go
diff options
context:
space:
mode:
authorRuben Vermeersch <ruben@rocketeer.be>2015-03-29 12:55:46 +0200
committerbep <bjorn.erik.pedersen@gmail.com>2015-03-29 13:01:44 +0200
commite8ca8602c003862fe3da87306c2b2c27918b29cc (patch)
treea0f40b8b8ea16f987668a6909c3846060ae1212a /helpers/pygments.go
parentbeaf5db3ff7d70d5644f3d3aaab3d38284c5fede (diff)
Hash all pygments parameters.
Ensures that Hugo rehighlights source code whenever one of the highlighting options changes.
Diffstat (limited to 'helpers/pygments.go')
-rw-r--r--helpers/pygments.go23
1 files changed, 15 insertions, 8 deletions
diff --git a/helpers/pygments.go b/helpers/pygments.go
index 0f06635fc..ca588870a 100644
--- a/helpers/pygments.go
+++ b/helpers/pygments.go
@@ -17,6 +17,7 @@ import (
"bytes"
"crypto/sha1"
"fmt"
+ "io"
"io/ioutil"
"os/exec"
"strings"
@@ -47,9 +48,21 @@ func Highlight(code string, lexer string) string {
fs := hugofs.OsFs
+ style := viper.GetString("PygmentsStyle")
+
+ noclasses := "true"
+ if viper.GetBool("PygmentsUseClasses") {
+ noclasses = "false"
+ }
+
// Try to read from cache first
- hash := sha1.Sum([]byte(code))
- cachefile := fmt.Sprintf("%s/pygments-%s-%x", viper.GetString("CacheDir"), lexer, hash)
+ hash := sha1.New()
+ io.WriteString(hash, lexer)
+ io.WriteString(hash, code)
+ io.WriteString(hash, style)
+ io.WriteString(hash, noclasses)
+
+ cachefile := fmt.Sprintf("%s/pygments-%x", viper.GetString("CacheDir"), hash.Sum(nil))
exists, err := Exists(cachefile, fs)
if err != nil {
jww.ERROR.Print(err.Error())
@@ -74,12 +87,6 @@ func Highlight(code string, lexer string) string {
// No cache file, render and cache it
var out bytes.Buffer
var stderr bytes.Buffer
- style := viper.GetString("PygmentsStyle")
-
- noclasses := "true"
- if viper.GetBool("PygmentsUseClasses") {
- noclasses = "false"
- }
cmd := exec.Command(pygmentsBin, "-l"+lexer, "-fhtml", "-O",
fmt.Sprintf("style=%s,noclasses=%s,encoding=utf8", style, noclasses))