mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-07 20:30:36 -05:00
parent
e9f87c4e3f
commit
028b356787
2 changed files with 22 additions and 0 deletions
|
@ -51,6 +51,13 @@ func init() {
|
||||||
[][2]string{},
|
[][2]string{},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
ns.AddMethodMapping(ctx.Count,
|
||||||
|
nil,
|
||||||
|
[][2]string{
|
||||||
|
{`{{"aabab" | strings.Count "a" }}`, `3`},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
ns.AddMethodMapping(ctx.FindRE,
|
ns.AddMethodMapping(ctx.FindRE,
|
||||||
[]string{"findRE"},
|
[]string{"findRE"},
|
||||||
[][2]string{
|
[][2]string{
|
||||||
|
|
|
@ -18,6 +18,7 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
|
"strings"
|
||||||
|
|
||||||
_strings "strings"
|
_strings "strings"
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
|
@ -90,6 +91,20 @@ func (ns *Namespace) CountWords(s interface{}) (int, error) {
|
||||||
return counter, nil
|
return counter, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Count counts the number of non-overlapping instances of substr in s.
|
||||||
|
// If substr is an empty string, Count returns 1 + the number of Unicode code points in s.
|
||||||
|
func (ns *Namespace) Count(substr, s interface{}) (int, error) {
|
||||||
|
substrs, err := cast.ToStringE(substr)
|
||||||
|
if err != nil {
|
||||||
|
return 0, _errors.Wrap(err, "Failed to convert substr to string")
|
||||||
|
}
|
||||||
|
ss, err := cast.ToStringE(s)
|
||||||
|
if err != nil {
|
||||||
|
return 0, _errors.Wrap(err, "Failed to convert s to string")
|
||||||
|
}
|
||||||
|
return strings.Count(ss, substrs), nil
|
||||||
|
}
|
||||||
|
|
||||||
// Chomp returns a copy of s with all trailing newline characters removed.
|
// Chomp returns a copy of s with all trailing newline characters removed.
|
||||||
func (ns *Namespace) Chomp(s interface{}) (interface{}, error) {
|
func (ns *Namespace) Chomp(s interface{}) (interface{}, error) {
|
||||||
ss, err := cast.ToStringE(s)
|
ss, err := cast.ToStringE(s)
|
||||||
|
|
Loading…
Reference in a new issue