Add PygmentsOptions option

This allows default pygments settings to be used, if none are explictly set per shortcode.

Fixes #1260
This commit is contained in:
Andrew Brampton 2015-07-08 18:51:54 -07:00 committed by Bjørn Erik Pedersen
parent 99acbb2eb2
commit c3931ef748
4 changed files with 101 additions and 38 deletions

View file

@ -159,10 +159,11 @@ func LoadDefaultSettings() {
viper.SetDefault("Taxonomies", map[string]string{"tag": "tags", "category": "categories"})
viper.SetDefault("Permalinks", make(hugolib.PermalinkOverrides, 0))
viper.SetDefault("Sitemap", hugolib.Sitemap{Priority: -1})
viper.SetDefault("PygmentsStyle", "monokai")
viper.SetDefault("DefaultExtension", "html")
viper.SetDefault("PygmentsStyle", "monokai")
viper.SetDefault("PygmentsUseClasses", false)
viper.SetDefault("PygmentsCodeFences", false)
viper.SetDefault("PygmentsOptions", "")
viper.SetDefault("DisableLiveReload", false)
viper.SetDefault("PluralizeListTitles", true)
viper.SetDefault("PreserveTaxonomyNames", false)

View file

@ -17,8 +17,9 @@ type HugoHtmlRenderer struct {
func (renderer *HugoHtmlRenderer) BlockCode(out *bytes.Buffer, text []byte, lang string) {
if viper.GetBool("PygmentsCodeFences") {
opts := viper.GetString("PygmentsOptions")
str := html.UnescapeString(string(text))
out.WriteString(Highlight(str, lang, ""))
out.WriteString(Highlight(str, lang, opts))
} else {
renderer.Renderer.BlockCode(out, text, lang)
}

View file

@ -139,47 +139,26 @@ func init() {
pygmentsKeywords["hl_lines"] = true
pygmentsKeywords["linenos"] = true
pygmentsKeywords["classprefix"] = true
pygmentsKeywords["startinline"] = true
}
func parsePygmentsOpts(in string) (string, error) {
func parseOptions(options map[string]string, in string) error {
in = strings.Trim(in, " ")
style := viper.GetString("PygmentsStyle")
noclasses := "true"
if viper.GetBool("PygmentsUseClasses") {
noclasses = "false"
}
if len(in) == 0 {
return fmt.Sprintf("style=%s,noclasses=%s,encoding=utf8", style, noclasses), nil
}
options := make(map[string]string)
o := strings.Split(in, ",")
for _, v := range o {
if in != "" {
for _, v := range strings.Split(in, ",") {
keyVal := strings.Split(v, "=")
key := strings.ToLower(strings.Trim(keyVal[0], " "))
if len(keyVal) != 2 || !pygmentsKeywords[key] {
return "", fmt.Errorf("invalid Pygments option: %s", key)
return fmt.Errorf("invalid Pygments option: %s", key)
}
options[key] = keyVal[1]
}
if _, ok := options["style"]; !ok {
options["style"] = style
}
if _, ok := options["noclasses"]; !ok {
options["noclasses"] = noclasses
}
if _, ok := options["encoding"]; !ok {
options["encoding"] = "utf8"
}
return nil
}
func createOptionsString(options map[string]string) string {
var keys []string
for k := range options {
keys = append(keys, k)
@ -193,5 +172,49 @@ func parsePygmentsOpts(in string) (string, error) {
optionsStr += ","
}
}
return optionsStr, nil
return optionsStr
}
func parseDefaultPygmentsOpts() (map[string]string, error) {
options := make(map[string]string)
err := parseOptions(options, viper.GetString("PygmentsOptions"))
if err != nil {
return nil, err
}
if viper.IsSet("PygmentsStyle") {
options["style"] = viper.GetString("PygmentsStyle")
}
if viper.IsSet("PygmentsUseClasses") {
if viper.GetBool("PygmentsUseClasses") {
options["noclasses"] = "false"
} else {
options["noclasses"] = "true"
}
}
if _, ok := options["encoding"]; !ok {
options["encoding"] = "utf8"
}
return options, nil
}
func parsePygmentsOpts(in string) (string, error) {
options, err := parseDefaultPygmentsOpts()
if err != nil {
return "", err
}
err = parseOptions(options, in)
if err != nil {
return "", err
}
return createOptionsString(options), nil
}

View file

@ -13,7 +13,7 @@ func TestParsePygmentsArgs(t *testing.T) {
pygmentsUseClasses bool
expect1 interface{}
}{
{"", "foo", true, "style=foo,noclasses=false,encoding=utf8"},
{"", "foo", true, "encoding=utf8,noclasses=false,style=foo"},
{"style=boo,noclasses=true", "foo", true, "encoding=utf8,noclasses=true,style=boo"},
{"Style=boo, noClasses=true", "foo", true, "encoding=utf8,noclasses=true,style=boo"},
{"noclasses=true", "foo", true, "encoding=utf8,noclasses=true,style=foo"},
@ -42,3 +42,41 @@ func TestParsePygmentsArgs(t *testing.T) {
}
}
}
func TestParseDefaultPygmentsArgs(t *testing.T) {
expect := "encoding=utf8,noclasses=false,style=foo"
for i, this := range []struct {
in string
pygmentsStyle interface{}
pygmentsUseClasses interface{}
pygmentsOptions string
}{
{"", "foo", true, "style=override,noclasses=override"},
{"", nil, nil, "style=foo,noclasses=false"},
{"style=foo,noclasses=false", nil, nil, "style=override,noclasses=override"},
{"style=foo,noclasses=false", "override", false, "style=override,noclasses=override"},
} {
viper.Reset()
viper.Set("PygmentsOptions", this.pygmentsOptions)
if s, ok := this.pygmentsStyle.(string); ok {
viper.Set("PygmentsStyle", s)
}
if b, ok := this.pygmentsUseClasses.(bool); ok {
viper.Set("PygmentsUseClasses", b)
}
result, err := parsePygmentsOpts(this.in)
if err != nil {
t.Errorf("[%d] parsePygmentArgs failed: %s", i, err)
continue
}
if result != expect {
t.Errorf("[%d] parsePygmentArgs got %v but expected %v", i, result, expect)
}
}
}