shithub: hugo

Download patch

ref: db29f57cc485debcea76e48dcbb3a4a4f33a3f7f
parent: fa29e94edb680943b3de801367d41174fc1343c8
author: spf13 <[email protected]>
date: Thu Dec 5 04:43:49 EST 2013

Adding (source code) Highlight template helper

--- a/template/bundle/template.go
+++ b/template/bundle/template.go
@@ -4,6 +4,7 @@
 	"errors"
 	"github.com/eknkc/amber"
 	"github.com/spf13/hugo/helpers"
+	"html"
 	"html/template"
 	"io"
 	"io/ioutil"
@@ -109,6 +110,23 @@
 	return ""
 }
 
+func Highlight(in interface{}, lang string) template.HTML {
+	var str string
+	av := reflect.ValueOf(in)
+	switch av.Kind() {
+	case reflect.String:
+		str = av.String()
+	}
+
+	if strings.HasPrefix(strings.TrimSpace(str), "<pre><code>") {
+		str = str[strings.Index(str, "<pre><code>")+11:]
+	}
+	if strings.HasSuffix(strings.TrimSpace(str), "</code></pre>") {
+		str = str[:strings.LastIndex(str, "</code></pre>")]
+	}
+	return template.HTML(helpers.Highlight(html.UnescapeString(str), lang))
+}
+
 func SafeHtml(text string) template.HTML {
 	return template.HTML(text)
 }
@@ -145,6 +163,7 @@
 		"echoParam": ReturnWhenSet,
 		"safeHtml":  SafeHtml,
 		"first":     First,
+		"highlight": Highlight,
 	}
 
 	templates.Funcs(funcMap)