Skip to content

Instantly share code, notes, and snippets.

@gordjw
Created January 4, 2016 23:27
Show Gist options
  • Save gordjw/c5e72d1d491d4348685a to your computer and use it in GitHub Desktop.
Save gordjw/c5e72d1d491d4348685a to your computer and use it in GitHub Desktop.
Watches a directory for Markdown file (*.md) changes, and refreshes output formats (PDF, DOCX, RTF)
#!/bin/bash
#
# Panda
#
# Watches a directory for Markdown file (*.md) changes, and refreshes output formats (PDF, DOCX, RTF)
#
PANDOC=`/usr/bin/which pandoc`
PANDOC_IN=/tmp/pandoc/in/
PANDOC_OUT=/tmp/pandoc/out/
INOT=`/usr/bin/which inotifywait`
# Pandoc and inotifywait are required
if [ -z $PANDOC ]
then
echo "Pandoc was not found on this system."
exit 1
fi
if [ -z $INOT ]
then
echo "inotifywait was not found on this system."
exit 1
fi
# Create the input/output directories if they don't exits
mkdir -p $PANDOC_IN
mkdir -p $PANDOC_OUT
# Watch the input dir for file create, incoming moves and modifications
$INOT -m ${PANDOC_IN} -e create -e moved_to -e modify |
while read path action file; do
# Split the filename into two parts
FILENAME="${file%.*}"
EXT=${file##*.}
IN=${path}${file}
OUT=${PANDOC_OUT}${FILENAME}
# We're only concerned with markdown docs, ending in .md
if [ $EXT = "md" ]
then
echo "$path$file was modified by $action"
echo "Producing outputs at ${OUT}.{docx,pdf,rtf}"
$PANDOC -s -S "${IN}" -o "${OUT}.docx"
$PANDOC -s -S "${IN}" -t latex -o "${OUT}.pdf"
$PANDOC -s -S "${IN}" -o "${OUT}.rtf"
echo "Done."
echo "===================="
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment