hugo

Unnamed repository; edit this file 'description' to name the repository.

git clone git://git.shimmy1996.com/hugo.git
commit c139c6e1ef8ed745bc96d7ba3f84a7e189c4a861
parent fdab118010f3b5ad027038bb2b2040d30478852e
Author: Andrew Brampton <github@bramp.net>
Date:   Fri,  3 Jul 2015 14:53:50 -0700

Add support for GitHub-flavoured markdown code fences for highlighting

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

Diffstat:
Mcommands/hugo.go | 1+
Mhelpers/content.go | 5++++-
Ahelpers/content_renderer.go | 23+++++++++++++++++++++++
3 files changed, 28 insertions(+), 1 deletion(-)
diff --git a/commands/hugo.go b/commands/hugo.go
@@ -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)
diff --git a/helpers/content.go b/helpers/content.go
@@ -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
@@ -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)
+	}
+}