commit b64fef29791a4a55de3edcc876813acbf8298e87 Author: Charles N Wyble Date: Mon Dec 9 14:43:00 2024 -0600 Squashed 'vendor/git.knownelement.com/ExternalVendorCode/mo/' content from commit 7e86c1a git-subtree-dir: vendor/git.knownelement.com/ExternalVendorCode/mo git-subtree-split: 7e86c1a5f525f352983077d743c2ce2f5d75f4fa diff --git a/.eslintrc.yaml b/.eslintrc.yaml new file mode 100644 index 0000000..6ee5d88 --- /dev/null +++ b/.eslintrc.yaml @@ -0,0 +1,168 @@ +parserOptions: + ecmaVersion: latest + sourceType: module +env: + es6: true + jasmine: true + node: true +extends: eslint:recommended +rules: + accessor-pairs: error + array-bracket-spacing: + - error + - never + array-callback-return: error + block-spacing: + - error + - never + brace-style: error + comma-dangle: error + comma-spacing: error + comma-style: error + complexity: + - error + - 10 + computed-property-spacing: error + consistent-return: error + consistent-this: error + constructor-super: error + curly: error + default-case: error + dot-notation: error + eol-last: error + eqeqeq: error + generator-star-spacing: error + global-require: off + guard-for-in: error + jsx-quotes: error + key-spacing: error + keyword-spacing: error + linebreak-style: error + lines-around-comment: + - error + - + allowBlockStart: true + allowObjectStart: true + allowArrayStart: true + max-statements-per-line: error + new-cap: error + new-parens: error + no-array-constructor: error + no-bitwise: error + no-caller: error + no-case-declarations: error + no-catch-shadow: error + no-class-assign: error + no-cond-assign: error + no-confusing-arrow: error + no-console: off + no-const-assign: error + no-constant-condition: error + no-continue: error + no-delete-var: error + no-dupe-args: error + no-dupe-class-members: error + no-dupe-keys: error + no-duplicate-case: error + no-duplicate-imports: error + no-empty: off + no-empty-character-class: error + no-empty-pattern: error + no-eq-null: error + no-eval: error + no-extend-native: error + no-extra-bind: error + no-extra-boolean-cast: error + no-extra-label: error + no-extra-semi: error + no-fallthrough: error + no-func-assign: error + no-implied-eval: error + no-inner-declarations: error + no-invalid-this: error + no-invalid-regexp: error + no-irregular-whitespace: error + no-iterator: error + no-label-var: error + no-labels: error + no-lone-blocks: error + no-lonely-if: error + no-loop-func: error + no-mixed-spaces-and-tabs: error + no-multi-spaces: error + no-multi-str: error + no-multiple-empty-lines: + - error + - + max: 2 + no-native-reassign: error + no-negated-condition: error + no-nested-ternary: error + no-new: error + no-new-func: error + no-new-object: error + no-new-symbol: error + no-new-wrappers: error + no-obj-calls: error + no-octal: error + no-octal-escape: error + no-path-concat: error + no-plusplus: error + no-proto: error + no-redeclare: error + no-regex-spaces: error + no-restricted-globals: error + no-return-assign: error + no-script-url: error + no-self-assign: error + no-self-compare: error + no-sequences: error + no-shadow: error + no-shadow-restricted-names: error + no-spaced-func: error + no-sparse-arrays: error + no-this-before-super: error + no-throw-literal: error + no-trailing-spaces: error + no-undef: error + no-undef-init: error + no-unexpected-multiline: error + no-unmodified-loop-condition: error + no-unneeded-ternary: error + no-unreachable: error + no-unsafe-finally: error + no-unused-expressions: error + no-unused-labels: error + no-unused-vars: error + no-useless-call: error + no-useless-computed-key: error + no-useless-concat: error + no-useless-constructor: error + no-useless-escape: error + no-void: error + no-warning-comments: warn + no-whitespace-before-property: error + no-with: error + operator-assignment: error + padded-blocks: + - error + - never + prefer-const: error + quote-props: + - error + - as-needed + radix: error + require-yield: error + semi: error + semi-spacing: error + space-before-blocks: error + space-in-parens: error + space-infix-ops: + - error + - + int32Hint: false + space-unary-ops: error + spaced-comment: error + use-isnan: error + valid-typeof: error + yield-star-spacing: error diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml new file mode 100644 index 0000000..ef4e8fb --- /dev/null +++ b/.github/workflows/ci.yaml @@ -0,0 +1,13 @@ +name: CI +on: [push] +jobs: + test: + name: Test + runs-on: ubuntu-latest + steps: + - name: Check out code + uses: actions/checkout@v1 + - name: Run tests + run: ./run-tests + - name: Run against spec + run: ./run-spec diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml new file mode 100644 index 0000000..118f5d3 --- /dev/null +++ b/.github/workflows/release.yaml @@ -0,0 +1,45 @@ +name: docker push +on: [push] +jobs: + push_to_registry: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@master + - name: Docker meta + if: startsWith(github.ref, 'refs/tags/') + id: docker_meta + uses: crazy-max/ghaction-docker-meta@v1 + with: + images: ghcr.io/${{ github.repository }} + tag-match: v(.*) + - name: Set up QEMU + if: startsWith(github.ref, 'refs/tags/') + uses: docker/setup-qemu-action@v1 + - name: Set up Docker Buildx + id: buildx + uses: docker/setup-buildx-action@v1 + - name: Cache Docker layers + if: startsWith(github.ref, 'refs/tags/') + uses: actions/cache@v2 + with: + path: /tmp/.buildx-cache + key: ${{ runner.os }}-buildx-${{ github.sha }} + restore-keys: | + ${{ runner.os }}-buildx- + - name: Login to GitHub Container Registry + if: startsWith(github.ref, 'refs/tags/') + uses: docker/login-action@v1 + with: + registry: ghcr.io + username: ${{ github.repository_owner }} + password: ${{ secrets.GITHUB_TOKEN }} + - name: Build and push + uses: docker/build-push-action@v2 + if: startsWith(github.ref, 'refs/tags/') + with: + builder: ${{ steps.buildx.outputs.name }} + platforms: linux/amd64,linux/arm64 + tags: ${{ steps.docker_meta.outputs.tags }} + cache-from: type=local,src=/tmp/.buildx-cache + cache-to: type=local,dest=/tmp/.buildx-cache + push: true \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1900b08 --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +*.swp +diagnostic.partial +diagnostic.test +tests/*.diff +spec/ +spec-runner/ +node_modules/ +package.json +package-lock.json diff --git a/API.md b/API.md new file mode 100644 index 0000000..b63865e --- /dev/null +++ b/API.md @@ -0,0 +1,616 @@ +API / Function Documentation +============================ + +This documentation is generated automatically from the source of [mo] thanks to [tomdoc.sh]. + + +`mo()` +------ + +Public: Template parser function. Writes templates to stdout. + +* $0 - Name of the mo file, used for getting the help message. +* $@ - Filenames to parse. + +Returns nothing. + + +`mo::debug()` +------------- + +Internal: Show a debug message + +* $1 - The debug message to show + +Returns nothing. + + +`mo::debugShowState()` +---------------------- + +Internal: Show a debug message and internal state information + +No arguments + +Returns nothing. + + +`mo::error()` +------------- + +Internal: Show an error message and exit + +* $1 - The error message to show +* $2 - Error code + +Returns nothing. Exits the program. + + +`mo::errorNear()` +----------------- + +Internal: Show an error message with a snippet of context and exit + +* $1 - The error message to show +* $2 - The starting point +* $3 - Error code + +Returns nothing. Exits the program. + + +`mo::usage()` +------------- + +Internal: Displays the usage for mo. Pulls this from the file that contained the `mo` function. Can only work when the right filename comes is the one argument, and that only happens when `mo` is called with `$0` set to this file. + +* $1 - Filename that has the help message + +Returns nothing. + + +`mo::content()` +--------------- + +Internal: Fetches the content to parse into MO_UNPARSED. Can be a list of partials for files or the content from stdin. + +* $1 - Destination variable name +* $2-@ - File names (optional), read from stdin otherwise + +Returns nothing. + + +`mo::contentFile()` +------------------- + +Internal: Read a file into MO_UNPARSED. + +* $1 - Destination variable name. +* $2 - Filename to load - if empty, defaults to /dev/stdin + +Returns nothing. + + +`mo::indirect()` +---------------- + +Internal: Send a variable up to the parent of the caller of this function. + +* $1 - Variable name +* $2 - Value + +Examples + + callFunc () { + local "$1" && mo::indirect "$1" "the value" + } + callFunc dest + echo "$dest" # writes "the value" + +Returns nothing. + + +`mo::indirectArray()` +--------------------- + +Internal: Send an array as a variable up to caller of a function + +* $1 - Variable name +* $2-@ - Array elements + +Examples + + callFunc () { + local myArray=(one two three) + local "$1" && mo::indirectArray "$1" "${myArray[@]}" + } + callFunc dest + echo "${dest[@]}" # writes "one two three" + +Returns nothing. + + +`mo::trimUnparsed()` +-------------------- + +Internal: Trim leading characters from MO_UNPARSED + +Returns nothing. + + +`mo::chomp()` +------------- + +Internal: Remove whitespace and content after whitespace + +* $1 - Name of the destination variable +* $2 - The string to chomp + +Returns nothing. + + +`mo::parse()` +------------- + +Public: Parses text, interpolates mustache tags. Utilizes the current value of MO_OPEN_DELIMITER, MO_CLOSE_DELIMITER, and MO_STANDALONE_CONTENT. Those three variables shouldn't be changed by user-defined functions. + +* $1 - Destination variable name - where to store the finished content +* $2 - Content to parse +* $3 - Preserve standalone status/content - truthy if not empty. When set to a value, that becomes the standalone content value + +Returns nothing. + + +`mo::parseInternal()` +--------------------- + +Internal: Parse MO_UNPARSED, writing content to MO_PARSED. Interpolates mustache tags. + +No arguments + +Returns nothing. + + +`mo::parseBlock()` +------------------ + +Internal: Handle parsing a block + +* $1 - Invert condition ("true" or "false") + +Returns nothing + + +`mo::parseBlockFunction()` +-------------------------- + +Internal: Handle parsing a block whose first argument is a function + +* $1 - Invert condition ("true" or "false") +* $2-@ - The parsed tokens from inside the block tags + +Returns nothing + + +`mo::parseBlockArray()` +----------------------- + +Internal: Handle parsing a block whose first argument is an array + +* $1 - Invert condition ("true" or "false") +* $2-@ - The parsed tokens from inside the block tags + +Returns nothing + + +`mo::parseBlockValue()` +----------------------- + +Internal: Handle parsing a block whose first argument is a value + +* $1 - Invert condition ("true" or "false") +* $2-@ - The parsed tokens from inside the block tags + +Returns nothing + + +`mo::parsePartial()` +-------------------- + +Internal: Handle parsing a partial + +No arguments. + +Indentation will be applied to the entire partial's contents before parsing. This indentation is based on the whitespace that ends the previously parsed content. + +Returns nothing + + +`mo::parseComment()` +-------------------- + +Internal: Handle parsing a comment + +No arguments. + +Returns nothing + + +`mo::parseDelimiter()` +---------------------- + +Internal: Handle parsing the change of delimiters + +No arguments. + +Returns nothing + + +`mo::parseValue()` +------------------ + +Internal: Handle parsing value or function call + +No arguments. + +Returns nothing + + +`mo::isFunction()` +------------------ + +Internal: Determine if the given name is a defined function. + +* $1 - Function name to check + +Be extremely careful. Even if strict mode is enabled, it is not honored in newer versions of Bash. Any errors that crop up here will not be caught automatically. + +Examples + + moo () { + echo "This is a function" + } + if mo::isFunction moo; then + echo "moo is a defined function" + fi + +Returns 0 if the name is a function, 1 otherwise. + + +`mo::isArray()` +--------------- + +Internal: Determine if a given environment variable exists and if it is an array. + +* $1 - Name of environment variable + +Be extremely careful. Even if strict mode is enabled, it is not honored in newer versions of Bash. Any errors that crop up here will not be caught automatically. + +Examples + + var=(abc) + if moIsArray var; then + echo "This is an array" + echo "Make sure you don't accidentally use \$var" + fi + +Returns 0 if the name is not empty, 1 otherwise. + + +`mo::isArrayIndexValid()` +------------------------- + +Internal: Determine if an array index exists. + +* $1 - Variable name to check +* $2 - The index to check + +Has to check if the variable is an array and if the index is valid for that type of array. + +Returns true (0) if everything was ok, 1 if there's any condition that fails. + + +`mo::isVarSet()` +---------------- + +Internal: Determine if a variable is assigned, even if it is assigned an empty value. + +* $1 - Variable name to check. + +Can not use logic like this in case invalid variable names are passed. [[ "${!1-a}" == "${!1-b}" ]] + +Returns true (0) if the variable is set, 1 if the variable is unset. + + +`mo::isTruthy()` +---------------- + +Internal: Determine if a value is considered truthy. + +* $1 - The value to test +* $2 - Invert the value, either "true" or "false" + +Returns true (0) if truthy, 1 otherwise. + + +`mo::evaluate()` +---------------- + +Internal: Convert token list to values + +* $1 - Destination variable name +* $2-@ - Tokens to convert + +Sample call: + + mo::evaluate dest NAME username VALUE abc123 PAREN 2 + +Returns nothing. + + +`mo::evaluateListOfSingles()` +----------------------------- + +Internal: Convert an argument list to individual values. + +* $1 - Destination variable name +* $2-@ - A list of argument types and argument name/value. + +This assumes each value is separate from the rest. In contrast, mo::evaluate will pass all arguments to a function if the first value is a function. + +Sample call: + + mo::evaluateListOfSingles dest NAME username VALUE abc123 + +Returns nothing. + + +`mo::evaluateSingle()` +---------------------- + +Internal: Evaluate a single argument + +* $1 - Name of variable for result +* $2 - Type of argument, either NAME or VALUE +* $3 - Argument + +Returns nothing + + +`mo::evaluateKey()` +------------------- + +Internal: Return the value for @key based on current's name + +* $1 - Name of variable for result + +Returns nothing + + +`mo::evaluateVariable()` +------------------------ + +Internal: Handle a variable name + +* $1 - Destination variable name +* $2 - Variable name + +Returns nothing. + + +`mo::findVariableName()` +------------------------ + +Internal: Find the name of a variable to use + +* $1 - Destination variable name, receives an array +* $2 - Variable name from the template + +The array contains the following values + * [0] - Variable name + * [1] - Array index, or empty string + +Example variables a="a" + b="b" + c=("c.0" "c.1") + d=([b]="d.b" [d]="d.d") + +Given these inputs (function input, current value), produce these outputs a c => a + a c.0 => a + b d => d.b + b d.d => d.b + a d => d.a + a d.d => d.a + c.0 d => c.0 + d.b d => d.b + '' c => c + '' c.0 => c.0 + Returns nothing. + + +`mo::join()` +------------ + +Internal: Join / implode an array + +* $1 - Variable name to receive the joined content +* $2 - Joiner +* $3-@ - Elements to join + +Returns nothing. + + +`mo::evaluateFunction()` +------------------------ + +Internal: Call a function. + +* $1 - Variable for output +* $2 - Content to pass +* $3 - Function to call +* $4-@ - Additional arguments as list of type, value/name + +Returns nothing. + + +`mo::standaloneCheck()` +----------------------- + +Internal: Check if a tag appears to have only whitespace before it and after it on a line. There must be a new line before and there must be a newline after or the end of a string + +No arguments. + +Returns 0 if this is a standalone tag, 1 otherwise. + + +`mo::standaloneProcess()` +------------------------- + +Internal: Process content before and after a tag. Remove prior whitespace up to the previous newline. Remove following whitespace up to and including the next newline. + +No arguments. + +Returns nothing. + + +`mo::indentLines()` +------------------- + +Internal: Apply indentation before any line that has content in MO_UNPARSED. + +* $1 - Destination variable name. +* $2 - The indentation string. +* $3 - The content that needs the indentation string prepended on each line. + +Returns nothing. + + +`mo::escape()` +-------------- + +Internal: Escape a value + +* $1 - Destination variable name +* $2 - Value to escape + +Returns nothing + + +`mo::getContentUntilClose()` +---------------------------- + +Internal: Get the content up to the end of the block by minimally parsing and balancing blocks. Returns the content before the end tag to the caller and removes the content + the end tag from MO_UNPARSED. This can change the delimiters, adjusting MO_OPEN_DELIMITER and MO_CLOSE_DELIMITER. + +* $1 - Destination variable name +* $2 - Token string to match for a closing tag + +Returns nothing. + + +`mo::tokensToString()` +---------------------- + +Internal: Convert a list of tokens to a string + +* $1 - Destination variable for the string +* $2-$@ - Token list + +Returns nothing. + + +`mo::getContentTrim()` +---------------------- + +Internal: Trims content from MO_UNPARSED, returns trimmed content. + +* $1 - Destination variable + +Returns nothing. + + +`mo::getContentComment()` +------------------------- + +Get the content up to and including a close tag + +* $1 - Destination variable + +Returns nothing. + + +`mo::getContentDelimiter()` +--------------------------- + +Get the content up to and including a close tag. First two non-whitespace tokens become the new open and close tag. + +* $1 - Destination variable + +Returns nothing. + + +`mo::getContentWithinTag()` +--------------------------- + +Get the content up to and including a close tag. First two non-whitespace tokens become the new open and close tag. + +* $1 - Destination variable, an array +* $2 - Terminator string + +The array contents: [0] The raw content within the tag + [1] The parsed tokens as a single string + +Returns nothing. + + +`mo::tokenizeTagContents()` +--------------------------- + +Internal: Parse MO_UNPARSED and retrieve the content within the tag delimiters. Converts everything into an array of string values. + +* $1 - Destination variable for the array of contents. +* $2 - Stop processing when this content is found. + +The list of tokens are in RPN form. The first item in the resulting array is the number of actual tokens (after combining command tokens) in the list. + +Given: a 'bc' "de\"\n" (f {g 'h'}) Result: ([0]=4 [1]=NAME [2]=a [3]=VALUE [4]=bc [5]=VALUE [6]=$'de\"\n' [7]=NAME [8]=f [9]=NAME [10]=g [11]=VALUE [12]=h [13]=BRACE [14]=2 [15]=PAREN [16]=2 + +Returns nothing + + +`mo::tokenizeTagContentsName()` +------------------------------- + +Internal: Get the contents of a variable name. + +* $1 - Destination variable name for the token list (array of strings) + +Returns nothing + + +`mo::tokenizeTagContentsDoubleQuote()` +-------------------------------------- + +Internal: Get the contents of a tag in double quotes. Parses the backslash sequences. + +* $1 - Destination variable name for the token list (array of strings) + +Returns nothing. + + +`mo::tokenizeTagContentsSingleQuote()` +-------------------------------------- + +Internal: Get the contents of a tag in single quotes. Only gets the raw value. + +* $1 - Destination variable name for the token list (array of strings) + +Returns nothing. + + +`MO_ORIGINAL_COMMAND` +--------------------- + +Save the original command's path for usage later + + +[mo]: ./mo +[tomdoc.sh]: https://github.com/tests-always-included/tomdoc.sh diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..663495e --- /dev/null +++ b/Dockerfile @@ -0,0 +1,7 @@ +FROM alpine + +RUN apk add --no-cache bash +ADD mo /usr/local/bin/mo +RUN chmod +x /usr/local/bin/mo + +ENTRYPOINT /usr/local/bin/mo diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..93622a5 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,7 @@ +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +Except as contained in this notice, the name(s) of the above copyright holders shall not be used in advertising or otherwise to promote the sale, use or other dealings in this Software without prior written authorization. + +The end-user documentation included with the redistribution, if any, must include the following acknowledgment: "This product includes software developed by contributors", in the same place and form as other third-party acknowledgments. Alternately, this acknowledgment may appear in the software itself, in the same form and location as other such third-party acknowledgments. diff --git a/README.md b/README.md new file mode 100644 index 0000000..7f6af1c --- /dev/null +++ b/README.md @@ -0,0 +1,340 @@ +Mo - Mustache Templates in Bash +=============================== + +[Mustache] templates are simple, logic-less templates. Because of their simplicity, they are able to be ported to many languages. The syntax is quite simple. + + Hello, {{NAME}}. + + I hope your {{TIME_PERIOD}} was fun. + +The above file is [`demo/fun-trip.mo`](demo/fun-trip.mo). Let's try using this template some data from bash's environment. Go to your checked out copy of the project and run a command like this: + + NAME=Tyler TIME_PERIOD=weekend ./mo demo/fun-trip.mo + +Your result? + + Hello, Tyler. + + I hope your weekend was fun. + +This bash version supports conditionals, functions (both as filters and as values), as well as indexed arrays (for iteration). You are able to leverage these additional features by adding more information into the environment. It is easiest to do this when you source `mo`. See the [demo scripts](demo/) for further examples. + + +Requirements +------------ + +* Bash 3.x (the aim is to make it work on Macs) +* The "coreutils" package (`basename` and `cat`) +* ... that's it. Why? Because bash **can**! + +If you intend to develop this and run the official specs, you also need node.js. + + +Installation +------------ + +There are a few ways you can install this tool. How you install it depends on how you want to use it. + + +### Globally; For Everyone + +You can install this file in `/usr/local/bin/` or `/usr/bin/` by simply downloading it, changing the permissions, then moving it to the right location. Double check that your system's PATH includes the destination folder, otherwise users may have a hard time starting the command. + + # Download + curl -sSL https://raw.githubusercontent.com/tests-always-included/mo/master/mo -o mo + + # Make executable + chmod +x mo + + # Move to the right folder + sudo mv mo /usr/local/bin/ + + # Test + echo "works" | mo + + +### Locally; For Yourself + +This is very similar to installing it globally but it does not require root privileges. It is very important that your PATH includes the destination folder otherwise it won't work. Some local folders that are typically used are `~/bin/` and `~/.local/bin/`. + + # Download + curl -sSL https://raw.githubusercontent.com/tests-always-included/mo/master/mo -o mo + + # Make executable + chmod +x mo + + # Ensure destination folder exists + mkdir -p ~/.local/bin/ + + # Move to the right folder + mv mo ~/.local/bin/ + + # Test + echo "works" | mo + + +### As A Library; For A Tool + +Bash scripts can source `mo` to include the functionality in their own routines. This usage typically would have `mo` saved to a `lib/` folder in an application and your other scripts would use `. lib/mo` to bring it into your project. + + # Download + curl -sSL https://raw.githubusercontent.com/tests-always-included/mo/master/mo -o mo + + # Move into your project folder + mv mo ~/projects/amazing-things/lib/ + +To allow it to work this way, you either should source the file (`. "lib/mo"`) or make it executable (`chmod +x lib/mo`) and run it from your scripts. + + +How to Use +---------- + +If you only plan using strings and numbers, nothing could be simpler. In your shell script you can choose to export the variables. The below script is [`demo/using-strings`](demo/using-strings). + + #!/usr/bin/env bash + cd "$(dirname "$0")" # Go to the script's directory + export TEST="This is a test" + echo "Your message: {{TEST}}" | ../mo + +The result? "Your message: This is a test". + +Using arrays adds a slight level of complexity. *You must source `mo`.* Look at [`demo/using-arrays`](demo/using-arrays). + + #!/usr/bin/env bash + cd "$(dirname "$0")" # Go to the script's directory + export ARRAY=( one two "three three three" four five ) + . ../mo # This loads the "mo" function + cat << EOF | mo + Here are the items in the array: + {{#ARRAY}} + * {{.}} + {{/ARRAY}} + EOF + +The result? You get a list of the five elements in the array. It is vital that you source `mo` and run the function when you want arrays to work because you can not execute a command and have arrays passed to that command's environment. Instead, we first source the file to load the function and then run the function directly. + +There are more scripts available in the [demos directory](demo/) that could help illustrate how you would use this program. + +There are additional features that the program supports. Try using `mo --help` to see what is available. + +Please note that this command is written in Bash and pulls data from either the environment or (when using `--source`) from a text file that will be sourced and loaded into the environment, which means you will need to have Bash-style variables defined. Please see the examples in `demo/` for different ways you can use `mo`. + + +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 +``` + + +### Helpers / Function Arguments + +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, these 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]}"; +} +``` + +### Triple Mustache, Parenthesis, and Quotes + +Normally, triple mustache syntax, such as `{{{var}}}` will avoid HTML escaping of the variable. Because HTML escaping is not supported in `mo`, this is now used differently. Anything within braces will be looked up and the values will be concatenated together and the result will be treated as a value. Anything in parenthesis will be looked up, concatenated, and treated as a name. Also, anything in single quotes is passed as a value; double quoted things first are unescaped and then passed as a value. + +``` +# Example input +var=abc +user=admin +admin=Administrator +u=user +abc=([0]=zero [1]=one [2]=two) +``` + +| Mustache syntax | Resulting output | Notes | +|-----------------|------------------|-------| +| `{{var}}` | `abc` | Normal behavior | +| `{{var us}}` | `abcus` | Concatenation | +| `{{'var'}}` | `var` | Passing as a value | +| `{{"a\tb"}}` | `a b` | There was an escaped tab in the value | +| `{{u}}` | `user` | Normal behavior | +| `{{{u}}}` | `user` | Look up "$u", treat as the value `{{'user'}}` | +| `{{(u)}}` | `admin` | Look up "$u", treat as the name `{{user}}` | +| `{{var user}}` | `abcuser` | Concatenation | +| `{{(var '.1')}}` | `one` | Look up "$var", treat as "abc", then concatenate ".1" and look up `{{abc.1}}` | + +In double-quoted strings, the following escape sequences are defined. + +* `\"` - Quote +* `\b` - Bell +* `\e` - Escape (note that Bash typically uses $'\E' for the same thing) +* `\f` - Form feed +* `\n` - Newline +* `\r` - Carriage return +* `\t` - Tab +* `\v` - Vertical tab +* Anything else will skip the `\` and place the next character. However, this implementation is allowed to change in the future if a different escape character mapping becomes commonplace. + + +Environment Variables and Functions +----------------------------------- + +There are several functions and variables used to process templates. `mo` reserves variables that start with `MO_` for variables exposing data or configuration, functions starting with `mo::`, and local variables starting with `mo[A-Z]`. You are welcome to use internal functions, though only ones that are marked as "Public" should not change their interface. Scripts may also read any of the variables. + +Functions are all executed in a subshell, with another subshell for lambdas. Thus, your lambda can't affect the parsing of a template. There's more information about lambdas when talking about tests that fail. + +* `MO_ALLOW_FUNCTION_ARGUMENTS` - When set to a non-empty value, this allows functions referenced in templates to receive additional options and arguments. +* `MO_CLOSE_DELIMITER` - The string used when closing a tag. Defaults to "}}". Used internally. +* `MO_CLOSE_DELIMITER_DEFAULT` - The default value of `MO_CLOSE_DELIMITER`. Used when resetting the close delimiter, such as when parsing a partial. +* `MO_CURRENT` - Variable name to use for ".". +* `MO_DEBUG` - When set to a non-empty value, additional debug information is written to stderr. +* `MO_FUNCTION_ARGS` - Arguments passed to the function. +* `MO_FAIL_ON_FILE` - If a filename from the command-line is missing or a partial does not exist, abort with an error. +* `MO_FAIL_ON_FUNCTION` - If a function returns a non-zero status code, abort with an error. +* `MO_FAIL_ON_UNSET` - When set to a non-empty value, expansion of an unset env variable will be aborted with an error. +* `MO_FALSE_IS_EMPTY` - When set to a non-empty value, the string "false" will be treated as an empty value for the purposes of conditionals. +* `MO_OPEN_DELIMITER` - The string used when opening a tag. Defaults to "{{". Used internally. +* `MO_OPEN_DELIMITER_DEFAULT` - The default value of MO_OPEN_DELIMITER. Used when resetting the open delimiter, such as when parsing a partial. +* `MO_ORIGINAL_COMMAND` - Used to find the `mo` program in order to generate a help message. +* `MO_PARSED` - Content that has made it through the template engine. +* `MO_STANDALONE_CONTENT` - The unparsed content that preceeded the current tag. When a standalone tag is encountered, this is checked to see if it only contains whitespace. If this and the whitespace condition after a tag is met, then this will be reset to $'\n'. +* `MO_UNPARSED` - Template content yet to make it through the parser. + + +Concessions +----------- + +I admit that implementing everything in bash just doesn't make a lot of sense. For example, the following things just don't work because they don't really mesh with the "bash way". + +Pull requests to solve the following issues would be helpful. + + +### Mustache Syntax + +* Dotted names are supported but only for associative arrays (Bash 4). See [`demo/associative-arrays`](demo/associative-arrays) for an example. +* There's no "top level" object, so `echo '{{.}}' | ./mo` does not do anything useful. In other languages you can say the data for the template is a string and in `mo` the data is always the environment. Luckily this type of usage is rare and `{{.}}` works great when iterating over an array. +* [Parents](https://mustache.github.io/mustache.5.html#Parents), where a template can override chunks of a partial, are not supported. +* HTML encoding is not built into `mo`. `{{{var}}}`, `{{&var}}` and `{{var}}` all do the same thing. `echo '{{TEST}}' | TEST='' mo` will give you "``" instead of "`>b<`". + + +### General Scripting Issues + +* Using binary files as templates is simply not allowed. +* Bash does not support anything more complex than strings/numbers inside of associative arrays. I'm not able to add objects nor nested arrays to bash - it's just a shell after all! +* You must make sure the data is in the environment when `mo` runs. The easiest way to do that is to source `mo` in your shell script after setting up lots of other environment variables / functions. + + +Developing +---------- + +Check out the code and hack away. Please add tests to show off bugs before fixing them. New functionality should also be covered by a test. + +First, make sure you install Node.js. After that, run `npm run install-tests` to get the dependencies and the repository of YAML tests. Run `npm run test` to run the JavaScript tests. There's over 100 of them, which is great. Not all of them will pass, but that's discussed later. + +When submitting patches, make sure to run them past [ShellCheck] and ensure no problems are found. Also please use Bash 3 syntax if you are manipulating arrays. + + +### Porting and Backporting + +In case of problems, setting MO_DEBUG to a non-empty value will give you LOTS of output. + + MO_DEBUG=1 ./mo my-template + + +### Failed Specs + +It is acceptable for some of the official spec tests to fail. The spec runner has specific exclusions and overrides to test similar functionality that avoid the following issues. + + * Using `{{.}}` outside of a loop - In order to access any variable, you must use its name. In a loop, `{{.}}` will refer to the current value, but outside the loop you are unable to use this dot notation because there is no current value. + * Deeply nested data - Bash doesn't support complex data structure. Basically, just strings and arrays of strings. + * Interpolation; Multiple Calls: This fails because lambdas execute in a subshell so their output can be captured. If you want state to be preserved, you will need to write it outside of the current environment and load it again later. + * HTML Escaping - Since bash is not often executed in a web server context, it makes no sense to have the output escaped as HTML. Performing shell escaping of variables may be an option in the future if there's a demand. + * Lambdas - Function results are *not* automatically interpreted again. If you want to parse the results as Mustache content, use `mo::parse`. When they use `mo::parse`, it will use the current delimiters. + + For lambdas, these examples may help. + + ```bash + # Retrieve content into a variable. + content=$(cat) + + # Retrieve all content and do not trim newlines at the end. + content=$(cat; echo -n '.') + content=${content%.} + + # Parse content using the current delimiters + mo::parse results "This is my content. Hello, {{username}}" + echo -n "$results" + + # Parse content using the default delimiters + MO_OPEN_DELIMITER=$MO_OPEN_DELIMITER_DEFAULT + MO_CLOSE_DELIMITER=$MO_CLOSE_DELIMITER_DEFAULT + mo::parse results "This is my content. Hello, {{username}}" + echo -n "$results" + ``` + + +### Future Enhancements + +There's a few places in the code marked with `TODO` to signify areas that could use improvement. Care to help? Keep in mind that this uses bash exclusively, so it might not look the prettiest. + + +License +------- + +This program is licensed under an MIT license with an additional non-advertising clause. See [LICENSE.md](LICENSE.md) for the full text. + + +[Mustache]: https://mustache.github.io/ +[ShellCheck]: https://github.com/koalaman/shellcheck diff --git a/demo/associative-arrays b/demo/associative-arrays new file mode 100755 index 0000000..f0d8950 --- /dev/null +++ b/demo/associative-arrays @@ -0,0 +1,19 @@ +#!/usr/bin/env bash + +cd "$(dirname "$0")" # Go to the script's directory + +declare -A DATA +export DATA=([one]=111 [two]=222) +. ../mo + +cat < partial}} + +And here's the same partial with a 4-space indent: + + {{> partial}} + +:-) +EOF diff --git a/demo/partial b/demo/partial new file mode 100644 index 0000000..367d1bb --- /dev/null +++ b/demo/partial @@ -0,0 +1 @@ +{{data}} diff --git a/demo/sourcing b/demo/sourcing new file mode 100755 index 0000000..c5fe201 --- /dev/null +++ b/demo/sourcing @@ -0,0 +1,13 @@ +#!/usr/bin/env bash +# +# This sources a simple script with the env. variables needed for the template. + +cd "$(dirname "$0")" # Go to the script's directory + +cat <&2 + exit 1 + fi + ;; + + -o=* | --open=*) + if [[ "$arg" == --open=* ]]; then + MO_OPEN_DELIMITER_DEFAULT="${arg#--open=}" + else + MO_OPEN_DELIMITER_DEFAULT="${arg#-o=}" + fi + ;; + + -c=* | --close=*) + if [[ "$arg" == --close=* ]]; then + MO_CLOSE_DELIMITER_DEFAULT="${arg#--close=}" + else + MO_CLOSE_DELIMITER_DEFAULT="${arg#-c=}" + fi + ;; + + -d | --debug) + MO_DEBUG=true + ;; + + --) + #: Set a flag indicating we've encountered double hyphens + moDoubleHyphens=true + ;; + + -*) + mo::error "Unknown option: $arg (See --help for options)" + ;; + + *) + #: Every arg that is not a flag or a option should be a file + moFiles=(${moFiles[@]+"${moFiles[@]}"} "$arg") + ;; + esac + fi + done + fi + + mo::debug "Debug enabled" + MO_OPEN_DELIMITER="$MO_OPEN_DELIMITER_DEFAULT" + MO_CLOSE_DELIMITER="$MO_CLOSE_DELIMITER_DEFAULT" + mo::content moContent ${moFiles[@]+"${moFiles[@]}"} || return 1 + mo::parse moParsed "$moContent" + echo -n "$moParsed" +) + + +# Internal: Show a debug message +# +# $1 - The debug message to show +# +# Returns nothing. +mo::debug() { + if [[ -n "${MO_DEBUG:-}" ]]; then + echo "DEBUG ${FUNCNAME[1]:-?} - $1" >&2 + fi +} + + +# Internal: Show a debug message and internal state information +# +# No arguments +# +# Returns nothing. +mo::debugShowState() { + if [[ -z "${MO_DEBUG:-}" ]]; then + return + fi + + local moState moTemp moIndex moDots + + mo::escape moTemp "$MO_OPEN_DELIMITER" + moState="open: $moTemp" + mo::escape moTemp "$MO_CLOSE_DELIMITER" + moState="$moState close: $moTemp" + mo::escape moTemp "$MO_STANDALONE_CONTENT" + moState="$moState standalone: $moTemp" + mo::escape moTemp "$MO_CURRENT" + moState="$moState current: $moTemp" + moIndex=$((${#MO_PARSED} - 20)) + moDots=... + + if [[ "$moIndex" -lt 0 ]]; then + moIndex=0 + moDots= + fi + + mo::escape moTemp "${MO_PARSED:$moIndex}" + moState="$moState parsed: $moDots$moTemp" + + moDots=... + + if [[ "${#MO_UNPARSED}" -le 20 ]]; then + moDots= + fi + + mo::escape moTemp "${MO_UNPARSED:0:20}$moDots" + moState="$moState unparsed: $moTemp" + + echo "DEBUG ${FUNCNAME[1]:-?} - $moState" >&2 +} + +# Internal: Show an error message and exit +# +# $1 - The error message to show +# $2 - Error code +# +# Returns nothing. Exits the program. +mo::error() { + echo "ERROR: $1" >&2 + exit "${2:-1}" +} + + +# Internal: Show an error message with a snippet of context and exit +# +# $1 - The error message to show +# $2 - The starting point +# $3 - Error code +# +# Returns nothing. Exits the program. +mo::errorNear() { + local moEscaped + + mo::escape moEscaped "${2:0:40}" + echo "ERROR: $1" >&2 + echo "ERROR STARTS NEAR: $moEscaped" + exit "${3:-1}" +} + + +# Internal: Displays the usage for mo. Pulls this from the file that +# contained the `mo` function. Can only work when the right filename +# comes is the one argument, and that only happens when `mo` is called +# with `$0` set to this file. +# +# $1 - Filename that has the help message +# +# Returns nothing. +mo::usage() { + while read -r line; do + if [[ "${line:0:2}" == "#/" ]]; then + echo "${line:3}" + fi + done < "$MO_ORIGINAL_COMMAND" + echo "" + echo "MO_VERSION=$MO_VERSION" +} + + +# Internal: Fetches the content to parse into MO_UNPARSED. Can be a list of +# partials for files or the content from stdin. +# +# $1 - Destination variable name +# $2-@ - File names (optional), read from stdin otherwise +# +# Returns nothing. +mo::content() { + local moTarget moContent moFilename + + moTarget=$1 + shift + moContent="" + + if [[ "${#@}" -gt 0 ]]; then + for moFilename in "$@"; do + mo::debug "Using template to load content from file: $moFilename" + #: This is so relative paths work from inside template files + moContent="$moContent$MO_OPEN_DELIMITER>$moFilename$MO_CLOSE_DELIMITER" + done + else + mo::debug "Will read content from stdin" + mo::contentFile moContent || return 1 + fi + + local "$moTarget" && mo::indirect "$moTarget" "$moContent" +} + + +# Internal: Read a file into MO_UNPARSED. +# +# $1 - Destination variable name. +# $2 - Filename to load - if empty, defaults to /dev/stdin +# +# Returns nothing. +mo::contentFile() { + local moFile moResult moContent + + #: The subshell removes any trailing newlines. We forcibly add + #: a dot to the content to preserve all newlines. Reading from + #: stdin with a `read` loop does not work as expected, so `cat` + #: needs to stay. + moFile=${2:-/dev/stdin} + + if [[ -e "$moFile" ]]; then + mo::debug "Loading content: $moFile" + moContent=$( + set +Ee + cat -- "$moFile" + moResult=$? + echo -n '.' + exit "$moResult" + ) || return 1 + moContent=${moContent%.} #: Remove last dot + elif [[ -n "${MO_FAIL_ON_FILE-}" ]]; then + mo::error "No such file: $moFile" + else + mo::debug "File does not exist: $moFile" + moContent="" + fi + + local "$1" && mo::indirect "$1" "$moContent" +} + + +# Internal: Send a variable up to the parent of the caller of this function. +# +# $1 - Variable name +# $2 - Value +# +# Examples +# +# callFunc () { +# local "$1" && mo::indirect "$1" "the value" +# } +# callFunc dest +# echo "$dest" # writes "the value" +# +# Returns nothing. +mo::indirect() { + unset -v "$1" + printf -v "$1" '%s' "$2" +} + + +# Internal: Send an array as a variable up to caller of a function +# +# $1 - Variable name +# $2-@ - Array elements +# +# Examples +# +# callFunc () { +# local myArray=(one two three) +# local "$1" && mo::indirectArray "$1" "${myArray[@]}" +# } +# callFunc dest +# echo "${dest[@]}" # writes "one two three" +# +# Returns nothing. +mo::indirectArray() { + unset -v "$1" + + #: IFS must be set to a string containing space or unset in order for + #: the array slicing to work regardless of the current IFS setting on + #: bash 3. This is detailed further at + #: https://github.com/fidian/gg-core/pull/7 + eval "$(printf "IFS= %s=(\"\${@:2}\") IFS=%q" "$1" "$IFS")" +} + + +# Internal: Trim leading characters from MO_UNPARSED +# +# Returns nothing. +mo::trimUnparsed() { + local moI moC + + moI=0 + moC=${MO_UNPARSED:0:1} + + while [[ "$moC" == " " || "$moC" == $'\r' || "$moC" == $'\n' || "$moC" == $'\t' ]]; do + moI=$((moI + 1)) + moC=${MO_UNPARSED:$moI:1} + done + + if [[ "$moI" != 0 ]]; then + MO_UNPARSED=${MO_UNPARSED:$moI} + fi +} + + +# Internal: Remove whitespace and content after whitespace +# +# $1 - Name of the destination variable +# $2 - The string to chomp +# +# Returns nothing. +mo::chomp() { + local moTemp moR moN moT + + moR=$'\r' + moN=$'\n' + moT=$'\t' + moTemp=${2%% *} + moTemp=${moTemp%%"$moR"*} + moTemp=${moTemp%%"$moN"*} + moTemp=${moTemp%%"$moT"*} + + local "$1" && mo::indirect "$1" "$moTemp" +} + + +# Public: Parses text, interpolates mustache tags. Utilizes the current value +# of MO_OPEN_DELIMITER, MO_CLOSE_DELIMITER, and MO_STANDALONE_CONTENT. Those +# three variables shouldn't be changed by user-defined functions. +# +# $1 - Destination variable name - where to store the finished content +# $2 - Content to parse +# $3 - Preserve standalone status/content - truthy if not empty. When set to a +# value, that becomes the standalone content value +# +# Returns nothing. +mo::parse() { + local moOldParsed moOldStandaloneContent moOldUnparsed moResult + + #: The standalone content is a trick to make the standalone tag detection + #: possible. When it's set to content with a newline and if the tag supports + #: it, the standalone content check happens. This check ensures only + #: whitespace is after the last newline up to the tag, and only whitespace + #: is after the tag up to the next newline. If that is the case, remove + #: whitespace and the trailing newline. By setting this to $'\n', we're + #: saying we are at the beginning of content. + mo::debug "Starting parse of ${#2} bytes" + moOldParsed=${MO_PARSED:-} + moOldUnparsed=${MO_UNPARSED:-} + MO_PARSED="" + MO_UNPARSED="$2" + + if [[ -z "${3:-}" ]]; then + moOldStandaloneContent=${MO_STANDALONE_CONTENT:-} + MO_STANDALONE_CONTENT=$'\n' + else + MO_STANDALONE_CONTENT=$3 + fi + + MO_CURRENT=${MO_CURRENT:-} + mo::parseInternal + moResult="$MO_PARSED$MO_UNPARSED" + MO_PARSED=$moOldParsed + MO_UNPARSED=$moOldUnparsed + + if [[ -z "${3:-}" ]]; then + MO_STANDALONE_CONTENT=$moOldStandaloneContent + fi + + local "$1" && mo::indirect "$1" "$moResult" +} + + +# Internal: Parse MO_UNPARSED, writing content to MO_PARSED. Interpolates +# mustache tags. +# +# No arguments +# +# Returns nothing. +mo::parseInternal() { + local moChunk + + mo::debug "Starting parse" + + while [[ -n "$MO_UNPARSED" ]]; do + mo::debugShowState + moChunk=${MO_UNPARSED%%"$MO_OPEN_DELIMITER"*} + MO_PARSED="$MO_PARSED$moChunk" + MO_STANDALONE_CONTENT="$MO_STANDALONE_CONTENT$moChunk" + MO_UNPARSED=${MO_UNPARSED:${#moChunk}} + + if [[ -n "$MO_UNPARSED" ]]; then + MO_UNPARSED=${MO_UNPARSED:${#MO_OPEN_DELIMITER}} + mo::trimUnparsed + + case "$MO_UNPARSED" in + '#'*) + #: Loop, if/then, or pass content through function + mo::parseBlock false + ;; + + '^'*) + #: Display section if named thing does not exist + mo::parseBlock true + ;; + + '>'*) + #: Load partial - get name of file relative to cwd + mo::parsePartial + ;; + + '/'*) + #: Closing tag + mo::errorNear "Unbalanced close tag" "$MO_UNPARSED" + ;; + + '!'*) + #: Comment - ignore the tag content entirely + mo::parseComment + ;; + + '='*) + #: Change delimiters + #: Any two non-whitespace sequences separated by whitespace. + mo::parseDelimiter + ;; + + '&'*) + #: Unescaped - mo doesn't escape/unescape + MO_UNPARSED=${MO_UNPARSED#&} + mo::trimUnparsed + mo::parseValue + ;; + + *) + #: Normal environment variable, string, subexpression, + #: current value, key, or function call + mo::parseValue + ;; + esac + fi + done +} + + +# Internal: Handle parsing a block +# +# $1 - Invert condition ("true" or "false") +# +# Returns nothing +mo::parseBlock() { + local moInvertBlock moTokens moTokensString + + moInvertBlock=$1 + MO_UNPARSED=${MO_UNPARSED:1} + mo::tokenizeTagContents moTokens "$MO_CLOSE_DELIMITER" + MO_UNPARSED=${MO_UNPARSED#"$MO_CLOSE_DELIMITER"} + mo::tokensToString moTokensString "${moTokens[@]:1}" + mo::debug "Parsing block: $moTokensString" + + if mo::standaloneCheck; then + mo::standaloneProcess + fi + + if [[ "${moTokens[1]}" == "NAME" ]] && mo::isFunction "${moTokens[2]}"; then + mo::parseBlockFunction "$moInvertBlock" "$moTokensString" "${moTokens[@]:1}" + elif [[ "${moTokens[1]}" == "NAME" ]] && mo::isArray "${moTokens[2]}"; then + mo::parseBlockArray "$moInvertBlock" "$moTokensString" "${moTokens[@]:1}" + else + mo::parseBlockValue "$moInvertBlock" "$moTokensString" "${moTokens[@]:1}" + fi +} + + +# Internal: Handle parsing a block whose first argument is a function +# +# $1 - Invert condition ("true" or "false") +# $2-@ - The parsed tokens from inside the block tags +# +# Returns nothing +mo::parseBlockFunction() { + local moTarget moInvertBlock moTokens moTemp moUnparsed moTokensString + + moInvertBlock=$1 + moTokensString=$2 + shift 2 + moTokens=(${@+"$@"}) + mo::debug "Parsing block function: $moTokensString" + mo::getContentUntilClose moTemp "$moTokensString" + #: Pass unparsed content to the function. + #: Keep the updated delimiters if they changed. + + if [[ "$moInvertBlock" != "true" ]]; then + mo::evaluateFunction moResult "$moTemp" "${moTokens[@]:1}" + MO_PARSED="$MO_PARSED$moResult" + fi + + mo::debug "Done parsing block function: $moTokensString" +} + + +# Internal: Handle parsing a block whose first argument is an array +# +# $1 - Invert condition ("true" or "false") +# $2-@ - The parsed tokens from inside the block tags +# +# Returns nothing +mo::parseBlockArray() { + local moInvertBlock moTokens moResult moArrayName moArrayIndexes moArrayIndex moTemp moUnparsed moOpenDelimiterBefore moCloseDelimiterBefore moOpenDelimiterAfter moCloseDelimiterAfter moParsed moTokensString moCurrent + + moInvertBlock=$1 + moTokensString=$2 + shift 2 + moTokens=(${@+"$@"}) + mo::debug "Parsing block array: $moTokensString" + moOpenDelimiterBefore=$MO_OPEN_DELIMITER + moCloseDelimiterBefore=$MO_CLOSE_DELIMITER + mo::getContentUntilClose moTemp "$moTokensString" + moOpenDelimiterAfter=$MO_OPEN_DELIMITER + moCloseDelimiterAfter=$MO_CLOSE_DELIMITER + moArrayName=${moTokens[1]} + eval "moArrayIndexes=(\"\${!${moArrayName}[@]}\")" + + if [[ "${#moArrayIndexes[@]}" -lt 1 ]]; then + #: No elements + if [[ "$moInvertBlock" == "true" ]]; then + #: Restore the delimiter before parsing + MO_OPEN_DELIMITER=$moOpenDelimiterBefore + MO_CLOSE_DELIMITER=$moCloseDelimiterBefore + moCurrent=$MO_CURRENT + MO_CURRENT=$moArrayName + mo::parse moParsed "$moTemp" "blockArrayInvert$MO_STANDALONE_CONTENT" + MO_CURRENT=$moCurrent + MO_PARSED="$MO_PARSED$moParsed" + fi + else + if [[ "$moInvertBlock" != "true" ]]; then + #: Process for each element in the array + moUnparsed=$MO_UNPARSED + + for moArrayIndex in "${moArrayIndexes[@]}"; do + #: Restore the delimiter before parsing + MO_OPEN_DELIMITER=$moOpenDelimiterBefore + MO_CLOSE_DELIMITER=$moCloseDelimiterBefore + moCurrent=$MO_CURRENT + MO_CURRENT=$moArrayName.$moArrayIndex + mo::debug "Iterate over array using element: $MO_CURRENT" + mo::parse moParsed "$moTemp" "blockArray$MO_STANDALONE_CONTENT" + MO_CURRENT=$moCurrent + MO_PARSED="$MO_PARSED$moParsed" + done + + MO_UNPARSED=$moUnparsed + fi + fi + + MO_OPEN_DELIMITER=$moOpenDelimiterAfter + MO_CLOSE_DELIMITER=$moCloseDelimiterAfter + mo::debug "Done parsing block array: $moTokensString" +} + + +# Internal: Handle parsing a block whose first argument is a value +# +# $1 - Invert condition ("true" or "false") +# $2-@ - The parsed tokens from inside the block tags +# +# Returns nothing +mo::parseBlockValue() { + local moInvertBlock moTokens moResult moUnparsed moOpenDelimiterBefore moOpenDelimiterAfter moCloseDelimiterBefore moCloseDelimiterAfter moParsed moTemp moTokensString moCurrent + + moInvertBlock=$1 + moTokensString=$2 + shift 2 + moTokens=(${@+"$@"}) + mo::debug "Parsing block value: $moTokensString" + moOpenDelimiterBefore=$MO_OPEN_DELIMITER + moCloseDelimiterBefore=$MO_CLOSE_DELIMITER + mo::getContentUntilClose moTemp "$moTokensString" + moOpenDelimiterAfter=$MO_OPEN_DELIMITER + moCloseDelimiterAfter=$MO_CLOSE_DELIMITER + + #: Variable, value, or list of mixed things + mo::evaluateListOfSingles moResult "${moTokens[@]}" + + if mo::isTruthy "$moResult" "$moInvertBlock"; then + mo::debug "Block is truthy: $moResult" + #: Restore the delimiter before parsing + MO_OPEN_DELIMITER=$moOpenDelimiterBefore + MO_CLOSE_DELIMITER=$moCloseDelimiterBefore + moCurrent=$MO_CURRENT + MO_CURRENT=${moTokens[1]} + mo::parse moParsed "$moTemp" "blockValue$MO_STANDALONE_CONTENT" + MO_PARSED="$MO_PARSED$moParsed" + MO_CURRENT=$moCurrent + fi + + MO_OPEN_DELIMITER=$moOpenDelimiterAfter + MO_CLOSE_DELIMITER=$moCloseDelimiterAfter + mo::debug "Done parsing block value: $moTokensString" +} + + +# Internal: Handle parsing a partial +# +# No arguments. +# +# Indentation will be applied to the entire partial's contents before parsing. +# This indentation is based on the whitespace that ends the previously parsed +# content. +# +# Returns nothing +mo::parsePartial() { + local moFilename moResult moIndentation moN moR moTemp moT + + MO_UNPARSED=${MO_UNPARSED:1} + mo::trimUnparsed + mo::chomp moFilename "${MO_UNPARSED%%"$MO_CLOSE_DELIMITER"*}" + MO_UNPARSED="${MO_UNPARSED#*"$MO_CLOSE_DELIMITER"}" + moIndentation="" + + if mo::standaloneCheck; then + moN=$'\n' + moR=$'\r' + moT=$'\t' + moIndentation="$moN${MO_PARSED//"$moR"/"$moN"}" + moIndentation=${moIndentation##*"$moN"} + moTemp=${moIndentation// } + moTemp=${moTemp//"$moT"} + + if [[ -n "$moTemp" ]]; then + moIndentation= + fi + + mo::debug "Adding indentation to partial: '$moIndentation'" + mo::standaloneProcess + fi + + mo::debug "Parsing partial: $moFilename" + + #: Execute in subshell to preserve current cwd and environment + moResult=$( + #: It would be nice to remove `dirname` and use a function instead, + #: but that is difficult when only given filenames. + cd "$(dirname -- "$moFilename")" || exit 1 + echo "$( + local moPartialContent moPartialParsed + + if ! mo::contentFile moPartialContent "${moFilename##*/}"; then + exit 1 + fi + + #: Reset delimiters before parsing + mo::indentLines moPartialContent "$moIndentation" "$moPartialContent" + MO_OPEN_DELIMITER="$MO_OPEN_DELIMITER_DEFAULT" + MO_CLOSE_DELIMITER="$MO_CLOSE_DELIMITER_DEFAULT" + mo::parse moPartialParsed "$moPartialContent" + + #: Fix bash handling of subshells and keep trailing whitespace. + echo -n "$moPartialParsed." + )" || exit 1 + ) || exit 1 + + if [[ -z "$moResult" ]]; then + mo::debug "Error detected when trying to read the file" + exit 1 + fi + + MO_PARSED="$MO_PARSED${moResult%.}" +} + + +# Internal: Handle parsing a comment +# +# No arguments. +# +# Returns nothing +mo::parseComment() { + local moContent moContent + + MO_UNPARSED=${MO_UNPARSED#*"$MO_CLOSE_DELIMITER"} + mo::debug "Parsing comment" + + if mo::standaloneCheck; then + mo::standaloneProcess + fi +} + + +# Internal: Handle parsing the change of delimiters +# +# No arguments. +# +# Returns nothing +mo::parseDelimiter() { + local moContent moOpen moClose + + MO_UNPARSED=${MO_UNPARSED:1} + mo::trimUnparsed + mo::chomp moOpen "$MO_UNPARSED" + MO_UNPARSED=${MO_UNPARSED:${#moOpen}} + mo::trimUnparsed + mo::chomp moClose "${MO_UNPARSED%%="$MO_CLOSE_DELIMITER"*}" + MO_UNPARSED=${MO_UNPARSED#*="$MO_CLOSE_DELIMITER"} + mo::debug "Parsing delimiters: $moOpen $moClose" + + if mo::standaloneCheck; then + mo::standaloneProcess + fi + + MO_OPEN_DELIMITER="$moOpen" + MO_CLOSE_DELIMITER="$moClose" +} + + +# Internal: Handle parsing value or function call +# +# No arguments. +# +# Returns nothing +mo::parseValue() { + local moUnparsedOriginal moTokens + + moUnparsedOriginal=$MO_UNPARSED + mo::tokenizeTagContents moTokens "$MO_CLOSE_DELIMITER" + mo::evaluate moResult "${moTokens[@]:1}" + MO_PARSED="$MO_PARSED$moResult" + + if [[ "${MO_UNPARSED:0:${#MO_CLOSE_DELIMITER}}" != "$MO_CLOSE_DELIMITER" ]]; then + mo::errorNear "Did not find closing tag" "$moUnparsedOriginal" + fi + + if mo::standaloneCheck; then + mo::standaloneProcess + fi + + MO_UNPARSED=${MO_UNPARSED:${#MO_CLOSE_DELIMITER}} +} + + +# Internal: Determine if the given name is a defined function. +# +# $1 - Function name to check +# +# Be extremely careful. Even if strict mode is enabled, it is not honored +# in newer versions of Bash. Any errors that crop up here will not be +# caught automatically. +# +# Examples +# +# moo () { +# echo "This is a function" +# } +# if mo::isFunction moo; then +# echo "moo is a defined function" +# fi +# +# Returns 0 if the name is a function, 1 otherwise. +mo::isFunction() { + local moFunctionName + + for moFunctionName in "${MO_FUNCTION_CACHE_HIT[@]}"; do + if [[ "$moFunctionName" == "$1" ]]; then + return 0 + fi + done + + for moFunctionName in "${MO_FUNCTION_CACHE_MISS[@]}"; do + if [[ "$moFunctionName" == "$1" ]]; then + return 1 + fi + done + + if declare -F "$1" &> /dev/null; then + MO_FUNCTION_CACHE_HIT=( ${MO_FUNCTION_CACHE_HIT[@]+"${MO_FUNCTION_CACHE_HIT[@]}"} "$1" ) + + return 0 + fi + + MO_FUNCTION_CACHE_MISS=( ${MO_FUNCTION_CACHE_MISS[@]+"${MO_FUNCTION_CACHE_MISS[@]}"} "$1" ) + + return 1 +} + + +# Internal: Determine if a given environment variable exists and if it is +# an array. +# +# $1 - Name of environment variable +# +# Be extremely careful. Even if strict mode is enabled, it is not honored +# in newer versions of Bash. Any errors that crop up here will not be +# caught automatically. +# +# Examples +# +# var=(abc) +# if moIsArray var; then +# echo "This is an array" +# echo "Make sure you don't accidentally use \$var" +# fi +# +# Returns 0 if the name is not empty, 1 otherwise. +mo::isArray() { + #: Namespace this variable so we don't conflict with what we're testing. + local moTestResult + + moTestResult=$(declare -p "$1" 2>/dev/null) || return 1 + [[ "${moTestResult:0:10}" == "declare -a" ]] && return 0 + [[ "${moTestResult:0:10}" == "declare -A" ]] && return 0 + + return 1 +} + + +# Internal: Determine if an array index exists. +# +# $1 - Variable name to check +# $2 - The index to check +# +# Has to check if the variable is an array and if the index is valid for that +# type of array. +# +# Returns true (0) if everything was ok, 1 if there's any condition that fails. +mo::isArrayIndexValid() { + local moDeclare moTest + + moDeclare=$(declare -p "$1") + moTest="" + + if [[ "${moDeclare:0:10}" == "declare -a" ]]; then + #: Numerically indexed array - must check if the index looks like a + #: number because using a string to index a numerically indexed array + #: will appear like it worked. + if [[ "$2" == "0" ]] || [[ "$2" =~ ^[1-9][0-9]*$ ]]; then + #: Index looks like a number + eval "moTest=\"\${$1[$2]+ok}\"" + fi + elif [[ "${moDeclare:0:10}" == "declare -A" ]]; then + #: Associative array + eval "moTest=\"\${$1[$2]+ok}\"" + fi + + if [[ -n "$moTest" ]]; then + return 0; + fi + + return 1 +} + + +# Internal: Determine if a variable is assigned, even if it is assigned an empty +# value. +# +# $1 - Variable name to check. +# +# Can not use logic like this in case invalid variable names are passed. +# [[ "${!1-a}" == "${!1-b}" ]] +# +# Using logic like this gives false positives. +# [[ -v "$a" ]] +# +# Declaring a variable is not the same as assigning the variable. +# export x +# declare -p x # Output: declare -x x +# export y="" +# declare -p y # Output: declare -x y="" +# unset z +# declare -p z # Error code 1 and output: bash: declare: z: not found +# +# Returns true (0) if the variable is set, 1 if the variable is unset. +mo::isVarSet() { + if declare -p "$1" &> /dev/null && [[ -v "$1" ]]; then + return 0 + fi + + return 1 +} + + +# Internal: Determine if a value is considered truthy. +# +# $1 - The value to test +# $2 - Invert the value, either "true" or "false" +# +# Returns true (0) if truthy, 1 otherwise. +mo::isTruthy() { + local moTruthy + + moTruthy=true + + if [[ -z "${1-}" ]]; then + moTruthy=false + elif [[ -n "${MO_FALSE_IS_EMPTY-}" ]] && [[ "${1-}" == "false" ]]; then + moTruthy=false + fi + + #: XOR the results + #: moTruthy inverse desiredResult + #: true false true + #: true true false + #: false false false + #: false true true + if [[ "$moTruthy" == "$2" ]]; then + mo::debug "Value is falsy, test result: $moTruthy inverse: $2" + return 1 + fi + + mo::debug "Value is truthy, test result: $moTruthy inverse: $2" + return 0 +} + + +# Internal: Convert token list to values +# +# $1 - Destination variable name +# $2-@ - Tokens to convert +# +# Sample call: +# +# mo::evaluate dest NAME username VALUE abc123 PAREN 2 +# +# Returns nothing. +mo::evaluate() { + local moTarget moStack moValue moType moIndex moCombined moResult + + moTarget=$1 + shift + + #: Phase 1 - remove all command tokens (PAREN, BRACE) + moStack=() + + while [[ $# -gt 0 ]]; do + case "$1" in + PAREN|BRACE) + moType=$1 + moValue=$2 + mo::debug "Combining $moValue tokens" + moIndex=$((${#moStack[@]} - (2 * moValue))) + mo::evaluateListOfSingles moCombined "${moStack[@]:$moIndex}" + + if [[ "$moType" == "PAREN" ]]; then + moStack=("${moStack[@]:0:$moIndex}" NAME "$moCombined") + else + moStack=("${moStack[@]:0:$moIndex}" VALUE "$moCombined") + fi + ;; + + *) + moStack=(${moStack[@]+"${moStack[@]}"} "$1" "$2") + ;; + esac + + shift 2 + done + + #: Phase 2 - check if this is a function or if we should just concatenate values + if [[ "${moStack[0]:-}" == "NAME" ]] && mo::isFunction "${moStack[1]}"; then + #: Special case - if the first argument is a function, then the rest are + #: passed to the function. + mo::debug "Evaluating function: ${moStack[1]}" + mo::evaluateFunction moResult "" "${moStack[@]:1}" + else + #: Concatenate + mo::debug "Concatenating ${#moStack[@]} stack items" + mo::evaluateListOfSingles moResult ${moStack[@]+"${moStack[@]}"} + fi + + local "$moTarget" && mo::indirect "$moTarget" "$moResult" +} + + +# Internal: Convert an argument list to individual values. +# +# $1 - Destination variable name +# $2-@ - A list of argument types and argument name/value. +# +# This assumes each value is separate from the rest. In contrast, mo::evaluate +# will pass all arguments to a function if the first value is a function. +# +# Sample call: +# +# mo::evaluateListOfSingles dest NAME username VALUE abc123 +# +# Returns nothing. +mo::evaluateListOfSingles() { + local moResult moTarget moTemp + + moTarget=$1 + shift + moResult="" + + while [[ $# -gt 1 ]]; do + mo::evaluateSingle moTemp "$1" "$2" + moResult="$moResult$moTemp" + shift 2 + done + + mo::debug "Evaluated list of singles: $moResult" + + local "$moTarget" && mo::indirect "$moTarget" "$moResult" +} + + +# Internal: Evaluate a single argument +# +# $1 - Name of variable for result +# $2 - Type of argument, either NAME or VALUE +# $3 - Argument +# +# Returns nothing +mo::evaluateSingle() { + local moResult moType moArg + + moType=$2 + moArg=$3 + mo::debug "Evaluating $moType: $moArg ($MO_CURRENT)" + + if [[ "$moType" == "VALUE" ]]; then + moResult=$moArg + elif [[ "$moArg" == "." ]]; then + mo::evaluateVariable moResult "" + elif [[ "$moArg" == "@key" ]]; then + mo::evaluateKey moResult + elif mo::isFunction "$moArg"; then + mo::evaluateFunction moResult "" "$moArg" + else + mo::evaluateVariable moResult "$moArg" + fi + + local "$1" && mo::indirect "$1" "$moResult" +} + + +# Internal: Return the value for @key based on current's name +# +# $1 - Name of variable for result +# +# Returns nothing +mo::evaluateKey() { + local moResult + + if [[ "$MO_CURRENT" == *.* ]]; then + moResult="${MO_CURRENT#*.}" + else + moResult="${MO_CURRENT}" + fi + + local "$1" && mo::indirect "$1" "$moResult" +} + + +# Internal: Handle a variable name +# +# $1 - Destination variable name +# $2 - Variable name +# +# Returns nothing. +mo::evaluateVariable() { + local moResult moArg moNameParts + + moArg=$2 + moResult="" + mo::findVariableName moNameParts "$moArg" + mo::debug "Evaluate variable ($moArg, $MO_CURRENT): ${moNameParts[*]}" + + if [[ -z "${moNameParts[1]}" ]]; then + if mo::isArray "${moNameParts[0]}"; then + eval mo::join moResult "," "\${${moNameParts[0]}[@]}" + else + if mo::isVarSet "${moNameParts[0]}"; then + moResult=${moNameParts[0]} + moResult="${!moResult}" + elif [[ -n "${MO_FAIL_ON_UNSET-}" ]]; then + mo::error "Environment variable not set: ${moNameParts[0]}" + fi + fi + else + if mo::isArray "${moNameParts[0]}"; then + eval "set +u;moResult=\"\${${moNameParts[0]}[${moNameParts[1]%%.*}]}\"" + else + mo::error "Unable to index a scalar as an array: $moArg" + fi + fi + + local "$1" && mo::indirect "$1" "$moResult" +} + + +# Internal: Find the name of a variable to use +# +# $1 - Destination variable name, receives an array +# $2 - Variable name from the template +# +# The array contains the following values +# [0] - Variable name +# [1] - Array index, or empty string +# +# Example variables +# a="a" +# b="b" +# c=("c.0" "c.1") +# d=([b]="d.b" [d]="d.d") +# +# Given these inputs (function input, current value), produce these outputs +# a c => a +# a c.0 => a +# b d => d.b +# b d.d => d.b +# a d => d.a +# a d.d => d.a +# c.0 d => c.0 +# d.b d => d.b +# '' c => c +# '' c.0 => c.0 +# Returns nothing. +mo::findVariableName() { + local moVar moNameParts moResultBase moResultIndex moCurrent + + moVar=$2 + moResultBase=$moVar + moResultIndex="" + + if [[ -z "$moVar" ]]; then + moResultBase=${MO_CURRENT%%.*} + + if [[ "$MO_CURRENT" == *.* ]]; then + moResultIndex=${MO_CURRENT#*.} + fi + elif [[ "$moVar" == *.* ]]; then + mo::debug "Find variable name; name has dot: $moVar" + moResultBase=${moVar%%.*} + moResultIndex=${moVar#*.} + elif [[ -n "$MO_CURRENT" ]]; then + moCurrent=${MO_CURRENT%%.*} + mo::debug "Find variable name; look in array: $moCurrent" + + if mo::isArrayIndexValid "$moCurrent" "$moVar"; then + moResultBase=$moCurrent + moResultIndex=$moVar + fi + fi + + local "$1" && mo::indirectArray "$1" "$moResultBase" "$moResultIndex" +} + + +# Internal: Join / implode an array +# +# $1 - Variable name to receive the joined content +# $2 - Joiner +# $3-@ - Elements to join +# +# Returns nothing. +mo::join() { + local joiner part result target + + target=$1 + joiner=$2 + result=$3 + shift 3 + + for part in "$@"; do + result="$result$joiner$part" + done + + local "$target" && mo::indirect "$target" "$result" +} + + +# Internal: Call a function. +# +# $1 - Variable for output +# $2 - Content to pass +# $3 - Function to call +# $4-@ - Additional arguments as list of type, value/name +# +# Returns nothing. +mo::evaluateFunction() { + local moArgs moContent moFunctionResult moTarget moFunction moTemp moFunctionCall + + moTarget=$1 + moContent=$2 + moFunction=$3 + shift 3 + moArgs=() + + while [[ $# -gt 1 ]]; do + mo::evaluateSingle moTemp "$1" "$2" + moArgs=(${moArgs[@]+"${moArgs[@]}"} "$moTemp") + shift 2 + done + + mo::escape moFunctionCall "$moFunction" + + if [[ -n "${MO_ALLOW_FUNCTION_ARGUMENTS-}" ]]; then + mo::debug "Function arguments are allowed" + + if [[ ${#moArgs[@]} -gt 0 ]]; then + for moTemp in "${moArgs[@]}"; do + mo::escape moTemp "$moTemp" + moFunctionCall="$moFunctionCall $moTemp" + done + fi + fi + + mo::debug "Calling function: $moFunctionCall" + + #: Call the function in a subshell for safety. Employ the trick to preserve + #: whitespace at the end of the output. + moContent=$( + export MO_FUNCTION_ARGS=(${moArgs[@]+"${moArgs[@]}"}) + echo -n "$moContent" | eval "$moFunctionCall ; moFunctionResult=\$? ; echo -n '.' ; exit \"\$moFunctionResult\"" + ) || { + moFunctionResult=$? + if [[ -n "${MO_FAIL_ON_FUNCTION-}" && "$moFunctionResult" != 0 ]]; then + mo::error "Function failed with status code $moFunctionResult: $moFunctionCall" "$moFunctionResult" + fi + } + + local "$moTarget" && mo::indirect "$moTarget" "${moContent%.}" +} + + +# Internal: Check if a tag appears to have only whitespace before it and after +# it on a line. There must be a new line before and there must be a newline +# after or the end of a string +# +# No arguments. +# +# Returns 0 if this is a standalone tag, 1 otherwise. +mo::standaloneCheck() { + local moContent moN moR moT + + moN=$'\n' + moR=$'\r' + moT=$'\t' + + #: Check the content before + moContent=${MO_STANDALONE_CONTENT//"$moR"/"$moN"} + + #: By default, signal to the next check that this one failed + MO_STANDALONE_CONTENT="" + + if [[ "$moContent" != *"$moN"* ]]; then + mo::debug "Not a standalone tag - no newline before" + + return 1 + fi + + moContent=${moContent##*"$moN"} + moContent=${moContent//"$moT"/} + moContent=${moContent// /} + + if [[ -n "$moContent" ]]; then + mo::debug "Not a standalone tag - non-whitespace detected before tag" + + return 1 + fi + + #: Check the content after + moContent=${MO_UNPARSED//"$moR"/"$moN"} + moContent=${moContent%%"$moN"*} + moContent=${moContent//"$moT"/} + moContent=${moContent// /} + + if [[ -n "$moContent" ]]; then + mo::debug "Not a standalone tag - non-whitespace detected after tag" + + return 1 + fi + + #: Signal to the next check that this tag removed content + MO_STANDALONE_CONTENT=$'\n' + + return 0 +} + + +# Internal: Process content before and after a tag. Remove prior whitespace up +# to the previous newline. Remove following whitespace up to and including the +# next newline. +# +# No arguments. +# +# Returns nothing. +mo::standaloneProcess() { + local moI moTemp + + mo::debug "Standalone tag - processing content before and after tag" + moI=$((${#MO_PARSED} - 1)) + mo::debug "zero done ${#MO_PARSED}" + mo::escape moTemp "$MO_PARSED" + mo::debug "$moTemp" + + while [[ "${MO_PARSED:$moI:1}" == " " || "${MO_PARSED:$moI:1}" == $'\t' ]]; do + moI=$((moI - 1)) + done + + if [[ $((moI + 1)) != "${#MO_PARSED}" ]]; then + MO_PARSED="${MO_PARSED:0:${moI}+1}" + fi + + moI=0 + + while [[ "${MO_UNPARSED:${moI}:1}" == " " || "${MO_UNPARSED:${moI}:1}" == $'\t' ]]; do + moI=$((moI + 1)) + done + + if [[ "${MO_UNPARSED:${moI}:1}" == $'\r' ]]; then + moI=$((moI + 1)) + fi + + if [[ "${MO_UNPARSED:${moI}:1}" == $'\n' ]]; then + moI=$((moI + 1)) + fi + + if [[ "$moI" != 0 ]]; then + MO_UNPARSED=${MO_UNPARSED:${moI}} + fi +} + + +# Internal: Apply indentation before any line that has content in MO_UNPARSED. +# +# $1 - Destination variable name. +# $2 - The indentation string. +# $3 - The content that needs the indentation string prepended on each line. +# +# Returns nothing. +mo::indentLines() { + local moContent moIndentation moResult moN moR moChunk + + moIndentation=$2 + moContent=$3 + + if [[ -z "$moIndentation" ]]; then + mo::debug "Not applying indentation, empty indentation" + + local "$1" && mo::indirect "$1" "$moContent" + return + fi + + if [[ -z "$moContent" ]]; then + mo::debug "Not applying indentation, empty contents" + + local "$1" && mo::indirect "$1" "$moContent" + return + fi + + moResult= + moN=$'\n' + moR=$'\r' + + mo::debug "Applying indentation: '${moIndentation}'" + + while [[ -n "$moContent" ]]; do + moChunk=${moContent%%"$moN"*} + moChunk=${moChunk%%"$moR"*} + moContent=${moContent:${#moChunk}} + + if [[ -n "$moChunk" ]]; then + moResult="$moResult$moIndentation$moChunk" + fi + + moResult="$moResult${moContent:0:1}" + moContent=${moContent:1} + done + + local "$1" && mo::indirect "$1" "$moResult" +} + + +# Internal: Escape a value +# +# $1 - Destination variable name +# $2 - Value to escape +# +# Returns nothing +mo::escape() { + local moResult + + moResult=$2 + moResult=$(declare -p moResult) + moResult=${moResult#*=} + + local "$1" && mo::indirect "$1" "$moResult" +} + + +# Internal: Get the content up to the end of the block by minimally parsing and +# balancing blocks. Returns the content before the end tag to the caller and +# removes the content + the end tag from MO_UNPARSED. This can change the +# delimiters, adjusting MO_OPEN_DELIMITER and MO_CLOSE_DELIMITER. +# +# $1 - Destination variable name +# $2 - Token string to match for a closing tag +# +# Returns nothing. +mo::getContentUntilClose() { + local moChunk moResult moTemp moTokensString moTokens moTarget moTagStack moResultTemp + + moTarget=$1 + moTagStack=("$2") + mo::debug "Get content until close tag: ${moTagStack[0]}" + moResult="" + + while [[ -n "$MO_UNPARSED" ]] && [[ "${#moTagStack[@]}" -gt 0 ]]; do + moChunk=${MO_UNPARSED%%"$MO_OPEN_DELIMITER"*} + moResult="$moResult$moChunk" + MO_UNPARSED=${MO_UNPARSED:${#moChunk}} + + if [[ -n "$MO_UNPARSED" ]]; then + moResultTemp="$MO_OPEN_DELIMITER" + MO_UNPARSED=${MO_UNPARSED:${#MO_OPEN_DELIMITER}} + mo::getContentTrim moTemp + moResultTemp="$moResultTemp$moTemp" + mo::debug "First character within tag: ${MO_UNPARSED:0:1}" + + case "$MO_UNPARSED" in + '#'*) + #: Increase block + moResultTemp="$moResultTemp${MO_UNPARSED:0:1}" + MO_UNPARSED=${MO_UNPARSED:1} + mo::getContentTrim moTemp + mo::getContentWithinTag moTemp "$MO_CLOSE_DELIMITER" + moResultTemp="$moResultTemp${moTemp[0]}" + moTagStack=("${moTemp[1]}" "${moTagStack[@]}") + ;; + + '^'*) + #: Increase block + moResultTemp="$moResultTemp${MO_UNPARSED:0:1}" + MO_UNPARSED=${MO_UNPARSED:1} + mo::getContentTrim moTemp + mo::getContentWithinTag moTemp "$MO_CLOSE_DELIMITER" + moResultTemp="$moResultTemp${moTemp[0]}" + moTagStack=("${moTemp[1]}" "${moTagStack[@]}") + ;; + + '>'*) + #: Partial - ignore + moResultTemp="$moResultTemp${MO_UNPARSED:0:1}" + MO_UNPARSED=${MO_UNPARSED:1} + mo::getContentTrim moTemp + mo::getContentWithinTag moTemp "$MO_CLOSE_DELIMITER" + moResultTemp="$moResultTemp${moTemp[0]}" + ;; + + '/'*) + #: Decrease block + moResultTemp="$moResultTemp${MO_UNPARSED:0:1}" + MO_UNPARSED=${MO_UNPARSED:1} + mo::getContentTrim moTemp + mo::getContentWithinTag moTemp "$MO_CLOSE_DELIMITER" + + if [[ "${moTagStack[0]}" == "${moTemp[1]}" ]]; then + moResultTemp="$moResultTemp${moTemp[0]}" + moTagStack=("${moTagStack[@]:1}") + + if [[ "${#moTagStack[@]}" -eq 0 ]]; then + #: Erase all portions of the close tag + moResultTemp="" + fi + else + mo::errorNear "Unbalanced closing tag, expected: ${moTagStack[0]}" "${moTemp[0]}${MO_UNPARSED}" + fi + ;; + + '!'*) + #: Comment - ignore + mo::getContentComment moTemp + moResultTemp="$moResultTemp$moTemp" + ;; + + '='*) + #: Change delimiters + mo::getContentDelimiter moTemp + moResultTemp="$moResultTemp$moTemp" + ;; + + '&'*) + #: Unescaped - bypass one then ignore + moResultTemp="$moResultTemp${MO_UNPARSED:0:1}" + MO_UNPARSED=${MO_UNPARSED:1} + mo::getContentTrim moTemp + moResultTemp="$moResultTemp$moTemp" + mo::getContentWithinTag moTemp "$MO_CLOSE_DELIMITER" + moResultTemp="$moResultTemp${moTemp[0]}" + ;; + + *) + #: Normal variable - ignore + mo::getContentWithinTag moTemp "$MO_CLOSE_DELIMITER" + moResultTemp="$moResultTemp${moTemp[0]}" + ;; + esac + + moResult="$moResult$moResultTemp" + fi + done + + MO_STANDALONE_CONTENT="$MO_STANDALONE_CONTENT$moResult" + + if mo::standaloneCheck; then + moResultTemp=$MO_PARSED + MO_PARSED=$moResult + mo::standaloneProcess + moResult=$MO_PARSED + MO_PARSED=$moResultTemp + fi + + local "$moTarget" && mo::indirect "$moTarget" "$moResult" +} + + +# Internal: Convert a list of tokens to a string +# +# $1 - Destination variable for the string +# $2-$@ - Token list +# +# Returns nothing. +mo::tokensToString() { + local moTarget moString moTokens + + moTarget=$1 + shift 1 + moTokens=("$@") + moString=$(declare -p moTokens) + moString=${moString#*=} + + local "$moTarget" && mo::indirect "$moTarget" "$moString" +} + + +# Internal: Trims content from MO_UNPARSED, returns trimmed content. +# +# $1 - Destination variable +# +# Returns nothing. +mo::getContentTrim() { + local moChar moResult + + moChar=${MO_UNPARSED:0:1} + moResult="" + + while [[ "$moChar" == " " ]] || [[ "$moChar" == $'\r' ]] || [[ "$moChar" == $'\t' ]] || [[ "$moChar" == $'\n' ]]; do + moResult="$moResult$moChar" + MO_UNPARSED=${MO_UNPARSED:1} + moChar=${MO_UNPARSED:0:1} + done + + local "$1" && mo::indirect "$1" "$moResult" +} + + +# Get the content up to and including a close tag +# +# $1 - Destination variable +# +# Returns nothing. +mo::getContentComment() { + local moResult + + mo::debug "Getting content for comment" + moResult=${MO_UNPARSED%%"$MO_CLOSE_DELIMITER"*} + MO_UNPARSED=${MO_UNPARSED:${#moResult}} + + if [[ "$MO_UNPARSED" == "$MO_CLOSE_DELIMITER"* ]]; then + moResult="$moResult$MO_CLOSE_DELIMITER" + MO_UNPARSED=${MO_UNPARSED#"$MO_CLOSE_DELIMITER"} + fi + + local "$1" && mo::indirect "$1" "$moResult" +} + + +# Get the content up to and including a close tag. First two non-whitespace +# tokens become the new open and close tag. +# +# $1 - Destination variable +# +# Returns nothing. +mo::getContentDelimiter() { + local moResult moTemp moOpen moClose + + mo::debug "Getting content for delimiter" + moResult="" + mo::getContentTrim moTemp + moResult="$moResult$moTemp" + mo::chomp moOpen "$MO_UNPARSED" + MO_UNPARSED="${MO_UNPARSED:${#moOpen}}" + moResult="$moResult$moOpen" + mo::getContentTrim moTemp + moResult="$moResult$moTemp" + mo::chomp moClose "${MO_UNPARSED%%="$MO_CLOSE_DELIMITER"*}" + MO_UNPARSED="${MO_UNPARSED:${#moClose}}" + moResult="$moResult$moClose" + mo::getContentTrim moTemp + moResult="$moResult$moTemp" + MO_OPEN_DELIMITER="$moOpen" + MO_CLOSE_DELIMITER="$moClose" + + local "$1" && mo::indirect "$1" "$moResult" +} + + +# Get the content up to and including a close tag. First two non-whitespace +# tokens become the new open and close tag. +# +# $1 - Destination variable, an array +# $2 - Terminator string +# +# The array contents: +# [0] The raw content within the tag +# [1] The parsed tokens as a single string +# +# Returns nothing. +mo::getContentWithinTag() { + local moUnparsed moTokens + + moUnparsed=${MO_UNPARSED} + mo::tokenizeTagContents moTokens "$MO_CLOSE_DELIMITER" + MO_UNPARSED=${MO_UNPARSED#"$MO_CLOSE_DELIMITER"} + mo::tokensToString moTokensString "${moTokens[@]:1}" + moParsed=${moUnparsed:0:$((${#moUnparsed} - ${#MO_UNPARSED}))} + + local "$1" && mo::indirectArray "$1" "$moParsed" "$moTokensString" +} + + +# Internal: Parse MO_UNPARSED and retrieve the content within the tag +# delimiters. Converts everything into an array of string values. +# +# $1 - Destination variable for the array of contents. +# $2 - Stop processing when this content is found. +# +# The list of tokens are in RPN form. The first item in the resulting array is +# the number of actual tokens (after combining command tokens) in the list. +# +# Given: a 'bc' "de\"\n" (f {g 'h'}) +# Result: ([0]=4 [1]=NAME [2]=a [3]=VALUE [4]=bc [5]=VALUE [6]=$'de\"\n' +# [7]=NAME [8]=f [9]=NAME [10]=g [11]=VALUE [12]=h +# [13]=BRACE [14]=2 [15]=PAREN [16]=2 +# +# Returns nothing +mo::tokenizeTagContents() { + local moResult moTerminator moTemp moUnparsedOriginal moTokenCount + + moTerminator=$2 + moResult=() + moUnparsedOriginal=$MO_UNPARSED + moTokenCount=0 + mo::debug "Tokenizing tag contents until terminator: $moTerminator" + + while true; do + mo::trimUnparsed + + case "$MO_UNPARSED" in + "") + mo::errorNear "Did not find matching terminator: $moTerminator" "$moUnparsedOriginal" + ;; + + "$moTerminator"*) + mo::debug "Found terminator" + local "$1" && mo::indirectArray "$1" "$moTokenCount" ${moResult[@]+"${moResult[@]}"} + return + ;; + + '('*) + #: Do not tokenize the open paren - treat this as RPL + MO_UNPARSED=${MO_UNPARSED:1} + mo::tokenizeTagContents moTemp ')' + moResult=(${moResult[@]+"${moResult[@]}"} "${moTemp[@]:1}" PAREN "${moTemp[0]}") + MO_UNPARSED=${MO_UNPARSED:1} + ;; + + '{'*) + #: Do not tokenize the open brace - treat this as RPL + MO_UNPARSED=${MO_UNPARSED:1} + mo::tokenizeTagContents moTemp '}' + moResult=(${moResult[@]+"${moResult[@]}"} "${moTemp[@]:1}" BRACE "${moTemp[0]}") + MO_UNPARSED=${MO_UNPARSED:1} + ;; + + ')'* | '}'*) + mo::errorNear "Unbalanced closing parenthesis or brace" "$MO_UNPARSED" + ;; + + "'"*) + mo::tokenizeTagContentsSingleQuote moTemp + moResult=(${moResult[@]+"${moResult[@]}"} "${moTemp[@]}") + ;; + + '"'*) + mo::tokenizeTagContentsDoubleQuote moTemp + moResult=(${moResult[@]+"${moResult[@]}"} "${moTemp[@]}") + ;; + + *) + mo::tokenizeTagContentsName moTemp + moResult=(${moResult[@]+"${moResult[@]}"} "${moTemp[@]}") + ;; + esac + + mo::debug "Got chunk: ${moTemp[0]} ${moTemp[1]}" + moTokenCount=$((moTokenCount + 1)) + done +} + + +# Internal: Get the contents of a variable name. +# +# $1 - Destination variable name for the token list (array of strings) +# +# Returns nothing +mo::tokenizeTagContentsName() { + local moTemp + + mo::chomp moTemp "${MO_UNPARSED%%"$MO_CLOSE_DELIMITER"*}" + moTemp=${moTemp%%(*} + moTemp=${moTemp%%)*} + moTemp=${moTemp%%\{*} + moTemp=${moTemp%%\}*} + MO_UNPARSED=${MO_UNPARSED:${#moTemp}} + mo::trimUnparsed + mo::debug "Parsed default token: $moTemp" + + local "$1" && mo::indirectArray "$1" "NAME" "$moTemp" +} + + +# Internal: Get the contents of a tag in double quotes. Parses the backslash +# sequences. +# +# $1 - Destination variable name for the token list (array of strings) +# +# Returns nothing. +mo::tokenizeTagContentsDoubleQuote() { + local moResult moUnparsedOriginal + + moUnparsedOriginal=$MO_UNPARSED + MO_UNPARSED=${MO_UNPARSED:1} + moResult= + mo::debug "Getting double quoted tag contents" + + while true; do + if [[ -z "$MO_UNPARSED" ]]; then + mo::errorNear "Unbalanced double quote" "$moUnparsedOriginal" + fi + + case "$MO_UNPARSED" in + '"'*) + MO_UNPARSED=${MO_UNPARSED:1} + local "$1" && mo::indirectArray "$1" "VALUE" "$moResult" + return + ;; + + \\b*) + moResult="$moResult"$'\b' + MO_UNPARSED=${MO_UNPARSED:2} + ;; + + \\e*) + #: Note, \e is ESC, but in Bash $'\E' is ESC. + moResult="$moResult"$'\E' + MO_UNPARSED=${MO_UNPARSED:2} + ;; + + \\f*) + moResult="$moResult"$'\f' + MO_UNPARSED=${MO_UNPARSED:2} + ;; + + \\n*) + moResult="$moResult"$'\n' + MO_UNPARSED=${MO_UNPARSED:2} + ;; + + \\r*) + moResult="$moResult"$'\r' + MO_UNPARSED=${MO_UNPARSED:2} + ;; + + \\t*) + moResult="$moResult"$'\t' + MO_UNPARSED=${MO_UNPARSED:2} + ;; + + \\v*) + moResult="$moResult"$'\v' + MO_UNPARSED=${MO_UNPARSED:2} + ;; + + \\*) + moResult="$moResult${MO_UNPARSED:1:1}" + MO_UNPARSED=${MO_UNPARSED:2} + ;; + + *) + moResult="$moResult${MO_UNPARSED:0:1}" + MO_UNPARSED=${MO_UNPARSED:1} + ;; + esac + done +} + + +# Internal: Get the contents of a tag in single quotes. Only gets the raw +# value. +# +# $1 - Destination variable name for the token list (array of strings) +# +# Returns nothing. +mo::tokenizeTagContentsSingleQuote() { + local moResult moUnparsedOriginal + + moUnparsedOriginal=$MO_UNPARSED + MO_UNPARSED=${MO_UNPARSED:1} + moResult= + mo::debug "Getting single quoted tag contents" + + while true; do + if [[ -z "$MO_UNPARSED" ]]; then + mo::errorNear "Unbalanced single quote" "$moUnparsedOriginal" + fi + + case "$MO_UNPARSED" in + "'"*) + MO_UNPARSED=${MO_UNPARSED:1} + local "$1" && mo::indirectArray "$1" VALUE "$moResult" + return + ;; + + *) + moResult="$moResult${MO_UNPARSED:0:1}" + MO_UNPARSED=${MO_UNPARSED:1} + ;; + esac + done +} + + +# Save the original command's path for usage later +MO_ORIGINAL_COMMAND="$(cd "${BASH_SOURCE[0]%/*}" || exit 1; pwd)/${BASH_SOURCE[0]##*/}" +MO_VERSION="3.0.7" + +# If sourced, load all functions. +# If executed, perform the actions as expected. +if [[ "$0" == "${BASH_SOURCE[0]}" ]] || [[ -z "${BASH_SOURCE[0]}" ]]; then + mo "$@" +fi diff --git a/run-spec b/run-spec new file mode 100755 index 0000000..132f057 --- /dev/null +++ b/run-spec @@ -0,0 +1,22 @@ +#!/usr/bin/env bash + +# Install or update the specs +if [[ ! -d spec ]]; then + git clone https://github.com/mustache/spec.git spec +else + ( + cd spec + git pull + ) +fi + +if [[ "$BASH_VERSION" == 3.* ]]; then + echo "WARNING! Specs assume you are using a version of Bash with associative arrays!" +fi + +# Actually run the specs +node run-spec.js spec/specs/*.json + +if [[ "$BASH_VERSION" == 3.* ]]; then + echo "Some tests may have failed because they assume Bash supports associative arays" +fi diff --git a/run-spec.js b/run-spec.js new file mode 100644 index 0000000..786a6ec --- /dev/null +++ b/run-spec.js @@ -0,0 +1,512 @@ +#!/usr/bin/env node + +const exec = require("child_process").exec; +const fsPromises = require("fs").promises; + +// Skip or override portions of tests. The goal is to still have as much +// coverage as possible, but skip things that Bash does not support. +// +// To skip a test, define a "skip" property and explain why the test is +// skipped. +// +// To override any test property, just define that property. It replaces the +// original property, not augmenting it. +const testOverrides = { + "Comments -> Variable Name Collision": { + // Can't use variables with exclamation points easily + data: { + comment: 4 + } + }, + "Interpolation -> Dotted Names - Arbitrary Depth": { + skip: "Not able to use more than one level of depth" + }, + "Interpolation -> Dotted Names - Broken Chain Resolution": { + data: { + a: { + b: "wrong" + }, + name: "Jim" + }, + template: '"{{a.name}}" == ""' + }, + "Interpolation -> Dotted Names - Initial Resolution": { + data: { + a: { + name: "Phil" + }, + name: "Wrong" + }, + template: "\"{{#a}}{{name}}{{/a}}\" == \"Phil\"" + }, + "Interpolation -> Implicit Iterators - Ampersand": { + skip: "HTML escaping is not supported" + }, + "Interpolation -> Implicit Iterators - Basic Interpolation": { + skip: "Can not use {{.}} outside of a loop. Need to use a variable name." + }, + "Interpolation -> Implicit Iterators - Basic Integer Interpolation": { + skip: "Can not use {{.}} outside of a loop. Need to use a variable name." + }, + "Interpolation -> Implicit Iterators - Triple Mustache": { + skip: "Can not use {{.}} outside of a loop. Need to use a variable name." + }, + "Interpolation -> HTML Escaping": { + skip: "HTML escaping is not supported" + }, + "Interpolation -> Implicit Iterators - HTML Escaping": { + skip: "HTML escaping is not supported" + }, + "Inverted -> Dotted Names - Falsey": { + data: { + a: { + b: "" + } + }, + template: '"{{^a.b}}Not Here{{/a.b}}" == "Not Here"' + }, + "Inverted -> Dotted Names - Truthy": { + data: { + a: { + b: "1" + } + }, + template: '"{{^a.b}}Not Here{{/a.b}}" == ""' + }, + "Lambdas -> Escaping": { + skip: "HTML escaping is not supported" + }, + "Lambdas -> Interpolation - Alternate Delimiters": { + skip: "There is no difference between a lamba used as a value and a lambda used as a block. Both will parse using the current delimiters." + }, + "Lambdas -> Inverted Section": { + // This one passed mostly by accident. Correcting so the test still + // tests what is was designed to illustrate. + data: { + static: "static", + lambda: { + __tag__: 'code', + bash: 'false' + } + } + }, + "Lambdas -> Interpolation": { + data: { + lambda: { + __tag__: 'code', + bash: 'echo -n "world"' + } + } + }, + "Lambdas -> Interpolation - Expansion": { + data: { + lambda: { + __tag__: 'code', + bash: 'mo::parse result "{{planet}}"; echo -n "$result"' + }, + planet: 'world' + } + }, + "Lambdas -> Interpolation - Multiple Calls": { + skip: "Calls are not cached, but they run in isolated environments, so saving a global variable does not work." + }, + "Lambdas -> Section": { + data: { + lambda: { + __tag__: 'code', + bash: 'if [[ "$(cat)" == "{{x}}" ]]; then echo -n yes; else echo -n no; fi' + }, + x: "Error!" + } + }, + "Lambdas -> Section - Alternate Delimiters": { + data: { + lambda: { + __tag__: 'code', + bash: 'local content=$(cat); mo::parse content "$content{{planet}} => |planet|$content"; echo -n "$content"' + }, + planet: 'Earth' + } + }, + "Lambdas -> Section - Expansion": { + data: { + lambda: { + __tag__: 'code', + bash: 'local content=$(cat); mo::parse content "$content{{planet}}$content"; echo -n "$content"' + }, + planet: "Earth" + } + }, + "Lambdas -> Section - Multiple Calls": { + data: { + lambda: { + __tag__: 'code', + bash: 'echo -n "__$(cat)__"' + } + } + }, + "Partials -> Recursion": { + skip: "Complex objects are not supported and context is reset to the global level, so the recursion will loop forever" + }, + "Sections -> Deeply Nested Contexts": { + skip: "Nested objects are not supported" + }, + "Sections -> Dotted Names - Broken Chains": { + // Complex objects are not supported + template: `"{{#a.b}}Here{{/a.b}}" == ""` + }, + "Sections -> Dotted Names - Falsey": { + // Complex objects are not supported + data: { a: { b: false } }, + template: `"{{#a.b}}Here{{/a.b}}" == ""` + }, + "Sections -> Dotted Names - Truthy": { + // Complex objects are not supported + data: { a: { b: true } }, + template: `"{{#a.b}}Here{{/a.b}}" == "Here"` + }, + "Sections -> Implicit Iterator - Array": { + skip: "Nested arrays are not supported" + }, + "Sections -> List": { + // Arrays of objects are not supported + data: { list: [1, 2, 3] }, + template: `"{{#list}}{{.}}{{/list}}"` + }, + "Sections -> List Context": { + skip: "Deeply nested objects are not supported" + }, + "Sections -> List Contexts": { + skip: "Deeply nested objects are not supported" + } +}; + +function specFileToName(file) { + return file + .replace(/.*\//, "") + .replace(".json", "") + .replace("~", "") + .replace(/(^|-)[a-z]/g, function (match) { + return match.toUpperCase(); + }); +} + +function processArraySequentially(array, callback) { + function processCopy() { + if (arrayCopy.length) { + const item = arrayCopy.shift(); + return Promise.resolve(item) + .then(callback) + .then((singleResult) => { + result.push(singleResult); + + return processCopy(); + }); + } else { + return Promise.resolve(result); + } + } + + const result = []; + const arrayCopy = array.slice(); + + return processCopy(); +} + +function debug(...args) { + if (process.env.DEBUG) { + console.debug(...args); + } +} + +function makeShellString(value) { + if (typeof value === "boolean") { + return value ? '"true"' : '""'; + } + + if (typeof value === "string") { + // Newlines are tricky + return value + .split(/\n/) + .map(function (chunk) { + return JSON.stringify(chunk); + }) + .join('"\n"'); + } + + if (typeof value === "number") { + return value; + } + + return "ERR_CONVERTING"; +} + +function addToEnvironmentArray(name, value) { + const result = ["("]; + value.forEach(function (subValue) { + result.push(makeShellString(subValue)); + }); + result.push(")"); + + return name + "=" + result.join(" "); +} + +function addToEnvironmentObjectConvertedToAssociativeArray(name, value) { + const values = []; + + for (const [k, v] of Object.entries(value)) { + if (typeof v === "object") { + if (v) { + // An object - abort + return `# ${name}.${k} is an object that can not be converted to an associative array`; + } + + // null + values.push(`[${k}]=`); + } else { + values.push(`[${k}]=${makeShellString(v)}`); + } + } + + return `declare -A ${name}\n${name}=(${values.join(" ")})`; +} + +function addToEnvironmentObject(name, value) { + if (!value) { + // null + return `#${name} is null`; + } + + if (value.__tag__ === "code") { + return `${name}() { ${value.bash || 'echo "NO BASH VERSION OF CODE"'}; }`; + } + + return addToEnvironmentObjectConvertedToAssociativeArray(name, value); +} + +function addToEnvironment(name, value) { + if (Array.isArray(value)) { + return addToEnvironmentArray(name, value); + } + + if (typeof value === "object") { + return addToEnvironmentObject(name, value); + } + + return `${name}=${makeShellString(value)}`; +} + +function buildScript(test) { + const script = ["#!/usr/bin/env bash"]; + Object.keys(test.data).forEach(function (name) { + script.push(addToEnvironment(name, test.data[name])); + }); + script.push(". ./mo"); + script.push("mo spec-runner/spec-template"); + script.push(""); + + return script.join("\n"); +} + +function writePartials(test) { + return processArraySequentially( + Object.keys(test.partials), + (partialName) => { + debug("Writing partial:", partialName); + + return fsPromises.writeFile( + "spec-runner/" + partialName, + test.partials[partialName] + ); + } + ); +} + +function setupEnvironment(test) { + return cleanup() + .then(() => fsPromises.mkdir("spec-runner/")) + .then(() => + fsPromises.writeFile("spec-runner/spec-script", test.script) + ) + .then(() => + fsPromises.writeFile("spec-runner/spec-template", test.template) + ) + .then(() => writePartials(test)); +} + +function executeScript(test) { + return new Promise((resolve) => { + exec( + "bash spec-runner/spec-script 2>&1", + { + timeout: 2000 + }, + (err, stdout) => { + if (err) { + test.scriptError = err.toString(); + } + + test.output = stdout; + resolve(); + } + ); + }); +} + +function cleanup() { + return fsPromises.rm("spec-runner/", { force: true, recursive: true }); +} + +function detectFailure(test) { + if (test.scriptError) { + return true; + } + + if (test.output !== test.expected) { + return true; + } + + return false; +} + +function showFailureDetails(test) { + console.log(`FAILURE: ${test.fullName}`); + console.log(""); + console.log(test.desc); + console.log(""); + console.log(JSON.stringify(test, null, 4)); +} + +function applyTestOverrides(test) { + const overrides = testOverrides[test.fullName]; + const originals = {}; + + if (!overrides) { + return; + } + + for (const [key, value] of Object.entries(overrides)) { + originals[key] = test[key]; + test[key] = value; + } + + test.overridesApplied = true; + test.valuesBeforeOverride = originals; +} + +function runTest(testSet, test) { + test.partials = test.partials || {}; + test.fullName = `${testSet.name} -> ${test.name}`; + applyTestOverrides(test); + test.script = buildScript(test); + + if (test.skip) { + debug("Skipping test:", test.fullName, `(${test.skip})`); + + return Promise.resolve(); + } + + debug("Running test:", test.fullName); + + return setupEnvironment(test) + .then(() => executeScript(test)) + .then(cleanup) + .then(() => { + test.isFailure = detectFailure(test); + + if (test.isFailure) { + showFailureDetails(test); + } else { + debug('Test pass:', test.fullName); + } + }); +} + +function processSpecFile(filename) { + debug("Read spec file:", filename); + + return fsPromises.readFile(filename, "utf8").then((fileContents) => { + const testSet = JSON.parse(fileContents); + testSet.name = specFileToName(filename); + + return processArraySequentially(testSet.tests, (test) => + runTest(testSet, test) + ).then(() => { + testSet.pass = 0; + testSet.fail = 0; + testSet.skip = 0; + testSet.passOverride = 0; + + for (const test of testSet.tests) { + if (test.isFailure) { + testSet.fail += 1; + } else if (test.skip) { + testSet.skip += 1; + } else { + testSet.pass += 1; + + if (test.overridesApplied) { + testSet.passOverride += 1; + } + } + } + console.log( + `### ${testSet.name} Results = ${testSet.pass} passed (with ${testSet.passOverride} overridden), ${testSet.fail} failed, ${testSet.skip} skipped` + ); + + return testSet; + }); + }); +} + +// 0 = node, 1 = script, 2 = file +if (process.argv.length < 3) { + console.log("Specify one or more JSON spec files on the command line"); + process.exit(); +} + +processArraySequentially(process.argv.slice(2), processSpecFile).then( + (result) => { + console.log("========================================="); + console.log(""); + console.log("Failed Test Summary"); + console.log(""); + let pass = 0, + fail = 0, + skip = 0, + total = 0, + passOverride = 0; + + for (const testSet of result) { + pass += testSet.pass; + fail += testSet.fail; + skip += testSet.skip; + total += testSet.tests.length; + passOverride += testSet.passOverride; + + console.log( + `* ${testSet.name}: ${testSet.tests.length} total, ${testSet.pass} pass (with ${passOverride} overridden), ${testSet.fail} fail, ${testSet.skip} skip` + ); + + for (const test of testSet.tests) { + if (test.isFailure) { + console.log(` * Failure: ${test.name}`); + } + } + } + + console.log(""); + console.log( + `Final result: ${total} total, ${pass} pass (with ${passOverride} overridden), ${fail} fail, ${skip} skip` + ); + + if (fail) { + process.exit(1); + } + }, + (err) => { + console.error(err); + console.error("FAILURE RUNNING SCRIPT"); + console.error("Testing artifacts are left in script-runner/ folder"); + } +); diff --git a/run-tests b/run-tests new file mode 100755 index 0000000..774d649 --- /dev/null +++ b/run-tests @@ -0,0 +1,162 @@ +#!/usr/bin/env bash +# +# Run one or more tests. +# +# Command-line usage to run all tests. +# +# ./run-tests +# +# To run only one test, run "tests/test-name". +# +# Usage within a test as a template. Source run-tests to get functions, export +# any necessary variables, then call runTest. +# +# #!/usr/bin/env bash +# cd "${0%/*}" || exit 1 +# . ../run-tests +# +# export template="This is a template" +# export expected="This is a template" +# runTest +# +# When used within the test, you control various aspects with environment +# variables or functions. +# +# - The content passed into mo is either the variable "$template" or the output +# of the function called template. +# - The expected result is either "$expected" or the function called expected. +# - The expected return code is "$returnCode" and defaults to 0. +# - The arguments to pass to mo is the array "${arguments[@]}" and defaults to (). +# +# When $MO_DEBUG is set to a non-empty value, the test does not run, but mo is +# simply executed directly. This allows for calling mo in the same manner as +# the test but does not buffer output nor expect the output to match the +# expected. +# +# When $MO_DEBUG_TEST is set to a non-empty value, the expected and actual +# results are shown using "declare -p" to provide an easier time seeing the +# differences, especially with whitespace. + +testCase() { + echo "Input: $1" + echo "Expected: $2" +} + +indirect() { + unset -v "$1" + printf -v "$1" '%s' "$2" +} + +getValue() { + local name temp len hardSpace + + name=$2 + hardSpace=" " + + if declare -f "$name" &> /dev/null; then + temp=$("$name"; echo -n "$hardSpace") + len=$((${#temp} - 1)) + + if [[ "${temp:$len}" == "$hardSpace" ]]; then + temp=${temp:0:$len} + fi + else + temp=${!name} + fi + + local "$1" && indirect "$1" "$temp" +} + +runTest() ( + local testTemplate testExpected testActual hardSpace len testReturnCode testFail + + hardSpace=" " + . ../mo + + getValue testTemplate template + getValue testExpected expected + + if [[ -n "${MO_DEBUG:-}" ]]; then + echo -n "$testTemplate" | mo ${arguments[@]+"${arguments[@]}"} 2>&1 + + return $? + fi + + testActual=$(echo -n "$testTemplate" | mo ${arguments[@]+"${arguments[@]}"} 2>&1; echo -n "$hardSpace$?") + testReturnCode=${testActual##*$hardSpace} + testActual=${testActual%$hardSpace*} + testFail=false + + if [[ "$testActual" != "$testExpected" ]]; then + echo "Failure" + echo "Expected:" + echo "$testExpected" + echo "Actual:" + echo "$testActual" + + if [[ -n "${MO_DEBUG_TEST-}" ]]; then + declare -p testExpected + # Align the two declare outputs + echo -n " " + declare -p testActual + fi + + testFail=true + fi + + if [[ "$testReturnCode" != "$returnCode" ]]; then + echo "Expected return code $returnCode, but got $testReturnCode" + testFail=true + fi + + if [[ "$testFail" == "true" ]]; then + return 1 + fi + + return 0 +) + +runTestFile() ( + local file=$1 + + echo "Test: $file" + "$file" +) + +runTests() ( + PASS=0 + FAIL=0 + + if [[ $# -gt 0 ]]; then + for TEST in "$@"; do + runTestFile "$TEST" && PASS=$((PASS + 1)) || FAIL=$((FAIL + 1)) + done + else + cd "${0%/*}" + for TEST in tests/*; do + if [[ -f "$TEST" ]]; then + runTestFile "$TEST" && PASS=$((PASS + 1)) || FAIL=$((FAIL + 1)) + fi + done + fi + + echo "" + echo "Pass: $PASS" + echo "Fail: $FAIL" + + if [[ $FAIL -gt 0 ]]; then + exit 1 + fi +) + +# Clear test related variables +template="Template not defined" +expected="Expected not defined" +returnCode=0 +arguments=() + +# If sourced, load functions. +# If executed, perform the actions as expected. +if [[ "$0" == "${BASH_SOURCE[0]}" ]] || [[ -z "${BASH_SOURCE[0]}" ]]; then + runTests ${@+"${@}"} +fi diff --git a/tests/ampersand b/tests/ampersand new file mode 100755 index 0000000..ff8914f --- /dev/null +++ b/tests/ampersand @@ -0,0 +1,9 @@ +#!/usr/bin/env bash +cd "${0%/*}" || exit 1 +. ../run-tests + +export thing="Works" +export template="{{&thing}}" +export expected="Works" + +runTest diff --git a/tests/array b/tests/array new file mode 100755 index 0000000..6ca5a2b --- /dev/null +++ b/tests/array @@ -0,0 +1,21 @@ +#!/usr/bin/env bash +cd "${0%/*}" || exit 1 +. ../run-tests + +export repo=( "resque" "hub" "rip" ) +template() { + cat <{{@key}} - {{.}} +{{/repo}} +EOF +} +expected() { + cat <0 - resque + 1 - hub + 2 - rip +EOF +} + +runTest diff --git a/tests/assoc-array b/tests/assoc-array new file mode 100755 index 0000000..338ef0b --- /dev/null +++ b/tests/assoc-array @@ -0,0 +1,25 @@ +#!/usr/bin/env bash +cd "${0%/*}" || exit 1 +. ../run-tests + +declare -A repo +# The order of the array elements can be shuffled depending on the version of +# Bash. Keeping this to a minimal set and alphabetized seems to help. +repo[hub]="Hub" +repo[rip]="Rip" +export repo +template() { + cat <{{@key}} - {{.}} +{{/repo}} +EOF +} +expected() { + cat <hub - Hub + rip - Rip +EOF +} + +runTest diff --git a/tests/comment b/tests/comment new file mode 100755 index 0000000..7a8708d --- /dev/null +++ b/tests/comment @@ -0,0 +1,8 @@ +#!/usr/bin/env bash +cd "${0%/*}" || exit 1 +. ../run-tests + +export template="Wor{{!comment}}ks" +export expected="Works" + +runTest diff --git a/tests/comment-newline b/tests/comment-newline new file mode 100755 index 0000000..0c1fea8 --- /dev/null +++ b/tests/comment-newline @@ -0,0 +1,15 @@ +#!/usr/bin/env bash +cd "${0%/*}" || exit 1 +. ../run-tests + +template() { + cat <Today{{! ignore me +and this can +run through multiple +lines}}. +EOF +} +export expected=$'

Today.

\n' + +runTest diff --git a/tests/comment-with-spaces b/tests/comment-with-spaces new file mode 100755 index 0000000..c8f8d5b --- /dev/null +++ b/tests/comment-with-spaces @@ -0,0 +1,8 @@ +#!/usr/bin/env bash +cd "${0%/*}" || exit 1 +. ../run-tests + +export template="Wor{{! comment }}ks" +export expected="Works" + +runTest diff --git a/tests/concatenated-variables b/tests/concatenated-variables new file mode 100755 index 0000000..0bd06aa --- /dev/null +++ b/tests/concatenated-variables @@ -0,0 +1,10 @@ +#!/usr/bin/env bash +cd "${0%/*}" || exit 1 +. ../run-tests + +export thing="Wor" +export thing2="ks" +export template="{{thing thing2}}" +export expected="Works" + +runTest diff --git a/tests/delimiters b/tests/delimiters new file mode 100755 index 0000000..a041811 --- /dev/null +++ b/tests/delimiters @@ -0,0 +1,9 @@ +#!/usr/bin/env bash +cd "${0%/*}" || exit 1 +. ../run-tests + +export thing="Works" +export template="{{=| |=}}|thing|" +export expected="Works" + +runTest diff --git a/tests/double-hyphen b/tests/double-hyphen new file mode 100755 index 0000000..3fc8d48 --- /dev/null +++ b/tests/double-hyphen @@ -0,0 +1,10 @@ +#!/usr/bin/env bash +cd "${0%/*}" || exit 1 +. ../run-tests + +export arguments=(--fail-on-file -- --help) +export returnCode=1 +export template="" +export expected=$'ERROR: No such file: --help\n' + +runTest diff --git a/tests/double-quote b/tests/double-quote new file mode 100755 index 0000000..526ac6a --- /dev/null +++ b/tests/double-quote @@ -0,0 +1,8 @@ +#!/usr/bin/env bash +cd "${0%/*}" || exit 1 +. ../run-tests + +export template='{{"Works"}}' +export expected="Works" + +runTest diff --git a/tests/fail-not-set b/tests/fail-not-set new file mode 100755 index 0000000..3104367 --- /dev/null +++ b/tests/fail-not-set @@ -0,0 +1,20 @@ +#!/usr/bin/env bash +cd "${0%/*}" || exit 1 +. ../run-tests + +unset __NO_SUCH_VAR +export POPULATED="words" +export EMPTY="" +export arguments=(--fail-not-set) +export returnCode=1 + +template() { + cat < +> diff --git a/tests/fixtures/multi-line-partial.partial b/tests/fixtures/multi-line-partial.partial new file mode 100644 index 0000000..da32746 --- /dev/null +++ b/tests/fixtures/multi-line-partial.partial @@ -0,0 +1 @@ +{{multilineData}} diff --git a/tests/fixtures/partial.partial b/tests/fixtures/partial.partial new file mode 100644 index 0000000..e25759d --- /dev/null +++ b/tests/fixtures/partial.partial @@ -0,0 +1 @@ +{{.}} diff --git a/tests/fixtures/source-multiple-1.vars b/tests/fixtures/source-multiple-1.vars new file mode 100644 index 0000000..433ea91 --- /dev/null +++ b/tests/fixtures/source-multiple-1.vars @@ -0,0 +1,2 @@ +export A=from1 +export B=from1 diff --git a/tests/fixtures/source-multiple-2.vars b/tests/fixtures/source-multiple-2.vars new file mode 100644 index 0000000..55b5263 --- /dev/null +++ b/tests/fixtures/source-multiple-2.vars @@ -0,0 +1,2 @@ +export B=from2 +export C=from2 diff --git a/tests/fixtures/source.vars b/tests/fixtures/source.vars new file mode 100644 index 0000000..fad535f --- /dev/null +++ b/tests/fixtures/source.vars @@ -0,0 +1,5 @@ +export VAR=value +export ARR=(1 2 3) +declare -A ASSOC_ARR +# Can not export associative arrays, otherwise they turn into indexed arrays +ASSOC_ARR=([a]=AAA [b]=BBB) diff --git a/tests/fixtures/standalone-indentation.partial b/tests/fixtures/standalone-indentation.partial new file mode 100644 index 0000000..96d7a30 --- /dev/null +++ b/tests/fixtures/standalone-indentation.partial @@ -0,0 +1,3 @@ +| +{{content}} +| diff --git a/tests/function b/tests/function new file mode 100755 index 0000000..967a128 --- /dev/null +++ b/tests/function @@ -0,0 +1,25 @@ +#!/usr/bin/env bash +cd "${0%/*}" || exit 1 +. ../run-tests + +export name=Willy +wrapped() { + # Wrapping 'cat' in a subshell eats the trailing whitespace + # The echo adds a newline, which is preserved. + echo "$(cat)" +} +template() { + cat < {{name}} is awesome.\n... this is the last line.\n' + +runTest diff --git a/tests/function-alternate-delimiters b/tests/function-alternate-delimiters new file mode 100755 index 0000000..a4fa6a8 --- /dev/null +++ b/tests/function-alternate-delimiters @@ -0,0 +1,16 @@ +#!/usr/bin/env bash +cd "${0%/*}" || exit 1 +. ../run-tests + +export planet=Earth +lambda() { + local content + + content=$(cat) + mo::parse content "$content{{planet}} => |planet|$content" + echo -n "$content" +} +export template="{{= | | =}}<|#lambda|-|/lambda|>" +export expected="<-{{planet}} => Earth->" + +runTest diff --git a/tests/function-args b/tests/function-args new file mode 100755 index 0000000..a7958d6 --- /dev/null +++ b/tests/function-args @@ -0,0 +1,35 @@ +#!/usr/bin/env bash +cd "${0%/*}" || exit 1 +. ../run-tests + +export name=Willy +MO_ALLOW_FUNCTION_ARGUMENTS=true + +pipeTo() { + cat | "$1" +} + +testArgs() { + printf "%d" "$#" + + # Display all arguments + printf " %q" ${@+"$@"} +} +template() { + cat < Willy is awesome.\n... this is the last line.\n' + +runTest diff --git a/tests/globals-in-loop b/tests/globals-in-loop new file mode 100755 index 0000000..538d5f1 --- /dev/null +++ b/tests/globals-in-loop @@ -0,0 +1,28 @@ +#!/usr/bin/env bash +cd "${0%/*}" || exit 1 +. ../run-tests + +export STR=abc +export DATA=(111 222) +template() { + cat < fixtures/indented-partials.partial}} + + {{> fixtures/indented-partials.partial}} + +Without spacing + {{> fixtures/indented-partials.partial}} + {{> fixtures/indented-partials.partial}} + +With text + {{> fixtures/indented-partials.partial}} + text + {{> fixtures/indented-partials.partial}} + +In a conditional +{{#thisIsTrue}} + {{> fixtures/indented-partials.partial}} +{{/thisIsTrue}} +EOF +} +expected() { + cat < fixtures/inline-indentation}} +EOF +} +expected() { + cat < +> + +EOF +} + +runTest diff --git a/tests/internal-whitespace b/tests/internal-whitespace new file mode 100755 index 0000000..8228215 --- /dev/null +++ b/tests/internal-whitespace @@ -0,0 +1,9 @@ +#!/usr/bin/env bash +cd "${0%/*}" || exit 1 +. ../run-tests + +export boolean=true +export template=$' | {{#boolean}} {{! Important Whitespace }}\n {{/boolean}} | \n' +export expected=$' | \n | \n' + +runTest diff --git a/tests/invalid-option b/tests/invalid-option new file mode 100755 index 0000000..8817896 --- /dev/null +++ b/tests/invalid-option @@ -0,0 +1,11 @@ +#!/usr/bin/env bash +cd "${0%/*}" || exit 1 +. ../run-tests + +export person="" +export template="" +export returnCode=1 +export arguments=(--something) +export expected=$'ERROR: Unknown option: --something (See --help for options)\n' + +runTest diff --git a/tests/inverted b/tests/inverted new file mode 100755 index 0000000..f706a04 --- /dev/null +++ b/tests/inverted @@ -0,0 +1,18 @@ +#!/usr/bin/env bash +cd "${0%/*}" || exit 1 +. ../run-tests + +export repo=() +template() { + cat <{{.}} +{{/repo}} +{{^repo}} + No repos :( +{{/repo}} +EOF +} +export expected=$' No repos :(\n' + +runTest diff --git a/tests/issue-75 b/tests/issue-75 new file mode 100755 index 0000000..6816dfd --- /dev/null +++ b/tests/issue-75 @@ -0,0 +1,9 @@ +#!/usr/bin/env bash +cd "${0%/*}" || exit 1 +. ../run-tests + +export uv +export template='{{^uv}}OK{{/uv}}{{#uv}}FAIL{{/uv}}' +export expected='OK' + +runTest diff --git a/tests/list-contexts b/tests/list-contexts new file mode 100755 index 0000000..9025d9b --- /dev/null +++ b/tests/list-contexts @@ -0,0 +1,16 @@ +#!/usr/bin/env bash +cd "${0%/*}" || exit 1 +. ../run-tests + +export a=foo +export b=wrong +declare -A sec +sec=([b]="bar") +export sec +declare -A c +c=([d]="baz") +export c +export template="{{#sec}}{{a}} {{b}} {{c.d}}{{/sec}}" +export expected="foo bar baz" + +runTest diff --git a/tests/miss b/tests/miss new file mode 100755 index 0000000..900f070 --- /dev/null +++ b/tests/miss @@ -0,0 +1,24 @@ +#!/usr/bin/env bash +cd "${0%/*}" || exit 1 +. ../run-tests + +export name="Chris" +export company="GitHub" +template() { + cat <GitHub. +* .GitHub. +EOF +} + +runTest diff --git a/tests/multi-line-partial b/tests/multi-line-partial new file mode 100755 index 0000000..0922f69 --- /dev/null +++ b/tests/multi-line-partial @@ -0,0 +1,35 @@ +#!/usr/bin/env bash +cd "${0%/*}" || exit 1 +. ../run-tests + +export multilineData=$'line 1\nline 2' +template() { + cat < fixtures/multi-line-partial.partial}} + +Indented: + + {{> fixtures/multi-line-partial.partial}} +EOF +} +expected() { + cat <Names +{{#names}} + {{> fixtures/partial.partial}} +{{/names}} +EOF +} +expected() { + cat <Names + Tyler + Abc +EOF +} + +runTest diff --git a/tests/partial-bad-file b/tests/partial-bad-file new file mode 100755 index 0000000..94906fa --- /dev/null +++ b/tests/partial-bad-file @@ -0,0 +1,9 @@ +#!/usr/bin/env bash +cd "${0%/*}" || exit 1 +. ../run-tests + +# This file intentionally does not exist +export template="{{>fixtures/partial-bad-file.partial}}" +export expected="" + +runTest diff --git a/tests/partial-missing b/tests/partial-missing new file mode 100755 index 0000000..6ca5b7b --- /dev/null +++ b/tests/partial-missing @@ -0,0 +1,19 @@ +#!/usr/bin/env bash +cd "${0%/*}" || exit 1 +. ../run-tests + +export returnCode=1 +export arguments=(--fail-on-file) +export person="" +template() { + cat < fixtures/partial-missing.partial}} +EOF +} +expected() { + cat <' +template() { + cat <<'EOF' +\ + {{>fixtures/standalone-indentation.partial}} +/ +EOF +} +expected() { + cat <<'EOF' +\ + | + < +-> + | +/ +EOF +} + +runTest diff --git a/tests/triple-brace b/tests/triple-brace new file mode 100755 index 0000000..1eb3616 --- /dev/null +++ b/tests/triple-brace @@ -0,0 +1,9 @@ +#!/usr/bin/env bash +cd "${0%/*}" || exit 1 +. ../run-tests + +export thing="Works" +export template="{{{thing}}}" +export expected="Works" + +runTest diff --git a/tests/typical b/tests/typical new file mode 100755 index 0000000..2274852 --- /dev/null +++ b/tests/typical @@ -0,0 +1,26 @@ +#!/usr/bin/env bash +cd "${0%/*}" || exit 1 +. ../run-tests + +export NAME="Chris" +export VALUE=10000 +export TAXED_VALUE=6000 +export IN_CA=true +template() { + cat <