tpl: Accept limit as interface in findRE func

Fixes #3018
This commit is contained in:
Cameron Moore 2017-02-08 10:40:11 -06:00 committed by Bjørn Erik Pedersen
parent 298ebc37c2
commit 5cc8b58907
2 changed files with 18 additions and 17 deletions

View file

@ -535,8 +535,8 @@ func first(limit interface{}, seq interface{}) (interface{}, error) {
}
// findRE returns a list of strings that match the regular expression. By default all matches
// will be included. The number of matches can be limitted with an optional third parameter.
func findRE(expr string, content interface{}, limit ...int) ([]string, error) {
// will be included. The number of matches can be limited with an optional third parameter.
func findRE(expr string, content interface{}, limit ...interface{}) ([]string, error) {
re, err := reCache.Get(expr)
if err != nil {
return nil, err
@ -546,11 +546,17 @@ func findRE(expr string, content interface{}, limit ...int) ([]string, error) {
if err != nil {
return nil, err
}
if len(limit) > 0 {
return re.FindAllString(conv, limit[0]), nil
if len(limit) == 0 {
return re.FindAllString(conv, -1), nil
}
return re.FindAllString(conv, -1), nil
lim, err := cast.ToIntE(limit[0])
if err != nil {
return nil, err
}
return re.FindAllString(conv, lim), nil
}
// last returns the last N items in a rangeable list.

View file

@ -121,7 +121,7 @@ div: {{div 6 3}}
echoParam: {{ echoParam .Params "langCode" }}
emojify: {{ "I :heart: Hugo" | emojify }}
eq: {{ if eq .Section "blog" }}current{{ end }}
findRE: {{ findRE "[G|g]o" "Hugo is a static side generator written in Go." 1 }}
findRE: {{ findRE "[G|g]o" "Hugo is a static side generator written in Go." "1" }}
hasPrefix 1: {{ hasPrefix "Hugo" "Hu" }}
hasPrefix 2: {{ hasPrefix "Hugo" "Fu" }}
htmlEscape 1: {{ htmlEscape "Cathal Garvey & The Sunshine Band <cathal@foo.bar>" | safeHTML}}
@ -2233,29 +2233,24 @@ func TestFindRE(t *testing.T) {
for i, this := range []struct {
expr string
content interface{}
limit int
limit interface{}
expect []string
ok bool
}{
{"[G|g]o", "Hugo is a static site generator written in Go.", 2, []string{"go", "Go"}, true},
{"[G|g]o", "Hugo is a static site generator written in Go.", -1, []string{"go", "Go"}, true},
{"[G|g]o", "Hugo is a static site generator written in Go.", 1, []string{"go"}, true},
{"[G|g]o", "Hugo is a static site generator written in Go.", 0, []string(nil), true},
{"[G|go", "Hugo is a static site generator written in Go.", 0, []string(nil), false},
{"[G|g]o", t, 0, []string(nil), false},
{"[G|g]o", "Hugo is a static site generator written in Go.", "1", []string{"go"}, true},
{"[G|g]o", "Hugo is a static site generator written in Go.", nil, []string(nil), true},
{"[G|go", "Hugo is a static site generator written in Go.", nil, []string(nil), false},
{"[G|g]o", t, nil, []string(nil), false},
} {
var (
res []string
err error
)
if this.limit >= 0 {
res, err = findRE(this.expr, this.content, this.limit)
} else {
res, err = findRE(this.expr, this.content)
}
res, err = findRE(this.expr, this.content, this.limit)
if err != nil && this.ok {
t.Errorf("[%d] returned an unexpected error: %s", i, err)
}