git.iko.soy/typeable/pandoc
on 1.5 help sign in
typeable/pandoc/markdown2pdf
136 lines 3.9 kB history raw
1#!/bin/sh -e
3latexprogram=pdflatex
4if (echo "$@" | grep -q xetex); then
5 latexprogram=xelatex
6fi
8REQUIRED=$latexprogram
9SYNOPSIS="converts markdown-formatted text to PDF, using pdflatex."
11THIS=${0##*/}
13NEWLINE='
16err () { echo "$*" | fold -s -w ${COLUMNS:-110} >&2; }
17errn () { printf "$*" | fold -s -w ${COLUMNS:-110} >&2; }
19usage () {
20 err "$1 - $2" # short description
21 err "Options:"
22 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
25# Portable which(1).
26pathfind () {
27 oldifs="$IFS"; IFS=':'
28 for _p in $PATH; do
29 if [ -x "$_p/$*" ] && [ -f "$_p/$*" ]; then
30 IFS="$oldifs"
31 return 0
32 fi
33 done
34 IFS="$oldifs"
35 return 1
38for p in pandoc $REQUIRED; do
39 pathfind $p || {
40 err "You need '$p' to use this program!"
41 exit 1
42 }
43done
45if (echo "$@" | grep -q -- "--help\|-h"); then
46 usage "$THIS" "$SYNOPSIS"
47 exit 0
48fi
50CONF=$(pandoc --dump-args "$@") || exit $?
51OUTPUT=$(echo "$CONF" | sed -ne '1p')
52ARGS=$(echo "$CONF" | sed -e '1d')
54# As a security measure refuse to proceed if mktemp is not available.
55pathfind mktemp || { err "Couldn't find 'mktemp'; aborting."; exit 1; }
57# Avoid issues with /tmp directory on Windows/Cygwin
58cygwin=
59cygwin=$(uname | sed -ne '/^CYGWIN/p')
60if [ -n "$cygwin" ]; then
61 TMPDIR=.
62 export TMPDIR
63fi
65THIS_TEMPDIR=
66THIS_TEMPDIR="$(mktemp -d -t $THIS.XXXXXXXX)" || exit 1
67readonly THIS_TEMPDIR
69trap 'exitcode=$?
70 [ -z "$THIS_TEMPDIR" ] || rm -rf "$THIS_TEMPDIR"
71 exit $exitcode' 0 1 2 3 13 15
73texname=output
74logfile=$THIS_TEMPDIR/log
76pandoc -s -r markdown -w latex "$@" -o $THIS_TEMPDIR/$texname.tex
78if [ "$OUTPUT" = "-" ]; then
79 firstinfile="$(echo $ARGS | sed -ne '1p')"
80 firstinfilebase="${firstinfile%.*}"
81 destname="${firstinfilebase:-stdin}.pdf"
82else
83 destname="$OUTPUT"
84fi
87 origdir=$(pwd)
88 cd $THIS_TEMPDIR
89 TEXINPUTS=$origdir:$TEXINPUTS:
90 export TEXINPUTS
91 finished=no
92 runs=0
93 while [ $finished = "no" ]; do
94 $latexprogram -interaction=batchmode $texname.tex >/dev/null || {
95 errcode=$?
96 err "${THIS}: $latexprogram failed with error code $errcode"
97 [ -f $texname.log ] && {
98 err "${THIS}: error context:"
99 sed -ne '/^!/,/^[[:space:]]*$/p' \
100 -ne '/^[Ll]a[Tt]e[Xx] [Ww]arning/,/^[[:space:]]*$/p' \
101 -ne '/^[Ee]rror/,/^[[:space:]]*$/p' $texname.log >&2
102 if grep -q "File \`ucs.sty' not found" $texname.log; then
103 err "${THIS}: Please install the 'unicode' package from CTAN:"
104 err " http://www.ctan.org/tex-archive/macros/latex/contrib/unicode/"
105 fi
106 if grep -q "File \`ulem.sty' not found" $texname.log; then
107 err "${THIS}: Please install the 'ulem' package from CTAN:"
108 err " http://www.ctan.org/tex-archive/macros/latex/contrib/misc/ulem.sty"
109 fi
110 }
111 exit $errcode
112 }
113 if [ $runs -lt 3 ] &&
114 ((grep -q "Warning: There were undefined references." $texname.log) ||
115 (echo "$@" | grep -q -- "--toc\|--table-of-contents")); then
116 runs=$(($runs + 1))
117 if grep -q "LaTeX Warning:.*[Cc]itation" $texname.log; then
118 bibtex $texname 2>&1 >bibtex.err
119 if [ $runs -gt 2 ]; then
120 if grep -q "error message" bibtex.err ||
121 grep -q "Warning" bibtex.err; then
122 cat bibtex.err >&2
123 fi
124 fi
125 fi
126 elif [ $runs -gt 1 ]; then # always run at least twice for pdf bookmarks
127 finished=yes
128 else
129 runs=$(($runs + 1))
130 fi
131 done
132) || exit $?
134mv -f $THIS_TEMPDIR/$texname.pdf "$destname"
136err "Created $destname"