2022-03-16 01:32:08 -07:00
|
|
|
#!/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:
|
2022-04-13 01:37:22 -07:00
|
|
|
$0 [output_type] [pandoc_args for file 1] [pandoc_args for file 1] [input_files]
|
2022-03-16 01:32:08 -07:00
|
|
|
|
2022-03-16 01:53:57 -07:00
|
|
|
Examples:
|
2022-04-13 01:37:22 -07:00
|
|
|
$0 pdf *.md
|
|
|
|
$0 .docx *.md # leading .'s are automatically removed from the output format
|
|
|
|
|
|
|
|
Note:
|
2022-04-14 16:24:41 -07:00
|
|
|
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
|
2022-04-14 16:25:19 -07:00
|
|
|
# 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-03-16 01:32:08 -07:00
|
|
|
|
2022-04-14 21:37:00 -07:00
|
|
|
confirm() {
|
|
|
|
echo -n "$@"
|
|
|
|
while true; do
|
|
|
|
read -r YN
|
|
|
|
case "$YN" in
|
|
|
|
[Yy]) return 0 ;;
|
|
|
|
*) return 1 ;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
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
|
2022-04-13 01:37:22 -07:00
|
|
|
|
|
|
|
OUTPUT_FORMAT="$1"
|
2022-04-14 21:37:00 -07:00
|
|
|
SKIP=true # don't process the first argument, it is the output format
|
2022-03-16 01:32:08 -07:00
|
|
|
|
|
|
|
for FILE in "$@";do
|
2022-04-14 21:37:00 -07:00
|
|
|
$SKIP && SKIP=false && continue
|
|
|
|
|
2022-04-14 16:25:19 -07:00
|
|
|
# PANDOC_ARGS="${PANDOC_DEFAULT_ARGS[*]:-"$PANDOC_ARGS"}"
|
2022-04-14 16:24:41 -07:00
|
|
|
if [[ -f "$FILE" ]] && { [[ "${FILE:0:2}" != "./" ]] || [[ "${FILE:0:3}" != "../" ]]; }; then
|
2022-04-14 21:37:00 -07:00
|
|
|
|
|
|
|
OUTPUT_FILE="${FILE%.*}.${OUTPUT_FORMAT##.}"
|
|
|
|
|
|
|
|
# Guard against overwriting md files and tex files
|
|
|
|
if { [[ "$OUTPUT_FORMAT" = md ]] || [[ "$OUTPUT_FORMAT" = tex ]]; } && [[ -f "$OUTPUT_FILE" ]]; then
|
|
|
|
if confirm "Overwrite $FILE? [y/N]: "; then
|
|
|
|
# Make new file names until there is no overwriting
|
|
|
|
while [[ -f "$OUTPUT_FILE" ]]; do
|
|
|
|
OUTPUT_FILE="${FILE%.*}-$((FILE_NUM++)).${OUTPUT_FORMAT##.}"
|
|
|
|
done
|
|
|
|
echo "Not overwriting, new name: '$OUTPUT_FILE'"
|
2022-05-27 17:26:13 -07:00
|
|
|
|
2022-04-14 21:37:00 -07:00
|
|
|
else
|
|
|
|
# Give some time to ctrl-c before overwrite…
|
|
|
|
SLEEP_DURATION=1
|
|
|
|
echo -n "'$FILE' will be overwritten in"
|
|
|
|
for N in {3..1};do
|
|
|
|
echo -n " $N" && sleep $SLEEP_DURATION
|
|
|
|
done
|
|
|
|
echo
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
pandoc "${PANDOC_ARGS[@]}" -o "$OUTPUT_FILE" "$FILE"
|
2022-04-13 01:37:22 -07:00
|
|
|
PANDOC_ARGS=()
|
2022-04-14 21:37:00 -07:00
|
|
|
|
2022-04-13 01:37:22 -07:00
|
|
|
else
|
2022-04-14 21:37:00 -07:00
|
|
|
|
2022-04-13 01:37:22 -07:00
|
|
|
PANDOC_ARGS+=("$FILE")
|
2022-04-14 21:37:00 -07:00
|
|
|
|
2022-03-16 01:32:08 -07:00
|
|
|
fi
|
|
|
|
done
|
2022-03-16 01:53:57 -07:00
|
|
|
|
2022-05-27 17:26:13 -07:00
|
|
|
# Error if no files were processed
|
|
|
|
if [ -z "$OUTPUT_FILE" ]; then
|
|
|
|
echo "Error: no files to process were found" >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2022-03-16 01:53:57 -07:00
|
|
|
# vim:et ts=3 sw=3
|