#!/bin/bash INPUT=$1 TEMPLATE=$2 # we build the OUTPUT dir by: # - taking the INPUT value and replacing `content` with `dist` # - replacing `.txt` with `.html` # # refs: # - # - function join { local IFS="$1"; shift; echo "$*"; } # create an array from INPUT_ARRAY # './content/worklog/' => ['.', 'content', 'worklog', ] IFS='/' read -ra INPUT_ARRAY <<< "$INPUT" ARR_LEN=${#INPUT_ARRAY[@]} # select last item from array INPUT_FILENAME=${INPUT_ARRAY[$ARR_LEN -1]} # split `.txt`, get an array, [, 'txt'] IFS='.' read -ra FILENAME_ARRAY <<< "$INPUT_FILENAME" # replace array's item `txt` w/ `html` OUTPUT_FILENAME_ARRAY=$(echo ${FILENAME_ARRAY[@]/txt/html}) # join array => `.html` OUTPUT_FILENAME=$(join . ${OUTPUT_FILENAME_ARRAY[@]}) # make new array w/ INPUT_ARRAY minus and OUTPUT_FILENAME OUTPUT_ARRAY=$(echo ${INPUT_ARRAY[@]:0:$ARR_LEN -1} $OUTPUT_FILENAME) # replace `content` w/ `dist` in array OUTPUT_ARRAY=$(echo ${OUTPUT_ARRAY[@]/content/dist}) # join final array into output string-path => OUTPUT_PATH=$(join / ${OUTPUT_ARRAY[@]}) #-- all the above code could probably be written simpler and more concise! COMMIT_DATE=$(git log -n 1 --pretty=format:%cd --date=format:'%Y-%m-%dT%H:%M:%S' -- $INPUT) COMMIT_HASH=$(git log -n 1 --pretty=format:%h -- $INPUT) #-- fetch template value from article YAML head block, eg `template: ` #-- the rg command: # - `N` does not print line number match # - `P` uses another regex engine (PCRE2) to perform a look-around operation # - `o` prints "only matching" TEMPLATE=$(rg -NPo '(?<=template: )\w.+' $INPUT) pandoc $INPUT --template templates/$TEMPLATE -o $OUTPUT_PATH -V commit_date=$COMMIT_DATE -V commit_hash=$COMMIT_HASH printf "rospo :: build-page: '$1' converted to html\n" printf " and saved in '$OUTPUT_PATH'\n" printf " with template $TEMPLATE\n" printf " with commit-hash $COMMIT_HASH and commit-date $COMMIT_DATE.\n"