├── .github
└── workflows
│ └── test.yml
├── LICENSE
├── Makefile
├── README.md
├── bpkg.json
├── examples
├── index.html.ms
└── page.html.ms
├── mush.1
├── mush.1.md
├── mush.sh
├── test.sh
└── test
└── tpl.ms
/.github/workflows/test.yml:
--------------------------------------------------------------------------------
1 | on:
2 | - push
3 | - pull_request
4 |
5 | jobs:
6 | must_pass:
7 | runs-on: ubuntu-latest
8 | steps:
9 | - uses: actions/checkout@v2
10 | - uses: pipeline-components/shellcheck@v0.10.0
11 | with:
12 | directory: mush.sh test.sh
13 | - name: Tests
14 | shell: bash
15 | run: |
16 | curl -Lso- get.bpkg.sh | bash
17 | ./mush.sh --help ## verify it runs and exits with 0
18 | ./mush.sh --version ## verify it runs and exits with 0
19 | bpkg run test
20 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2013 Joseph Werle
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy of
6 | this software and associated documentation files (the "Software"), to deal in
7 | the Software without restriction, including without limitation the rights to
8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 | the Software, and to permit persons to whom the Software is furnished to do so,
10 | subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 |
--------------------------------------------------------------------------------
/Makefile:
--------------------------------------------------------------------------------
1 |
2 | BIN = mush
3 | PREFIX ?= /usr/local
4 | MANPREFIX ?= $(PREFIX)/share/man/man1
5 |
6 | all:
7 | @:
8 |
9 | install:
10 | cp $(BIN).sh $(PREFIX)/bin/$(BIN)
11 | install $(BIN).1 $(MANPREFIX)
12 |
13 | uninstall:
14 | rm -f $(PREFIX)/bin/$(BIN)
15 | rm -f $(MANPREFIX)/$(BIN).1
16 |
17 | test:
18 | ./test.sh
19 |
20 | doc:
21 | @curl -# -F page=@$(BIN).1.md -o $(BIN).1 http://mantastic.herokuapp.com
22 | @echo "$(BIN).1"
23 |
24 | .PHONY: test
25 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | mush
2 | ====
3 |
4 | Mustache templates for bash
5 |
6 | ## install
7 |
8 | With [bpkg](https://github.com/bpkg/bpkg):
9 |
10 | ```sh
11 | $ bpkg install -g jwerle/mush
12 | ```
13 |
14 | From source:
15 |
16 | ```sh
17 | $ make install
18 | ```
19 |
20 | ## usage
21 |
22 | Suppose you have a template file:
23 |
24 | ***./template.ms***
25 |
26 | ```
27 | VAR={{VAR}}
28 | ```
29 |
30 | You can compile it like such:
31 |
32 | ```sh
33 | $ cat ./template.ms | VAR=VALUE mush
34 | VAR=123
35 | ```
36 |
37 | You can utilize stdin in the same way with `echo`
38 |
39 | ```sh
40 | echo "Today's date is {{DATE}}" | DATE=`date +%D` mush
41 | Today's date is 12/17/13
42 | ```
43 |
44 | Variables are passed to the view environment variable
45 | definition. Due to the way variables are scoped to the
46 | templates. All environment variables are available to the
47 | template. This includes variables like `$HOME` and `$USER`
48 |
49 | Compile a HTML file with partial:
50 |
51 | ***index.html.ms*** (layout)
52 |
53 | ```html
54 |
55 |
56 |
57 |
58 | {{title}}
59 |
60 |
61 |
62 |
63 |
64 | {{content}}
65 |
66 |
67 |
68 | ```
69 |
70 | ***page.html.ms*** (partial)
71 |
72 | ```html
73 | This is the {{name}} page
74 | ```
75 |
76 | ```sh
77 | $ cat index.html.ms | \
78 | title="Awesome Web Site" \
79 | main_css="/css/main.css" \
80 | main_js="/js/main.js" \
81 | content="`cat page.html.ms | name=home mush`" \
82 | mush
83 | ```
84 |
85 | This will yield:
86 |
87 | ```html
88 |
89 |
90 |
91 |
92 | foo
93 |
94 |
95 |
96 |
97 |
98 |
This is the home page
99 |
100 |
101 |
102 | ```
103 |
104 | ## api
105 |
106 | ```
107 | usage: mush [-ehV] [-f ] [-o ]
108 |
109 | examples:
110 | $ cat file.ms | FOO=BAR mush
111 | $ VALUE=123 mush -f file.ms -o file
112 | $ echo "Today's date is {{DATE}}" | DATE=`date +%D` mush
113 | $ cat ./template.ms | VAR=VALUE mush
114 |
115 | options:
116 | -f, --file file to parse
117 | -o, --out output file
118 | -e, --escape escapes html html entities
119 | -h, --help display this message
120 | -V, --version output version
121 | ```
122 |
123 | ## license
124 |
125 | MIT
126 |
--------------------------------------------------------------------------------
/bpkg.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "mush",
3 | "version": "0.1.4",
4 | "description": "Mustache templates for bash",
5 | "scripts": [ "mush.sh" ],
6 | "license": "MIT",
7 | "install": "bpkg run install",
8 | "commands": {
9 | "install": "mkdir -p ${PREFIX:-/usr/local}/{bin,share/man/man1} && cp mush.sh ${PREFIX:-/usr/local}/bin/mush && cp mush.1 ${PREFIX:-/usr/local}/share/man/man1",
10 | "lint": "shellcheck **/*.sh",
11 | "test": "./test.sh"
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/examples/index.html.ms:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {{title}}
6 |
7 |
8 |
9 |
10 |
11 | {{content}}
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/examples/page.html.ms:
--------------------------------------------------------------------------------
1 | This is the {{name}} page
2 |
--------------------------------------------------------------------------------
/mush.1:
--------------------------------------------------------------------------------
1 | .\" Generated with Ronnjs 0.3.8
2 | .\" http://github.com/kapouer/ronnjs/
3 | .
4 | .TH "MUSH" "1" "December 2022" "" ""
5 | .
6 | .SH "NAME"
7 | \fBmush\fR \-\- Mustache templates for bash
8 | .
9 | .SH "SYNOPSIS"
10 | \fBmush\fR [\-ehV] [\-f ] [\-o ]
11 | .
12 | .SH "OPTIONS"
13 | \fB\-f, \-\-file \fR file to parse
14 | \fB\-o, \-\-out \fR output file
15 | \fB\-e, \-\-escape\fR escapes html html entities
16 | \fB\-h, \-\-help\fR display this message
17 | \fB\-V, \-\-version\fR output version
18 | .
19 | .SH "EXAMPLES"
20 | $ cat file\.ms | FOO=BAR mush
21 | $ VALUE=123 mush \-f file\.ms \-o file
22 | $ echo "Today\'s date is {{DATE}}" | DATE=`date +%D` mush
23 | $ cat \./template\.ms | VAR=VALUE mush
24 | .
25 | .SH "USAGE"
26 | Suppose you have a template file:
27 | .
28 | .P
29 | \./template\.ms
30 | .
31 | .P
32 | \fB
33 | VAR={{VAR}}
34 | \fR
35 | .
36 | .P
37 | You can compile it like such:
38 | .
39 | .P
40 | \fB
41 | $ cat \./template\.ms | VAR=VALUE mush
42 | VAR=123
43 | \fR
44 | .
45 | .P
46 | You can utilize stdin in the same way with \fBecho\fR
47 | .
48 | .P
49 | \fB
50 | $ echo "Today\'s date is {{DATE}}" | DATE=`date +%D` mush
51 | Today\'s date is 12/17/13
52 | \fR
53 | .
54 | .P
55 | Variables are passed to the view vie environment variable
56 | definition\. Due to the way variables are scoped to the
57 | templates\. All environment variables are available to the
58 | template\. This includes variables like \fB$HOME\fR and \fB$USER\fR
59 | .
60 | .P
61 | Compile a HTML file with partial:
62 | .
63 | .P
64 | index\.html\.ms (layout)
65 | .
66 | .P
67 | \fB
68 |
69 |
70 |
71 |
72 | {{title}}
73 |
74 |
75 |
76 |
77 |
78 | {{content}}
79 |
80 |
81 |
82 | \fR
83 | .
84 | .P
85 | page\.html\.ms (partial)
86 | .
87 | .P
88 | \fB
89 | This is the {{name}} page
90 | \fR
91 | .
92 | .P
93 | \fB
94 | $ cat index\.html\.ms | \\
95 | title="Awesome Web Site" \\
96 | main_css="/css/main\.css" \\
97 | main_js="/js/main\.js" \\
98 | content="`cat page\.html\.ms | name="home" mush`" \\
99 | mush
100 | \fR
101 | .
102 | .P
103 | This will yield:
104 | .
105 | .P
106 | \fB
107 |
108 |
109 |
110 |
111 | foo
112 |
113 |
114 |
115 |
116 |
117 |
This is the home page
118 |
119 |
120 |
121 | \fR
122 | .
123 | .SH "AUTHOR"
124 | .
125 | .IP "\(bu" 4
126 | Joseph Werle \fIjoseph\.werle@gmail\.com\fR
127 | .
128 | .IP "" 0
129 | .
130 | .SH "REPORTING BUGS"
131 | .
132 | .IP "\(bu" 4
133 | https://github\.com/jwerle/mush/issues
134 | .
135 | .IP "" 0
136 | .
137 | .SH "SEE ALSO"
138 | .
139 | .IP "\(bu" 4
140 | http://mustache\.github\.io/
141 | .
142 | .IP "\(bu" 4
143 | https://github\.com/jwerle/mush
144 | .
145 | .IP "" 0
146 | .
147 | .SH "LICENSE"
148 | MIT (C) Copyright Joseph Werle 2013-2022
149 |
--------------------------------------------------------------------------------
/mush.1.md:
--------------------------------------------------------------------------------
1 | mush(1) -- Mustache templates for bash
2 | =================================
3 |
4 | ## SYNOPSIS
5 |
6 | `mush` [-ehV] [-f ] [-o ]
7 |
8 | ## OPTIONS
9 |
10 | `-f, --file ` file to parse
11 | `-o, --out ` output file
12 | `-e, --escape` escapes html html entities
13 | `-h, --help` display this message
14 | `-V, --version` output version
15 |
16 | ## EXAMPLES
17 |
18 | ```
19 | $ cat file.ms | FOO=BAR mush
20 | $ VALUE=123 mush -f file.ms -o file
21 | $ echo "Today's date is {{DATE}}" | DATE=\`date +%D\` mush
22 | $ cat ./template.ms | VAR=VALUE mush
23 | ```
24 |
25 | ## USAGE
26 |
27 | Suppose you have a template file:
28 |
29 | ./template.ms
30 |
31 | ```
32 | VAR={{VAR}}
33 | ```
34 |
35 | You can compile it like such:
36 |
37 | ```
38 | $ cat ./template.ms | VAR=VALUE mush
39 | VAR=123
40 | ```
41 |
42 | You can utilize stdin in the same way with `echo`
43 |
44 | ```
45 | $ echo "Today's date is {{DATE}}" | DATE=`date +%D` mush
46 | Today's date is 12/17/13
47 | ```
48 |
49 | Variables are passed to the view vie environment variable
50 | definition. Due to the way variables are scoped to the
51 | templates. All environment variables are available to the
52 | template. This includes variables like `$HOME` and `$USER`
53 |
54 | Compile a HTML file with partial:
55 |
56 | index.html.ms (layout)
57 |
58 | ```
59 |
60 |
61 |
62 |
63 | {{title}}
64 |
65 |
66 |
67 |
68 |
69 | {{content}}
70 |
71 |
72 |
73 | ```
74 |
75 | page.html.ms (partial)
76 |
77 | ```
78 | This is the {{name}} page
79 | ```
80 |
81 | ```
82 | $ cat index.html.ms | \
83 | title="Awesome Web Site" \
84 | main_css="/css/main.css" \
85 | main_js="/js/main.js" \
86 | content="`cat page.html.ms | name="home" mush`" \
87 | mush
88 | ```
89 |
90 | This will yield:
91 |
92 | ```
93 |
94 |
95 |
96 |
97 | foo
98 |
99 |
100 |
101 |
102 |
103 |
This is the home page
104 |
105 |
106 |
107 | ```
108 |
109 | ## AUTHOR
110 |
111 | - Joseph Werle
112 |
113 | ## REPORTING BUGS
114 |
115 | - https://github.com/jwerle/mush/issues
116 |
117 | ## SEE ALSO
118 |
119 | - http://mustache.github.io/
120 | - https://github.com/jwerle/mush
121 |
122 | ## LICENSE
123 |
124 | MIT (C) Copyright Joseph Werle 2013
125 |
--------------------------------------------------------------------------------
/mush.sh:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | mush_version () {
4 | echo "0.1.4"
5 | }
6 |
7 | mush_usage () {
8 | echo "usage: mush [-ehV] [-f ] [-o ]"
9 |
10 | if [ "$1" = "1" ]; then
11 | echo
12 | echo "examples:"
13 | echo " $ cat file.ms | FOO=BAR mush"
14 | echo " $ VALUE=123 mush -f file.ms -o file"
15 | echo " $ echo \"Today's date is {{DATE}}\" | DATE=\`date +%D\` mush"
16 | echo " $ cat ./template.ms | VAR=VALUE mush"
17 | echo
18 | echo "options:"
19 | echo " -f, --file file to parse"
20 | echo " -o, --out output file"
21 | echo " -e, --escape escapes html html entities"
22 | echo " -h, --help display this message"
23 | echo " -V, --version output version"
24 | fi
25 | }
26 |
27 | mush () {
28 | # shellcheck disable=SC2034
29 | local SELF="$0"
30 | # shellcheck disable=SC2034
31 | local NULL=/dev/null
32 | # shellcheck disable=SC2034
33 | local STDIN=0
34 | local STDOUT=1
35 | local STDERR=2
36 | local LEFT_DELIM="{{"
37 | local RIGHT_DELIM="}}"
38 | # shellcheck disable=SC2034
39 | local INDENT_LEVEL=" "
40 | local ESCAPE=0
41 | local ENV=""
42 | local out=">&$STDOUT"
43 |
44 | ENV="$(env)"
45 |
46 | ## parse opts
47 | while true; do
48 | arg="$1"
49 |
50 | if [ "" = "$1" ]; then
51 | break;
52 | fi
53 |
54 | if [ "${arg:0:1}" != "-" ]; then
55 | shift
56 | continue
57 | fi
58 |
59 | case $arg in
60 | -f|--file)
61 | file="$2";
62 | shift 2;
63 | ;;
64 | -o|--out)
65 | out="> $2";
66 | shift 2;
67 | ;;
68 | -e|--escape)
69 | ESCAPE=1
70 | shift
71 | ;;
72 | -h|--help)
73 | mush_usage 1
74 | exit 1
75 | ;;
76 | -V|--version)
77 | mush_version
78 | exit 0
79 | ;;
80 | *)
81 | {
82 | echo "unknown option \`$arg'"
83 | } >&$STDERR
84 | mush_usage
85 | exit 1
86 | ;;
87 | esac
88 | done
89 |
90 | ## read each line
91 | while IFS= read -r line; do
92 | printf '%q\n' "${line}" | {
93 | ## read each ENV variable
94 | echo "$ENV" | {
95 | while read -r var; do
96 | ## split each ENV variable by '='
97 | ## and parse the line replacing
98 | ## occurrence of the key with
99 | ## guarded by the values of
100 | ## `LEFT_DELIM' and `RIGHT_DELIM'
101 | ## with the value of the variable
102 | case "$var" in
103 | (*"="*)
104 | key=${var%%"="*}
105 | val=${var#*"="*}
106 | ;;
107 |
108 | (*)
109 | key=$var
110 | val=
111 | ;;
112 | esac
113 |
114 | line="${line//${LEFT_DELIM}$key${RIGHT_DELIM}/$val}"
115 | done
116 |
117 | if [ "1" = "$ESCAPE" ]; then
118 | line="${line//&/&}"
119 | line="${line//\"/"}"
120 | line="${line//\<}"
121 | line="${line//\>/>}"
122 | fi
123 |
124 | ## output to stdout
125 | echo "$line" | {
126 | ## parse undefined variables
127 | sed -e "s#${LEFT_DELIM}[A-Za-z]*${RIGHT_DELIM}##g" | \
128 | ## parse comments
129 | sed -e "s#${LEFT_DELIM}\!.*${RIGHT_DELIM}##g"
130 | };
131 | }
132 | };
133 | done
134 | }
135 |
136 |
137 | if [[ "${BASH_SOURCE[0]}" != "$0" ]]; then
138 | export -f mush
139 | else
140 | if [ ! -t 0 ]; then
141 | eval "mush $out"
142 | elif [ -n "$file" ]; then
143 | eval "cat $file | mush $out"
144 | elif (( $# > 0 )); then
145 | mush "$@"
146 | else
147 | mush_usage
148 | exit 1
149 | fi
150 | exit $?
151 | fi
152 |
153 |
--------------------------------------------------------------------------------
/test.sh:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | BIN="./mush.sh"
4 | FILE="./test/tpl.ms"
5 | i=0
6 |
7 | throw () {
8 | {
9 | printf " \n error: \`%s'\n" "$@"
10 | } >&2
11 | exit 1
12 | }
13 |
14 | export USER="jwerle"
15 | export PRONOUNS="he/him"
16 | export THING="apple"
17 | export COLOR="red"
18 | export PERSON="tobi"
19 | export ADJECTIVE="cool"
20 |
21 | echo
22 | echo " $FILE"
23 | cat < "$FILE" | $BIN | while read -r line; do
24 | ((++i))
25 | if [ "$line" = "" ]
26 | then
27 | # ignore empty lines
28 | continue
29 | fi
30 |
31 | echo " $i $line"
32 | case $i in
33 | 1)
34 | # {{! this is a comment }}END
35 | expected="END"
36 |
37 | if [ "$line" != "$expected" ]; then
38 | throw "$i '$line' != '$expected'"
39 | fi
40 | ;;
41 |
42 | 2)
43 | expected=""
44 |
45 | if [ "$line" != "$expected" ]; then
46 | throw "$i '$line' != '$expected'"
47 | fi
48 | ;;
49 |
50 | 3)
51 | expected="$USER is $PRONOUNS"
52 |
53 | if [ "$line" != "$expected" ]; then
54 | throw "$i '$line' != '$expected'"
55 | fi
56 | ;;
57 |
58 | 4)
59 | expected="$THING is $COLOR"
60 |
61 | if [ "$line" != "$expected" ]; then
62 | throw "$i '$line' != '$expected'"
63 | fi
64 | ;;
65 |
66 | 5)
67 | expected="$PERSON is $ADJECTIVE"
68 |
69 | if [ "$line" != "$expected" ]; then
70 | throw "$i '$line' != '$expected'"
71 | fi
72 | ;;
73 |
74 | 6)
75 | expected="$USER is friends with $PERSON"
76 |
77 | if [ "$line" != "$expected" ]; then
78 | throw "$i '$line' != '$expected'"
79 | fi
80 | ;;
81 |
82 | 7)
83 | expected=''
84 |
85 | if [ "$line" != "$expected" ]; then
86 | throw "$i '$line' != '$expected'"
87 | fi
88 | ;;
89 |
90 | 8)
91 | expected="has \w backslashes"
92 |
93 | if [ "$line" != "$expected" ]; then
94 | throw "$i '$line' != '$expected'"
95 | fi
96 | ;;
97 |
98 | 9)
99 | expected='Hello \"jwerle\"'
100 |
101 | if [ "$line" != "$expected" ]; then
102 | throw "$i '$line' != '$expected'"
103 | fi
104 | ;;
105 |
106 | 10)
107 | expected='Hello "jwerle"'
108 |
109 | if [ "$line" != "$expected" ]; then
110 | throw "$i '$line' != '$expected'"
111 | fi
112 | ;;
113 |
114 | *)
115 | throw "Line not tested : $line"
116 | ;;
117 |
118 | esac
119 | done
120 |
121 |
122 | # shellcheck disable=SC2181
123 | if (( $? == 0 )); then
124 | echo
125 | echo "+ ok!"
126 | else
127 | echo
128 | echo "x fail"
129 | exit 1
130 | fi
131 |
--------------------------------------------------------------------------------
/test/tpl.ms:
--------------------------------------------------------------------------------
1 | {{! this is a comment }}END
2 |
3 | {{USER}} is {{PRONOUNS}}
4 | {{THING}} is {{COLOR}}
5 | {{PERSON}} is {{ADJECTIVE}}
6 | {{USER}} is friends with {{PERSON}}
7 | {{var}} {{value}}
8 | has \w backslashes
9 | Hello \"{{USER}}\"
10 | Hello "{{USER}}"
11 |
--------------------------------------------------------------------------------