Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/gohugoio/hugo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/tpl
diff options
context:
space:
mode:
authordigitalcraftsman <digitalcraftsman@protonmail.com>2016-04-05 19:02:18 +0300
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2016-04-05 23:26:03 +0300
commit5bfe16ef8d37b7030da9bdf1b68bdb18ff0079a3 (patch)
treeda3a3b17188b64d97266a6589d08ad81de51de52 /tpl
parent54750b078059734c3f2047ae3f56cac64a9f0666 (diff)
tpl: Add findRE template func
Diffstat (limited to 'tpl')
-rw-r--r--tpl/template_funcs.go17
-rw-r--r--tpl/template_funcs_test.go26
2 files changed, 43 insertions, 0 deletions
diff --git a/tpl/template_funcs.go b/tpl/template_funcs.go
index cd7fff26c..c3e807176 100644
--- a/tpl/template_funcs.go
+++ b/tpl/template_funcs.go
@@ -437,6 +437,22 @@ func first(limit interface{}, seq interface{}) (interface{}, error) {
return seqv.Slice(0, limitv).Interface(), nil
}
+// 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) {
+ re, err := reCache.Get(expr)
+ if err != nil {
+ return nil, err
+ }
+
+ conv := cast.ToString(content)
+ if len(limit) > 0 {
+ return re.FindAllString(conv, limit[0]), nil
+ }
+
+ return re.FindAllString(conv, -1), nil
+}
+
// last returns the last N items in a rangeable list.
func last(limit interface{}, seq interface{}) (interface{}, error) {
if limit == nil || seq == nil {
@@ -1729,6 +1745,7 @@ func init() {
"echoParam": returnWhenSet,
"emojify": emojify,
"eq": eq,
+ "findRE": findRE,
"first": first,
"ge": ge,
"getCSV": getCSV,
diff --git a/tpl/template_funcs_test.go b/tpl/template_funcs_test.go
index c16dfb804..7e83542fb 100644
--- a/tpl/template_funcs_test.go
+++ b/tpl/template_funcs_test.go
@@ -89,6 +89,7 @@ delimit: {{ delimit (slice "A" "B" "C") ", " " and " }}
div: {{div 6 3}}
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 }}
hasPrefix 1: {{ hasPrefix "Hugo" "Hu" }}
hasPrefix 2: {{ hasPrefix "Hugo" "Fu" }}
in: {{ if in "this string contains a substring" "substring" }}Substring found!{{ end }}
@@ -138,6 +139,7 @@ delimit: A, B and C
div: 2
emojify: I ❤️ Hugo
eq: current
+findRE: [go]
hasPrefix 1: true
hasPrefix 2: false
in: Substring found!
@@ -1801,6 +1803,30 @@ func TestReplaceRE(t *testing.T) {
}
}
+func TestFindRE(t *testing.T) {
+ for i, this := range []struct {
+ expr string
+ content string
+ limit int
+ expect []string
+ ok bool
+ }{
+ {"[G|g]o", "Hugo is a static side generator written in Go.", 2, []string{"go", "Go"}, true},
+ {"[G|g]o", "Hugo is a static side generator written in Go.", -1, []string{"go", "Go"}, true},
+ {"[G|g]o", "Hugo is a static side generator written in Go.", 1, []string{"go"}, true},
+ {"[G|g]o", "Hugo is a static side generator written in Go.", 0, []string(nil), true},
+ {"[G|go", "Hugo is a static side generator written in Go.", 0, []string(nil), false},
+ } {
+ res, err := findRE(this.expr, this.content, this.limit)
+
+ if err != nil && this.ok {
+ t.Errorf("[%d] returned an unexpected error: %s", i, err)
+ }
+
+ assert.Equal(t, this.expect, res)
+ }
+}
+
func TestTrim(t *testing.T) {
for i, this := range []struct {