funcmap

package module
v0.0.0-...-7e19e9c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 27, 2024 License: BlueOak-1.0.0 Imports: 22 Imported by: 21

README

This package aims to provide an independent implementation of the Hugo template functions. https://gohugo.io/functions/ (view source)

Usage

Import the package:

import "github.com/drone/funcmap"

Provide the Funcmap when compiling your templates:

t, err := template.New("_").Funcs(funcmap.Funcs).Parse(text)

Functions

Crypto Functions

Encoding Functions

Date Functions

OS Functions

String Functions

Transform Functions

URL Functions

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Funcs = map[string]interface{}{
	"append":       Append,
	"base64Decode": DecodeBase64,
	"base64Encode": EncodeBase64,
	"chomp":        Chomp,
	"contains":     Contains,
	"containsAny":  ContainsAny,
	"dateFormat":   TimeFormat,
	"env":          Getenv,
	"getenv":       Getenv,
	"findRE":       FindRE,
	"fileExists":   FileExists,
	"firstUpper":   FirstUpper,
	"htmlEscape":   HTMLEscape,
	"htmlUnescape": HTMLUnescape,
	"hasPrefix":    HasPrefix,
	"hasSuffix":    HasSuffix,
	"jsonify":      EncodeJSON,
	"jsonEncode":   EncodeJSON,
	"jsonDecode":   DecodeJSON,
	"lower":        ToLower,
	"md5":          MD5,
	"now":          Now,
	"padLeft":      PadLeft,
	"padRight":     PadRight,
	"prepend":      Prepend,
	"readDir":      ReadDir,
	"readFile":     ReadFile,
	"replace":      Replace,
	"replaceRE":    ReplaceRE,
	"repeat":       Repeat,
	"sha1":         SHA1,
	"sha256":       SHA256,
	"slicestr":     SliceString,
	"split":        Split,
	"splitn":       SplitN,
	"stat":         Stat,
	"title":        ToTitle,
	"time":         Time,
	"trimLeft":     TrimLeft,
	"trimRight":    TrimRight,
	"trimPrefix":   TrimPrefix,
	"trimSuffix":   TrimSuffix,
	"trim":         Trim,
	"upper":        ToUpper,
	"urlize":       Urlize,
	"yamlEncode":   EncodeYAML,
	"yamlDecode":   DecodeYAML,
}

Funcs is a map of custom template functions.

View Source
var SafeFuncs = map[string]interface{}{
	"append":       Append,
	"base64Decode": DecodeBase64,
	"base64Encode": EncodeBase64,
	"chomp":        Chomp,
	"contains":     Contains,
	"containsAny":  ContainsAny,
	"dateFormat":   TimeFormat,
	"findRE":       FindRE,
	"firstUpper":   FirstUpper,
	"htmlEscape":   HTMLEscape,
	"htmlUnescape": HTMLUnescape,
	"hasPrefix":    HasPrefix,
	"hasSuffix":    HasSuffix,
	"jsonify":      EncodeJSON,
	"jsonEncode":   EncodeJSON,
	"jsonDecode":   DecodeJSON,
	"lower":        ToLower,
	"md5":          MD5,
	"now":          Now,
	"padLeft":      PadLeft,
	"padRight":     PadRight,
	"prepend":      Prepend,
	"repeat":       Repeat,
	"replace":      Replace,
	"replaceRE":    ReplaceRE,
	"sha1":         SHA1,
	"sha256":       SHA256,
	"slicestr":     SliceString,
	"split":        Split,
	"splitn":       SplitN,
	"title":        ToTitle,
	"time":         Time,
	"trimLeft":     TrimLeft,
	"trimRight":    TrimRight,
	"trimPrefix":   TrimPrefix,
	"trimSuffix":   TrimSuffix,
	"trim":         Trim,
	"upper":        ToUpper,
	"urlize":       Urlize,
	"yamlEncode":   EncodeYAML,
	"yamlDecode":   DecodeYAML,
}

SafeFuncs is a map of custom template functions. Functions that expose the environment and filesystem are excluded.

Functions

func Append

func Append(s, append interface{}) (string, error)

Append returns a slice of the string s, with append appended.

func Chomp

func Chomp(s interface{}) (string, error)

Chomp returns a copy of s with all trailing newline characters removed.

func Combine

func Combine(funcmaps ...map[string]interface{}) template.FuncMap

Combine combines function maps.

func Contains

func Contains(s, substr interface{}) (bool, error)

Contains reports whether substr is within s.

func ContainsAny

func ContainsAny(s, chars interface{}) (bool, error)

ContainsAny reports whether any Unicode code points in chars are within s.

func DecodeBase64

func DecodeBase64(s interface{}) (string, error)

DecodeBase64 returns the base64 decoding of s.

func DecodeJSON

func DecodeJSON(s interface{}) (map[string]interface{}, error)

DecodeJSON returns the JSON decoding of s.

func DecodeYAML

func DecodeYAML(s interface{}) (map[string]interface{}, error)

DecodeYAML returns the YAML decoding of s.

func EncodeBase64

func EncodeBase64(s interface{}) (string, error)

EncodeBase64 returns the base64 encoding of s.

func EncodeJSON

func EncodeJSON(v interface{}) (template.HTML, error)

EncodeJSON returns the JSON encoding of v.

func EncodeYAML

func EncodeYAML(v interface{}) (template.HTML, error)

EncodeYAML returns the YAML encoding of v.

func FileExists

func FileExists(name interface{}) (bool, error)

FileExists returns true if the file exists.

func FindRE

func FindRE(expr string, content interface{}, limit ...interface{}) ([]string, 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 limited with an optional third parameter.

func FirstUpper

func FirstUpper(s interface{}) (string, error)

FirstUpper returns a string with the first character as upper case.

func Getenv

func Getenv(key interface{}) (string, error)

Getenv retrieves the value of the environment variable named by the key.

func HTMLEscape

func HTMLEscape(s interface{}) (string, error)

HTMLEscape returns a copy of s with reserved HTML characters escaped.

func HTMLUnescape

func HTMLUnescape(s interface{}) (string, error)

HTMLUnescape returns a copy of s with HTML escape sequences converted to plain text.

func HasPrefix

func HasPrefix(s, prefix interface{}) (bool, error)

HasPrefix tests whether the string s begins with prefix.

func HasSuffix

func HasSuffix(s, suffix interface{}) (bool, error)

HasSuffix tests whether the string s ends with suffix.

func MD5

func MD5(s interface{}) (string, error)

MD5 returns the sha checksum of s.

func Now

func Now() time.Time

Now returns the current local time.

func PadLeft

func PadLeft(s, padding, n interface{}) (string, error)

PadLeft returns a slice of the string s, prefixed with n copies of padding.

func PadRight

func PadRight(s, padding, n interface{}) (string, error)

PadRight returns a slice of the string s, suffixed with n instances of the padding string.

func Prepend

func Prepend(s, prepend interface{}) (string, error)

Prepend returns a slice of the string s, prepended with prepend.

func ReadDir

func ReadDir(dirname interface{}) ([]os.FileInfo, error)

ReadDir reads the directory named by dirname and returns a list of directory entries sorted by filename.

func ReadFile

func ReadFile(filename interface{}) ([]byte, error)

ReadFile reads the directory named by dirname and returns a list of directory entries sorted by filename.

func Repeat

func Repeat(s, count interface{}) (string, error)

Repeat returns a new string consisting of count copies of the string s.

func Replace

func Replace(s, old, new interface{}) (string, error)

Replace returns a copy of the string s with instances of old replaced by new.

func ReplaceRE

func ReplaceRE(pattern, repl, s interface{}) (string, error)

ReplaceRE returns a copy of s, replacing all matches of the regular expression pattern with the replacement text repl.

func SHA1

func SHA1(s interface{}) (string, error)

SHA1 returns the sha checksum of s.

func SHA256

func SHA256(s interface{}) (string, error)

SHA256 returns the sha checksum of s.

func SliceString

func SliceString(a interface{}, startEnd ...interface{}) (string, error)

SliceString slices a string by specifying a half-open range with 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.

func Split

func Split(s interface{}, sep string) ([]string, error)

Split slices s into all substrings separated by sep and returns a slice of the substrings between those separators.

func SplitN

func SplitN(s, sep, n interface{}) ([]string, error)

SplitN slices s into substrings separated by sep and returns a slice of the substrings between those separators.

func Stat

func Stat(i interface{}) (os.FileInfo, error)

Stat returns the os.FileInfo structure describing file.

func Time

func Time(v interface{}, args ...interface{}) (interface{}, error)

Time converts the textual representation of the datetime string into a time.Time interface.

func TimeFormat

func TimeFormat(layout string, v interface{}) (string, error)

TimeFormat converts the textual representation of the datetime string into the other form or returns it of the time.Time value. These are formatted with the layout string

func ToLower

func ToLower(s interface{}) (string, error)

ToLower returns a copy of the input s with all Unicode letters mapped to their lower case.

func ToTitle

func ToTitle(s interface{}) (string, error)

ToTitle returns a copy of the input s with all Unicode letters mapped to their title case.

func ToUpper

func ToUpper(s interface{}) (string, error)

ToUpper returns a copy of the input s with all Unicode letters mapped to their upper case.

func Trim

func Trim(s interface{}) (string, error)

Trim returns a slice of the string s, with all leading and trailing white space removed, as defined by Unicode.

func TrimLeft

func TrimLeft(s, cutset interface{}) (string, error)

TrimLeft returns a slice of the string s with all leading Unicode code points contained in cutset removed.

func TrimPrefix

func TrimPrefix(s, prefix interface{}) (string, error)

TrimPrefix returns a slice of the string s, with all trailing Unicode code points contained in cutset removed.

func TrimRight

func TrimRight(s, cutset interface{}) (string, error)

TrimRight returns a slice of the string s, with all trailing Unicode code points contained in cutset removed.

func TrimSuffix

func TrimSuffix(s, suffix interface{}) (string, error)

TrimSuffix returns s without the provided trailing suffix string. If s doesn't end with suffix, s is returned unchanged.

func Urlize

func Urlize(s interface{}) (string, error)

Urlize returns a copy of the input s that is sanities so it can be safely placed inside a URL path segment, with spaces converted to hypens.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL