mirror of
https://github.com/gohugoio/hugo.git
synced 2025-04-20 02:23:11 +00:00
Only write highlight to cache when CacheDir is set
To avoid writing cache files when testing.
This commit is contained in:
parent
bee52f85ae
commit
be0d977009
1 changed files with 25 additions and 18 deletions
|
@ -60,10 +60,14 @@ 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
|
||||||
|
|
||||||
|
cacheDir := viper.GetString("CacheDir")
|
||||||
|
var cachefile string
|
||||||
|
|
||||||
|
if cacheDir != "" {
|
||||||
|
cachefile = filepath.Join(cacheDir, fmt.Sprintf("pygments-%x", hash.Sum(nil)))
|
||||||
|
|
||||||
exists, err := Exists(cachefile, fs)
|
exists, err := Exists(cachefile, fs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
jww.ERROR.Print(err.Error())
|
jww.ERROR.Print(err.Error())
|
||||||
|
@ -84,6 +88,7 @@ func Highlight(code, lang, optsStr string) string {
|
||||||
|
|
||||||
return string(s)
|
return string(s)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// No cache file, render and cache it
|
// No cache file, render and cache it
|
||||||
var out bytes.Buffer
|
var out bytes.Buffer
|
||||||
|
@ -99,10 +104,12 @@ func Highlight(code, lang, optsStr string) string {
|
||||||
return code
|
return code
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if cachefile != "" {
|
||||||
// Write cache file
|
// Write cache file
|
||||||
if err := WriteToDisk(cachefile, bytes.NewReader(out.Bytes()), fs); err != nil {
|
if err := WriteToDisk(cachefile, bytes.NewReader(out.Bytes()), fs); err != nil {
|
||||||
jww.ERROR.Print(stderr.String())
|
jww.ERROR.Print(stderr.String())
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return out.String()
|
return out.String()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue