Skip to content

Instantly share code, notes, and snippets.

@mmcclimon
Created November 4, 2013 23:56
Show Gist options
  • Save mmcclimon/7311538 to your computer and use it in GitHub Desktop.
Save mmcclimon/7311538 to your computer and use it in GitHub Desktop.
Automatically converts Pandoc Markdown to PDF with live preview, via latexmk.
#!/bin/bash
############################
# CONFIG: Change these as necessary
############################
# pandoc
PANDOC_CMD="pandoc"
PANDOC_OPTS="-s --latex-engine=xelatex"
# latexmk
LATEXMK_CMD="latexmk"
LATEXMK_OPTS="-xelatex -pvc"
SLEEP_TIME=3 # in seconds
############################
path=$1
# must have a filename
if [ -z "$path" ]
then
echo "Must provide a path to file"
exit 1
fi
# check to make sure this is a markdown file
name=$( basename $path )
ext=${name##*.}
if [ "$ext" != "md" ]
then
echo "File must be a markdown file (with extension .md)"
exit 1
fi
# time to sleep in between checks
# command to run
basename=${name%.*}
tex="${basename}.tex"
cmd="${PANDOC_CMD} ${PANDOC_OPTS} -o ${tex} ${path}"
echo "Listening for changes to ${path}"
echo "Will run this command on change:"
echo " ${cmd}"
echo "(Ctrl-C to exit)"
echo
sleep 1
while true
do
ATIME=`stat -f %Z ${path}`
if [[ "$ATIME" != "$LTIME" ]]
then
eval $cmd
LTIME=$ATIME
fi
sleep "$SLEEPTIME"
done &
eval "${LATEXMK_CMD} ${LATEXMK_OPTS} ${basename}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment