aboutsummaryrefslogtreecommitdiff
path: root/src/wrappers/markdown2pdf.in
blob: c222c1cbd894fcbc6a37ef8a538c218ff28a05ff (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
#!/bin/sh -e

REQUIRED="markdown2latex pdflatex"

### common.sh

### tempdir.sh

texname=output
logfile=$THIS_TEMPDIR/log

if ! markdown2latex -s -d "$@" >$THIS_TEMPDIR/$texname.tex 2>$logfile; then
    [ -f $logfile ] && sed -e 's/markdown2latex/markdown2pdf/g' \
        -e '/^INPUT=/d' -e '/^OUTPUT=/d' $logfile >&2
    exit 1
fi

outfile="$(sed -ne 's/^OUTPUT=//p' $logfile)"
IFS="$NEWLINE"
set -- $(sed -ne 's/^INPUT=//p' $logfile)
firstinfilebase="${1%.*}"
defaultdest="${firstinfilebase:-stdin}.pdf"
destname="${outfile:-$defaultdest}"

(
    cd $THIS_TEMPDIR
    if ! pdflatex -interaction=batchmode $texname.tex >/dev/null 2>&1; then
        err "LaTeX errors:"
        sed -ne '/^!/,/^ *$/p' $texname.log >&2
        if grep -q "File \`ucs.sty' not found" $texname.log; then
            err "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 "Please install the 'fancyvrb' package from CTAN:"
            err "http://www.ctan.org/tex-archive/macros/latex/contrib/fancyvrb/"
        fi
        exit 1
    fi
) || 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 .