From 4959411e10be54261a3bb79438dca5b117c72000 Mon Sep 17 00:00:00 2001 From: Landon Curt Noll Date: Fri, 29 Dec 2023 23:14:51 -0800 Subject: [PATCH] release pandoc wrapper VERSION 1.0 2023-12-29 --- inc/pandoc_wrapper.sh | 197 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 196 insertions(+), 1 deletion(-) diff --git a/inc/pandoc_wrapper.sh b/inc/pandoc_wrapper.sh index 9785a7804..ac6c2e4d1 100755 --- a/inc/pandoc_wrapper.sh +++ b/inc/pandoc_wrapper.sh @@ -1,4 +1,199 @@ #!/usr/bin/env bash # -# pandoc_wrapper.sh - stub for an actual pandoc wrapper tool +# pandoc_wrapper.sh - wrapper tool to run pandoc + +# firewall - must be bash with a version 4.2 or later +# +# We must declare arrays with -ag or -Ag +# +if [[ -z ${BASH_VERSINFO[0]} || ${BASH_VERSINFO[0]} -lt 4 || ${BASH_VERSINFO[0]} -eq 4 && ${BASH_VERSINFO[1]} -lt 2 ]]; then + echo "$0: ERROR: bash version must be >= 4.2: $BASH_VERSION" 1>&2 + exit 4 +fi + +# setup bash file matching +# +# Requires bash with a version 4.2 or later +# +shopt -s nullglob # enable expanded to nothing rather than remaining unexpanded +shopt -u failglob # disable error message if no matches are found +shopt -u dotglob # disable matching files starting with . +shopt -s globskipdots # enable never matching . nor .. +shopt -u nocaseglob # disable strict case matching +shopt -u extglob # enable extended globbing patterns +shopt -s globstar # enable ** to match all files and zero or more directories and subdirectories + +# setup +# +export VERSION="1.0 2023-12-29" +NAME=$(basename "$0") +export NAME +# +export PANDOC_TOOL +PANDOC_TOOL=$(type -P pandoc) +if [[ -z $PANDOC_TOOL ]]; then + PANDOC_TOOL="/opt/homebrew/bin/pandoc" +fi +# +PANDOC_ARGS="-f markdown -t html --fail-if-warnings=true" +# +export V_FLAG=0 +export NOOP=0 +export DO_NOT_PROCESS=0 + +# set usage message +# +export USAGE="usage: $0 [-h] [-v level] [-V] [-n] [-N] + [ -p pandoc_tool] [-P pandoc_opts] [-e string ..] [-E exitcode] + file.md output.html + + -h print help message and exit + -v level set verbosity level (def level: 0) + -V print version string and exit + + -n go thru the actions, but do not update any files (def: do the action) + -N do not process file, just parse arguments and ignore the file (def: process the file) + + -p pandoc_tool path to the pandoc tool (not the wrapper) (def: $PANDOC_TOOL) + -P pandoc_opts options given to the pandoc tool (def: $PANDOC_ARGS) + + -e string output string, followed by newline, to stderr (def: do not) + -E exitcode force exit with exitcode (def: exit based on success or failure of the action) + + file.md markdown file to convert into HTML + output.html HTML generated by pandoc from file.md + NOTE: Without -n, will prepend -o output.html as first pandoc options + With -n, will append -o /dv/null as last pandoc options before file.md + +Exit codes: + 0 all OK + 1 pandoc exited non-zero + 2 -h and help string printed or -V and version string printed + 3 command line error + 4 bash version is < 4.2 + 5 file.md file not found or not readable file + 6 pandoc tool not found or not executable + >= 10 internal error + +$NAME version: $VERSION" + +# parse command line +# +while getopts :hv:VnNp:P:e:E: flag; do + case "$flag" in + h) echo "$USAGE" 1>&2 + exit 2 + ;; + v) V_FLAG="$OPTARG" + ;; + V) echo "$VERSION" + exit 2 + ;; + n) NOOP="true" + ;; + N) DO_NOT_PROCESS="true" + ;; + p) PANDOC_TOOL="$OPTARG" + ;; + P) PANDOC_ARGS="$OPTARG" + ;; + e) echo "$OPTARG" 1>&2 + ;; + E) exit "$OPTARG" + ;; + \?) echo "$0: ERROR: invalid option: -$OPTARG" 1>&2 + echo 1>&2 + print_usage 1>&2 + exit 3 + ;; + :) echo "$0: ERROR: option -$OPTARG requires an argument" 1>&2 + echo 1>&2 + echo "$USAGE" 1>&2 + exit 3 + ;; + *) echo "$0: ERROR: unexpected value from getopts: $flag" 1>&2 + echo 1>&2 + echo "$USAGE" 1>&2 + exit 3 + ;; + esac +done + +# parse the command line arguments +# +if [[ $V_FLAG -ge 1 ]]; then + echo "$0: debug[1]: debug level: $V_FLAG" 1>&2 +fi +# +shift $(( OPTIND - 1 )); +# +if [[ $V_FLAG -ge 5 ]]; then + echo "$0: debug[5]: argument count: $#" 1>&2 +fi +if [[ $# -ne 2 ]]; then + echo "$0: ERROR: expected 2 args, found: $#" 1>&2 + exit 3 +fi +# +export MARKDOWN_INPUT="$1" +export HTML_OUTPUT="$2" +if [[ $V_FLAG -ge 1 ]]; then + echo "$0: debug[1]: MARKDOWN_INPUT=$MARKDOWN_INPUT" 1>&2 + echo "$0: debug[1]: HTML_OUTPUT=$HTML_OUTPUT" 1>&2 +fi + +# firewall - validate args +# +if [[ ! -e $MARKDOWN_INPUT ]]; then + echo "$0: ERROR: markdown input file does not exist: $MARKDOWN_INPUT" 1>&2 + exit 5 +fi +if [[ ! -f $MARKDOWN_INPUT ]]; then + echo "$0: ERROR: markdown input is not a file: $MARKDOWN_INPUT" 1>&2 + exit 5 +fi +if [[ ! -r $MARKDOWN_INPUT ]]; then + echo "$0: ERROR: markdown input is not a readable file: $MARKDOWN_INPUT" 1>&2 + exit 5 +fi +if [[ ! -x $PANDOC_TOOL ]]; then + echo "$0: ERROR: cannot find an executable pandoc tool: $PANDOC_TOOL" 1>&2 + exit 6 +fi + +# If -N, time to exit +# +if [[ -n $DO_NOT_PROCESS ]]; then + if [[ $V_FLAG -ge 1 ]]; then + echo "$0: debug[5]: arguments parsed, -N given, exit 0" 1>&2 + fi + exit 0 +fi + +# execute pandoc +# +if [[ -z $NOOP ]]; then + if [[ $V_FLAG -ge 1 ]]; then + echo "$0: debug[1]: will execute pandoc as: $PANDOC_TOOL -o $HTML_OUTPUT $PANDOC_ARGS $MARKDOWN_INPUT" 1>&2 + fi + # SC2086 (info): Double quote to prevent globbing and word splitting. + # shellcheck disable=SC2086 + "$PANDOC_TOOL" -o "$HTML_OUTPUT" $PANDOC_ARGS "$MARKDOWN_INPUT" + status="$?" +else + if [[ $V_FLAG -ge 1 ]]; then + echo "$0: debug[3]: will execute pandoc as: $PANDOC_TOOL $PANDOC_ARGS -o /dev/null $MARKDOWN_INPUT" 1>&2 + fi + # SC2086 (info): Double quote to prevent globbing and word splitting. + # shellcheck disable=SC2086 + "$PANDOC_TOOL" $PANDOC_ARGS -o /dev/null "$MARKDOWN_INPUT" + status="$?" +fi +if [[ $status -ne 0 ]]; then + echo "$0: ERROR: pandoc: $PANDOC_TOOL exited non-zero: $status" 1>&2 + exit 1 +fi + +# All Done!!! -- Jessica Noll, Age 2 +# exit 0