git.iko.soy/typeable/pandoc
on master help sign in
Modified shell scripts to use new Pandoc --dump-args and

--ignore-args features. This allows a simpler, cleaner design. Make use of TEXINPUTS environment variable to ensure that pdflatex will find images and other sources in the working directory from which markdown2pdf is called. git-svn-id: https://pandoc.googlecode.com/svn/trunk@456 788f1e2b-df1e-0410-8736-df70ead52e1b

commit58697ebe785b1f7eb4713d8874cd5aaae4d6a03b authorfiddlosopher <fiddlosopher@788f1e2b-df1e-0410-8736-df70ead52e1b> dateMon 8 Jan 2007, 19:55 (20 years ago) parent9eafa97
3 files changed+70−71
src/wrappers/common.sh+16 src/wrappers/html2markdown.in+37−53 src/wrappers/markdown2pdf.in+17−18
src/wrappers/common.sh+16
@@ -6,6 +6,11 @@ NEWLINE='
66err () { echo "$*" | fold -s -w ${COLUMNS:-110} >&2; }
77errn () { printf "$*" | fold -s -w ${COLUMNS:-110} >&2; }
88
9+usage () {
10+ err "$1 - $2" # short description
11+ err "See the $1(1) man page for usage."
12+}
13+
914# Portable which(1).
1015pathfind () {
1116 oldifs="$IFS"; IFS=':'
@@ -25,3 +30,14 @@ for p in pandoc $REQUIRED; do
2530 exit 1
2631 }
2732done
33+
34+CONF=$(pandoc --dump-args "$@" 2>&1) || {
35+ errcode=$?
36+ echo "$CONF" | sed -e '/^pandoc \[OPTIONS\] \[FILES\]/,$d' >&2
37+ [ $errcode -eq 2 ] && usage "$THIS" "$SYNOPSIS"
38+ exit $errcode
39+}
40+
41+OUTPUT=$(echo "$CONF" | sed -ne '1p')
42+ARGS=$(echo "$CONF" | sed -e '1d')
43+
src/wrappers/html2markdown.in+37−53
@@ -3,6 +3,7 @@
33# uses an available program to fetch URL and tidy to normalize it first
44
55REQUIRED="tidy"
6+SYNOPSIS="converts HTML from a URL, file, or STDIN to markdown-formatted text."
67
78### common.sh
89
@@ -59,63 +60,46 @@ grab_url_with () {
5960 $prog "$@" "$url"
6061}
6162
62−add_option () {
63− options="$options$NEWLINE$1"
64−}
65−
66−options=
6763argument=
6864encoding=
6965grabber=
7066
7167# Parse command-line arguments
72−while [ $# -gt 0 ]; do
73− case "$1" in
74− -h|--help)
75− pandoc -h 2>&1 | sed -e 's/pandoc/html2markdown/' \
76− -e '/^[[:space:]]*\(-f\|-t\|-S\|-N\|-m\|-i\|-c\|-T\|-D\|-d\)/,/./d'\
77− 1>&2
78− err " -e ENCODING, --encoding=ENCODING"
79− err " Specify character encoding of input"
80− err " -g COMMAND, --grabber=COMMAND"
81− err " Specify command to be used to grab contents of URL"
82− exit 0 ;;
83− -v|--version)
84− pandoc -v 2>&1 | sed -e 's/pandoc/html2markdown/' 1>&2
85− exit 0 ;;
86− -e)
87− shift
88− encoding=$1 ;;
89− --encoding=*)
90− wholeopt=$1
91− # extract encoding from after =
92− encoding=${wholeopt#*=} ;;
93− -g)
94− shift
95− grabber=$1 ;;
96− --grabber=*)
97− wholeopt=$1
98− # extract encoding from after =
99− grabber=${wholeopt#*=} ;;
100− -o|--output|-b|--tab-stop|-H|--include-in-header| \
101− -A|--include-after-body|-C|-B|--include-before-body| \
102− -C|--custom-header|-T|--title-prefix)
103− add_option $1
104− shift
105− add_option $1 ;;
106− -*) add_option $1 ;;
107− *)
108− if [ -z "$argument" ]; then
109− argument=$1
110− else
111− err "Warning: extra argument '$1' will be ignored."
112− fi ;;
113− esac
114− shift
115−done
68+parse_arguments () {
69+ while [ $# -gt 0 ]; do
70+ case "$1" in
71+ --encoding=*)
72+ wholeopt="$1"
73+ # extract encoding from after =
74+ encoding="${wholeopt#*=}" ;;
75+ -e|--encoding|-encoding)
76+ shift
77+ encoding="$1" ;;
78+ --grabber=*)
79+ wholeopt="$1"
80+ # extract encoding from after =
81+ grabber="\"${wholeopt#*=}\"" ;;
82+ -g|--grabber|-grabber)
83+ shift
84+ grabber="$1" ;;
85+ *)
86+ if [ -z "$argument" ]; then
87+ argument="$1"
88+ else
89+ err "Warning: extra argument '$1' will be ignored."
90+ fi ;;
91+ esac
92+ shift
93+ done
94+ export encoding
95+ export grabber
96+ export argument
97+}
11698
117−# Unpack options. Now "$@" will hold the pandoc options.
118−oldifs="$IFS"; IFS="$NEWLINE"; set -- $options; IFS="$oldifs"
99+oldifs="$IFS"
100+IFS=$NEWLINE
101+parse_arguments $ARGS
102+IFS="$oldifs"
119103
120104inurl=
121105if [ -n "$argument" ] && ! [ -f "$argument" ]; then
@@ -164,11 +148,11 @@ else # assume UTF-8
164148fi
165149
166150if [ -z "$argument" ]; then
167− tidy -utf8 2>/dev/null | pandoc -r html -w markdown "$@"
151+ tidy -utf8 2>/dev/null | pandoc --ignore-args -r html -w markdown "$@"
168152else
169153 if [ -f "$argument" ]; then
170154 to_utf8 "$argument" |
171− tidy -utf8 2>/dev/null | pandoc -r html -w markdown "$@"
155+ tidy -utf8 2>/dev/null | pandoc --ignore-args -r html -w markdown "$@"
172156 else
173157 err "File '$argument' not found."
174158 exit 1
src/wrappers/markdown2pdf.in+17−18
@@ -1,6 +1,7 @@
11#!/bin/sh -e
22
33REQUIRED="pdflatex"
4+SYNOPSIS="converts markdown-formatted text to PDF, using pdflatex."
45
56### common.sh
67
@@ -8,27 +9,25 @@ REQUIRED="pdflatex"
89
910texname=output
1011logfile=$THIS_TEMPDIR/log
12+origdir=$(pwd)
1113
12−if ! pandoc -s -d -r markdown -w latex "$@" >$THIS_TEMPDIR/$texname.tex \
13−2>$logfile; then
14− [ -f $logfile ] && sed -e 's/^pandoc/markdown2pdf/g' \
15− -e '/^INPUT=/d' -e '/^OUTPUT=/d' \
16− -e '/^[[:space:]]*\(-f\|-t\|-s\|-R\|-S\|-m\|-i\|-c\|-T\|-D\|-d\)/,/./d'\
17− -e 's/(implies -s)//g' $logfile >&2
18− exit 1
19−fi
14+pandoc -s -r markdown -w latex "$@" -o - >$THIS_TEMPDIR/$texname.tex \
15+2>$logfile
2016
21−outfile="$(sed -ne 's/^OUTPUT=//p' $logfile)"
22−IFS="$NEWLINE"
23−set -- $(sed -ne 's/^INPUT=//p' $logfile)
24−firstinfilebase="${1%.*}"
25−defaultdest="${firstinfilebase:-stdin}.pdf"
26−destname="${outfile:-$defaultdest}"
17+if [ "$OUTPUT" = "-" ]; then
18+ firstinfile="$(echo $ARGS | sed -ne '1p')"
19+ firstinfilebase="${firstinfile%.*}"
20+ destname="${firstinfilebase:-stdin}.pdf"
21+else
22+ destname="$OUTPUT"
23+fi
2724
2825(
2926 cd $THIS_TEMPDIR
30− if ! pdflatex -interaction=batchmode $texname.tex >/dev/null 2>&1; then
31− err "${THIS}: pdfLaTeX error context:"
27+ TEXINPUTS=$TEXINPUTS:$origdir pdflatex -interaction=batchmode \
28+ $texname.tex >/dev/null 2>&1 || {
29+ errorcode=$?
30+ err "${THIS}: pdfLaTeX error context:"
3231 sed -ne '/^!/,/^[[:space:]]*$/p' \
3332 -ne '/^[Ll]a[Tt]e[Xx] [Ww]arning/,/^[[:space:]]*$/p' \
3433 -ne '/^[Ee]rror/,/^[[:space:]]*$/p' $texname.log >&2
@@ -40,8 +39,8 @@ destname="${outfile:-$defaultdest}"
4039 err "${THIS}: Please install the 'fancyvrb' package from CTAN:"
4140 err " http://www.ctan.org/tex-archive/macros/latex/contrib/fancyvrb/"
4241 fi
43− exit 1
44− fi
42+ exit $errorcode
43+ }
4544) || exit $?
4645
4746is_target_exists=