summaryrefslogtreecommitdiff
path: root/markdown2pdf
blob: 6a76aab7d0a53cd0ff37d07e6bcaa98cd16c1369 (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
#!/bin/sh -e
# converts markdown to latex, then uses latex to make a PDF

[ -n "$(which pandoc)" ] || {
    echo >&2 "You need 'pandoc' to use this program!"
    exit 1
}
[ -n "$(which pdflatex)" ] || {
    echo >&2 "You need 'pdflatex' to use this program!"
    exit 1
}

TEMP=${TMPDIR-/tmp}/markdown2pdf.$$
trap "status=$?; rm -rf $TEMP; exit $status" 0 INT

if [ -z "$1" ]; then
    BASE='stdin' # input is STDIN, since no argument given
else
    filename=${1##*/}
    BASE=${filename%\.*}
fi

mkdir -p $TEMP && iconv -t utf-8 $* | pandoc -w latex -s > $TEMP/$BASE.tex  
(
    cd $TEMP
    if ! pdflatex -interaction=batchmode $BASE.tex >/dev/null 2>&1; then
	echo >&2 "LaTeX errors:"
	cat >&2 $BASE.log
	exit 1
    fi
) || exit $?

is_target_exists=
if [ -f $BASE.pdf ]; then
    is_target_exists=1
fi

cp --suffix=~ --backup $TEMP/$BASE.pdf .

echo -n >&2 "Created $BASE.pdf"
[ -z "$is_target_exists" ] || {
    echo -n >&2 " (previous file has been backed up as '$BASE.pdf~')"
}
echo >&2 .