pandoc/src/wrappers/markdown2pdf.in

62 lines
1.7 KiB
Text
Raw Normal View History

#!/bin/sh -e
REQUIRED="pdflatex"
SYNOPSIS="converts markdown-formatted text to PDF, using pdflatex."
### common.sh
### tempdir.sh
texname=output
logfile=$THIS_TEMPDIR/log
pandoc -s -r markdown -w latex "$@" -o $THIS_TEMPDIR/$texname.tex 2>$logfile
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
pdflatex -interaction=batchmode $texname.tex >/dev/null || {
errcode=$?
err "${THIS}: pdfLaTeX failed with error code $errcode"
[ -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 \`fancyvrb.sty' not found" $texname.log; then
err "${THIS}: Please install the 'fancyvrb' package from CTAN:"
err " http://www.ctan.org/tex-archive/macros/latex/contrib/fancyvrb/"
fi
}
exit $errcode
}
) || exit $?
is_target_exists=
if [ -f "$destname" ]; then
is_target_exists=1
mv "$destname" "$destname~"
fi
mv -f $THIS_TEMPDIR/$texname.pdf "$destname"
errn "Created $destname"
[ -z "$is_target_exists" ] || {
errn " (previous file has been backed up as $destname~)"
}
err .