blob: 59c0ba634ca6a6d6b214bd5626184e6393c04f6a (
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
|
#!/bin/sh
# converts markdown to latex, then uses latex to make a PDF
pathfind () { # portable which(1), code taken from Debian Developer's Reference
OLDIFS="$IFS"
IFS=:
for _p in $PATH; do
if [ -x "$_p/$*" ]; then
IFS="$OLDIFS"
return 0
fi
done
IFS="$OLDIFS"
return 1
}
for p in pandoc pdflatex; do
pathfind $p || {
echo >&2 "You need '$p' to use this program!"
exit 1
}
done
outfile=
for option; do
if [ -n "$prev" ]; then
eval "$prev=\$option"
prev=
shift
continue
fi
optarg=$(expr "x$option" : 'x[^=]*=\(.*\)')
case $option in
-h | --h | --help )
help=1
shift ;;
-o | --o | --output )
prev=outfile
shift ;;
-o=* | --o=* | --output=* )
outfile=$optarg
shift ;;
-* ) echo >&2 "$0: unknown option: $option; aborting"
exit 1 ;;
* ) break ;;
esac
done
[ -z "$help" ] || {
echo >&2 "Usage: $0 [-o|--output output-file] [input-file]"
exit 0
}
infile=$1
if [ -z "$outfile" ]; then
if [ -n "$infile" ]; then
outfile=${infile%.*}.pdf
else
outfile='stdin.pdf' # input is STDIN, since no argument given
fi
fi
BASE=${outfile##*/}
BASE=${BASE%.*}
set -e
TEMP=${TMPDIR-/tmp}/markdown2pdf.$$
trap "status=$?; rm -rf $TEMP; exit $status" 0 INT
mkdir -p $TEMP
iconv -t utf-8 $infile | \
pandoc $PANDOC_OPTS -w latex -s | \
iconv -f utf-8 > $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 $outfile ]; then
is_target_exists=1
mv -f $outfile $outfile~
fi
mv -f $TEMP/$BASE.pdf $outfile
printf "Created $outfile" >&2
[ -z "$is_target_exists" ] || {
printf " (previous file has been backed up as '$outfile~')" >&2
}
echo >&2 .
|