git.iko.soy/typeable/pandoc
on fc8a031 help sign in
typeable/pandoc/markdown2pdf
82 lines 1.6 kB history raw
1#!/bin/sh
2# converts markdown to latex, then uses latex to make a PDF
4[ -n "$(which pandoc)" ] || {
5 echo >&2 "You need 'pandoc' to use this program!"
6 exit 1
7}
8[ -n "$(which pdflatex)" ] || {
9 echo >&2 "You need 'pdflatex' to use this program!"
10 exit 1
13outfile=
14for option; do
15 if [ -n "$prev" ]; then
16 eval "$prev=\$option"
17 prev=
18 shift
19 continue
20 fi
21 optarg=$(expr "x$option" : 'x[^=]*=\(.*\)')
22 case $option in
23 -h | --h | --help )
24 help=1
25 shift ;;
26 -o | --o | --output )
27 prev=outfile
28 shift ;;
29 -o=* | --o=* | --output=* )
30 outfile=$optarg
31 shift ;;
32 -* ) echo >&2 "$0: unknown option: $option; aborting"
33 exit 1 ;;
34 * ) break ;;
35 esac
36done
38[ -z "$help" ] || {
39 echo >&2 "Usage: $0 [-o|--output output-file] [input-file]"
40 exit 0
43infile=$1
44if [ -z "$outfile" ]; then
45 if [ -n "$infile" ]; then
46 outfile=${infile%.*}.pdf
47 else
48 outfile='stdin.pdf' # input is STDIN, since no argument given
49 fi
50fi
52BASE=${outfile##*/}
53BASE=${BASE%.*}
55set -e
57TEMP=${TMPDIR-/tmp}/markdown2pdf.$$
58trap "status=$?; rm -rf $TEMP; exit $status" 0 INT
59mkdir -p $TEMP
61iconv -t utf-8 $infile | pandoc -w latex -s > $TEMP/$BASE.tex
63 cd $TEMP
64 if ! pdflatex -interaction=batchmode $BASE.tex >/dev/null 2>&1; then
65 echo >&2 "LaTeX errors:"
66 cat >&2 $BASE.log
67 exit 1
68 fi
69) || exit $?
71is_target_exists=
72if [ -f $outfile ]; then
73 is_target_exists=1
74fi
76mv --suffix=~ --backup $TEMP/$BASE.pdf $outfile
78echo -n >&2 "Created $outfile"
79[ -z "$is_target_exists" ] || {
80 echo -n >&2 " (previous file has been backed up as '$outfile~')"
82echo >&2 .