diff options
| author | Paul Buetow <paul@buetow.org> | 2023-03-15 00:06:25 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2023-03-15 00:06:25 +0200 |
| commit | 365a195cc0362bfeb52ebc0e9cb958b0b455aac4 (patch) | |
| tree | b8357b1a1122953e66ba37421b017180d1ac50a5 | |
| parent | 06310ae8a5649bb41f32d1c752f369dc5e077ee1 (diff) | |
initial templating support
| -rwxr-xr-x | gemtexter | 9 | ||||
| -rw-r--r-- | lib/gemfeed.source.sh | 2 | ||||
| -rw-r--r-- | lib/generate.source.sh | 1 | ||||
| -rw-r--r-- | lib/git.source.sh | 4 | ||||
| -rw-r--r-- | lib/template.source.sh | 49 |
5 files changed, 60 insertions, 5 deletions
@@ -5,12 +5,13 @@ declare -r ARG="$1"; shift declare CONTENT_FILTER="$1"; shift -declare -r VERSION=1.2.0 +declare -r VERSION=2.0.0 declare -r VERSION_DESCR=develop declare -r DATE_FORMAT='--iso-8601=seconds' declare DATE=date declare SED=sed declare GREP=grep +declare BASH=bash declare XMLLINT='' which gdate &>/dev/null && DATE=gdate which gsed &>/dev/null && SED=gsed @@ -38,6 +39,7 @@ source ./lib/html.source.sh source ./lib/log.source.sh source ./lib/md.source.sh source ./lib/notes.source.sh +source ./lib/template.source.sh help () { cat <<HELPHERE @@ -110,6 +112,7 @@ main () { --test) LOG_VERBOSE=yes assert::shellcheck + template::test html::test md::test ;; @@ -118,6 +121,7 @@ main () { atomfeed::generate ;; --generate) + template::generate log INFO 'Generating feeds - this may will take a while' gemfeed::generate & atomfeed::generate & @@ -125,6 +129,9 @@ main () { wait generate::fromgmi html md ;; + --template) + template::generate + ;; --git) git::add_all "$GIT_COMMIT_MESSAGE" wait diff --git a/lib/gemfeed.source.sh b/lib/gemfeed.source.sh index a0c0b09..d888866 100644 --- a/lib/gemfeed.source.sh +++ b/lib/gemfeed.source.sh @@ -24,7 +24,7 @@ gemfeed::updatemainindex () { gemfeed::_get_word_count () { local -r gmi_file="$1"; shift - sed '/^```/,/^```/d' "$gmi_file" | wc -w | cut -d' ' -f1 + $SED '/^```/,/^```/d' "$gmi_file" | wc -w | cut -d' ' -f1 } # Generate a index.gmi in the ./gemfeed subdir. diff --git a/lib/generate.source.sh b/lib/generate.source.sh index 281fbe0..848e7fd 100644 --- a/lib/generate.source.sh +++ b/lib/generate.source.sh @@ -128,7 +128,6 @@ generate::fromgmi () { # Add content while read -r src; do - # User can specify a content filter if test -n "$CONTENT_FILTER" && ! $GREP -q "$CONTENT_FILTER" <<< "$src"; then continue fi diff --git a/lib/git.source.sh b/lib/git.source.sh index 67dc93e..1a71ade 100644 --- a/lib/git.source.sh +++ b/lib/git.source.sh @@ -23,7 +23,7 @@ git::add_all () { git::_add_all () { local -r message="$1"; shift local -r content_dir="$1"; shift - cd "$content_dir" + cd "$content_dir" || log PANIC "Unable to chdir to $content_dir" find . -type f -not -path '*/\.git*' | while read -r file; do git add "$file" @@ -44,7 +44,7 @@ git::sync_all () { git::_sync_all () { local -r content_dir="$1"; shift - cd "$content_dir" + cd "$content_dir" || log PANIC "Unable to chdir to $content_dir" git pull git push diff --git a/lib/template.source.sh b/lib/template.source.sh new file mode 100644 index 0000000..1b0df9d --- /dev/null +++ b/lib/template.source.sh @@ -0,0 +1,49 @@ +template::generate () { + log INFO 'Generating files from templates' + local -i num_tpl_files=0 + + while read -r tpl_path; do + if test -n "$CONTENT_FILTER" && ! $GREP -q "$CONTENT_FILTER" <<< "$tpl_path"; then + continue + fi + num_tpl_files=$(( num_tpl_files + 1 )) + template::_generate "$tpl_path" & + done < <(find "$CONTENT_BASE_DIR/gemtext" -type f -name \*.tpl) + + wait + log INFO "Converted $num_tpl_files template files" +} + +template::_generate () { + local -r tpl_path="$1"; shift + local -r tpl_dir="$(dirname "$tpl_path")" + local -r tpl="$(basename "$tpl_path")" + local -r dest="${tpl/.tpl/}" + + cd "$tpl_dir" || log PANIC "Unable to chdir to $tpl_dir" + log INFO "$tpl_path -> $dest" + + while IFS='' read -r line; do + case "$line" in + '<< '*) + template::_line "$line" + ;; + *) + echo "$line" + ;; + esac + done < "$tpl" > "$dest.tmp" + + mv "$dest.tmp" "$dest" +} + +template::_line () { + 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" +} |
