[shtpl] Added utility functions

This commit is contained in:
dig 2019-07-26 13:03:48 +02:00
parent 9d10846ff9
commit e513897de7
2 changed files with 55 additions and 0 deletions

View File

@ -94,3 +94,33 @@ Loops: [0][1][2][3][4][5][6][7][8][9][10]
Call sub templates: what the template returns with "aze"
```
## Additional helper functions
SHTPL declares somme utility functions to use in templates as a sub-commands,
generally it is a 1 argument function that can be piped:
### lowerCase
To transform a string in lowercase:
```
Original value: $value
Lowercase value: $(lowerCase $value)
Lowercase piped: $(echo $value | lowerCase)
```
### slug
To transform a string in a safe name, special characters are turned into "_":
```
Original value: $value
Slug value: $(slug $value)
Slug piped: $(echo $value | slug)
```
### user-input
Asks the user to enter text input at template execution and put it in place,
multiline text is supported because you'd have to type (ctrl-D) to validate input:
```
You entered: $(user-input Enter a value)
and the 2nd time this: $(user-input 'Question with simple quote')
the last one is turned to lowercase: $(user-input To be turned in lowercase | lowerCase)
```

View File

@ -10,6 +10,31 @@ __tpl_name__=$(basename $__tpl_path__)
__tpl_dir__=$(dirname $__tpl_path__)
# Broken, don't use!
pipe()
{
[[ ! -t 0 ]] && cat
}
user-input()
{
echo "$@ (ctrl-D): " 1>&2
res="$(sed '/^\04$/q')"
echo "$res"
}
lowerCase()
{
( [[ ! -z "$@" ]] && echo $@ || cat ) | awk '{print tolower($0)}'
}
slug()
{
( [[ ! -z "$@" ]] && echo $@ || cat ) | lowerCase | sed 's/[^a-zA-Z0-9-]/_/g'
}
# Execution
eval "echo \"$(cat $1 | sed '