-
jojomi/io — A tool that easily generates documents from a template and data with the powerful functions defined here
-
This very documentation is built on the functions provided by
tplfuncs
. After all you should dogfood as a programmer!
There is a total number of 114 functions contained in this package.
Functions related to spacing (spaces, newlines). These functions help controlling the space generated in the target string more precisely than the built-in removal options within the template strings ({{- like this -}}).
space
inserts a number of space characters, default is one. Often this function is used in a block that removes whitespace around it.
Signature: space(count …int) string
first
{{- space -}}
second and
{{- space 3 }} third word
The output would be:
first second and third word
tab
inserts a number of tab characters, default is one. Often this function is used in a block that removes whitespace around it.
Signature: tab(count …int) string
newline
inserts a number of newline characters, default is one. Often this function is used in a block that removes whitespace around it.
Signature: newline(count …int) string
first
{{- newline -}}
second and
{{- newline 3 }} third word
The output would be:
first
second and
third word
noop
does nothing. This can be useful to control spacing between elements because {{- -}} is not valid in itself.
Signature: noop() string
gr
{{- noop -}}
apefruit
The output would be:
grapefruit
Functions related to default values.
(a total of 11 functions: firstNonNilBool, defaultBool, firstNonNilInt, firstSetInt, defaultInt, firstNonNilString, firstSetString, defaultString, firstNonNilFloat, firstSetFloat, and defaultFloat)
firstNonNilBool
returns the first element in the given list of bool values that is not nil.
Signature: firstNonNilBool(inputs …any) (bool, error)
defaultBool
is an alias for firstNonNilBool
.
Signature: defaultBool(inputs …any) (bool, error)
firstNonNilInt
returns the first element in the given list of int values that is not nil.
Signature: firstNonNilInt(inputs …any) (int, error)
firstSetInt
returns the first element in the given list of int values that is not the empty value forInt.
Signature: firstSetInt(inputs …any) (*int, error)
defaultInt
is an alias for firstSetInt
.
Signature: defaultInt(inputs …any) (*int, error)
firstNonNilString
returns the first element in the given list of string values that is not nil.
Signature: firstNonNilString(inputs …any) (string, error)
firstSetString
returns the first element in the given list of string values that is not the empty value forString.
Signature: firstSetString(inputs …any) (*string, error)
defaultString
is an alias for firstSetString
.
Signature: defaultString(inputs …any) (*string, error)
firstNonNilFloat
returns the first element in the given list of float values that is not nil.
Signature: firstNonNilFloat(inputs …any) (float64, error)
firstSetFloat
returns the first element in the given list of float values that is not the empty value forFloat.
Signature: firstSetFloat(inputs …any) (*float64, error)
Functions related to strings.
(a total of 20 functions: trim, eqFold, eqIgnoreCase, stringContains, stringHasPrefix, stringHasSuffix, toUpperCase, toLowerCase, toCamelCase, toLowerCamelCase, toSnakeCase, toKebabCase, trimPrefix, trimSuffix, stringToFilename, stringToURL, diff, deHTML, replace, and regexpReplace)
trim
removes all leading and trailing whitespace from the given string. Returns the string unchanged if neither exists.
Signature: trim(input string) string
a{{ trim " bc " -}}
de
{{- " fg" | trim }}hi
The output would be:
eqFold
compares two strings disregarding their casing.
Signature: eqFold(a string, b string) bool
{{ if eqFold "ABC" "abc" -}}
the first strings are equal
{{- else -}}
the first strings are not equal
{{- end }}
{{ if eqFold "abcd" "abcf" -}}
the second strings are equal
{{- else -}}
the second strings are not equal
{{- end }}
The output would be:
the first strings are equal
the second strings are not equal
stringContains
checks if one string is contained in another.
Signature: stringContains(needle string, haystack string) bool
{{ if stringContains "ABC" "ABCDEFGHIJKLMNOPQRSTUVXYZ" -}}
first example contained
{{- else -}}
first example not contained
{{- end }}
{{ if stringContains "BFO" "ABCDEFGHIJKLMNOPQRSTUVXYZ" -}}
second example contained
{{- else -}}
second example not contained
{{- end }}
The output would be:
first example contained
second example not contained
stringHasPrefix
determines if a string starts with a given other string.
Signature: stringHasPrefix(prefix string, testString string) bool
stringHasSuffix
determines if a string ends with a given other string.
Signature: stringHasSuffix(suffix string, testString string) bool
toUpperCase
returns the given string converted to all uppercase letters.
Signature: toUpperCase(input string) string
{{ toUpperCase "abc" }} {{ "DEF" | toUpperCase }}
The output would be:
toLowerCase
returns the given string converted to all lowercase letters.
Signature: toLowerCase(input string) string
{{ toLowerCase "abc" }} {{ "DEF" | toLowerCase }}
The output would be:
toCamelCase
returns the given string converted to camel case (https://en.wikipedia.org/wiki/Camel_case), first letter uppercase.
Signature: toCamelCase(input string) string
toLowerCamelCase
returns the given string converted to lower camel case (https://en.wikipedia.org/wiki/Camel_case), first letter lowercase.
Signature: toLowerCamelCase(input string) string
toSnakeCase
returns the given string converted to snake case (https://en.wikipedia.org/wiki/Snake_case).
Signature: toSnakeCase(input string) string
toKebabCase
returns the given string converted to kebab case (https://en.wikipedia.org/wiki/Kebab_case).
Signature: toKebabCase(input string) string
trimPrefix
returns the given string without the given prefix if there is one, otherwise the string is returned unchanged.
Signature: trimPrefix(prefix string, input string) string
{{ "www.my-domain.com" | trimPrefix "www." -}}
The output would be:
my-domain.com
trimSuffix
returns the given string without the given suffix if there is one, otherwise the string is returned unchanged.
Signature: trimSuffix(suffix string, input string) string
{{ "my-domain.com" | trimSuffix ".com" -}}
The output would be:
'stringToFilename' returns the given string suitable for a filename.
Signature: stringToFilename(input string) string
'stringToURL' returns the given string suitable for a URL.
Signature: stringToURL(input string) string
diffFunc
returns the diff between two strings with their associated names.
Signature: diff(nameA string, contentA string, nameB string, contentB string, numContextLines int) string
deHTML
returns the raw string contained in a template.HTML.
Signature: deHTML(input htmlTemplate.HTML) string
replace
returns a given string with all occurrences of the given substring replaced by the replacement string.
Signature: replace(search string, replacement string, input string) string
Functions related to multiline strings.
(a total of 20 functions: lineOrErr, line, sortLines, head, skipHead, tail, skipTail, trimLines, trimAll, notMatch, match, withoutEmptyLines, withoutLineComments, wrapLines, indentSpaceLines, prefixLines, indentTabLines, getLines, asString, and regexpReplaceLine)
lineOrErr
returns a single line from the multiline input. The index is 1-based. Returns an error, if the line does not exist.
Signature: lineOrErr(number int, input string) (string, error)
line
returns a single line from the multiline input. The index is 1-based. Returns an empty string, if the line does not exist.
Signature: line(number int, input string) string
An excerpt from Shakespeare's "Spring And Winter":
{{ .poem | line 4 }}
The output would be:
An excerpt from Shakespeare's "Spring And Winter":
Do paint the meadows with delight,
Return the multiline input sorted alphabetically line by line.
Signature: sortLines(input string) string
head
returns the first n lines of a multiline string as one string, or all of it if there is less than n lines in total.
Signature: head(count int, input string) string
skipHead
returns the multiline string given without the first n lines or an empty string if there is less than n lines in total.
Signature: skipHead(count int, input string) string
tail
returns the last n lines of a multiline string as one string, or all of it if there is less than n lines in total.
Signature: tail(count int, input string) string
skipTail
returns the multiline string given without the last n lines or an empty string if there is less than n lines in total.
Signature: skipTail(count int, input string) string
trimLines
returns the multiline string given without leading and trailing empty lines.
Signature: trimLines(input string) string
trimAll
returns the multiline string given with leading and trailing space removed for any line individually.
Signature: trimAll(input string) string
notMatch
does return a string with all lines from the given multiline string that do not match the regexp given.
Signature: notMatch(regExp string, input string) string
match
does return a string with all lines from the given multiline string that do match the regexp given.
Signature: match(regExp string, input string) string
withoutEmptyLines
returns the multiline string given without empty lines.
Signature: withoutEmptyLines(input string) string
withoutLineComments
returns the multiline string given without line comments (lines starting with optional whitespace and // or #).
Signature: withoutLineComments(input string) string
wrapLines
returns the multiline string with every single line wrapped with the given leading and trailing string.
Signature: wrapLines(leading string, trailing string, input string) string
indentSpaceLines
returns the multiline string given with every line indented by additional n spaces.
Signature: indentSpaceLines(spaceCount int, input string) string
prefixLines
returns the multiline string given with every line prefixed with the string given.
Signature: prefixLines(prefix string, input string) string
indentTabLines
returns the multiline string given with every line indented by additional n tab characters.
Signature: indentTabLines(tabCount int, input string) string
getLines
returns the individual lines of a multiline string.
Signature: getLines(input string) []string
asString
returns a string separated by newline characters from a string slice.
Signature: asString(lines []string) string
Mathematical functions.
(a total of 10 functions: addInt, subtractInt, subtractFromInt, multiplyInt, divideIntBy, addFloat, subtractFloat, subtractFromFloat, multiplyFloat, and divideFloatBy)
addInt
adds a number of int values and returns the total sum.
Signature: addInt(inputs …int) int
subtractInt
subtracts a number of int values from the first one and returns the remaining value.
Signature: subtractInt(start int, inputs …int) int
subtractFromInt
subtracts a number of int values from the last one and returns the remaining value.
Signature: subtractFromInt(inputs …int) int
multiplyInt
multiplies a number of int values and returns the total value.
Signature: multiplyInt(inputs …int) int
divideIntBy
divides a int value by another one. Note the inverted order to make 24 | divideBy 12
nicely expressive.
Signature: divideIntBy(divisor int, value int) int
addFloat
adds a number of float64 values and returns the total sum.
Signature: addFloat(inputs …float64) float64
subtractFloat
subtracts a number of float64 values from the first one and returns the remaining value.
Signature: subtractFloat(start float64, inputs …float64) float64
subtractFromFloat
subtracts a number of float64 values from the last one and returns the remaining value.
Signature: subtractFromFloat(inputs …float64) float64
multiplyFloat
multiplies a number of float64 values and returns the total value.
Signature: multiplyFloat(inputs …float64) float64
Execute system commands. This is relying on https://github.com/jojomi/gorun.
(a total of 4 functions: run, runSSH, runner, and localCommandFrom)
runSSH
executes a command via SSH.
Signature: runSSH(sshAlias string, command string) (string, error)
Functions related to I/O operations.
(a total of 4 functions: include, readFile, writeFile, and writeFileWithPerms)
readFile
does return the content of a file as a string.
Signature: readFile(filename string) (string, error)
writeFile
writes as string to a file.
Signature: writeFile(filename string, content string) error
Functions related to filesystem operations.
glob
returns a list of files and/or directories matching the patter given.
Signature: glob(pattern string) ([]string, error)
Functions related to interacting with the system ENV.
envIsSet
checks if an ENV variable is set by its name.
Signature: envIsSet(key string) bool
Functions related to network operations.
(a total of 1 function: download)
Functions related to generating random numbers.
(a total of 2 functions: seededRandom and Random)
seededRandom
returns a rand.Source that is seeded with the given int value.
Signature: seededRandom(seed int) *rand.Rand
Functions related to semantic versioning.
(a total of 3 functions: parseSemver, semverToMajor, and semverToMinor)
parseSemver
converts a string to a *semver.Version.
Signature: parseSemver(semverString string) (*semver.Version, error)
semverToMajor
converts a string to the major version part of a *semver.Version.
Signature: semverToMajor(semverString string) (string, error)
Functions related to date and time.
Functions related to JSON encoding and decoding.
(a total of 4 functions: toJSON, parseJSON, jsonPath, and jsonPathWithDefault)
toJSON
returns the given data JSON encoded.
Signature: toJSON(input interface{}) (string, error)
parseJSON
decodes the give JSON string.
Signature: parseJSON(jsonString string) (interface{}, error)
jsonPath
extracts data from a JSON struct using a JSON path expression.
Signature: jsonPath(expression string, jsonData interface{}) (interface{}, error)
Functions related to hashing.
Functions related to go code.
(a total of 1 function: asGoCode)
Printing helpers.
(a total of 3 functions: printfLocalized, printInt, and printInt64)
printfLocalized
returns the
Signature: printfLocalized(key message.Reference, data …interface{}) (string, error)
Functions related to NLP.
(a total of 7 functions: plural, pluralInt64, pluralFloat, pluralWithNum, pluralInt64WithNum, pluralFloatWithNum, and joinText)
plural
returns the correct string depending on an int value given.
Signature: plural(singular string, plural string, value int) string
pluralInt64
returns the correct string depending on an int64 value given.
Signature: pluralInt64(singular string, plural string, value int64) string
pluralFloat
returns the correct string depending on a float value given.
Signature: pluralFloat(singular string, plural string, value float64) string
pluralWithNum
returns the number and the correct string depending on an int value given.
Signature: pluralWithNum(singular string, plural string, value int) string
pluralInt64WithNum
returns the number and the correct string depending on an int64 value given.
Signature: pluralInt64WithNum(singular string, plural string, value int64) string
pluralFloatWithNum
returns the number and the correct string depending on a float value given.
Signature: pluralFloatWithNum(singular string, plural string, value float64) string
joinText
joins elements suitable for a human-readable text.
Signature: joinText(delim string, twoDelim string, lastDelim string, input []string) string
{{ (makeStringList "A" "B" "C" "D").All | joinText ", " " and " ", and " }}
The output would be:
A, B, C, and D
Functions related to casting between datatypes. Uses the https://github.com/spf13/cast library.
(a total of 5 functions: toBool, toString, toInt, toFloat, and toStringSlice)
toString
converts any given input to a string.
Signature: toString(input interface{}) string
toFloat
converts any given content to a float64.
Signature: toFloat(input interface{}) float64
You can assert the data types of values you are dealing with using these functions.
(a total of 3 functions: assertString, assertInt, and assertFloat)
assertString
makes sure the given variable is of type string.
Signature: assertString(input interface{}) error
{{ assertString .Name }} /* will just continue */
{{ assertString .Age }} /* will make the template evaluation fail */
assertInt
makes sure the given variable is of type int.
Signature: assertInt(input interface{}) error