From c28bffe708e65cab199014c0a978594674e2cd1d Mon Sep 17 00:00:00 2001 From: Joseph Dalrymple Date: Mon, 3 Apr 2023 21:12:17 -0500 Subject: [PATCH 1/2] Added @key loop accessor --- README.md | 40 ++++++++++++++++++++++++++++++++++++++ mo | 13 ++++++++++++- tests/array.expected | 6 +++--- tests/array.template | 2 +- tests/assoc-array.env | 4 ++++ tests/assoc-array.expected | 3 +++ tests/assoc-array.template | 3 +++ 7 files changed, 66 insertions(+), 5 deletions(-) create mode 100644 tests/assoc-array.env create mode 100644 tests/assoc-array.expected create mode 100644 tests/assoc-array.template diff --git a/README.md b/README.md index 9af081d..0fac682 100644 --- a/README.md +++ b/README.md @@ -118,6 +118,46 @@ There are more scripts available in the [demos directory](demo/) that could help There are additional features that the program supports. Try using `mo --help` to see what is available. +Enhancements +----------- + +In addition to many of the features built-in to Mustache, `mo` includes a number of unique features that make it a bit more powerful. + +### Loop @key + +`mo` implements Handlebar's `@key` references for outputting the key inside of a loop: + +Env: +```bash +myarr=( foo bar ) + +# Bash v4+ +declare -A myassoc +myassoc[hello]="mo" +myassoc[world]="is great" +``` + +Template: +```handlebars +{{#myarr}} + - {{@key}} {{.}} +{{/myarr}} + +{{#myassoc}} + * {{@key}} {{.}} +{{/myassoc}} +``` + +Output: +```markdown + - 0 foo + - 1 bar + + * hello mo + * world is great +``` + + Concessions ----------- diff --git a/mo b/mo index c928d7c..bb14fbf 100755 --- a/mo +++ b/mo @@ -666,7 +666,7 @@ moLoop() { moParse() { # Keep naming variables mo* here to not overwrite needed variables # used in the string replacements - local moArgs moBlock moContent moCurrent moIsBeginning moNextIsBeginning moTag + local moArgs moBlock moContent moCurrent moIsBeginning moNextIsBeginning moTag moKey moCurrent=$2 moIsBeginning=$3 @@ -782,6 +782,17 @@ moParse() { moShow "$moTag" "$moCurrent" ;; + '@key') + # Special vars + moStandaloneDenied moContent "${moContent[@]}" + # Current content (environment variable or function) + if [[ "$moCurrent" == *.* ]]; then + echo -n "${moCurrent#*.}" + else + echo -n "$moCurrent" + fi + ;; + *) # Normal environment variable or function call moStandaloneDenied moContent "${moContent[@]}" diff --git a/tests/array.expected b/tests/array.expected index a45557c..94d9b8e 100644 --- a/tests/array.expected +++ b/tests/array.expected @@ -1,3 +1,3 @@ - resque - hub - rip + 0 - resque + 1 - hub + 2 - rip diff --git a/tests/array.template b/tests/array.template index 4c61e9c..8671607 100644 --- a/tests/array.template +++ b/tests/array.template @@ -1,3 +1,3 @@ {{#repo}} - {{.}} + {{@index}} - {{.}} {{/repo}} diff --git a/tests/assoc-array.env b/tests/assoc-array.env new file mode 100644 index 0000000..934cade --- /dev/null +++ b/tests/assoc-array.env @@ -0,0 +1,4 @@ +declare -A repo +repo[resque]="Resque" +repo[hub]="Hub" +repo[rip]="Rip" diff --git a/tests/assoc-array.expected b/tests/assoc-array.expected new file mode 100644 index 0000000..6ef4ced --- /dev/null +++ b/tests/assoc-array.expected @@ -0,0 +1,3 @@ + hub - Hub + rip - Rip + resque - Resque diff --git a/tests/assoc-array.template b/tests/assoc-array.template new file mode 100644 index 0000000..771906f --- /dev/null +++ b/tests/assoc-array.template @@ -0,0 +1,3 @@ +{{#repo}} + {{@key}} - {{.}} +{{/repo}} From b22baa977688ab682d12477acbdce05a82f30801 Mon Sep 17 00:00:00 2001 From: Joseph Dalrymple Date: Tue, 4 Apr 2023 19:01:40 -0500 Subject: [PATCH 2/2] Added Functions/Helpers to README --- README.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/README.md b/README.md index 0fac682..37fd6b7 100644 --- a/README.md +++ b/README.md @@ -158,6 +158,37 @@ Output: ``` +### Helpers / Function Arguments with + +Function Arguments are not a part of the official Mustache implementation, and are more often associated with Handlebar's Helper functionality. + +`mo` allows for passing strings to functions. + +```handlebars +{{myfunc foo bar}} +``` + +For security reasons, this arguments are not immediately available to function calls without a flag. + +#### with `--allow-function-arguments` + +```bash +myfunc() { + # Outputs "foo, bar" + echo "$1, $2"; +} +``` + +#### Using `$MO_FUNCTION_ARGS` + +```bash +myfunc() { + # Outputs "foo, bar" + echo "${MO_FUNCTION_ARGS[0]}, ${MO_FUNCTION_ARGS[1]}"; +} +``` + + Concessions -----------