Only write highlight to cache when CacheDir is set

To avoid writing cache files when testing.
This commit is contained in:
bep 2015-04-16 02:27:37 +02:00
parent bee52f85ae
commit be0d977009

View file

@ -60,29 +60,34 @@ func Highlight(code, lang, optsStr string) string {
io.WriteString(hash, lang) io.WriteString(hash, lang)
io.WriteString(hash, options) io.WriteString(hash, options)
cachefile := filepath.Join(viper.GetString("CacheDir"), fmt.Sprintf("pygments-%x", hash.Sum(nil)))
fs := hugofs.OsFs fs := hugofs.OsFs
exists, err := Exists(cachefile, fs) cacheDir := viper.GetString("CacheDir")
if err != nil { var cachefile string
jww.ERROR.Print(err.Error())
return code if cacheDir != "" {
} cachefile = filepath.Join(cacheDir, fmt.Sprintf("pygments-%x", hash.Sum(nil)))
if exists {
f, err := fs.Open(cachefile) exists, err := Exists(cachefile, fs)
if err != nil { if err != nil {
jww.ERROR.Print(err.Error()) jww.ERROR.Print(err.Error())
return code return code
} }
if exists {
f, err := fs.Open(cachefile)
if err != nil {
jww.ERROR.Print(err.Error())
return code
}
s, err := ioutil.ReadAll(f) s, err := ioutil.ReadAll(f)
if err != nil { if err != nil {
jww.ERROR.Print(err.Error()) jww.ERROR.Print(err.Error())
return code return code
}
return string(s)
} }
return string(s)
} }
// No cache file, render and cache it // No cache file, render and cache it
@ -99,9 +104,11 @@ func Highlight(code, lang, optsStr string) string {
return code return code
} }
// Write cache file if cachefile != "" {
if err := WriteToDisk(cachefile, bytes.NewReader(out.Bytes()), fs); err != nil { // Write cache file
jww.ERROR.Print(stderr.String()) if err := WriteToDisk(cachefile, bytes.NewReader(out.Bytes()), fs); err != nil {
jww.ERROR.Print(stderr.String())
}
} }
return out.String() return out.String()