If no language is provided to Pygments, then try and guess it
authorAndrew Brampton <github@bramp.net>
Fri, 3 Jul 2015 21:51:43 +0000 (14:51 -0700)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Wed, 8 Jul 2015 06:12:06 +0000 (08:12 +0200)
Previously if no language was specified, then illegal args would be passed to pygments, for example `pygments -l -fhtml`, which would result in pygments printing an error.

helpers/pygments.go

index 3f0f90b7a434049d5c39d1ea42495d45b831803b..ecbdf99d0e64ba8af63ac43411ac77abe4e3cb66 100644 (file)
@@ -94,7 +94,14 @@ func Highlight(code, lang, optsStr string) string {
        var out bytes.Buffer
        var stderr bytes.Buffer
 
-       cmd := exec.Command(pygmentsBin, "-l"+lang, "-fhtml", "-O", options)
+       var langOpt string
+       if lang == "" {
+               langOpt = "-g" // Try guessing the language
+       } else {
+               langOpt = "-l"+lang
+       }
+
+       cmd := exec.Command(pygmentsBin, langOpt, "-fhtml", "-O", options)
        cmd.Stdin = strings.NewReader(code)
        cmd.Stdout = &out
        cmd.Stderr = &stderr