From c3931ef748d1bb0ec60e45e1cecdfc1bde850bfc Mon Sep 17 00:00:00 2001 From: Andrew Brampton Date: Wed, 8 Jul 2015 18:51:54 -0700 Subject: Add PygmentsOptions option This allows default pygments settings to be used, if none are explictly set per shortcode. Fixes #1260 --- helpers/pygments.go | 87 +++++++++++++++++++++++++++++++++-------------------- 1 file changed, 55 insertions(+), 32 deletions(-) (limited to 'helpers/pygments.go') diff --git a/helpers/pygments.go b/helpers/pygments.go index 17c30ee0a..7d62fa0e7 100644 --- a/helpers/pygments.go +++ b/helpers/pygments.go @@ -139,59 +139,82 @@ 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, " ") + 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) + } + options[key] = keyVal[1] + } + } - style := viper.GetString("PygmentsStyle") + return nil +} - noclasses := "true" - if viper.GetBool("PygmentsUseClasses") { - noclasses = "false" +func createOptionsString(options map[string]string) string { + var keys []string + for k := range options { + keys = append(keys, k) } + sort.Strings(keys) - if len(in) == 0 { - return fmt.Sprintf("style=%s,noclasses=%s,encoding=utf8", style, noclasses), nil + var optionsStr string + for i, k := range keys { + optionsStr += fmt.Sprintf("%s=%s", k, options[k]) + if i < len(options)-1 { + optionsStr += "," + } } - options := make(map[string]string) + return optionsStr +} - o := strings.Split(in, ",") - for _, v := range o { - 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) - } - options[key] = keyVal[1] +func parseDefaultPygmentsOpts() (map[string]string, error) { + + options := make(map[string]string) + err := parseOptions(options, viper.GetString("PygmentsOptions")) + if err != nil { + return nil, err } - if _, ok := options["style"]; !ok { - options["style"] = style + if viper.IsSet("PygmentsStyle") { + options["style"] = viper.GetString("PygmentsStyle") } - if _, ok := options["noclasses"]; !ok { - options["noclasses"] = noclasses + if viper.IsSet("PygmentsUseClasses") { + if viper.GetBool("PygmentsUseClasses") { + options["noclasses"] = "false" + } else { + options["noclasses"] = "true" + } + } if _, ok := options["encoding"]; !ok { options["encoding"] = "utf8" } - var keys []string - for k := range options { - keys = append(keys, k) + return options, nil +} + +func parsePygmentsOpts(in string) (string, error) { + + options, err := parseDefaultPygmentsOpts() + if err != nil { + return "", err } - sort.Strings(keys) - var optionsStr string - for i, k := range keys { - optionsStr += fmt.Sprintf("%s=%s", k, options[k]) - if i < len(options)-1 { - optionsStr += "," - } + err = parseOptions(options, in) + if err != nil { + return "", err } - return optionsStr, nil + + return createOptionsString(options), nil } -- cgit v1.2.3