2008-08-27 07:50:26 +02:00
|
|
|
#!/bin/sh -e
|
|
|
|
|
2009-12-31 02:18:06 +01:00
|
|
|
latexprogram=pdflatex
|
|
|
|
if (echo "$@" | grep -q xetex); then
|
|
|
|
latexprogram=xelatex
|
|
|
|
fi
|
|
|
|
|
|
|
|
REQUIRED=$latexprogram
|
2008-08-27 07:50:26 +02:00
|
|
|
SYNOPSIS="converts markdown-formatted text to PDF, using pdflatex."
|
|
|
|
|
|
|
|
THIS=${0##*/}
|
|
|
|
|
|
|
|
NEWLINE='
|
|
|
|
'
|
|
|
|
|
|
|
|
err () { echo "$*" | fold -s -w ${COLUMNS:-110} >&2; }
|
|
|
|
errn () { printf "$*" | fold -s -w ${COLUMNS:-110} >&2; }
|
|
|
|
|
|
|
|
usage () {
|
|
|
|
err "$1 - $2" # short description
|
2010-01-02 19:18:59 +01:00
|
|
|
err "Options:"
|
|
|
|
pandoc -h | sed -ne '/--strict\|--preserve-tabs\|--tab-stop\|--parse-raw\|--toc\|--xetex\|--number-sections\|--include-in-header\|--include-before-body\|--include-after-body\|--custom-header\|--output\|--template\|--variable\|--help\|--version/p' >&2
|
2008-08-27 07:50:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# Portable which(1).
|
|
|
|
pathfind () {
|
|
|
|
oldifs="$IFS"; IFS=':'
|
|
|
|
for _p in $PATH; do
|
|
|
|
if [ -x "$_p/$*" ] && [ -f "$_p/$*" ]; then
|
|
|
|
IFS="$oldifs"
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
IFS="$oldifs"
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
for p in pandoc $REQUIRED; do
|
|
|
|
pathfind $p || {
|
|
|
|
err "You need '$p' to use this program!"
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
done
|
|
|
|
|
2010-01-02 19:18:59 +01:00
|
|
|
if (echo "$@" | grep -q -- "--help\|-h"); then
|
|
|
|
usage "$THIS" "$SYNOPSIS"
|
|
|
|
exit 0
|
|
|
|
fi
|
2008-08-27 07:50:26 +02:00
|
|
|
|
2010-01-02 19:18:59 +01:00
|
|
|
CONF=$(pandoc --dump-args "$@") || exit $?
|
2008-08-27 07:50:26 +02:00
|
|
|
OUTPUT=$(echo "$CONF" | sed -ne '1p')
|
|
|
|
ARGS=$(echo "$CONF" | sed -e '1d')
|
|
|
|
|
|
|
|
# As a security measure refuse to proceed if mktemp is not available.
|
|
|
|
pathfind mktemp || { err "Couldn't find 'mktemp'; aborting."; exit 1; }
|
|
|
|
|
|
|
|
# Avoid issues with /tmp directory on Windows/Cygwin
|
|
|
|
cygwin=
|
|
|
|
cygwin=$(uname | sed -ne '/^CYGWIN/p')
|
|
|
|
if [ -n "$cygwin" ]; then
|
|
|
|
TMPDIR=.
|
|
|
|
export TMPDIR
|
|
|
|
fi
|
|
|
|
|
|
|
|
THIS_TEMPDIR=
|
|
|
|
THIS_TEMPDIR="$(mktemp -d -t $THIS.XXXXXXXX)" || exit 1
|
|
|
|
readonly THIS_TEMPDIR
|
|
|
|
|
|
|
|
trap 'exitcode=$?
|
|
|
|
[ -z "$THIS_TEMPDIR" ] || rm -rf "$THIS_TEMPDIR"
|
|
|
|
exit $exitcode' 0 1 2 3 13 15
|
|
|
|
|
|
|
|
texname=output
|
|
|
|
logfile=$THIS_TEMPDIR/log
|
|
|
|
|
|
|
|
pandoc -s -r markdown -w latex "$@" -o $THIS_TEMPDIR/$texname.tex
|
|
|
|
|
|
|
|
if [ "$OUTPUT" = "-" ]; then
|
|
|
|
firstinfile="$(echo $ARGS | sed -ne '1p')"
|
|
|
|
firstinfilebase="${firstinfile%.*}"
|
|
|
|
destname="${firstinfilebase:-stdin}.pdf"
|
|
|
|
else
|
|
|
|
destname="$OUTPUT"
|
|
|
|
fi
|
|
|
|
|
|
|
|
(
|
|
|
|
origdir=$(pwd)
|
|
|
|
cd $THIS_TEMPDIR
|
|
|
|
TEXINPUTS=$origdir:$TEXINPUTS:
|
|
|
|
export TEXINPUTS
|
|
|
|
finished=no
|
|
|
|
runs=0
|
|
|
|
while [ $finished = "no" ]; do
|
2009-12-31 02:18:06 +01:00
|
|
|
$latexprogram -interaction=batchmode $texname.tex >/dev/null || {
|
2008-08-27 07:50:26 +02:00
|
|
|
errcode=$?
|
2009-12-31 02:18:06 +01:00
|
|
|
err "${THIS}: $latexprogram failed with error code $errcode"
|
2008-08-27 07:50:26 +02:00
|
|
|
[ -f $texname.log ] && {
|
|
|
|
err "${THIS}: error context:"
|
|
|
|
sed -ne '/^!/,/^[[:space:]]*$/p' \
|
|
|
|
-ne '/^[Ll]a[Tt]e[Xx] [Ww]arning/,/^[[:space:]]*$/p' \
|
|
|
|
-ne '/^[Ee]rror/,/^[[:space:]]*$/p' $texname.log >&2
|
|
|
|
if grep -q "File \`ucs.sty' not found" $texname.log; then
|
|
|
|
err "${THIS}: Please install the 'unicode' package from CTAN:"
|
|
|
|
err " http://www.ctan.org/tex-archive/macros/latex/contrib/unicode/"
|
|
|
|
fi
|
|
|
|
if grep -q "File \`ulem.sty' not found" $texname.log; then
|
|
|
|
err "${THIS}: Please install the 'ulem' package from CTAN:"
|
|
|
|
err " http://www.ctan.org/tex-archive/macros/latex/contrib/misc/ulem.sty"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
exit $errcode
|
|
|
|
}
|
|
|
|
if [ $runs -lt 3 ] &&
|
2010-01-02 19:18:59 +01:00
|
|
|
((grep -q "Warning: There were undefined references." $texname.log) ||
|
2008-08-27 07:50:26 +02:00
|
|
|
(echo "$@" | grep -q -- "--toc\|--table-of-contents")); then
|
|
|
|
runs=$(($runs + 1))
|
|
|
|
if grep -q "LaTeX Warning:.*[Cc]itation" $texname.log; then
|
|
|
|
bibtex $texname 2>&1 >bibtex.err
|
|
|
|
if [ $runs -gt 2 ]; then
|
|
|
|
if grep -q "error message" bibtex.err ||
|
|
|
|
grep -q "Warning" bibtex.err; then
|
|
|
|
cat bibtex.err >&2
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
finished=yes
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
) || exit $?
|
|
|
|
|
|
|
|
mv -f $THIS_TEMPDIR/$texname.pdf "$destname"
|
|
|
|
|
2009-11-22 19:07:33 +01:00
|
|
|
err "Created $destname"
|