14 Commits
2.3.2 ... 2.4.1

Author SHA1 Message Date
Tyler Akins
8e9fd680d4 Fixing a test, release 2.4.1 2023-04-05 21:14:19 -05:00
Tyler Akins
24e80aedaa Bumping version
2.3.0 was already tagged
2023-04-05 20:55:16 -05:00
Tyler Akins
4311f29879 Fixing the build, some doc cleanup, update version 2023-04-05 20:54:27 -05:00
Tyler Akins
d6794de1e2 Merge pull request #57 from Swivelgames/loop-accessors
Feature: Key and Index Accessors Inside Loops
2023-04-05 20:51:13 -05:00
Joseph Dalrymple
b22baa9776 Added Functions/Helpers to README 2023-04-05 20:19:42 -05:00
Joseph Dalrymple
c28bffe708 Added @key loop accessor 2023-04-05 20:19:29 -05:00
Tyler Akins
b01fc43580 Merge pull request #60 from Swivelgames/portability
Improved portability by using /usr/bin/env pragma
2023-04-04 09:50:21 -05:00
Joseph Dalrymple
53e3208ba0 Improved portability by using /usr/bin/env pragma 2023-04-04 02:00:31 -05:00
Tyler Akins
6dc284f05a Updating URLs
Closes #53
2023-04-03 12:57:14 -05:00
Tyler Akins
a62541fc98 Adding missing character, reported by brainchild0 2022-01-26 19:07:12 -06:00
Tyler Akins
b31a97cfb1 Merge pull request #51 from brainchild0/docker
improve docker image
2022-01-26 10:00:28 -06:00
Eric Levy
71f85fa4f2 Use latest Alpine image as Docker base 2022-01-25 17:34:56 -05:00
Eric Levy
14003ba24a Introduce general improvements for Docker image 2022-01-24 20:51:27 -05:00
Tyler Akins
54195a6c6e Fixing test script to return 0 on success 2021-11-25 16:02:21 -06:00
21 changed files with 123 additions and 33 deletions

View File

@@ -1,11 +1,7 @@
FROM alpine:3.13.4
FROM alpine
COPY ./mo .
RUN apk add --update bash && \
chmod +x mo &&\
mv mo /usr/local/bin/mo && \
rm -rf /var/cache/apk/*
RUN apk add --no-cache bash
ADD mo /usr/local/bin/mo
RUN chmod +x /usr/local/bin/mo
WORKDIR /opt
ENTRYPOINT ["/usr/local/bin/mo"]
ENTRYPOINT /usr/local/bin/mo

View File

@@ -41,7 +41,7 @@ There are a few ways you can install this tool. How you install it depends on h
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://git.io/get-mo -o mo
curl -sSL https://raw.githubusercontent.com/tests-always-included/mo/master/mo -o mo
# Make executable
chmod +x mo
@@ -58,7 +58,7 @@ You can install this file in `/usr/local/bin/` or `/usr/bin/` by simply download
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://git.io/get-mo -o mo
curl -sSL https://raw.githubusercontent.com/tests-always-included/mo/master/mo -o mo
# Make executable
chmod +x mo
@@ -78,7 +78,7 @@ This is very similar to installing it globally but it does not require root priv
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://git.io/get-mo -o mo
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/
@@ -91,7 +91,7 @@ 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).
#!/bin/bash
#!/usr/bin/env bash
cd "$(dirname "$0")" # Go to the script's directory
export TEST="This is a test"
echo "Your message: {{TEST}}" | ../mo
@@ -100,7 +100,7 @@ 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).
#!/bin/bash
#!/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
@@ -118,6 +118,77 @@ There are more scripts available in the [demos directory](demo/) that could help
There are additional features that the program supports. Try using `mo --help` to see what is available.
Enhancements
-----------
In addition to many of the features built-in to Mustache, `mo` includes a number of unique features that make it a bit more powerful.
### Loop @key
`mo` implements Handlebar's `@key` references for outputting the key inside of a loop:
Env:
```bash
myarr=( foo bar )
# Bash v4+
declare -A myassoc
myassoc[hello]="mo"
myassoc[world]="is great"
```
Template:
```handlebars
{{#myarr}}
- {{@key}} {{.}}
{{/myarr}}
{{#myassoc}}
* {{@key}} {{.}}
{{/myassoc}}
```
Output:
```markdown
- 0 foo
- 1 bar
* hello mo
* world is great
```
### 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]}";
}
```
Concessions
-----------

View File

@@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env bash
cd "$(dirname "$0")" # Go to the script's directory

View File

@@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env bash
#
# This embeds a template in the script without using strange `cat` syntax.

View File

@@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env bash
#
# This sources a simple script with the env. variables needed for the template.

View File

@@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env bash
cd "$(dirname "$0")" # Go to the script's directory

View File

@@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env bash
cd "$(dirname "$0")" # Go to the script's directory

View File

@@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env bash
cd "$(dirname "$0")"/..

View File

@@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env bash
#
# This sources a simple script with the env. variables needed for the template.

View File

@@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env bash
cd "$(dirname "$0")" # Go to the script's directory
export ARRAY=( one two "three three three" four five )
. ../mo

View File

@@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env bash
#
# This example does not source `mo` and is intentionally restricted to
# variables that are not arrays.

View File

@@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env bash
#
# This requires tomdoc.sh to be in your PATH.
# https://github.com/tests-always-included/tomdoc.sh

15
mo
View File

@@ -666,7 +666,7 @@ moLoop() {
moParse() {
# Keep naming variables mo* here to not overwrite needed variables
# used in the string replacements
local moArgs moBlock moContent moCurrent moIsBeginning moNextIsBeginning moTag
local moArgs moBlock moContent moCurrent moIsBeginning moNextIsBeginning moTag moKey
moCurrent=$2
moIsBeginning=$3
@@ -782,6 +782,17 @@ moParse() {
moShow "$moTag" "$moCurrent"
;;
'@key')
# Special vars
moStandaloneDenied moContent "${moContent[@]}"
# Current content (environment variable or function)
if [[ "$moCurrent" == *.* ]]; then
echo -n "${moCurrent#*.}"
else
echo -n "$moCurrent"
fi
;;
*)
# Normal environment variable or function call
moStandaloneDenied moContent "${moContent[@]}"
@@ -1097,7 +1108,7 @@ moUsage() {
# Save the original command's path for usage later
MO_ORIGINAL_COMMAND="$(cd "${BASH_SOURCE[0]%/*}" || exit 1; pwd)/${BASH_SOURCE[0]##*/}"
MO_VERSION="2.2.0"
MO_VERSION="2.4.1"
# If sourced, load all functions.
# If executed, perform the actions as expected.

View File

@@ -64,7 +64,7 @@ function runTest(test, done) {
var output, partials, script;
script = [
'#!/bin/bash'
'#!/usr/bin/env bash'
];
partials = test.partials || {};

View File

@@ -40,4 +40,6 @@ done
echo ""
echo "Pass: $PASS"
echo "Fail: $FAIL"
[[ $FAIL -gt 0 ]] && exit 1
if [[ $FAIL -gt 0 ]]; then
exit 1
fi

View File

@@ -1,3 +1,3 @@
<b>resque</b>
<b>hub</b>
<b>rip</b>
<b>0 - resque</b>
<b>1 - hub</b>
<b>2 - rip</b>

View File

@@ -1,3 +1,3 @@
{{#repo}}
<b>{{.}}</b>
<b>{{@key}} - {{.}}</b>
{{/repo}}

4
tests/assoc-array.env Normal file
View File

@@ -0,0 +1,4 @@
declare -A repo
repo[resque]="Resque"
repo[hub]="Hub"
repo[rip]="Rip"

View File

@@ -0,0 +1,3 @@
<b>hub - Hub</b>
<b>rip - Rip</b>
<b>resque - Resque</b>

View File

@@ -0,0 +1,3 @@
{{#repo}}
<b>{{@key}} - {{.}}</b>
{{/repo}}

View File

@@ -25,4 +25,4 @@ Options:
Load FILE into the environment before processing templates.
Can be used multiple times.
MO_VERSION=2.2.0
MO_VERSION=2.4.1