aboutsummaryrefslogtreecommitdiff
path: root/markdown2pdf
blob: ab0f3ae78cd62a059037a39f753ebd2129cd69ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/bin/sh -e

REQUIRED="pdflatex"
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
    err "See the $1(1) man page for usage."
}

# 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

CONF=$(pandoc --dump-args "$@" 2>&1) || {
    errcode=$?
    echo "$CONF" | sed -e '/^pandoc \[OPTIONS\] \[FILES\]/,$d' >&2
    [ $errcode -eq 2 ] && usage "$THIS" "$SYNOPSIS"
    exit $errcode
}

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
      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 \`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 ] && 
      ((grep -q "LaTeX Warning: There were undefined references." $texname.log) ||
       (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 $?

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 .