Skip to content

Instantly share code, notes, and snippets.

@lebedov
Last active December 3, 2015 18:24
Show Gist options
  • Save lebedov/5f5a0c686008d77cf12c to your computer and use it in GitHub Desktop.
Save lebedov/5f5a0c686008d77cf12c to your computer and use it in GitHub Desktop.
Script for continuously previewing graphviz dot files using dot2tex.
#!/bin/bash
# Script for continuously previewing graphviz dot files using dot2tex.
# Copyright (c) 2015, Lev Givon
# All rights reserved
# Distributed under the terms of the BSD license:
# http://www.opensource.org/licenses/bsd-license
SCRIPT_NAME=`basename $0`
USAGE="Usage: $SCRIPT_NAME [-h] [-s] [dot2tex options] <input.dot>\n\nPreview graphviz dot file using dot2tex."
function cleanup() {
FILE_BASE=$1
PID=$2
kill -TERM $PID
latexmk -c $FILE_BASE
rm -f $FILE_BASE.pdf
rm -f $FILE_BASE.tex
exit 0
}
if ! [[ `which inotifywait` ]]; then
echo "inotifywait not found"
exit 1
fi
if ! [[ `which dot2tex` ]]; then
echo "dot2tex not found"
exit 1
elif ! [[ `which latexmk` ]]; then
echo "latexmk not found"
exit 1
else
if [[ $# == 0 ]]; then
echo -e $USAGE
exit 0
fi
DOT_FILE=''
OTHER_ARGS=''
for arg in $@; do
if [[ $arg =~ ^\-h$ ]]; then
echo -e $USAGE
exit 0
elif [[ $arg =~ ^\-S$ ]]; then
SAVE=1
elif [[ $arg =~ ^.*\.dot$ ]]; then
DOT_FILE=$arg
else
OTHER_ARGS="$OTHER_ARGS $arg"
fi
done
if [[ $DOT_FILE =~ ^\ *$ ]]; then
echo no .dot file specified
exit 1
fi
if ! [[ -e $DOT_FILE ]]; then
echo $DOT_FILE not found
exit 1
fi
FILE_BASE=`basename -s .dot $DOT_FILE`
TEX_FILE=$FILE_BASE.tex
dot2tex $OTHER_ARGS $DOT_FILE -o $TEX_FILE
if [[ $SAVE == 1 ]]; then
latexmk -pdf $TEX_FILE
latexmk -c $FILE_BASE
else
latexmk -pvc -pdf -interaction=nonstopmode $TEX_FILE &
# Clean up files created by dot2tex and latexmk:
trap "cleanup $FILE_BASE $!" SIGINT
# Rerun dot2tex when the dot file changes; need to run additional
# outer loop because inotifywait exits after a move_self event
# (which will occur if the dot file is edited with applications
# such as vim):
while true; do
while inotifywait -qq $DOT_FILE; do
dot2tex $OTHER_ARGS $DOT_FILE -o $TEX_FILE
done
done
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment