mirror of
https://github.com/gohugoio/hugo.git
synced 2025-04-20 01:43:14 +00:00
tpl: Add highlight test
This commit is contained in:
parent
0a8583a451
commit
4dcb63c2f6
2 changed files with 27 additions and 7 deletions
|
@ -1132,15 +1132,14 @@ func returnWhenSet(a, k interface{}) interface{} {
|
||||||
}
|
}
|
||||||
|
|
||||||
// highlight returns an HTML string with syntax highlighting applied.
|
// highlight returns an HTML string with syntax highlighting applied.
|
||||||
func highlight(in interface{}, lang, opts string) template.HTML {
|
func highlight(in interface{}, lang, opts string) (template.HTML, error) {
|
||||||
var str string
|
str, err := cast.ToStringE(in)
|
||||||
av := reflect.ValueOf(in)
|
|
||||||
switch av.Kind() {
|
if err != nil {
|
||||||
case reflect.String:
|
return "", err
|
||||||
str = av.String()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return template.HTML(helpers.Highlight(html.UnescapeString(str), lang, opts))
|
return template.HTML(helpers.Highlight(html.UnescapeString(str), lang, opts)), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var markdownTrimPrefix = []byte("<p>")
|
var markdownTrimPrefix = []byte("<p>")
|
||||||
|
|
|
@ -25,6 +25,7 @@ import (
|
||||||
"path"
|
"path"
|
||||||
"reflect"
|
"reflect"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
@ -1533,6 +1534,26 @@ func TestChomp(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestHighlight(t *testing.T) {
|
||||||
|
code := "func boo() {}"
|
||||||
|
highlighted, err := highlight(code, "go", "")
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal("Highlight returned error:", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// this depends on a Pygments installation, but will always contain the function name.
|
||||||
|
if !strings.Contains(string(highlighted), "boo") {
|
||||||
|
t.Errorf("Highlight mismatch, got %v", highlighted)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = highlight(t, "go", "")
|
||||||
|
|
||||||
|
if err == nil {
|
||||||
|
t.Error("Expected highlight error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestInflect(t *testing.T) {
|
func TestInflect(t *testing.T) {
|
||||||
for i, this := range []struct {
|
for i, this := range []struct {
|
||||||
inflectFunc func(i interface{}) (string, error)
|
inflectFunc func(i interface{}) (string, error)
|
||||||
|
|
Loading…
Add table
Reference in a new issue