shithub: hugo

Download patch

ref: 5cc8b58907c63e6cfb668c575b40fbc3636a9655
parent: 298ebc37c2d485ab34735f81541a5deaf079b03e
author: Cameron Moore <[email protected]>
date: Wed Feb 8 05:40:11 EST 2017

tpl: Accept limit as interface in findRE func

Fixes #3018

--- a/tpl/template_funcs.go
+++ b/tpl/template_funcs.go
@@ -535,8 +535,8 @@
 }
 
 // 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 @@
 	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.
--- a/tpl/template_funcs_test.go
+++ b/tpl/template_funcs_test.go
@@ -121,7 +121,7 @@
 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 <[email protected]>" | safeHTML}}
@@ -2233,7 +2233,7 @@
 	for i, this := range []struct {
 		expr    string
 		content interface{}
-		limit   int
+		limit   interface{}
 		expect  []string
 		ok      bool
 	}{
@@ -2240,9 +2240,10 @@
 		{"[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
@@ -2249,13 +2250,7 @@
 			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)
 		}