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