mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-29 19:02:07 -05:00
Adding (source code) Highlight template helper
This commit is contained in:
parent
fa29e94edb
commit
db29f57cc4
1 changed files with 19 additions and 0 deletions
|
@ -4,6 +4,7 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"github.com/eknkc/amber"
|
"github.com/eknkc/amber"
|
||||||
"github.com/spf13/hugo/helpers"
|
"github.com/spf13/hugo/helpers"
|
||||||
|
"html"
|
||||||
"html/template"
|
"html/template"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
@ -109,6 +110,23 @@ func ReturnWhenSet(a interface{}, index int) interface{} {
|
||||||
return ""
|
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 {
|
func SafeHtml(text string) template.HTML {
|
||||||
return template.HTML(text)
|
return template.HTML(text)
|
||||||
}
|
}
|
||||||
|
@ -145,6 +163,7 @@ func NewTemplate() Template {
|
||||||
"echoParam": ReturnWhenSet,
|
"echoParam": ReturnWhenSet,
|
||||||
"safeHtml": SafeHtml,
|
"safeHtml": SafeHtml,
|
||||||
"first": First,
|
"first": First,
|
||||||
|
"highlight": Highlight,
|
||||||
}
|
}
|
||||||
|
|
||||||
templates.Funcs(funcMap)
|
templates.Funcs(funcMap)
|
||||||
|
|
Loading…
Reference in a new issue