2016-02-24 18:52:11 -05:00
|
|
|
// Copyright 2016 The Hugo Authors. All rights reserved.
|
2015-04-05 15:03:16 -04:00
|
|
|
//
|
2016-03-10 14:59:41 -05:00
|
|
|
// Portions Copyright The Go Authors.
|
|
|
|
|
2015-11-23 22:16:36 -05:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
2015-04-05 15:03:16 -04:00
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2015-11-23 22:16:36 -05:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2015-04-05 15:03:16 -04:00
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package tpl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2016-03-07 15:40:47 -05:00
|
|
|
_md5 "crypto/md5"
|
|
|
|
_sha1 "crypto/sha1"
|
2015-09-12 15:45:12 -04:00
|
|
|
"encoding/base64"
|
2016-03-07 15:40:47 -05:00
|
|
|
"encoding/hex"
|
2016-03-06 08:58:15 -05:00
|
|
|
"encoding/json"
|
2015-04-05 15:03:16 -04:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"html"
|
|
|
|
"html/template"
|
2015-12-06 05:28:03 -05:00
|
|
|
"math/rand"
|
2016-07-04 21:35:24 -04:00
|
|
|
"net/url"
|
2015-04-05 15:03:16 -04:00
|
|
|
"os"
|
|
|
|
"reflect"
|
2016-02-08 17:57:52 -05:00
|
|
|
"regexp"
|
2015-04-05 15:03:16 -04:00
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2016-03-14 00:17:42 -04:00
|
|
|
"sync"
|
2015-05-26 06:33:32 -04:00
|
|
|
"time"
|
2015-09-20 12:15:45 -04:00
|
|
|
"unicode/utf8"
|
2015-05-26 06:33:32 -04:00
|
|
|
|
2016-04-13 13:13:33 -04:00
|
|
|
"github.com/bep/inflect"
|
2016-09-12 05:03:53 -04:00
|
|
|
"github.com/spf13/afero"
|
2015-05-26 06:33:32 -04:00
|
|
|
"github.com/spf13/cast"
|
|
|
|
"github.com/spf13/hugo/helpers"
|
2016-09-12 05:03:53 -04:00
|
|
|
"github.com/spf13/hugo/hugofs"
|
2015-05-26 06:33:32 -04:00
|
|
|
jww "github.com/spf13/jwalterweatherman"
|
2016-09-12 05:03:53 -04:00
|
|
|
"github.com/spf13/viper"
|
2015-04-05 15:03:16 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var funcMap template.FuncMap
|
|
|
|
|
2016-02-06 09:53:39 -05:00
|
|
|
// eq returns the boolean truth of arg1 == arg2.
|
|
|
|
func eq(x, y interface{}) bool {
|
2015-04-05 15:03:16 -04:00
|
|
|
normalize := func(v interface{}) interface{} {
|
|
|
|
vv := reflect.ValueOf(v)
|
|
|
|
switch vv.Kind() {
|
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
|
|
return vv.Int()
|
|
|
|
case reflect.Float32, reflect.Float64:
|
|
|
|
return vv.Float()
|
|
|
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
|
|
|
return vv.Uint()
|
|
|
|
default:
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
x = normalize(x)
|
|
|
|
y = normalize(y)
|
|
|
|
return reflect.DeepEqual(x, y)
|
|
|
|
}
|
|
|
|
|
2016-02-06 09:53:39 -05:00
|
|
|
// ne returns the boolean truth of arg1 != arg2.
|
|
|
|
func ne(x, y interface{}) bool {
|
|
|
|
return !eq(x, y)
|
2015-04-05 15:03:16 -04:00
|
|
|
}
|
|
|
|
|
2016-02-06 09:53:39 -05:00
|
|
|
// ge returns the boolean truth of arg1 >= arg2.
|
|
|
|
func ge(a, b interface{}) bool {
|
2015-04-05 15:03:16 -04:00
|
|
|
left, right := compareGetFloat(a, b)
|
|
|
|
return left >= right
|
|
|
|
}
|
|
|
|
|
2016-02-06 09:53:39 -05:00
|
|
|
// gt returns the boolean truth of arg1 > arg2.
|
|
|
|
func gt(a, b interface{}) bool {
|
2015-04-05 15:03:16 -04:00
|
|
|
left, right := compareGetFloat(a, b)
|
|
|
|
return left > right
|
|
|
|
}
|
|
|
|
|
2016-02-06 09:53:39 -05:00
|
|
|
// le returns the boolean truth of arg1 <= arg2.
|
|
|
|
func le(a, b interface{}) bool {
|
2015-04-05 15:03:16 -04:00
|
|
|
left, right := compareGetFloat(a, b)
|
|
|
|
return left <= right
|
|
|
|
}
|
|
|
|
|
2016-02-06 09:53:39 -05:00
|
|
|
// lt returns the boolean truth of arg1 < arg2.
|
|
|
|
func lt(a, b interface{}) bool {
|
2015-04-05 15:03:16 -04:00
|
|
|
left, right := compareGetFloat(a, b)
|
|
|
|
return left < right
|
|
|
|
}
|
|
|
|
|
2016-02-06 09:53:39 -05:00
|
|
|
// dictionary creates a map[string]interface{} from the given parameters by
|
2016-02-05 01:05:04 -05:00
|
|
|
// walking the parameters and treating them as key-value pairs. The number
|
|
|
|
// of parameters must be even.
|
2016-02-06 09:53:39 -05:00
|
|
|
func dictionary(values ...interface{}) (map[string]interface{}, error) {
|
2015-10-02 11:30:21 -04:00
|
|
|
if len(values)%2 != 0 {
|
|
|
|
return nil, errors.New("invalid dict call")
|
|
|
|
}
|
|
|
|
dict := make(map[string]interface{}, len(values)/2)
|
2015-10-12 14:47:06 -04:00
|
|
|
for i := 0; i < len(values); i += 2 {
|
2015-10-02 11:30:21 -04:00
|
|
|
key, ok := values[i].(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("dict keys must be strings")
|
|
|
|
}
|
2015-10-12 14:47:06 -04:00
|
|
|
dict[key] = values[i+1]
|
2015-10-02 11:30:21 -04:00
|
|
|
}
|
|
|
|
return dict, nil
|
|
|
|
}
|
|
|
|
|
2016-02-27 10:51:07 -05:00
|
|
|
// slice returns a slice of all passed arguments
|
|
|
|
func slice(args ...interface{}) []interface{} {
|
|
|
|
return args
|
|
|
|
}
|
|
|
|
|
2015-04-05 15:03:16 -04:00
|
|
|
func compareGetFloat(a interface{}, b interface{}) (float64, float64) {
|
|
|
|
var left, right float64
|
|
|
|
var leftStr, rightStr *string
|
|
|
|
av := reflect.ValueOf(a)
|
|
|
|
|
|
|
|
switch av.Kind() {
|
|
|
|
case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice:
|
|
|
|
left = float64(av.Len())
|
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
|
|
left = float64(av.Int())
|
|
|
|
case reflect.Float32, reflect.Float64:
|
|
|
|
left = av.Float()
|
|
|
|
case reflect.String:
|
2016-02-05 01:05:04 -05:00
|
|
|
var err error
|
2015-04-05 15:03:16 -04:00
|
|
|
left, err = strconv.ParseFloat(av.String(), 64)
|
|
|
|
if err != nil {
|
|
|
|
str := av.String()
|
|
|
|
leftStr = &str
|
|
|
|
}
|
2015-11-20 05:53:25 -05:00
|
|
|
case reflect.Struct:
|
|
|
|
switch av.Type() {
|
|
|
|
case timeType:
|
2016-03-05 18:35:35 -05:00
|
|
|
left = float64(toTimeUnix(av))
|
2015-11-20 05:53:25 -05:00
|
|
|
}
|
2015-04-05 15:03:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bv := reflect.ValueOf(b)
|
|
|
|
|
|
|
|
switch bv.Kind() {
|
|
|
|
case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice:
|
|
|
|
right = float64(bv.Len())
|
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
|
|
right = float64(bv.Int())
|
|
|
|
case reflect.Float32, reflect.Float64:
|
|
|
|
right = bv.Float()
|
|
|
|
case reflect.String:
|
2016-02-05 01:05:04 -05:00
|
|
|
var err error
|
2015-04-05 15:03:16 -04:00
|
|
|
right, err = strconv.ParseFloat(bv.String(), 64)
|
|
|
|
if err != nil {
|
|
|
|
str := bv.String()
|
|
|
|
rightStr = &str
|
|
|
|
}
|
2015-11-20 05:53:25 -05:00
|
|
|
case reflect.Struct:
|
|
|
|
switch bv.Type() {
|
|
|
|
case timeType:
|
2016-03-05 18:35:35 -05:00
|
|
|
right = float64(toTimeUnix(bv))
|
2015-11-20 05:53:25 -05:00
|
|
|
}
|
2015-04-05 15:03:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case leftStr == nil || rightStr == nil:
|
|
|
|
case *leftStr < *rightStr:
|
|
|
|
return 0, 1
|
|
|
|
case *leftStr > *rightStr:
|
|
|
|
return 1, 0
|
|
|
|
default:
|
|
|
|
return 0, 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return left, right
|
|
|
|
}
|
|
|
|
|
2016-02-06 09:53:39 -05:00
|
|
|
// slicestr slices a string by specifying a half-open range with
|
2015-04-05 15:03:16 -04:00
|
|
|
// two indices, start and end. 1 and 4 creates a slice including elements 1 through 3.
|
|
|
|
// The end index can be omitted, it defaults to the string's length.
|
2016-02-06 09:53:39 -05:00
|
|
|
func slicestr(a interface{}, startEnd ...interface{}) (string, error) {
|
2015-04-05 15:03:16 -04:00
|
|
|
aStr, err := cast.ToStringE(a)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2015-08-14 04:36:56 -04:00
|
|
|
var argStart, argEnd int
|
|
|
|
|
|
|
|
argNum := len(startEnd)
|
|
|
|
|
|
|
|
if argNum > 0 {
|
2015-08-15 09:47:16 -04:00
|
|
|
if argStart, err = cast.ToIntE(startEnd[0]); err != nil {
|
|
|
|
return "", errors.New("start argument must be integer")
|
2015-08-14 04:36:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if argNum > 1 {
|
2015-08-15 09:47:16 -04:00
|
|
|
if argEnd, err = cast.ToIntE(startEnd[1]); err != nil {
|
|
|
|
return "", errors.New("end argument must be integer")
|
2015-08-14 04:36:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if argNum > 2 {
|
2015-04-05 15:03:16 -04:00
|
|
|
return "", errors.New("too many arguments")
|
|
|
|
}
|
|
|
|
|
2015-08-07 02:52:22 -04:00
|
|
|
asRunes := []rune(aStr)
|
|
|
|
|
2015-08-14 04:36:56 -04:00
|
|
|
if argNum > 0 && (argStart < 0 || argStart >= len(asRunes)) {
|
2015-04-30 05:26:34 -04:00
|
|
|
return "", errors.New("slice bounds out of range")
|
|
|
|
}
|
|
|
|
|
2015-08-14 04:36:56 -04:00
|
|
|
if argNum == 2 {
|
|
|
|
if argEnd < 0 || argEnd > len(asRunes) {
|
2015-04-30 05:26:34 -04:00
|
|
|
return "", errors.New("slice bounds out of range")
|
|
|
|
}
|
2015-08-14 04:36:56 -04:00
|
|
|
return string(asRunes[argStart:argEnd]), nil
|
|
|
|
} else if argNum == 1 {
|
|
|
|
return string(asRunes[argStart:]), nil
|
2015-04-05 15:03:16 -04:00
|
|
|
} else {
|
2015-08-07 02:52:22 -04:00
|
|
|
return string(asRunes[:]), nil
|
2015-04-05 15:03:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-02-06 09:53:39 -05:00
|
|
|
// substr extracts parts of a string, beginning at the character at the specified
|
2015-04-05 15:03:16 -04:00
|
|
|
// position, and returns the specified number of characters.
|
|
|
|
//
|
|
|
|
// It normally takes two parameters: start and length.
|
|
|
|
// It can also take one parameter: start, i.e. length is omitted, in which case
|
|
|
|
// the substring starting from start until the end of the string will be returned.
|
|
|
|
//
|
|
|
|
// To extract characters from the end of the string, use a negative start number.
|
|
|
|
//
|
|
|
|
// In addition, borrowing from the extended behavior described at http://php.net/substr,
|
|
|
|
// if length is given and is negative, then that many characters will be omitted from
|
|
|
|
// the end of string.
|
2016-02-06 09:53:39 -05:00
|
|
|
func substr(a interface{}, nums ...interface{}) (string, error) {
|
2015-04-05 15:03:16 -04:00
|
|
|
aStr, err := cast.ToStringE(a)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
var start, length int
|
2015-06-06 11:21:14 -04:00
|
|
|
|
2015-08-07 02:52:22 -04:00
|
|
|
asRunes := []rune(aStr)
|
|
|
|
|
2015-04-05 15:03:16 -04:00
|
|
|
switch len(nums) {
|
2015-06-06 11:21:14 -04:00
|
|
|
case 0:
|
|
|
|
return "", errors.New("too less arguments")
|
2015-04-05 15:03:16 -04:00
|
|
|
case 1:
|
2015-08-15 09:47:16 -04:00
|
|
|
if start, err = cast.ToIntE(nums[0]); err != nil {
|
|
|
|
return "", errors.New("start argument must be integer")
|
2015-06-06 11:21:14 -04:00
|
|
|
}
|
2015-08-07 02:52:22 -04:00
|
|
|
length = len(asRunes)
|
2015-04-05 15:03:16 -04:00
|
|
|
case 2:
|
2015-08-15 09:47:16 -04:00
|
|
|
if start, err = cast.ToIntE(nums[0]); err != nil {
|
|
|
|
return "", errors.New("start argument must be integer")
|
2015-06-06 11:21:14 -04:00
|
|
|
}
|
2015-08-15 09:47:16 -04:00
|
|
|
if length, err = cast.ToIntE(nums[1]); err != nil {
|
|
|
|
return "", errors.New("length argument must be integer")
|
2015-06-06 11:21:14 -04:00
|
|
|
}
|
2015-04-05 15:03:16 -04:00
|
|
|
default:
|
|
|
|
return "", errors.New("too many arguments")
|
|
|
|
}
|
|
|
|
|
2015-08-07 02:52:22 -04:00
|
|
|
if start < -len(asRunes) {
|
2015-04-05 15:03:16 -04:00
|
|
|
start = 0
|
|
|
|
}
|
2015-08-07 02:52:22 -04:00
|
|
|
if start > len(asRunes) {
|
2016-02-05 01:05:04 -05:00
|
|
|
return "", fmt.Errorf("start position out of bounds for %d-byte string", len(aStr))
|
2015-04-05 15:03:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var s, e int
|
|
|
|
if start >= 0 && length >= 0 {
|
|
|
|
s = start
|
|
|
|
e = start + length
|
|
|
|
} else if start < 0 && length >= 0 {
|
2015-08-07 02:52:22 -04:00
|
|
|
s = len(asRunes) + start - length + 1
|
|
|
|
e = len(asRunes) + start + 1
|
2015-04-05 15:03:16 -04:00
|
|
|
} else if start >= 0 && length < 0 {
|
|
|
|
s = start
|
2015-08-07 02:52:22 -04:00
|
|
|
e = len(asRunes) + length
|
2015-04-05 15:03:16 -04:00
|
|
|
} else {
|
2015-08-07 02:52:22 -04:00
|
|
|
s = len(asRunes) + start
|
|
|
|
e = len(asRunes) + length
|
2015-04-05 15:03:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if s > e {
|
2016-02-05 01:05:04 -05:00
|
|
|
return "", fmt.Errorf("calculated start position greater than end position: %d > %d", s, e)
|
2015-04-05 15:03:16 -04:00
|
|
|
}
|
2015-08-07 02:52:22 -04:00
|
|
|
if e > len(asRunes) {
|
|
|
|
e = len(asRunes)
|
2015-04-05 15:03:16 -04:00
|
|
|
}
|
|
|
|
|
2015-08-07 02:52:22 -04:00
|
|
|
return string(asRunes[s:e]), nil
|
2015-04-05 15:03:16 -04:00
|
|
|
}
|
|
|
|
|
2016-02-06 09:53:39 -05:00
|
|
|
// split slices an input string into all substrings separated by delimiter.
|
|
|
|
func split(a interface{}, delimiter string) ([]string, error) {
|
2015-04-05 15:03:16 -04:00
|
|
|
aStr, err := cast.ToStringE(a)
|
|
|
|
if err != nil {
|
|
|
|
return []string{}, err
|
|
|
|
}
|
|
|
|
return strings.Split(aStr, delimiter), nil
|
|
|
|
}
|
|
|
|
|
2016-02-06 09:53:39 -05:00
|
|
|
// intersect returns the common elements in the given sets, l1 and l2. l1 and
|
2016-02-05 01:05:04 -05:00
|
|
|
// l2 must be of the same type and may be either arrays or slices.
|
2016-02-06 09:53:39 -05:00
|
|
|
func intersect(l1, l2 interface{}) (interface{}, error) {
|
2015-04-05 15:03:16 -04:00
|
|
|
if l1 == nil || l2 == nil {
|
|
|
|
return make([]interface{}, 0), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
l1v := reflect.ValueOf(l1)
|
|
|
|
l2v := reflect.ValueOf(l2)
|
|
|
|
|
|
|
|
switch l1v.Kind() {
|
|
|
|
case reflect.Array, reflect.Slice:
|
|
|
|
switch l2v.Kind() {
|
|
|
|
case reflect.Array, reflect.Slice:
|
|
|
|
r := reflect.MakeSlice(l1v.Type(), 0, 0)
|
|
|
|
for i := 0; i < l1v.Len(); i++ {
|
|
|
|
l1vv := l1v.Index(i)
|
|
|
|
for j := 0; j < l2v.Len(); j++ {
|
|
|
|
l2vv := l2v.Index(j)
|
|
|
|
switch l1vv.Kind() {
|
|
|
|
case reflect.String:
|
2016-03-13 17:39:03 -04:00
|
|
|
if l1vv.Type() == l2vv.Type() && l1vv.String() == l2vv.String() && !in(r.Interface(), l2vv.Interface()) {
|
2015-04-05 15:03:16 -04:00
|
|
|
r = reflect.Append(r, l2vv)
|
|
|
|
}
|
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
|
|
switch l2vv.Kind() {
|
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
2016-03-13 17:39:03 -04:00
|
|
|
if l1vv.Int() == l2vv.Int() && !in(r.Interface(), l2vv.Interface()) {
|
2015-04-05 15:03:16 -04:00
|
|
|
r = reflect.Append(r, l2vv)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case reflect.Float32, reflect.Float64:
|
|
|
|
switch l2vv.Kind() {
|
|
|
|
case reflect.Float32, reflect.Float64:
|
2016-03-13 17:39:03 -04:00
|
|
|
if l1vv.Float() == l2vv.Float() && !in(r.Interface(), l2vv.Interface()) {
|
2015-04-05 15:03:16 -04:00
|
|
|
r = reflect.Append(r, l2vv)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return r.Interface(), nil
|
|
|
|
default:
|
|
|
|
return nil, errors.New("can't iterate over " + reflect.ValueOf(l2).Type().String())
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return nil, errors.New("can't iterate over " + reflect.ValueOf(l1).Type().String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-06 09:53:39 -05:00
|
|
|
// in returns whether v is in the set l. l may be an array or slice.
|
|
|
|
func in(l interface{}, v interface{}) bool {
|
2015-04-05 15:03:16 -04:00
|
|
|
lv := reflect.ValueOf(l)
|
|
|
|
vv := reflect.ValueOf(v)
|
|
|
|
|
|
|
|
switch lv.Kind() {
|
|
|
|
case reflect.Array, reflect.Slice:
|
|
|
|
for i := 0; i < lv.Len(); i++ {
|
|
|
|
lvv := lv.Index(i)
|
2015-10-12 11:58:40 -04:00
|
|
|
lvv, isNil := indirect(lvv)
|
|
|
|
if isNil {
|
|
|
|
continue
|
|
|
|
}
|
2015-04-05 15:03:16 -04:00
|
|
|
switch lvv.Kind() {
|
|
|
|
case reflect.String:
|
|
|
|
if vv.Type() == lvv.Type() && vv.String() == lvv.String() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
|
|
switch vv.Kind() {
|
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
|
|
if vv.Int() == lvv.Int() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case reflect.Float32, reflect.Float64:
|
|
|
|
switch vv.Kind() {
|
|
|
|
case reflect.Float32, reflect.Float64:
|
|
|
|
if vv.Float() == lvv.Float() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case reflect.String:
|
|
|
|
if vv.Type() == lv.Type() && strings.Contains(lv.String(), vv.String()) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-02-06 09:53:39 -05:00
|
|
|
// first returns the first N items in a rangeable list.
|
|
|
|
func first(limit interface{}, seq interface{}) (interface{}, error) {
|
2015-04-05 15:03:16 -04:00
|
|
|
if limit == nil || seq == nil {
|
|
|
|
return nil, errors.New("both limit and seq must be provided")
|
|
|
|
}
|
|
|
|
|
|
|
|
limitv, err := cast.ToIntE(limit)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if limitv < 1 {
|
|
|
|
return nil, errors.New("can't return negative/empty count of items from sequence")
|
|
|
|
}
|
|
|
|
|
|
|
|
seqv := reflect.ValueOf(seq)
|
|
|
|
seqv, isNil := indirect(seqv)
|
|
|
|
if isNil {
|
|
|
|
return nil, errors.New("can't iterate over a nil value")
|
|
|
|
}
|
|
|
|
|
|
|
|
switch seqv.Kind() {
|
|
|
|
case reflect.Array, reflect.Slice, reflect.String:
|
|
|
|
// okay
|
|
|
|
default:
|
|
|
|
return nil, errors.New("can't iterate over " + reflect.ValueOf(seq).Type().String())
|
|
|
|
}
|
|
|
|
if limitv > seqv.Len() {
|
|
|
|
limitv = seqv.Len()
|
|
|
|
}
|
|
|
|
return seqv.Slice(0, limitv).Interface(), nil
|
|
|
|
}
|
|
|
|
|
2016-04-05 12:02:18 -04:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2016-08-12 18:43:15 -04:00
|
|
|
conv, err := cast.ToStringE(content)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-04-05 12:02:18 -04:00
|
|
|
if len(limit) > 0 {
|
|
|
|
return re.FindAllString(conv, limit[0]), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return re.FindAllString(conv, -1), nil
|
|
|
|
}
|
|
|
|
|
2016-02-06 09:53:39 -05:00
|
|
|
// last returns the last N items in a rangeable list.
|
|
|
|
func last(limit interface{}, seq interface{}) (interface{}, error) {
|
2015-06-10 18:11:47 -04:00
|
|
|
if limit == nil || seq == nil {
|
|
|
|
return nil, errors.New("both limit and seq must be provided")
|
|
|
|
}
|
|
|
|
|
|
|
|
limitv, err := cast.ToIntE(limit)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if limitv < 1 {
|
|
|
|
return nil, errors.New("can't return negative/empty count of items from sequence")
|
|
|
|
}
|
|
|
|
|
|
|
|
seqv := reflect.ValueOf(seq)
|
|
|
|
seqv, isNil := indirect(seqv)
|
|
|
|
if isNil {
|
|
|
|
return nil, errors.New("can't iterate over a nil value")
|
|
|
|
}
|
|
|
|
|
|
|
|
switch seqv.Kind() {
|
|
|
|
case reflect.Array, reflect.Slice, reflect.String:
|
|
|
|
// okay
|
|
|
|
default:
|
|
|
|
return nil, errors.New("can't iterate over " + reflect.ValueOf(seq).Type().String())
|
|
|
|
}
|
|
|
|
if limitv > seqv.Len() {
|
|
|
|
limitv = seqv.Len()
|
|
|
|
}
|
|
|
|
return seqv.Slice(seqv.Len()-limitv, seqv.Len()).Interface(), nil
|
|
|
|
}
|
|
|
|
|
2016-02-06 09:53:39 -05:00
|
|
|
// after returns all the items after the first N in a rangeable list.
|
|
|
|
func after(index interface{}, seq interface{}) (interface{}, error) {
|
2015-06-10 17:58:57 -04:00
|
|
|
if index == nil || seq == nil {
|
2015-06-10 17:49:55 -04:00
|
|
|
return nil, errors.New("both limit and seq must be provided")
|
|
|
|
}
|
|
|
|
|
2015-06-10 17:58:57 -04:00
|
|
|
indexv, err := cast.ToIntE(index)
|
2015-06-10 17:49:55 -04:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-06-10 17:58:57 -04:00
|
|
|
if indexv < 1 {
|
2015-06-10 17:49:55 -04:00
|
|
|
return nil, errors.New("can't return negative/empty count of items from sequence")
|
|
|
|
}
|
|
|
|
|
|
|
|
seqv := reflect.ValueOf(seq)
|
|
|
|
seqv, isNil := indirect(seqv)
|
|
|
|
if isNil {
|
|
|
|
return nil, errors.New("can't iterate over a nil value")
|
|
|
|
}
|
|
|
|
|
|
|
|
switch seqv.Kind() {
|
|
|
|
case reflect.Array, reflect.Slice, reflect.String:
|
|
|
|
// okay
|
|
|
|
default:
|
|
|
|
return nil, errors.New("can't iterate over " + reflect.ValueOf(seq).Type().String())
|
|
|
|
}
|
2015-06-10 17:58:57 -04:00
|
|
|
if indexv >= seqv.Len() {
|
2015-06-10 17:49:55 -04:00
|
|
|
return nil, errors.New("no items left")
|
|
|
|
}
|
2015-06-10 17:58:57 -04:00
|
|
|
return seqv.Slice(indexv, seqv.Len()).Interface(), nil
|
2015-06-10 17:49:55 -04:00
|
|
|
}
|
|
|
|
|
2016-02-06 09:53:39 -05:00
|
|
|
// shuffle returns the given rangeable list in a randomised order.
|
|
|
|
func shuffle(seq interface{}) (interface{}, error) {
|
2015-12-07 00:27:37 -05:00
|
|
|
if seq == nil {
|
2015-12-06 05:28:03 -05:00
|
|
|
return nil, errors.New("both count and seq must be provided")
|
|
|
|
}
|
|
|
|
|
|
|
|
seqv := reflect.ValueOf(seq)
|
|
|
|
seqv, isNil := indirect(seqv)
|
|
|
|
if isNil {
|
|
|
|
return nil, errors.New("can't iterate over a nil value")
|
|
|
|
}
|
|
|
|
|
|
|
|
switch seqv.Kind() {
|
|
|
|
case reflect.Array, reflect.Slice, reflect.String:
|
|
|
|
// okay
|
|
|
|
default:
|
|
|
|
return nil, errors.New("can't iterate over " + reflect.ValueOf(seq).Type().String())
|
|
|
|
}
|
|
|
|
|
2015-12-07 00:27:37 -05:00
|
|
|
shuffled := reflect.MakeSlice(reflect.TypeOf(seq), seqv.Len(), seqv.Len())
|
2015-12-06 05:28:03 -05:00
|
|
|
|
|
|
|
rand.Seed(time.Now().UTC().UnixNano())
|
|
|
|
randomIndices := rand.Perm(seqv.Len())
|
|
|
|
|
|
|
|
for index, value := range randomIndices {
|
2015-12-07 00:27:37 -05:00
|
|
|
shuffled.Index(value).Set(seqv.Index(index))
|
2015-12-06 05:28:03 -05:00
|
|
|
}
|
|
|
|
|
2015-12-07 00:27:37 -05:00
|
|
|
return shuffled.Interface(), nil
|
2015-12-06 05:28:03 -05:00
|
|
|
}
|
|
|
|
|
2015-04-05 15:03:16 -04:00
|
|
|
func evaluateSubElem(obj reflect.Value, elemName string) (reflect.Value, error) {
|
|
|
|
if !obj.IsValid() {
|
|
|
|
return zero, errors.New("can't evaluate an invalid value")
|
|
|
|
}
|
|
|
|
typ := obj.Type()
|
|
|
|
obj, isNil := indirect(obj)
|
|
|
|
|
|
|
|
// first, check whether obj has a method. In this case, obj is
|
|
|
|
// an interface, a struct or its pointer. If obj is a struct,
|
|
|
|
// to check all T and *T method, use obj pointer type Value
|
|
|
|
objPtr := obj
|
|
|
|
if objPtr.Kind() != reflect.Interface && objPtr.CanAddr() {
|
|
|
|
objPtr = objPtr.Addr()
|
|
|
|
}
|
|
|
|
mt, ok := objPtr.Type().MethodByName(elemName)
|
|
|
|
if ok {
|
|
|
|
if mt.PkgPath != "" {
|
|
|
|
return zero, fmt.Errorf("%s is an unexported method of type %s", elemName, typ)
|
|
|
|
}
|
|
|
|
// struct pointer has one receiver argument and interface doesn't have an argument
|
|
|
|
if mt.Type.NumIn() > 1 || mt.Type.NumOut() == 0 || mt.Type.NumOut() > 2 {
|
|
|
|
return zero, fmt.Errorf("%s is a method of type %s but doesn't satisfy requirements", elemName, typ)
|
|
|
|
}
|
|
|
|
if mt.Type.NumOut() == 1 && mt.Type.Out(0).Implements(errorType) {
|
|
|
|
return zero, fmt.Errorf("%s is a method of type %s but doesn't satisfy requirements", elemName, typ)
|
|
|
|
}
|
|
|
|
if mt.Type.NumOut() == 2 && !mt.Type.Out(1).Implements(errorType) {
|
|
|
|
return zero, fmt.Errorf("%s is a method of type %s but doesn't satisfy requirements", elemName, typ)
|
|
|
|
}
|
|
|
|
res := objPtr.Method(mt.Index).Call([]reflect.Value{})
|
|
|
|
if len(res) == 2 && !res[1].IsNil() {
|
|
|
|
return zero, fmt.Errorf("error at calling a method %s of type %s: %s", elemName, typ, res[1].Interface().(error))
|
|
|
|
}
|
|
|
|
return res[0], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// elemName isn't a method so next start to check whether it is
|
|
|
|
// a struct field or a map value. In both cases, it mustn't be
|
|
|
|
// a nil value
|
|
|
|
if isNil {
|
|
|
|
return zero, fmt.Errorf("can't evaluate a nil pointer of type %s by a struct field or map key name %s", typ, elemName)
|
|
|
|
}
|
|
|
|
switch obj.Kind() {
|
|
|
|
case reflect.Struct:
|
|
|
|
ft, ok := obj.Type().FieldByName(elemName)
|
|
|
|
if ok {
|
2016-01-28 05:27:35 -05:00
|
|
|
if ft.PkgPath != "" && !ft.Anonymous {
|
2015-04-05 15:03:16 -04:00
|
|
|
return zero, fmt.Errorf("%s is an unexported field of struct type %s", elemName, typ)
|
|
|
|
}
|
|
|
|
return obj.FieldByIndex(ft.Index), nil
|
|
|
|
}
|
|
|
|
return zero, fmt.Errorf("%s isn't a field of struct type %s", elemName, typ)
|
|
|
|
case reflect.Map:
|
|
|
|
kv := reflect.ValueOf(elemName)
|
|
|
|
if kv.Type().AssignableTo(obj.Type().Key()) {
|
|
|
|
return obj.MapIndex(kv), nil
|
|
|
|
}
|
|
|
|
return zero, fmt.Errorf("%s isn't a key of map type %s", elemName, typ)
|
|
|
|
}
|
|
|
|
return zero, fmt.Errorf("%s is neither a struct field, a method nor a map element of type %s", elemName, typ)
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkCondition(v, mv reflect.Value, op string) (bool, error) {
|
2015-06-27 13:15:42 -04:00
|
|
|
v, vIsNil := indirect(v)
|
|
|
|
if !v.IsValid() {
|
|
|
|
vIsNil = true
|
|
|
|
}
|
|
|
|
mv, mvIsNil := indirect(mv)
|
|
|
|
if !mv.IsValid() {
|
|
|
|
mvIsNil = true
|
|
|
|
}
|
|
|
|
if vIsNil || mvIsNil {
|
|
|
|
switch op {
|
|
|
|
case "", "=", "==", "eq":
|
|
|
|
return vIsNil == mvIsNil, nil
|
|
|
|
case "!=", "<>", "ne":
|
|
|
|
return vIsNil != mvIsNil, nil
|
|
|
|
}
|
2015-04-05 15:03:16 -04:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2015-12-07 15:28:36 -05:00
|
|
|
if v.Kind() == reflect.Bool && mv.Kind() == reflect.Bool {
|
|
|
|
switch op {
|
|
|
|
case "", "=", "==", "eq":
|
|
|
|
return v.Bool() == mv.Bool(), nil
|
|
|
|
case "!=", "<>", "ne":
|
|
|
|
return v.Bool() != mv.Bool(), nil
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2015-04-05 15:03:16 -04:00
|
|
|
var ivp, imvp *int64
|
|
|
|
var svp, smvp *string
|
2016-04-11 16:58:27 -04:00
|
|
|
var slv, slmv interface{}
|
2015-04-05 15:03:16 -04:00
|
|
|
var ima []int64
|
|
|
|
var sma []string
|
|
|
|
if mv.Type() == v.Type() {
|
|
|
|
switch v.Kind() {
|
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
|
|
iv := v.Int()
|
|
|
|
ivp = &iv
|
|
|
|
imv := mv.Int()
|
|
|
|
imvp = &imv
|
|
|
|
case reflect.String:
|
|
|
|
sv := v.String()
|
|
|
|
svp = &sv
|
|
|
|
smv := mv.String()
|
|
|
|
smvp = &smv
|
2015-05-26 06:33:32 -04:00
|
|
|
case reflect.Struct:
|
|
|
|
switch v.Type() {
|
|
|
|
case timeType:
|
2016-03-05 18:35:35 -05:00
|
|
|
iv := toTimeUnix(v)
|
2015-05-26 06:33:32 -04:00
|
|
|
ivp = &iv
|
2016-03-05 18:35:35 -05:00
|
|
|
imv := toTimeUnix(mv)
|
2015-05-26 06:33:32 -04:00
|
|
|
imvp = &imv
|
|
|
|
}
|
2016-04-11 16:58:27 -04:00
|
|
|
case reflect.Array, reflect.Slice:
|
|
|
|
slv = v.Interface()
|
|
|
|
slmv = mv.Interface()
|
2015-04-05 15:03:16 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if mv.Kind() != reflect.Array && mv.Kind() != reflect.Slice {
|
|
|
|
return false, nil
|
|
|
|
}
|
2016-03-05 18:35:35 -05:00
|
|
|
|
|
|
|
if mv.Len() == 0 {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.Kind() != reflect.Interface && mv.Type().Elem().Kind() != reflect.Interface && mv.Type().Elem() != v.Type() {
|
2015-04-05 15:03:16 -04:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
switch v.Kind() {
|
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
|
|
iv := v.Int()
|
|
|
|
ivp = &iv
|
|
|
|
for i := 0; i < mv.Len(); i++ {
|
2016-03-05 18:35:35 -05:00
|
|
|
if anInt := toInt(mv.Index(i)); anInt != -1 {
|
|
|
|
ima = append(ima, anInt)
|
|
|
|
}
|
|
|
|
|
2015-04-05 15:03:16 -04:00
|
|
|
}
|
|
|
|
case reflect.String:
|
|
|
|
sv := v.String()
|
|
|
|
svp = &sv
|
|
|
|
for i := 0; i < mv.Len(); i++ {
|
2016-03-05 18:35:35 -05:00
|
|
|
if aString := toString(mv.Index(i)); aString != "" {
|
|
|
|
sma = append(sma, aString)
|
|
|
|
}
|
2015-04-05 15:03:16 -04:00
|
|
|
}
|
2015-05-26 06:33:32 -04:00
|
|
|
case reflect.Struct:
|
|
|
|
switch v.Type() {
|
|
|
|
case timeType:
|
2016-03-05 18:35:35 -05:00
|
|
|
iv := toTimeUnix(v)
|
2015-05-26 06:33:32 -04:00
|
|
|
ivp = &iv
|
|
|
|
for i := 0; i < mv.Len(); i++ {
|
2016-03-05 18:35:35 -05:00
|
|
|
ima = append(ima, toTimeUnix(mv.Index(i)))
|
2015-05-26 06:33:32 -04:00
|
|
|
}
|
|
|
|
}
|
2015-04-05 15:03:16 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch op {
|
|
|
|
case "", "=", "==", "eq":
|
|
|
|
if ivp != nil && imvp != nil {
|
|
|
|
return *ivp == *imvp, nil
|
|
|
|
} else if svp != nil && smvp != nil {
|
|
|
|
return *svp == *smvp, nil
|
|
|
|
}
|
|
|
|
case "!=", "<>", "ne":
|
|
|
|
if ivp != nil && imvp != nil {
|
|
|
|
return *ivp != *imvp, nil
|
|
|
|
} else if svp != nil && smvp != nil {
|
|
|
|
return *svp != *smvp, nil
|
|
|
|
}
|
|
|
|
case ">=", "ge":
|
|
|
|
if ivp != nil && imvp != nil {
|
|
|
|
return *ivp >= *imvp, nil
|
|
|
|
} else if svp != nil && smvp != nil {
|
|
|
|
return *svp >= *smvp, nil
|
|
|
|
}
|
|
|
|
case ">", "gt":
|
|
|
|
if ivp != nil && imvp != nil {
|
|
|
|
return *ivp > *imvp, nil
|
|
|
|
} else if svp != nil && smvp != nil {
|
|
|
|
return *svp > *smvp, nil
|
|
|
|
}
|
|
|
|
case "<=", "le":
|
|
|
|
if ivp != nil && imvp != nil {
|
|
|
|
return *ivp <= *imvp, nil
|
|
|
|
} else if svp != nil && smvp != nil {
|
|
|
|
return *svp <= *smvp, nil
|
|
|
|
}
|
|
|
|
case "<", "lt":
|
|
|
|
if ivp != nil && imvp != nil {
|
|
|
|
return *ivp < *imvp, nil
|
|
|
|
} else if svp != nil && smvp != nil {
|
|
|
|
return *svp < *smvp, nil
|
|
|
|
}
|
|
|
|
case "in", "not in":
|
|
|
|
var r bool
|
|
|
|
if ivp != nil && len(ima) > 0 {
|
2016-02-06 09:53:39 -05:00
|
|
|
r = in(ima, *ivp)
|
2015-04-05 15:03:16 -04:00
|
|
|
} else if svp != nil {
|
|
|
|
if len(sma) > 0 {
|
2016-02-06 09:53:39 -05:00
|
|
|
r = in(sma, *svp)
|
2015-04-05 15:03:16 -04:00
|
|
|
} else if smvp != nil {
|
2016-02-06 09:53:39 -05:00
|
|
|
r = in(*smvp, *svp)
|
2015-04-05 15:03:16 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
if op == "not in" {
|
|
|
|
return !r, nil
|
|
|
|
}
|
2016-02-05 01:05:04 -05:00
|
|
|
return r, nil
|
2016-04-11 16:58:27 -04:00
|
|
|
case "intersect":
|
|
|
|
r, err := intersect(slv, slmv)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if reflect.TypeOf(r).Kind() == reflect.Slice {
|
|
|
|
s := reflect.ValueOf(r)
|
|
|
|
|
|
|
|
if s.Len() > 0 {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
} else {
|
|
|
|
return false, errors.New("invalid intersect values")
|
|
|
|
}
|
2015-04-05 15:03:16 -04:00
|
|
|
default:
|
2016-04-11 16:58:27 -04:00
|
|
|
return false, errors.New("no such operator")
|
2015-04-05 15:03:16 -04:00
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2016-09-12 05:03:53 -04:00
|
|
|
// parseWhereArgs parses the end arguments to the where function. Return a
|
|
|
|
// match value and an operator, if one is defined.
|
|
|
|
func parseWhereArgs(args ...interface{}) (mv reflect.Value, op string, err error) {
|
2015-04-05 15:03:16 -04:00
|
|
|
switch len(args) {
|
|
|
|
case 1:
|
|
|
|
mv = reflect.ValueOf(args[0])
|
|
|
|
case 2:
|
|
|
|
var ok bool
|
|
|
|
if op, ok = args[0].(string); !ok {
|
2016-09-12 05:03:53 -04:00
|
|
|
err = errors.New("operator argument must be string type")
|
|
|
|
return
|
2015-04-05 15:03:16 -04:00
|
|
|
}
|
|
|
|
op = strings.TrimSpace(strings.ToLower(op))
|
|
|
|
mv = reflect.ValueOf(args[1])
|
|
|
|
default:
|
2016-09-12 05:03:53 -04:00
|
|
|
err = errors.New("can't evaluate the array by no match argument or more than or equal to two arguments")
|
2016-04-12 21:31:14 -04:00
|
|
|
}
|
2016-09-12 05:03:53 -04:00
|
|
|
return
|
|
|
|
}
|
2016-04-12 21:31:14 -04:00
|
|
|
|
2016-09-12 05:03:53 -04:00
|
|
|
// checkWhereArray handles the where-matching logic when the seqv value is an
|
|
|
|
// Array or Slice.
|
|
|
|
func checkWhereArray(seqv, kv, mv reflect.Value, path []string, op string) (interface{}, error) {
|
|
|
|
rv := reflect.MakeSlice(seqv.Type(), 0, 0)
|
|
|
|
for i := 0; i < seqv.Len(); i++ {
|
|
|
|
var vvv reflect.Value
|
|
|
|
rvv := seqv.Index(i)
|
|
|
|
if kv.Kind() == reflect.String {
|
|
|
|
vvv = rvv
|
|
|
|
for _, elemName := range path {
|
|
|
|
var err error
|
|
|
|
vvv, err = evaluateSubElem(vvv, elemName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
vv, _ := indirect(rvv)
|
|
|
|
if vv.Kind() == reflect.Map && kv.Type().AssignableTo(vv.Type().Key()) {
|
|
|
|
vvv = vv.MapIndex(kv)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ok, err := checkCondition(vvv, mv, op); ok {
|
|
|
|
rv = reflect.Append(rv, rvv)
|
|
|
|
} else if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rv.Interface(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkWhereMap handles the where-matching logic when the seqv value is a Map.
|
|
|
|
func checkWhereMap(seqv, kv, mv reflect.Value, path []string, op string) (interface{}, error) {
|
|
|
|
rv := reflect.MakeMap(seqv.Type())
|
|
|
|
keys := seqv.MapKeys()
|
|
|
|
for _, k := range keys {
|
|
|
|
elemv := seqv.MapIndex(k)
|
|
|
|
switch elemv.Kind() {
|
|
|
|
case reflect.Array, reflect.Slice:
|
|
|
|
r, err := checkWhereArray(elemv, kv, mv, path, op)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch rr := reflect.ValueOf(r); rr.Kind() {
|
|
|
|
case reflect.Slice:
|
|
|
|
if rr.Len() > 0 {
|
|
|
|
rv.SetMapIndex(k, elemv)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case reflect.Interface:
|
|
|
|
elemvv, isNil := indirect(elemv)
|
|
|
|
if isNil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
switch elemvv.Kind() {
|
|
|
|
case reflect.Array, reflect.Slice:
|
|
|
|
r, err := checkWhereArray(elemvv, kv, mv, path, op)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch rr := reflect.ValueOf(r); rr.Kind() {
|
|
|
|
case reflect.Slice:
|
|
|
|
if rr.Len() > 0 {
|
|
|
|
rv.SetMapIndex(k, elemv)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rv.Interface(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// where returns a filtered subset of a given data type.
|
|
|
|
func where(seq, key interface{}, args ...interface{}) (interface{}, error) {
|
|
|
|
seqv, isNil := indirect(reflect.ValueOf(seq))
|
2015-04-05 15:03:16 -04:00
|
|
|
if isNil {
|
|
|
|
return nil, errors.New("can't iterate over a nil value of type " + reflect.ValueOf(seq).Type().String())
|
|
|
|
}
|
|
|
|
|
2016-09-12 05:03:53 -04:00
|
|
|
mv, op, err := parseWhereArgs(args...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-04-05 15:03:16 -04:00
|
|
|
var path []string
|
2016-09-12 05:03:53 -04:00
|
|
|
kv := reflect.ValueOf(key)
|
2015-04-05 15:03:16 -04:00
|
|
|
if kv.Kind() == reflect.String {
|
|
|
|
path = strings.Split(strings.Trim(kv.String(), "."), ".")
|
|
|
|
}
|
|
|
|
|
|
|
|
switch seqv.Kind() {
|
|
|
|
case reflect.Array, reflect.Slice:
|
2016-09-12 05:03:53 -04:00
|
|
|
return checkWhereArray(seqv, kv, mv, path, op)
|
|
|
|
case reflect.Map:
|
|
|
|
return checkWhereMap(seqv, kv, mv, path, op)
|
2015-04-05 15:03:16 -04:00
|
|
|
default:
|
2016-02-09 13:00:07 -05:00
|
|
|
return nil, fmt.Errorf("can't iterate over %v", seq)
|
2015-04-05 15:03:16 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-06 09:53:39 -05:00
|
|
|
// apply takes a map, array, or slice and returns a new slice with the function fname applied over it.
|
|
|
|
func apply(seq interface{}, fname string, args ...interface{}) (interface{}, error) {
|
2015-04-05 15:03:16 -04:00
|
|
|
if seq == nil {
|
|
|
|
return make([]interface{}, 0), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if fname == "apply" {
|
|
|
|
return nil, errors.New("can't apply myself (no turtles allowed)")
|
|
|
|
}
|
|
|
|
|
|
|
|
seqv := reflect.ValueOf(seq)
|
|
|
|
seqv, isNil := indirect(seqv)
|
|
|
|
if isNil {
|
|
|
|
return nil, errors.New("can't iterate over a nil value")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn, found := funcMap[fname]
|
|
|
|
if !found {
|
|
|
|
return nil, errors.New("can't find function " + fname)
|
|
|
|
}
|
|
|
|
|
|
|
|
fnv := reflect.ValueOf(fn)
|
|
|
|
|
|
|
|
switch seqv.Kind() {
|
|
|
|
case reflect.Array, reflect.Slice:
|
|
|
|
r := make([]interface{}, seqv.Len())
|
|
|
|
for i := 0; i < seqv.Len(); i++ {
|
|
|
|
vv := seqv.Index(i)
|
|
|
|
|
|
|
|
vvv, err := applyFnToThis(fnv, vv, args...)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
r[i] = vvv.Interface()
|
|
|
|
}
|
|
|
|
|
|
|
|
return r, nil
|
|
|
|
default:
|
2016-02-09 13:00:07 -05:00
|
|
|
return nil, fmt.Errorf("can't apply over %v", seq)
|
2015-04-05 15:03:16 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func applyFnToThis(fn, this reflect.Value, args ...interface{}) (reflect.Value, error) {
|
|
|
|
n := make([]reflect.Value, len(args))
|
|
|
|
for i, arg := range args {
|
|
|
|
if arg == "." {
|
|
|
|
n[i] = this
|
|
|
|
} else {
|
|
|
|
n[i] = reflect.ValueOf(arg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-30 04:51:01 -04:00
|
|
|
num := fn.Type().NumIn()
|
|
|
|
|
|
|
|
if fn.Type().IsVariadic() {
|
|
|
|
num--
|
|
|
|
}
|
|
|
|
|
2015-05-02 05:32:38 -04:00
|
|
|
// TODO(bep) see #1098 - also see template_tests.go
|
|
|
|
/*if len(args) < num {
|
2015-04-30 04:51:01 -04:00
|
|
|
return reflect.ValueOf(nil), errors.New("Too few arguments")
|
2015-04-30 05:41:25 -04:00
|
|
|
} else if len(args) > num {
|
|
|
|
return reflect.ValueOf(nil), errors.New("Too many arguments")
|
2015-05-02 05:32:38 -04:00
|
|
|
}*/
|
2015-04-30 04:51:01 -04:00
|
|
|
|
2015-05-01 11:00:22 -04:00
|
|
|
for i := 0; i < num; i++ {
|
|
|
|
if xt, targ := n[i].Type(), fn.Type().In(i); !xt.AssignableTo(targ) {
|
|
|
|
return reflect.ValueOf(nil), errors.New("called apply using " + xt.String() + " as type " + targ.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-05 15:03:16 -04:00
|
|
|
res := fn.Call(n)
|
|
|
|
|
|
|
|
if len(res) == 1 || res[1].IsNil() {
|
|
|
|
return res[0], nil
|
|
|
|
}
|
2016-02-05 01:05:04 -05:00
|
|
|
return reflect.ValueOf(nil), res[1].Interface().(error)
|
2015-04-05 15:03:16 -04:00
|
|
|
}
|
|
|
|
|
2016-02-06 09:53:39 -05:00
|
|
|
// delimit takes a given sequence and returns a delimited HTML string.
|
2016-02-05 01:05:04 -05:00
|
|
|
// If last is passed to the function, it will be used as the final delimiter.
|
2016-02-06 09:53:39 -05:00
|
|
|
func delimit(seq, delimiter interface{}, last ...interface{}) (template.HTML, error) {
|
2015-04-05 15:03:16 -04:00
|
|
|
d, err := cast.ToStringE(delimiter)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
var dLast *string
|
|
|
|
for _, l := range last {
|
|
|
|
dStr, err := cast.ToStringE(l)
|
|
|
|
if err != nil {
|
|
|
|
dLast = nil
|
|
|
|
}
|
|
|
|
dLast = &dStr
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
seqv := reflect.ValueOf(seq)
|
|
|
|
seqv, isNil := indirect(seqv)
|
|
|
|
if isNil {
|
|
|
|
return "", errors.New("can't iterate over a nil value")
|
|
|
|
}
|
|
|
|
|
|
|
|
var str string
|
|
|
|
switch seqv.Kind() {
|
|
|
|
case reflect.Map:
|
2016-02-06 09:53:39 -05:00
|
|
|
sortSeq, err := sortSeq(seq)
|
2015-04-05 15:03:16 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
seqv = reflect.ValueOf(sortSeq)
|
|
|
|
fallthrough
|
|
|
|
case reflect.Array, reflect.Slice, reflect.String:
|
|
|
|
for i := 0; i < seqv.Len(); i++ {
|
|
|
|
val := seqv.Index(i).Interface()
|
|
|
|
valStr, err := cast.ToStringE(val)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
switch {
|
|
|
|
case i == seqv.Len()-2 && dLast != nil:
|
|
|
|
str += valStr + *dLast
|
|
|
|
case i == seqv.Len()-1:
|
|
|
|
str += valStr
|
|
|
|
default:
|
|
|
|
str += valStr + d
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
2016-02-09 13:00:07 -05:00
|
|
|
return "", fmt.Errorf("can't iterate over %v", seq)
|
2015-04-05 15:03:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return template.HTML(str), nil
|
|
|
|
}
|
|
|
|
|
2016-02-06 09:53:39 -05:00
|
|
|
// sortSeq returns a sorted sequence.
|
|
|
|
func sortSeq(seq interface{}, args ...interface{}) (interface{}, error) {
|
2016-03-29 19:54:46 -04:00
|
|
|
if seq == nil {
|
|
|
|
return nil, errors.New("sequence must be provided")
|
|
|
|
}
|
|
|
|
|
2015-04-05 15:03:16 -04:00
|
|
|
seqv := reflect.ValueOf(seq)
|
|
|
|
seqv, isNil := indirect(seqv)
|
|
|
|
if isNil {
|
|
|
|
return nil, errors.New("can't iterate over a nil value")
|
|
|
|
}
|
|
|
|
|
2015-08-07 07:05:18 -04:00
|
|
|
switch seqv.Kind() {
|
|
|
|
case reflect.Array, reflect.Slice, reflect.Map:
|
|
|
|
// ok
|
|
|
|
default:
|
|
|
|
return nil, errors.New("can't sort " + reflect.ValueOf(seq).Type().String())
|
|
|
|
}
|
|
|
|
|
2015-04-05 15:03:16 -04:00
|
|
|
// Create a list of pairs that will be used to do the sort
|
2015-08-06 13:27:15 -04:00
|
|
|
p := pairList{SortAsc: true, SliceType: reflect.SliceOf(seqv.Type().Elem())}
|
2015-04-05 15:03:16 -04:00
|
|
|
p.Pairs = make([]pair, seqv.Len())
|
|
|
|
|
2015-08-07 07:05:18 -04:00
|
|
|
var sortByField string
|
2015-04-05 15:03:16 -04:00
|
|
|
for i, l := range args {
|
|
|
|
dStr, err := cast.ToStringE(l)
|
|
|
|
switch {
|
|
|
|
case i == 0 && err != nil:
|
2015-08-07 07:05:18 -04:00
|
|
|
sortByField = ""
|
2015-04-05 15:03:16 -04:00
|
|
|
case i == 0 && err == nil:
|
2015-08-07 07:05:18 -04:00
|
|
|
sortByField = dStr
|
2015-04-05 15:03:16 -04:00
|
|
|
case i == 1 && err == nil && dStr == "desc":
|
|
|
|
p.SortAsc = false
|
|
|
|
case i == 1:
|
|
|
|
p.SortAsc = true
|
|
|
|
}
|
|
|
|
}
|
2015-08-07 07:05:18 -04:00
|
|
|
path := strings.Split(strings.Trim(sortByField, "."), ".")
|
2015-04-05 15:03:16 -04:00
|
|
|
|
|
|
|
switch seqv.Kind() {
|
|
|
|
case reflect.Array, reflect.Slice:
|
|
|
|
for i := 0; i < seqv.Len(); i++ {
|
|
|
|
p.Pairs[i].Value = seqv.Index(i)
|
2015-08-07 07:05:18 -04:00
|
|
|
if sortByField == "" || sortByField == "value" {
|
2016-09-11 04:42:50 -04:00
|
|
|
p.Pairs[i].Key = p.Pairs[i].Value
|
2015-08-07 07:05:18 -04:00
|
|
|
} else {
|
|
|
|
v := p.Pairs[i].Value
|
|
|
|
var err error
|
|
|
|
for _, elemName := range path {
|
|
|
|
v, err = evaluateSubElem(v, elemName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2016-09-11 04:42:50 -04:00
|
|
|
p.Pairs[i].Key = v
|
2015-08-07 07:05:18 -04:00
|
|
|
}
|
2015-04-05 15:03:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
case reflect.Map:
|
|
|
|
keys := seqv.MapKeys()
|
|
|
|
for i := 0; i < seqv.Len(); i++ {
|
|
|
|
p.Pairs[i].Value = seqv.MapIndex(keys[i])
|
2015-08-07 07:05:18 -04:00
|
|
|
if sortByField == "" {
|
2016-09-11 04:42:50 -04:00
|
|
|
p.Pairs[i].Key = keys[i]
|
2015-08-07 07:05:18 -04:00
|
|
|
} else if sortByField == "value" {
|
2016-09-11 04:42:50 -04:00
|
|
|
p.Pairs[i].Key = p.Pairs[i].Value
|
2015-08-07 07:05:18 -04:00
|
|
|
} else {
|
|
|
|
v := p.Pairs[i].Value
|
|
|
|
var err error
|
|
|
|
for _, elemName := range path {
|
|
|
|
v, err = evaluateSubElem(v, elemName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2016-09-11 04:42:50 -04:00
|
|
|
p.Pairs[i].Key = v
|
2015-08-07 07:05:18 -04:00
|
|
|
}
|
2015-04-05 15:03:16 -04:00
|
|
|
}
|
|
|
|
}
|
2015-08-06 13:27:15 -04:00
|
|
|
return p.sort(), nil
|
2015-04-05 15:03:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Credit for pair sorting method goes to Andrew Gerrand
|
|
|
|
// https://groups.google.com/forum/#!topic/golang-nuts/FT7cjmcL7gw
|
|
|
|
// A data structure to hold a key/value pair.
|
|
|
|
type pair struct {
|
2016-09-11 04:42:50 -04:00
|
|
|
Key reflect.Value
|
|
|
|
Value reflect.Value
|
2015-04-05 15:03:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// A slice of pairs that implements sort.Interface to sort by Value.
|
|
|
|
type pairList struct {
|
2015-08-07 07:05:18 -04:00
|
|
|
Pairs []pair
|
|
|
|
SortAsc bool
|
|
|
|
SliceType reflect.Type
|
2015-04-05 15:03:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p pairList) Swap(i, j int) { p.Pairs[i], p.Pairs[j] = p.Pairs[j], p.Pairs[i] }
|
|
|
|
func (p pairList) Len() int { return len(p.Pairs) }
|
|
|
|
func (p pairList) Less(i, j int) bool {
|
2016-09-11 04:42:50 -04:00
|
|
|
iv := p.Pairs[i].Key
|
|
|
|
jv := p.Pairs[j].Key
|
2016-03-29 21:50:54 -04:00
|
|
|
|
|
|
|
if iv.IsValid() {
|
|
|
|
if jv.IsValid() {
|
|
|
|
// can only call Interface() on valid reflect Values
|
|
|
|
return lt(iv.Interface(), jv.Interface())
|
|
|
|
}
|
|
|
|
// if j is invalid, test i against i's zero value
|
|
|
|
return lt(iv.Interface(), reflect.Zero(iv.Type()))
|
|
|
|
}
|
|
|
|
|
|
|
|
if jv.IsValid() {
|
|
|
|
// if i is invalid, test j against j's zero value
|
|
|
|
return lt(reflect.Zero(jv.Type()), jv.Interface())
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
2015-04-05 15:03:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// sorts a pairList and returns a slice of sorted values
|
2015-08-06 13:27:15 -04:00
|
|
|
func (p pairList) sort() interface{} {
|
2015-04-05 15:03:16 -04:00
|
|
|
if p.SortAsc {
|
|
|
|
sort.Sort(p)
|
|
|
|
} else {
|
|
|
|
sort.Sort(sort.Reverse(p))
|
|
|
|
}
|
2015-08-06 13:27:15 -04:00
|
|
|
sorted := reflect.MakeSlice(p.SliceType, len(p.Pairs), len(p.Pairs))
|
2015-04-05 15:03:16 -04:00
|
|
|
for i, v := range p.Pairs {
|
2015-08-06 13:27:15 -04:00
|
|
|
sorted.Index(i).Set(v.Value)
|
2015-04-05 15:03:16 -04:00
|
|
|
}
|
|
|
|
|
2015-08-06 13:27:15 -04:00
|
|
|
return sorted.Interface()
|
2015-04-05 15:03:16 -04:00
|
|
|
}
|
|
|
|
|
2016-02-06 09:53:39 -05:00
|
|
|
// isSet returns whether a given array, channel, slice, or map has a key
|
2016-02-05 01:05:04 -05:00
|
|
|
// defined.
|
2016-02-06 09:53:39 -05:00
|
|
|
func isSet(a interface{}, key interface{}) bool {
|
2015-04-05 15:03:16 -04:00
|
|
|
av := reflect.ValueOf(a)
|
|
|
|
kv := reflect.ValueOf(key)
|
|
|
|
|
|
|
|
switch av.Kind() {
|
|
|
|
case reflect.Array, reflect.Chan, reflect.Slice:
|
|
|
|
if int64(av.Len()) > kv.Int() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
case reflect.Map:
|
|
|
|
if kv.Type() == av.Type().Key() {
|
|
|
|
return av.MapIndex(kv).IsValid()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-02-06 09:53:39 -05:00
|
|
|
// returnWhenSet returns a given value if it set. Otherwise, it returns an
|
2016-02-05 01:05:04 -05:00
|
|
|
// empty string.
|
2016-02-06 09:53:39 -05:00
|
|
|
func returnWhenSet(a, k interface{}) interface{} {
|
2015-04-05 15:03:16 -04:00
|
|
|
av, isNil := indirect(reflect.ValueOf(a))
|
|
|
|
if isNil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
var avv reflect.Value
|
|
|
|
switch av.Kind() {
|
|
|
|
case reflect.Array, reflect.Slice:
|
|
|
|
index, ok := k.(int)
|
|
|
|
if ok && av.Len() > index {
|
|
|
|
avv = av.Index(index)
|
|
|
|
}
|
|
|
|
case reflect.Map:
|
|
|
|
kv := reflect.ValueOf(k)
|
|
|
|
if kv.Type().AssignableTo(av.Type().Key()) {
|
|
|
|
avv = av.MapIndex(kv)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-07 15:03:09 -04:00
|
|
|
avv, isNil = indirect(avv)
|
|
|
|
|
|
|
|
if isNil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2015-04-05 15:03:16 -04:00
|
|
|
if avv.IsValid() {
|
|
|
|
switch avv.Kind() {
|
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
|
|
return avv.Int()
|
|
|
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
|
|
|
return avv.Uint()
|
|
|
|
case reflect.Float32, reflect.Float64:
|
|
|
|
return avv.Float()
|
|
|
|
case reflect.String:
|
|
|
|
return avv.String()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2016-02-06 09:53:39 -05:00
|
|
|
// highlight returns an HTML string with syntax highlighting applied.
|
2016-02-07 09:15:27 -05:00
|
|
|
func highlight(in interface{}, lang, opts string) (template.HTML, error) {
|
|
|
|
str, err := cast.ToStringE(in)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2015-04-05 15:03:16 -04:00
|
|
|
}
|
|
|
|
|
2016-02-07 09:15:27 -05:00
|
|
|
return template.HTML(helpers.Highlight(html.UnescapeString(str), lang, opts)), nil
|
2015-04-05 15:03:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var markdownTrimPrefix = []byte("<p>")
|
|
|
|
var markdownTrimSuffix = []byte("</p>\n")
|
|
|
|
|
2016-02-06 09:53:39 -05:00
|
|
|
// markdownify renders a given string from Markdown to HTML.
|
2016-08-12 18:43:15 -04:00
|
|
|
func markdownify(in interface{}) (template.HTML, error) {
|
|
|
|
text, err := cast.ToStringE(in)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2016-08-07 16:01:55 -04:00
|
|
|
|
|
|
|
language := viper.Get("CurrentContentLanguage").(*helpers.Language)
|
|
|
|
|
2016-08-07 08:03:03 -04:00
|
|
|
m := helpers.RenderBytes(&helpers.RenderingContext{
|
2016-08-07 16:01:55 -04:00
|
|
|
ConfigProvider: language,
|
2016-08-07 08:03:03 -04:00
|
|
|
Content: []byte(text), PageFmt: "markdown"})
|
2015-04-05 15:03:16 -04:00
|
|
|
m = bytes.TrimPrefix(m, markdownTrimPrefix)
|
|
|
|
m = bytes.TrimSuffix(m, markdownTrimSuffix)
|
2016-08-12 18:43:15 -04:00
|
|
|
return template.HTML(m), nil
|
2015-04-05 15:03:16 -04:00
|
|
|
}
|
|
|
|
|
2016-03-06 08:58:15 -05:00
|
|
|
// jsonify encodes a given object to JSON.
|
|
|
|
func jsonify(v interface{}) (template.HTML, error) {
|
|
|
|
b, err := json.Marshal(v)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return template.HTML(b), nil
|
2016-02-24 18:52:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// emojify "emojifies" the given string.
|
|
|
|
//
|
|
|
|
// See http://www.emoji-cheat-sheet.com/
|
|
|
|
func emojify(in interface{}) (template.HTML, error) {
|
|
|
|
str, err := cast.ToStringE(in)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2016-03-03 09:53:15 -05:00
|
|
|
|
2016-02-24 18:52:11 -05:00
|
|
|
return template.HTML(helpers.Emojify([]byte(str))), nil
|
2016-03-06 08:58:15 -05:00
|
|
|
}
|
|
|
|
|
2016-03-03 09:53:15 -05:00
|
|
|
// plainify strips any HTML and returns the plain text version.
|
|
|
|
func plainify(in interface{}) (string, error) {
|
|
|
|
s, err := cast.ToStringE(in)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return helpers.StripHTML(s), nil
|
|
|
|
}
|
|
|
|
|
2015-04-05 15:03:16 -04:00
|
|
|
func refPage(page interface{}, ref, methodName string) template.HTML {
|
|
|
|
value := reflect.ValueOf(page)
|
|
|
|
|
|
|
|
method := value.MethodByName(methodName)
|
|
|
|
|
|
|
|
if method.IsValid() && method.Type().NumIn() == 1 && method.Type().NumOut() == 2 {
|
|
|
|
result := method.Call([]reflect.Value{reflect.ValueOf(ref)})
|
|
|
|
|
|
|
|
url, err := result[0], result[1]
|
|
|
|
|
|
|
|
if !err.IsNil() {
|
|
|
|
jww.ERROR.Printf("%s", err.Interface())
|
|
|
|
return template.HTML(fmt.Sprintf("%s", err.Interface()))
|
|
|
|
}
|
|
|
|
|
|
|
|
if url.String() == "" {
|
|
|
|
jww.ERROR.Printf("ref %s could not be found\n", ref)
|
|
|
|
return template.HTML(ref)
|
|
|
|
}
|
|
|
|
|
|
|
|
return template.HTML(url.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
jww.ERROR.Printf("Can only create references from Page and Node objects.")
|
|
|
|
return template.HTML(ref)
|
|
|
|
}
|
|
|
|
|
2016-02-06 09:53:39 -05:00
|
|
|
// ref returns the absolute URL path to a given content item.
|
|
|
|
func ref(page interface{}, ref string) template.HTML {
|
2015-04-05 15:03:16 -04:00
|
|
|
return refPage(page, ref, "Ref")
|
|
|
|
}
|
|
|
|
|
2016-02-06 09:53:39 -05:00
|
|
|
// relRef returns the relative URL path to a given content item.
|
|
|
|
func relRef(page interface{}, ref string) template.HTML {
|
2015-04-05 15:03:16 -04:00
|
|
|
return refPage(page, ref, "RelRef")
|
|
|
|
}
|
|
|
|
|
2016-02-06 09:53:39 -05:00
|
|
|
// chomp removes trailing newline characters from a string.
|
2016-03-03 13:45:23 -05:00
|
|
|
func chomp(text interface{}) (template.HTML, error) {
|
2015-04-05 15:03:16 -04:00
|
|
|
s, err := cast.ToStringE(text)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2016-03-03 13:45:23 -05:00
|
|
|
return template.HTML(strings.TrimRight(s, "\r\n")), nil
|
2015-04-05 15:03:16 -04:00
|
|
|
}
|
|
|
|
|
2016-02-06 09:53:39 -05:00
|
|
|
// trim leading/trailing characters defined by b from a
|
|
|
|
func trim(a interface{}, b string) (string, error) {
|
2015-04-05 15:03:16 -04:00
|
|
|
aStr, err := cast.ToStringE(a)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return strings.Trim(aStr, b), nil
|
|
|
|
}
|
|
|
|
|
2016-02-13 04:24:25 -05:00
|
|
|
// replace all occurrences of b with c in a
|
2016-02-06 09:53:39 -05:00
|
|
|
func replace(a, b, c interface{}) (string, error) {
|
2015-04-05 15:03:16 -04:00
|
|
|
aStr, err := cast.ToStringE(a)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
bStr, err := cast.ToStringE(b)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
cStr, err := cast.ToStringE(c)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return strings.Replace(aStr, bStr, cStr, -1), nil
|
|
|
|
}
|
|
|
|
|
2016-03-14 00:17:42 -04:00
|
|
|
// regexpCache represents a cache of regexp objects protected by a mutex.
|
|
|
|
type regexpCache struct {
|
|
|
|
mu sync.RWMutex
|
|
|
|
re map[string]*regexp.Regexp
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get retrieves a regexp object from the cache based upon the pattern.
|
|
|
|
// If the pattern is not found in the cache, create one
|
|
|
|
func (rc *regexpCache) Get(pattern string) (re *regexp.Regexp, err error) {
|
|
|
|
var ok bool
|
|
|
|
|
|
|
|
if re, ok = rc.get(pattern); !ok {
|
|
|
|
re, err = regexp.Compile(pattern)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rc.set(pattern, re)
|
|
|
|
}
|
|
|
|
|
|
|
|
return re, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rc *regexpCache) get(key string) (re *regexp.Regexp, ok bool) {
|
|
|
|
rc.mu.RLock()
|
|
|
|
re, ok = rc.re[key]
|
|
|
|
rc.mu.RUnlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rc *regexpCache) set(key string, re *regexp.Regexp) {
|
|
|
|
rc.mu.Lock()
|
|
|
|
rc.re[key] = re
|
|
|
|
rc.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
var reCache = regexpCache{re: make(map[string]*regexp.Regexp)}
|
2016-02-08 17:57:52 -05:00
|
|
|
|
|
|
|
// replaceRE exposes a regular expression replacement function to the templates.
|
|
|
|
func replaceRE(pattern, repl, src interface{}) (_ string, err error) {
|
|
|
|
patternStr, err := cast.ToStringE(pattern)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
replStr, err := cast.ToStringE(repl)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
srcStr, err := cast.ToStringE(src)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-03-14 00:17:42 -04:00
|
|
|
re, err := reCache.Get(patternStr)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2016-02-08 17:57:52 -05:00
|
|
|
}
|
2016-03-14 00:17:42 -04:00
|
|
|
return re.ReplaceAllString(srcStr, replStr), nil
|
2016-02-08 17:57:52 -05:00
|
|
|
}
|
|
|
|
|
2016-08-03 19:55:24 -04:00
|
|
|
// asTime converts the textual representation of the datetime string into
|
|
|
|
// a time.Time interface.
|
|
|
|
func asTime(v interface{}) (interface{}, error) {
|
|
|
|
t, err := cast.ToTimeE(v)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return t, nil
|
|
|
|
}
|
|
|
|
|
2016-02-06 09:53:39 -05:00
|
|
|
// dateFormat converts the textual representation of the datetime string into
|
2015-04-05 15:03:16 -04:00
|
|
|
// the other form or returns it of the time.Time value. These are formatted
|
|
|
|
// with the layout string
|
2016-02-06 09:53:39 -05:00
|
|
|
func dateFormat(layout string, v interface{}) (string, error) {
|
2015-04-05 15:03:16 -04:00
|
|
|
t, err := cast.ToTimeE(v)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return t.Format(layout), nil
|
|
|
|
}
|
|
|
|
|
2016-03-09 15:40:00 -05:00
|
|
|
// dfault checks whether a given value is set and returns a default value if it
|
2016-03-16 13:45:50 -04:00
|
|
|
// is not. "Set" in this context means non-zero for numeric types and times;
|
|
|
|
// non-zero length for strings, arrays, slices, and maps;
|
|
|
|
// any boolean or struct value; or non-nil for any other types.
|
2016-03-09 17:27:56 -05:00
|
|
|
func dfault(dflt interface{}, given ...interface{}) (interface{}, error) {
|
|
|
|
// given is variadic because the following construct will not pass a piped
|
|
|
|
// argument when the key is missing: {{ index . "key" | default "foo" }}
|
|
|
|
// The Go template will complain that we got 1 argument when we expectd 2.
|
|
|
|
|
|
|
|
if given == nil || len(given) == 0 {
|
|
|
|
return dflt, nil
|
|
|
|
}
|
|
|
|
if len(given) != 1 {
|
|
|
|
return nil, fmt.Errorf("wrong number of args for default: want 2 got %d", len(given)+1)
|
|
|
|
}
|
|
|
|
|
|
|
|
g := reflect.ValueOf(given[0])
|
2016-03-09 15:40:00 -05:00
|
|
|
if !g.IsValid() {
|
2016-03-09 17:27:56 -05:00
|
|
|
return dflt, nil
|
2016-03-09 15:40:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
set := false
|
|
|
|
|
|
|
|
switch g.Kind() {
|
|
|
|
case reflect.Bool:
|
2016-03-16 13:45:50 -04:00
|
|
|
set = true
|
2016-03-09 15:40:00 -05:00
|
|
|
case reflect.String, reflect.Array, reflect.Slice, reflect.Map:
|
|
|
|
set = g.Len() != 0
|
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
|
|
set = g.Int() != 0
|
|
|
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
|
|
|
set = g.Uint() != 0
|
|
|
|
case reflect.Float32, reflect.Float64:
|
|
|
|
set = g.Float() != 0
|
|
|
|
case reflect.Complex64, reflect.Complex128:
|
|
|
|
set = g.Complex() != 0
|
|
|
|
case reflect.Struct:
|
2016-03-09 17:27:56 -05:00
|
|
|
switch actual := given[0].(type) {
|
|
|
|
case time.Time:
|
|
|
|
set = !actual.IsZero()
|
|
|
|
default:
|
|
|
|
set = true
|
|
|
|
}
|
2016-03-09 15:40:00 -05:00
|
|
|
default:
|
|
|
|
set = !g.IsNil()
|
|
|
|
}
|
|
|
|
|
|
|
|
if set {
|
2016-03-09 17:27:56 -05:00
|
|
|
return given[0], nil
|
2016-03-09 15:40:00 -05:00
|
|
|
}
|
|
|
|
|
2016-03-09 17:27:56 -05:00
|
|
|
return dflt, nil
|
2016-03-09 15:40:00 -05:00
|
|
|
}
|
|
|
|
|
2016-03-10 09:21:20 -05:00
|
|
|
// canBeNil reports whether an untyped nil can be assigned to the type. See reflect.Zero.
|
|
|
|
//
|
|
|
|
// Copied from Go stdlib src/text/template/exec.go.
|
|
|
|
func canBeNil(typ reflect.Type) bool {
|
|
|
|
switch typ.Kind() {
|
|
|
|
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// prepareArg checks if value can be used as an argument of type argType, and
|
|
|
|
// converts an invalid value to appropriate zero if possible.
|
|
|
|
//
|
|
|
|
// Copied from Go stdlib src/text/template/funcs.go.
|
|
|
|
func prepareArg(value reflect.Value, argType reflect.Type) (reflect.Value, error) {
|
|
|
|
if !value.IsValid() {
|
|
|
|
if !canBeNil(argType) {
|
|
|
|
return reflect.Value{}, fmt.Errorf("value is nil; should be of type %s", argType)
|
|
|
|
}
|
|
|
|
value = reflect.Zero(argType)
|
|
|
|
}
|
|
|
|
if !value.Type().AssignableTo(argType) {
|
|
|
|
return reflect.Value{}, fmt.Errorf("value has type %s; should be %s", value.Type(), argType)
|
|
|
|
}
|
|
|
|
return value, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// index returns the result of indexing its first argument by the following
|
|
|
|
// arguments. Thus "index x 1 2 3" is, in Go syntax, x[1][2][3]. Each
|
|
|
|
// indexed item must be a map, slice, or array.
|
|
|
|
//
|
|
|
|
// Copied from Go stdlib src/text/template/funcs.go.
|
2016-03-10 14:59:41 -05:00
|
|
|
// Can hopefully be removed in Go 1.7, see https://github.com/golang/go/issues/14751
|
2016-03-10 09:21:20 -05:00
|
|
|
func index(item interface{}, indices ...interface{}) (interface{}, error) {
|
|
|
|
v := reflect.ValueOf(item)
|
|
|
|
if !v.IsValid() {
|
|
|
|
return nil, fmt.Errorf("index of untyped nil")
|
|
|
|
}
|
|
|
|
for _, i := range indices {
|
|
|
|
index := reflect.ValueOf(i)
|
|
|
|
var isNil bool
|
|
|
|
if v, isNil = indirect(v); isNil {
|
|
|
|
return nil, fmt.Errorf("index of nil pointer")
|
|
|
|
}
|
|
|
|
switch v.Kind() {
|
|
|
|
case reflect.Array, reflect.Slice, reflect.String:
|
|
|
|
var x int64
|
|
|
|
switch index.Kind() {
|
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
|
|
x = index.Int()
|
|
|
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
|
|
|
x = int64(index.Uint())
|
|
|
|
case reflect.Invalid:
|
|
|
|
return nil, fmt.Errorf("cannot index slice/array with nil")
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("cannot index slice/array with type %s", index.Type())
|
|
|
|
}
|
|
|
|
if x < 0 || x >= int64(v.Len()) {
|
|
|
|
// We deviate from stdlib here. Don't return an error if the
|
|
|
|
// index is out of range.
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
v = v.Index(int(x))
|
|
|
|
case reflect.Map:
|
|
|
|
index, err := prepareArg(index, v.Type().Key())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if x := v.MapIndex(index); x.IsValid() {
|
|
|
|
v = x
|
|
|
|
} else {
|
|
|
|
v = reflect.Zero(v.Type().Elem())
|
|
|
|
}
|
|
|
|
case reflect.Invalid:
|
|
|
|
// the loop holds invariant: v.IsValid()
|
|
|
|
panic("unreachable")
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("can't index item of type %s", v.Type())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return v.Interface(), nil
|
|
|
|
}
|
|
|
|
|
2016-03-21 19:28:42 -04:00
|
|
|
// readFile reads the file named by filename relative to the given basepath
|
|
|
|
// and returns the contents as a string.
|
|
|
|
// There is a upper size limit set at 1 megabytes.
|
|
|
|
func readFile(fs *afero.BasePathFs, filename string) (string, error) {
|
|
|
|
if filename == "" {
|
|
|
|
return "", errors.New("readFile needs a filename")
|
|
|
|
}
|
|
|
|
|
|
|
|
if info, err := fs.Stat(filename); err == nil {
|
|
|
|
if info.Size() > 1000000 {
|
|
|
|
return "", fmt.Errorf("File %q is too big", filename)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
b, err := afero.ReadFile(fs, filename)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(b), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// readFileFromWorkingDir reads the file named by filename relative to the
|
|
|
|
// configured WorkingDir.
|
|
|
|
// It returns the contents as a string.
|
|
|
|
// There is a upper size limit set at 1 megabytes.
|
|
|
|
func readFileFromWorkingDir(i interface{}) (string, error) {
|
2016-08-12 18:43:15 -04:00
|
|
|
s, err := cast.ToStringE(i)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return readFile(hugofs.WorkingDir(), s)
|
2016-03-21 19:28:42 -04:00
|
|
|
}
|
|
|
|
|
2016-03-31 17:06:51 -04:00
|
|
|
// readDirFromWorkingDir listst the directory content relative to the
|
|
|
|
// configured WorkingDir.
|
|
|
|
func readDirFromWorkingDir(i interface{}) ([]os.FileInfo, error) {
|
2016-08-12 18:43:15 -04:00
|
|
|
path, err := cast.ToStringE(i)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-03-31 17:06:51 -04:00
|
|
|
|
|
|
|
list, err := afero.ReadDir(hugofs.WorkingDir(), path)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to read Directory %s with error message %s", path, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return list, nil
|
|
|
|
}
|
|
|
|
|
2016-02-06 09:53:39 -05:00
|
|
|
// safeHTMLAttr returns a given string as html/template HTMLAttr content.
|
2016-08-12 18:43:15 -04:00
|
|
|
func safeHTMLAttr(a interface{}) (template.HTMLAttr, error) {
|
|
|
|
s, err := cast.ToStringE(a)
|
|
|
|
return template.HTMLAttr(s), err
|
2015-04-05 15:03:16 -04:00
|
|
|
}
|
|
|
|
|
2016-02-06 09:53:39 -05:00
|
|
|
// safeCSS returns a given string as html/template CSS content.
|
2016-08-12 18:43:15 -04:00
|
|
|
func safeCSS(a interface{}) (template.CSS, error) {
|
|
|
|
s, err := cast.ToStringE(a)
|
|
|
|
return template.CSS(s), err
|
2015-04-05 15:03:16 -04:00
|
|
|
}
|
|
|
|
|
2016-02-06 09:53:39 -05:00
|
|
|
// safeURL returns a given string as html/template URL content.
|
2016-08-12 18:43:15 -04:00
|
|
|
func safeURL(a interface{}) (template.URL, error) {
|
|
|
|
s, err := cast.ToStringE(a)
|
|
|
|
return template.URL(s), err
|
2015-04-05 15:03:16 -04:00
|
|
|
}
|
|
|
|
|
2016-02-06 09:53:39 -05:00
|
|
|
// safeHTML returns a given string as html/template HTML content.
|
2016-08-12 18:43:15 -04:00
|
|
|
func safeHTML(a interface{}) (template.HTML, error) {
|
|
|
|
s, err := cast.ToStringE(a)
|
|
|
|
return template.HTML(s), err
|
|
|
|
}
|
2015-11-15 11:10:35 -05:00
|
|
|
|
2016-02-06 09:53:39 -05:00
|
|
|
// safeJS returns the given string as a html/template JS content.
|
2016-08-12 18:43:15 -04:00
|
|
|
func safeJS(a interface{}) (template.JS, error) {
|
|
|
|
s, err := cast.ToStringE(a)
|
|
|
|
return template.JS(s), err
|
|
|
|
}
|
2015-11-15 15:30:57 -05:00
|
|
|
|
2016-02-06 09:53:39 -05:00
|
|
|
// mod returns a % b.
|
|
|
|
func mod(a, b interface{}) (int64, error) {
|
2015-04-05 15:03:16 -04:00
|
|
|
av := reflect.ValueOf(a)
|
|
|
|
bv := reflect.ValueOf(b)
|
|
|
|
var ai, bi int64
|
|
|
|
|
|
|
|
switch av.Kind() {
|
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
|
|
ai = av.Int()
|
|
|
|
default:
|
|
|
|
return 0, errors.New("Modulo operator can't be used with non integer value")
|
|
|
|
}
|
|
|
|
|
|
|
|
switch bv.Kind() {
|
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
|
|
bi = bv.Int()
|
|
|
|
default:
|
|
|
|
return 0, errors.New("Modulo operator can't be used with non integer value")
|
|
|
|
}
|
|
|
|
|
|
|
|
if bi == 0 {
|
|
|
|
return 0, errors.New("The number can't be divided by zero at modulo operation")
|
|
|
|
}
|
|
|
|
|
|
|
|
return ai % bi, nil
|
|
|
|
}
|
|
|
|
|
2016-02-06 09:53:39 -05:00
|
|
|
// modBool returns the boolean of a % b. If a % b == 0, return true.
|
|
|
|
func modBool(a, b interface{}) (bool, error) {
|
|
|
|
res, err := mod(a, b)
|
2015-04-05 15:03:16 -04:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return res == int64(0), nil
|
|
|
|
}
|
|
|
|
|
2016-02-06 09:53:39 -05:00
|
|
|
// base64Decode returns the base64 decoding of the given content.
|
|
|
|
func base64Decode(content interface{}) (string, error) {
|
2015-09-12 15:45:12 -04:00
|
|
|
conv, err := cast.ToStringE(content)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
dec, err := base64.StdEncoding.DecodeString(conv)
|
|
|
|
|
2016-02-07 08:58:23 -05:00
|
|
|
return string(dec), err
|
2015-09-12 15:45:12 -04:00
|
|
|
}
|
|
|
|
|
2016-02-06 09:53:39 -05:00
|
|
|
// base64Encode returns the base64 encoding of the given content.
|
|
|
|
func base64Encode(content interface{}) (string, error) {
|
2015-09-12 15:45:12 -04:00
|
|
|
conv, err := cast.ToStringE(content)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return base64.StdEncoding.EncodeToString([]byte(conv)), nil
|
|
|
|
}
|
|
|
|
|
2016-02-06 09:53:39 -05:00
|
|
|
// countWords returns the approximate word count of the given content.
|
|
|
|
func countWords(content interface{}) (int, error) {
|
2015-09-20 12:15:45 -04:00
|
|
|
conv, err := cast.ToStringE(content)
|
|
|
|
|
|
|
|
if err != nil {
|
2016-02-07 08:51:06 -05:00
|
|
|
return 0, fmt.Errorf("Failed to convert content to string: %s", err.Error())
|
2015-09-20 12:15:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
counter := 0
|
|
|
|
for _, word := range strings.Fields(helpers.StripHTML(conv)) {
|
|
|
|
runeCount := utf8.RuneCountInString(word)
|
|
|
|
if len(word) == runeCount {
|
|
|
|
counter++
|
|
|
|
} else {
|
|
|
|
counter += runeCount
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return counter, nil
|
|
|
|
}
|
|
|
|
|
2016-02-06 09:53:39 -05:00
|
|
|
// countRunes returns the approximate rune count of the given content.
|
|
|
|
func countRunes(content interface{}) (int, error) {
|
2015-09-20 12:15:45 -04:00
|
|
|
conv, err := cast.ToStringE(content)
|
|
|
|
|
|
|
|
if err != nil {
|
2016-02-07 08:51:06 -05:00
|
|
|
return 0, fmt.Errorf("Failed to convert content to string: %s", err.Error())
|
2015-09-20 12:15:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
counter := 0
|
|
|
|
for _, r := range helpers.StripHTML(conv) {
|
|
|
|
if !helpers.IsWhitespace(r) {
|
|
|
|
counter++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return counter, nil
|
|
|
|
}
|
|
|
|
|
2016-07-13 16:09:59 -04:00
|
|
|
// humanize returns the humanized form of a single parameter.
|
|
|
|
// If the parameter is either an integer or a string containing an integer
|
|
|
|
// value, the behavior is to add the appropriate ordinal.
|
2016-02-05 01:05:04 -05:00
|
|
|
// Example: "my-first-post" -> "My first post"
|
2016-07-13 16:09:59 -04:00
|
|
|
// Example: "103" -> "103rd"
|
|
|
|
// Example: 52 -> "52nd"
|
2016-02-06 09:53:39 -05:00
|
|
|
func humanize(in interface{}) (string, error) {
|
2016-02-05 01:05:04 -05:00
|
|
|
word, err := cast.ToStringE(in)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2016-07-10 09:10:22 -04:00
|
|
|
|
|
|
|
if word == "" {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2016-07-13 16:09:59 -04:00
|
|
|
_, ok := in.(int) // original param was literal int value
|
|
|
|
_, err = strconv.Atoi(word) // original param was string containing an int value
|
|
|
|
if ok == true || err == nil {
|
|
|
|
return inflect.Ordinalize(word), nil
|
|
|
|
}
|
2016-02-05 01:05:04 -05:00
|
|
|
return inflect.Humanize(word), nil
|
|
|
|
}
|
|
|
|
|
2016-02-06 09:53:39 -05:00
|
|
|
// pluralize returns the plural form of a single word.
|
|
|
|
func pluralize(in interface{}) (string, error) {
|
2016-02-05 01:05:04 -05:00
|
|
|
word, err := cast.ToStringE(in)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return inflect.Pluralize(word), nil
|
|
|
|
}
|
|
|
|
|
2016-02-06 09:53:39 -05:00
|
|
|
// singularize returns the singular form of a single word.
|
|
|
|
func singularize(in interface{}) (string, error) {
|
2016-02-05 01:05:04 -05:00
|
|
|
word, err := cast.ToStringE(in)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return inflect.Singularize(word), nil
|
|
|
|
}
|
|
|
|
|
2016-03-07 15:40:47 -05:00
|
|
|
// md5 hashes the given input and returns its MD5 checksum
|
|
|
|
func md5(in interface{}) (string, error) {
|
|
|
|
conv, err := cast.ToStringE(in)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
hash := _md5.Sum([]byte(conv))
|
|
|
|
return hex.EncodeToString(hash[:]), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// sha1 hashes the given input and returns its SHA1 checksum
|
|
|
|
func sha1(in interface{}) (string, error) {
|
|
|
|
conv, err := cast.ToStringE(in)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
hash := _sha1.Sum([]byte(conv))
|
|
|
|
return hex.EncodeToString(hash[:]), nil
|
|
|
|
}
|
|
|
|
|
2016-07-06 14:57:37 -04:00
|
|
|
// querify encodes the given parameters “URL encoded” form ("bar=baz&foo=quux") sorted by key.
|
|
|
|
func querify(params ...interface{}) (string, error) {
|
2016-07-04 21:35:24 -04:00
|
|
|
qs := url.Values{}
|
|
|
|
vals, err := dictionary(params...)
|
|
|
|
if err != nil {
|
2016-07-06 14:57:37 -04:00
|
|
|
return "", fmt.Errorf("querify keys must be strings")
|
2016-07-04 21:35:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for name, value := range vals {
|
2016-07-14 09:53:33 -04:00
|
|
|
qs.Add(name, fmt.Sprintf("%v", value))
|
2016-07-04 21:35:24 -04:00
|
|
|
}
|
|
|
|
|
2016-07-06 14:57:37 -04:00
|
|
|
return qs.Encode(), nil
|
2016-07-04 21:35:24 -04:00
|
|
|
}
|
|
|
|
|
2016-07-18 18:14:05 -04:00
|
|
|
func htmlEscape(in interface{}) (string, error) {
|
|
|
|
conv, err := cast.ToStringE(in)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return html.EscapeString(conv), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func htmlUnescape(in interface{}) (string, error) {
|
|
|
|
conv, err := cast.ToStringE(in)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return html.UnescapeString(conv), nil
|
|
|
|
}
|
|
|
|
|
2016-08-12 18:43:15 -04:00
|
|
|
func absURL(a interface{}) (template.HTML, error) {
|
|
|
|
s, err := cast.ToStringE(a)
|
|
|
|
if err != nil {
|
|
|
|
return "", nil
|
|
|
|
}
|
2016-08-07 16:01:55 -04:00
|
|
|
return template.HTML(helpers.AbsURL(s, false)), nil
|
2016-08-12 18:43:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func relURL(a interface{}) (template.HTML, error) {
|
|
|
|
s, err := cast.ToStringE(a)
|
|
|
|
if err != nil {
|
|
|
|
return "", nil
|
|
|
|
}
|
2016-08-07 16:01:55 -04:00
|
|
|
return template.HTML(helpers.RelURL(s, false)), nil
|
2016-08-12 18:43:15 -04:00
|
|
|
}
|
|
|
|
|
2015-04-05 15:03:16 -04:00
|
|
|
func init() {
|
|
|
|
funcMap = template.FuncMap{
|
2016-09-13 11:07:52 -04:00
|
|
|
"absURL": absURL,
|
|
|
|
"absLangURL": func(i interface{}) (template.HTML, error) {
|
|
|
|
s, err := cast.ToStringE(i)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return template.HTML(helpers.AbsURL(s, true)), nil
|
|
|
|
},
|
2016-03-18 18:18:40 -04:00
|
|
|
"add": func(a, b interface{}) (interface{}, error) { return helpers.DoArithmetic(a, b, '+') },
|
2016-02-06 09:53:39 -05:00
|
|
|
"after": after,
|
|
|
|
"apply": apply,
|
|
|
|
"base64Decode": base64Decode,
|
|
|
|
"base64Encode": base64Encode,
|
|
|
|
"chomp": chomp,
|
|
|
|
"countrunes": countRunes,
|
|
|
|
"countwords": countWords,
|
2016-03-09 15:40:00 -05:00
|
|
|
"default": dfault,
|
2016-02-06 09:53:39 -05:00
|
|
|
"dateFormat": dateFormat,
|
|
|
|
"delimit": delimit,
|
|
|
|
"dict": dictionary,
|
2016-03-18 18:18:40 -04:00
|
|
|
"div": func(a, b interface{}) (interface{}, error) { return helpers.DoArithmetic(a, b, '/') },
|
2016-02-06 09:53:39 -05:00
|
|
|
"echoParam": returnWhenSet,
|
2016-02-24 18:52:11 -05:00
|
|
|
"emojify": emojify,
|
2016-02-06 09:53:39 -05:00
|
|
|
"eq": eq,
|
2016-04-05 12:02:18 -04:00
|
|
|
"findRE": findRE,
|
2016-02-06 09:53:39 -05:00
|
|
|
"first": first,
|
|
|
|
"ge": ge,
|
2016-03-03 19:00:41 -05:00
|
|
|
"getCSV": getCSV,
|
|
|
|
"getJSON": getJSON,
|
2016-02-05 01:05:04 -05:00
|
|
|
"getenv": func(varName string) string { return os.Getenv(varName) },
|
2016-02-06 09:53:39 -05:00
|
|
|
"gt": gt,
|
2016-02-05 01:05:04 -05:00
|
|
|
"hasPrefix": func(a, b string) bool { return strings.HasPrefix(a, b) },
|
2016-02-06 09:53:39 -05:00
|
|
|
"highlight": highlight,
|
2016-07-18 18:14:05 -04:00
|
|
|
"htmlEscape": htmlEscape,
|
|
|
|
"htmlUnescape": htmlUnescape,
|
2016-02-06 09:53:39 -05:00
|
|
|
"humanize": humanize,
|
|
|
|
"in": in,
|
2016-03-10 09:21:20 -05:00
|
|
|
"index": index,
|
2016-08-12 18:43:15 -04:00
|
|
|
"int": func(v interface{}) (int, error) { return cast.ToIntE(v) },
|
2016-02-06 09:53:39 -05:00
|
|
|
"intersect": intersect,
|
|
|
|
"isSet": isSet,
|
|
|
|
"isset": isSet,
|
2016-03-06 08:58:15 -05:00
|
|
|
"jsonify": jsonify,
|
2016-02-06 09:53:39 -05:00
|
|
|
"last": last,
|
|
|
|
"le": le,
|
2016-02-05 01:05:04 -05:00
|
|
|
"lower": func(a string) string { return strings.ToLower(a) },
|
2016-02-06 09:53:39 -05:00
|
|
|
"lt": lt,
|
|
|
|
"markdownify": markdownify,
|
2016-03-07 15:40:47 -05:00
|
|
|
"md5": md5,
|
2016-02-06 09:53:39 -05:00
|
|
|
"mod": mod,
|
|
|
|
"modBool": modBool,
|
2016-03-18 18:18:40 -04:00
|
|
|
"mul": func(a, b interface{}) (interface{}, error) { return helpers.DoArithmetic(a, b, '*') },
|
2016-02-06 09:53:39 -05:00
|
|
|
"ne": ne,
|
2016-03-03 19:00:41 -05:00
|
|
|
"partial": partial,
|
2016-03-03 09:53:15 -05:00
|
|
|
"plainify": plainify,
|
2016-02-06 09:53:39 -05:00
|
|
|
"pluralize": pluralize,
|
2016-07-04 21:35:24 -04:00
|
|
|
"querify": querify,
|
2016-03-31 17:06:51 -04:00
|
|
|
"readDir": readDirFromWorkingDir,
|
2016-03-21 19:28:42 -04:00
|
|
|
"readFile": readFileFromWorkingDir,
|
2016-02-06 09:53:39 -05:00
|
|
|
"ref": ref,
|
2016-08-12 18:43:15 -04:00
|
|
|
"relURL": relURL,
|
2016-09-13 11:07:52 -04:00
|
|
|
"relLangURL": func(i interface{}) (template.HTML, error) {
|
|
|
|
s, err := cast.ToStringE(i)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return template.HTML(helpers.RelURL(s, true)), nil
|
|
|
|
},
|
2016-02-06 09:53:39 -05:00
|
|
|
"relref": relRef,
|
|
|
|
"replace": replace,
|
2016-02-08 17:57:52 -05:00
|
|
|
"replaceRE": replaceRE,
|
2016-02-06 09:53:39 -05:00
|
|
|
"safeCSS": safeCSS,
|
|
|
|
"safeHTML": safeHTML,
|
2016-06-22 07:21:04 -04:00
|
|
|
"safeHTMLAttr": safeHTMLAttr,
|
2016-02-06 09:53:39 -05:00
|
|
|
"safeJS": safeJS,
|
|
|
|
"safeURL": safeURL,
|
2016-02-05 01:05:04 -05:00
|
|
|
"sanitizeURL": helpers.SanitizeURL,
|
|
|
|
"sanitizeurl": helpers.SanitizeURL,
|
2015-09-12 15:45:12 -04:00
|
|
|
"seq": helpers.Seq,
|
2016-03-07 15:40:47 -05:00
|
|
|
"sha1": sha1,
|
2016-02-06 09:53:39 -05:00
|
|
|
"shuffle": shuffle,
|
|
|
|
"singularize": singularize,
|
2016-02-27 10:51:07 -05:00
|
|
|
"slice": slice,
|
2016-02-06 09:53:39 -05:00
|
|
|
"slicestr": slicestr,
|
|
|
|
"sort": sortSeq,
|
|
|
|
"split": split,
|
2016-08-12 18:43:15 -04:00
|
|
|
"string": func(v interface{}) (string, error) { return cast.ToStringE(v) },
|
2016-03-18 18:18:40 -04:00
|
|
|
"sub": func(a, b interface{}) (interface{}, error) { return helpers.DoArithmetic(a, b, '-') },
|
2016-02-06 09:53:39 -05:00
|
|
|
"substr": substr,
|
2016-02-05 01:05:04 -05:00
|
|
|
"title": func(a string) string { return strings.Title(a) },
|
2016-08-03 19:55:24 -04:00
|
|
|
"time": asTime,
|
2016-02-06 09:53:39 -05:00
|
|
|
"trim": trim,
|
2016-02-05 01:05:04 -05:00
|
|
|
"upper": func(a string) string { return strings.ToUpper(a) },
|
|
|
|
"urlize": helpers.URLize,
|
2016-02-06 09:53:39 -05:00
|
|
|
"where": where,
|
2016-05-14 00:35:16 -04:00
|
|
|
"i18n": I18nTranslate,
|
|
|
|
"T": I18nTranslate,
|
2015-04-05 15:03:16 -04:00
|
|
|
}
|
|
|
|
}
|