diff options
| author | Paul Buetow <paul@buetow.org> | 2023-03-20 22:04:04 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2023-03-20 22:04:04 +0200 |
| commit | b06fbce5d2201364a101d0ddc457964cb9f3e69b (patch) | |
| tree | ff5997f35b37da21c512f496dfd12cc2b3261cce | |
| parent | e813283509ffd204e8dd6f721a83ec50a53e5411 (diff) | |
add support for multiline template
| -rw-r--r-- | README.md | 19 | ||||
| -rw-r--r-- | lib/template.source.sh | 79 |
2 files changed, 85 insertions, 13 deletions
@@ -106,7 +106,7 @@ This will help you to quickly review the results once in a while. Once you are h ### Templating -Since version `2.0.0`, Gemtexter supports templating. A template file name must have the suffix `gmi.tpl`. A template must be put into the same directory as the Gemtext `.gmi` file to be generated. Gemtexter will generate a Gemtext file `index.gmi` from a given template `index.gmi.tpl`. All lines starting with `<< ` will be evaluated as Bash code and the output will be written into the resulting Gemtext file. +Since version `2.0.0`, Gemtexter supports templating. A template file name must have the suffix `gmi.tpl`. A template must be put into the same directory as the Gemtext `.gmi` file to be generated. Gemtexter will generate a Gemtext file `index.gmi` from a given template `index.gmi.tpl`. All lines starting with `<< ` will be evaluated as a single line of Bash code and the output will be written into the resulting Gemtext file. A `<<<` and `>>>` encloses a multiline template. For example, the template `index.gmi.tpl`: @@ -116,6 +116,12 @@ For example, the template `index.gmi.tpl`: << echo "> This site was generated at $(date --iso-8601=seconds) by \`Gemtexter\`" Welcome to this capsule! + +<<< + for i in {1..10}; do + echo Multiline template line $i + done +>>> ``` ... results into the following `index.gmi` after running `./gemtexter --generate` (or `./gemtexter --template`, which instructs to do only template processing and nothing else): @@ -126,6 +132,17 @@ Welcome to this capsule! > This site was generated at 2023-03-15T19:07:59+02:00 by `Gemtexter` Welcome to this capsule! + +Multiline template line 0 +Multiline template line 1 +Multiline template line 2 +Multiline template line 3 +Multiline template line 4 +Multiline template line 5 +Multiline template line 6 +Multiline template line 7 +Multiline template line 8 +Multiline template line 9 ``` ### Alternative configuration file path diff --git a/lib/template.source.sh b/lib/template.source.sh index 26d5acb..f2777e5 100644 --- a/lib/template.source.sh +++ b/lib/template.source.sh @@ -7,14 +7,14 @@ template::generate () { continue fi num_tpl_files=$(( num_tpl_files + 1 )) - template::_generate "$tpl_path" & + template::_generate_file "$tpl_path" done < <(find "$CONTENT_BASE_DIR/gemtext" -type f -name \*.gmi.tpl) wait log INFO "Converted $num_tpl_files template files" } -template::_generate () { +template::_generate_file () { local -r tpl_path="$1"; shift local -r tpl_dir="$(dirname "$tpl_path")" local -r tpl="$(basename "$tpl_path")" @@ -23,28 +23,83 @@ template::_generate () { cd "$tpl_dir" || log PANIC "Unable to chdir to $tpl_dir" log INFO "$tpl_path -> $dest" + template::_generate < "$tpl" > "$dest.tmp" + mv "$dest.tmp" "$dest" + cd - +} + +template::_generate () { + local is_block=no + local block='' + while IFS='' read -r line; do + if [ "$is_block" = yes ]; then + if [ "$line" = '>>>' ]; then + template::_eval "$block" + is_block=no + else + block="$block +$line" + fi + continue + fi case "$line" in '<< '*) - template::_line "$line" + template::_eval "$line" + ;; + '<<<') + is_block=yes ;; *) echo "$line" ;; esac - done < "$tpl" > "$dest.tmp" - - mv "$dest.tmp" "$dest" - cd - + done } -template::_line () { +template::_eval () { eval "${1/<< /}" } template::test () { - assert::equals "$(template::_line '<< echo -n foo')" 'foo' - assert::equals "$(template::_line '<< echo foo')" 'foo' - assert::equals "$(template::_line '<< $DATE --date @0 +%Y%m%d')" '19700101' - assert::equals "$(template::_line '<< echo "$AUTHOR"')" "$AUTHOR" + assert::equals "$(template::_eval '<< echo -n foo')" 'foo' + assert::equals "$(template::_eval '<< echo foo')" 'foo' + assert::equals "$(template::_eval '<< $DATE --date @0 +%Y%m%d')" '19700101' + assert::equals "$(template::_eval '<< echo "$AUTHOR"')" "$AUTHOR" + + template::_eval '<< foo=bar' + assert::equals "$(template::_eval '<< echo $foo')" bar + + local -r template1='# Hello Mister +<<< + echo -n "Epoch 0 starts at: " + $DATE --date @0 +%Y%m%d + echo Just so that you know +>>>' + local -r expect1='# Hello Mister +Epoch 0 starts at: 19700101 +Just so that you know' + assert::equals "$(template::_generate <<< "$template1")" "$expect1" + + local -r template2='<<< + for i in {1..10}; do + echo -n $i + done +>>>' + assert::equals "$(template::_generate <<< "$template2")" 12345678910 + + local -r template3='<<< + foo=baz +>>> +<<< + echo $foo +>>>' + assert::equals "$(template::_generate <<< "$template3")" baz + + local -r template4='<<< +>>> +<<< +>>> +<< :' + assert::equals "$(template::_generate <<< "$template4")" '' } |
