dotfiles/.config/shell/bin/pandoc-make

53 lines
1.5 KiB
Text
Raw Normal View History

#!/bin/bash
# Simple script for compiling/converting documents with pandoc because I can never remember how to use it
# Usage:
# pandoc-make html *.md
2022-03-16 01:53:57 -07:00
usage() {
cat <<-EOF
Usage:
$0 [output_type] [pandoc_args for file 1] [pandoc_args for file 1] [input_files]
2022-03-16 01:53:57 -07:00
Examples:
$0 pdf *.md
$0 .docx *.md # leading .'s are automatically removed from the output format
Note:
1) To put a file in pandoc arguments without having it be compiled to its own document, put a './' or '../' at the start of the path to it
- You can have as many arg/file combinations you want
- The argument array is cleared between input files
2022-03-16 01:53:57 -07:00
EOF
# TODO:
# 2) You can set default arguments in the array PANDOC_DEFAULT_ARGS and export them for this script to use
2022-03-16 01:53:57 -07:00
}
2022-04-06 01:05:14 -07:00
# Help menu (yes, I forget how to use my own programs sometimes)
2022-04-01 06:23:18 -07:00
if [[ -z "$1" ]]; then
2022-03-16 01:53:57 -07:00
usage && exit 1
fi
2022-04-01 06:23:18 -07:00
if [[ "$1" == "-h" ]] || [[ "$1" == "--help" ]]; then
usage && exit
fi
2022-03-16 01:53:57 -07:00
2022-04-01 06:23:18 -07:00
if [[ -z "$2" ]]; then
2022-03-16 01:53:57 -07:00
echo "error: no files specified"
usage && exit 1
fi
OUTPUT_FORMAT="$1"
SKIP_FIRST=true # don't process the first argument, it is the output format
for FILE in "$@";do
2022-03-16 01:53:57 -07:00
$SKIP_FIRST && SKIP_FIRST=false && continue
# PANDOC_ARGS="${PANDOC_DEFAULT_ARGS[*]:-"$PANDOC_ARGS"}"
if [[ -f "$FILE" ]] && { [[ "${FILE:0:2}" != "./" ]] || [[ "${FILE:0:3}" != "../" ]]; }; then
pandoc -s "$FILE" "${PANDOC_ARGS[@]}" -o "${FILE%.*}.${OUTPUT_FORMAT##.}"
PANDOC_ARGS=()
else
PANDOC_ARGS+=("$FILE")
fi
done
2022-03-16 01:53:57 -07:00
# vim:et ts=3 sw=3