Adding (source code) Highlight template helper
authorspf13 <steve.francia@gmail.com>
Thu, 5 Dec 2013 14:43:49 +0000 (09:43 -0500)
committerspf13 <steve.francia@gmail.com>
Thu, 5 Dec 2013 14:43:49 +0000 (09:43 -0500)
template/bundle/template.go

index a53b38fadbbc896f959347625f55175c2b4a8aea..8ce8561c0755c88a68f32177c63b4ba7c7018f1e 100644 (file)
@@ -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), "<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 @@ func NewTemplate() Template {
                "echoParam": ReturnWhenSet,
                "safeHtml":  SafeHtml,
                "first":     First,
+               "highlight": Highlight,
        }
 
        templates.Funcs(funcMap)