diff options
author | Cameron Moore <moorereason@gmail.com> | 2017-02-08 10:40:11 -0600 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2017-02-10 20:02:28 +0700 |
commit | 5cc8b58907c63e6cfb668c575b40fbc3636a9655 (patch) | |
tree | 4f37154c28bd4217f4d42301d6a12c7e958758cf /tpl | |
parent | 298ebc37c2d485ab34735f81541a5deaf079b03e (diff) |
tpl: Accept limit as interface in findRE func
Fixes #3018
Diffstat (limited to 'tpl')
-rw-r--r-- | tpl/template_funcs.go | 16 | ||||
-rw-r--r-- | tpl/template_funcs_test.go | 19 |
2 files changed, 18 insertions, 17 deletions
diff --git a/tpl/template_funcs.go b/tpl/template_funcs.go index 5db5e54ac..01b975665 100644 --- a/tpl/template_funcs.go +++ b/tpl/template_funcs.go @@ -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 + } + + lim, err := cast.ToIntE(limit[0]) + if err != nil { + return nil, err } - return re.FindAllString(conv, -1), nil + return re.FindAllString(conv, lim), nil } // last returns the last N items in a rangeable list. diff --git a/tpl/template_funcs_test.go b/tpl/template_funcs_test.go index e5d0193a6..bbe31216b 100644 --- a/tpl/template_funcs_test.go +++ b/tpl/template_funcs_test.go @@ -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) } |