From: spf13 Date: Thu, 5 Dec 2013 14:43:49 +0000 (-0500) Subject: Adding (source code) Highlight template helper X-Git-Tag: v0.10~100 X-Git-Url: http://git.maquefel.me/?a=commitdiff_plain;h=db29f57cc485debcea76e48dcbb3a4a4f33a3f7f;p=brevno-suite%2Fhugo Adding (source code) Highlight template helper --- diff --git a/template/bundle/template.go b/template/bundle/template.go index a53b38fa..8ce8561c 100644 --- a/template/bundle/template.go +++ b/template/bundle/template.go @@ -4,6 +4,7 @@ import ( "errors" "github.com/eknkc/amber" "github.com/spf13/hugo/helpers" + "html" "html/template" "io" "io/ioutil" @@ -109,6 +110,23 @@ func ReturnWhenSet(a interface{}, index int) interface{} { 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), "
") {
+		str = str[strings.Index(str, "
")+11:]
+	}
+	if strings.HasSuffix(strings.TrimSpace(str), "
") { + str = str[:strings.LastIndex(str, "
")] + } + return template.HTML(helpers.Highlight(html.UnescapeString(str), lang)) +} + func SafeHtml(text string) template.HTML { return template.HTML(text) } @@ -145,6 +163,7 @@ func NewTemplate() Template { "echoParam": ReturnWhenSet, "safeHtml": SafeHtml, "first": First, + "highlight": Highlight, } templates.Funcs(funcMap)