note-stitch/stitch

77 lines
1.4 KiB
Bash
Executable File

#!/usr/bin/env bash
usage() {
cat <<EOF
usage $0 [-o OUTPUT][-t TITLE][-h]
Options:
-o, --output saves file to OUTPUT.pdf in current directory
-t, --title title for the OUTPUT.pdf
-h, --help show this message
EOF
}
working_dir=$(pwd)
header="$working_dir/header.md"
formatting() {
cat <<EOF
---
toc-title: $1
documentclass: report
linkcolor: blue
geometry: margin=1in
---
EOF
}
# parsing long form
for arg in "$@"; do
shift
case "$arg" in
'--help') set -- "$@" '-h' ;;
'--title') set -- "$@" '-t' ;;
'--output') set -- "$@" '-o' ;;
*) set -- "$@" "$arg" ;;
esac
done
# parsing args
while getopts "ht:o:" opt ; do
case "$opt" in
'h')
usage
exit 0
;;
't')
TITLE="$OPTARG"
;;
'o')
OUTPUT="$OPTARG"
;;
'?')
usage
exit 1
;;
esac
done
if [ -z "$OUTPUT" ] ; then
# default to folder name
OUTPUT=$(echo "$working_dir" | awk -F '/' '{print $NF}')
fi
if [ -z "$TITLE" ] ; then
# defaults to output which will default to directory name
TITLE="$OUTPUT"
fi
# creates a temporary file for formatting
formatting "$TITLE" > $header
pandoc $header $working_dir/*.md --toc --toc-depth=1 -o "$OUTPUT.pdf"
# cleanup
rm "$working_dir/header.md"