pandoc/markdown2pdf
roktas 3ed8fc8784 Portability fixes and various cleanups in wrapper scripts:
+ Fix the tests at the header of wrappers.  which(1) doesn't behave as 
  expected on some systems.  We should only assume that it's pretty widely 
  available (for example, it's a builtin in csh) and we should only rely on 
  its exit code by ignoring its output.
+ Replace 'echo -n' with 'printf' as the latter is recommended.
+ In markdown2pdf script, '--suffix' and '--backup' options of mv(1) appear 
  to be GNU-ism.  Apply a workaround.
+ Wrap some long lines to fit in an 80-column screen.
+ Remove spaces at the line ends.


git-svn-id: https://pandoc.googlecode.com/svn/trunk@92 788f1e2b-df1e-0410-8736-df70ead52e1b
2006-11-12 12:11:25 +00:00

82 lines
1.5 KiB
Bash

#!/bin/sh
# converts markdown to latex, then uses latex to make a PDF
for p in pandoc pdflatex; do
which $p >/dev/null 2>&1 || {
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 .