summaryrefslogtreecommitdiffstats
path: root/helpers/pygments.go
diff options
context:
space:
mode:
authorbep <bjorn.erik.pedersen@gmail.com>2015-04-16 02:27:37 +0200
committerbep <bjorn.erik.pedersen@gmail.com>2015-04-16 02:27:23 +0200
commitbe0d9770095a6497a69a610783a5710982591084 (patch)
tree8aa80f22f0bc80d6474bf305b396f71b82ad8f29 /helpers/pygments.go
parentbee52f85aee6383b357406baa9ba1e3b725a5a80 (diff)
Only write highlight to cache when CacheDir is set
To avoid writing cache files when testing.
Diffstat (limited to 'helpers/pygments.go')
-rw-r--r--helpers/pygments.go45
1 files changed, 26 insertions, 19 deletions
diff --git a/helpers/pygments.go b/helpers/pygments.go
index 74327c6f5..11b0f23d4 100644
--- a/helpers/pygments.go
+++ b/helpers/pygments.go
@@ -60,29 +60,34 @@ func Highlight(code, lang, optsStr string) string {
io.WriteString(hash, lang)
io.WriteString(hash, options)
- cachefile := filepath.Join(viper.GetString("CacheDir"), fmt.Sprintf("pygments-%x", hash.Sum(nil)))
-
fs := hugofs.OsFs
- exists, err := Exists(cachefile, fs)
- if err != nil {
- jww.ERROR.Print(err.Error())
- return code
- }
- if exists {
- f, err := fs.Open(cachefile)
- if err != nil {
- jww.ERROR.Print(err.Error())
- return code
- }
+ cacheDir := viper.GetString("CacheDir")
+ var cachefile string
- s, err := ioutil.ReadAll(f)
+ if cacheDir != "" {
+ cachefile = filepath.Join(cacheDir, fmt.Sprintf("pygments-%x", hash.Sum(nil)))
+
+ exists, err := Exists(cachefile, fs)
if err != nil {
jww.ERROR.Print(err.Error())
return code
}
-
- return string(s)
+ if exists {
+ f, err := fs.Open(cachefile)
+ if err != nil {
+ jww.ERROR.Print(err.Error())
+ return code
+ }
+
+ s, err := ioutil.ReadAll(f)
+ if err != nil {
+ jww.ERROR.Print(err.Error())
+ return code
+ }
+
+ return string(s)
+ }
}
// No cache file, render and cache it
@@ -99,9 +104,11 @@ func Highlight(code, lang, optsStr string) string {
return code
}
- // Write cache file
- if err := WriteToDisk(cachefile, bytes.NewReader(out.Bytes()), fs); err != nil {
- jww.ERROR.Print(stderr.String())
+ if cachefile != "" {
+ // Write cache file
+ if err := WriteToDisk(cachefile, bytes.NewReader(out.Bytes()), fs); err != nil {
+ jww.ERROR.Print(stderr.String())
+ }
}
return out.String()