Add support for GitHub-flavoured markdown code fences for highlighting
authorAndrew Brampton <github@bramp.net>
Fri, 3 Jul 2015 21:53:50 +0000 (14:53 -0700)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Wed, 8 Jul 2015 06:12:52 +0000 (08:12 +0200)
This commit adds a new PygmentsCodeFences config option (default false), which if true will allow GitHub style backtick code fences around code, which will then be rendered by Pygments.

For example:

``` language
your code
```

can be used instead of {{< highlight language >}}your code {{< /highlight >}}.

Fixes #362

commands/hugo.go
helpers/content.go
helpers/content_renderer.go [new file with mode: 0644]

index c57f5fe4e727f317037a2632de05b376425454ab..cc4e19516d1f60991a2e79bc39a9f94c9bb3642b 100644 (file)
@@ -148,6 +148,7 @@ func LoadDefaultSettings() {
        viper.SetDefault("PygmentsStyle", "monokai")
        viper.SetDefault("DefaultExtension", "html")
        viper.SetDefault("PygmentsUseClasses", false)
+       viper.SetDefault("PygmentsCodeFences", false)
        viper.SetDefault("DisableLiveReload", false)
        viper.SetDefault("PluralizeListTitles", true)
        viper.SetDefault("PreserveTaxonomyNames", false)
index cd45634417e79f9c2416e5edab1497dbbeb56f8f..a846d2fa158fa104d1efa43e2d1881cf5ad65924 100644 (file)
@@ -167,9 +167,12 @@ func GetHTMLRenderer(defaultFlags int, ctx *RenderingContext) blackfriday.Render
                htmlFlags |= blackfriday.HTML_SMARTYPANTS_LATEX_DASHES
        }
 
-       return blackfriday.HtmlRendererWithParameters(htmlFlags, "", "", renderParameters)
+       return &HugoHtmlRenderer{
+               blackfriday.HtmlRendererWithParameters(htmlFlags, "", "", renderParameters),
+       }
 }
 
+
 func getMarkdownExtensions(ctx *RenderingContext) int {
        flags := 0 | blackfriday.EXTENSION_NO_INTRA_EMPHASIS |
                blackfriday.EXTENSION_TABLES | blackfriday.EXTENSION_FENCED_CODE |
diff --git a/helpers/content_renderer.go b/helpers/content_renderer.go
new file mode 100644 (file)
index 0000000..bb5b810
--- /dev/null
@@ -0,0 +1,23 @@
+package helpers
+
+import (
+       "bytes"
+       "html"
+
+       "github.com/russross/blackfriday"
+       "github.com/spf13/viper"
+)
+
+// Wraps a blackfriday.Renderer, typically a blackfriday.Html
+type HugoHtmlRenderer struct {
+       blackfriday.Renderer
+}
+
+func (renderer *HugoHtmlRenderer) BlockCode(out *bytes.Buffer, text []byte, lang string) {
+       if viper.GetBool("PygmentsCodeFences") {
+               str := html.UnescapeString(string(text))
+               out.WriteString(Highlight(str, lang, ""))
+       } else {
+               renderer.Renderer.BlockCode(out, text, lang)
+       }
+}