From e513897de745c89e16a2900c7206129672c53a51 Mon Sep 17 00:00:00 2001 From: dig Date: Fri, 26 Jul 2019 13:03:48 +0200 Subject: [PATCH] [shtpl] Added utility functions --- shtpl/README.md | 30 ++++++++++++++++++++++++++++++ shtpl/shtpl | 25 +++++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/shtpl/README.md b/shtpl/README.md index daaae6c..eb15324 100644 --- a/shtpl/README.md +++ b/shtpl/README.md @@ -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) +``` diff --git a/shtpl/shtpl b/shtpl/shtpl index d79168c..b63426b 100755 --- a/shtpl/shtpl +++ b/shtpl/shtpl @@ -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 '