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
This commit is contained in:
fiddlosopher 2007-01-08 19:55:34 +00:00
parent 9eafa97156
commit 58697ebe78
3 changed files with 70 additions and 71 deletions

View file

@ -6,6 +6,11 @@ NEWLINE='
err () { echo "$*" | fold -s -w ${COLUMNS:-110} >&2; }
errn () { printf "$*" | fold -s -w ${COLUMNS:-110} >&2; }
usage () {
err "$1 - $2" # short description
err "See the $1(1) man page for usage."
}
# Portable which(1).
pathfind () {
oldifs="$IFS"; IFS=':'
@ -25,3 +30,14 @@ for p in pandoc $REQUIRED; do
exit 1
}
done
CONF=$(pandoc --dump-args "$@" 2>&1) || {
errcode=$?
echo "$CONF" | sed -e '/^pandoc \[OPTIONS\] \[FILES\]/,$d' >&2
[ $errcode -eq 2 ] && usage "$THIS" "$SYNOPSIS"
exit $errcode
}
OUTPUT=$(echo "$CONF" | sed -ne '1p')
ARGS=$(echo "$CONF" | sed -e '1d')

View file

@ -3,6 +3,7 @@
# uses an available program to fetch URL and tidy to normalize it first
REQUIRED="tidy"
SYNOPSIS="converts HTML from a URL, file, or STDIN to markdown-formatted text."
### common.sh
@ -59,63 +60,46 @@ grab_url_with () {
$prog "$@" "$url"
}
add_option () {
options="$options$NEWLINE$1"
}
options=
argument=
encoding=
grabber=
# Parse command-line arguments
while [ $# -gt 0 ]; do
case "$1" in
-h|--help)
pandoc -h 2>&1 | sed -e 's/pandoc/html2markdown/' \
-e '/^[[:space:]]*\(-f\|-t\|-S\|-N\|-m\|-i\|-c\|-T\|-D\|-d\)/,/./d'\
1>&2
err " -e ENCODING, --encoding=ENCODING"
err " Specify character encoding of input"
err " -g COMMAND, --grabber=COMMAND"
err " Specify command to be used to grab contents of URL"
exit 0 ;;
-v|--version)
pandoc -v 2>&1 | sed -e 's/pandoc/html2markdown/' 1>&2
exit 0 ;;
-e)
shift
encoding=$1 ;;
--encoding=*)
wholeopt=$1
# extract encoding from after =
encoding=${wholeopt#*=} ;;
-g)
shift
grabber=$1 ;;
--grabber=*)
wholeopt=$1
# extract encoding from after =
grabber=${wholeopt#*=} ;;
-o|--output|-b|--tab-stop|-H|--include-in-header| \
-A|--include-after-body|-C|-B|--include-before-body| \
-C|--custom-header|-T|--title-prefix)
add_option $1
shift
add_option $1 ;;
-*) add_option $1 ;;
*)
if [ -z "$argument" ]; then
argument=$1
else
err "Warning: extra argument '$1' will be ignored."
fi ;;
esac
shift
done
parse_arguments () {
while [ $# -gt 0 ]; do
case "$1" in
--encoding=*)
wholeopt="$1"
# extract encoding from after =
encoding="${wholeopt#*=}" ;;
-e|--encoding|-encoding)
shift
encoding="$1" ;;
--grabber=*)
wholeopt="$1"
# extract encoding from after =
grabber="\"${wholeopt#*=}\"" ;;
-g|--grabber|-grabber)
shift
grabber="$1" ;;
*)
if [ -z "$argument" ]; then
argument="$1"
else
err "Warning: extra argument '$1' will be ignored."
fi ;;
esac
shift
done
export encoding
export grabber
export argument
}
# Unpack options. Now "$@" will hold the pandoc options.
oldifs="$IFS"; IFS="$NEWLINE"; set -- $options; IFS="$oldifs"
oldifs="$IFS"
IFS=$NEWLINE
parse_arguments $ARGS
IFS="$oldifs"
inurl=
if [ -n "$argument" ] && ! [ -f "$argument" ]; then
@ -164,11 +148,11 @@ else # assume UTF-8
fi
if [ -z "$argument" ]; then
tidy -utf8 2>/dev/null | pandoc -r html -w markdown "$@"
tidy -utf8 2>/dev/null | pandoc --ignore-args -r html -w markdown "$@"
else
if [ -f "$argument" ]; then
to_utf8 "$argument" |
tidy -utf8 2>/dev/null | pandoc -r html -w markdown "$@"
tidy -utf8 2>/dev/null | pandoc --ignore-args -r html -w markdown "$@"
else
err "File '$argument' not found."
exit 1

View file

@ -1,6 +1,7 @@
#!/bin/sh -e
REQUIRED="pdflatex"
SYNOPSIS="converts markdown-formatted text to PDF, using pdflatex."
### common.sh
@ -8,27 +9,25 @@ REQUIRED="pdflatex"
texname=output
logfile=$THIS_TEMPDIR/log
origdir=$(pwd)
if ! pandoc -s -d -r markdown -w latex "$@" >$THIS_TEMPDIR/$texname.tex \
2>$logfile; then
[ -f $logfile ] && sed -e 's/^pandoc/markdown2pdf/g' \
-e '/^INPUT=/d' -e '/^OUTPUT=/d' \
-e '/^[[:space:]]*\(-f\|-t\|-s\|-R\|-S\|-m\|-i\|-c\|-T\|-D\|-d\)/,/./d'\
-e 's/(implies -s)//g' $logfile >&2
exit 1
pandoc -s -r markdown -w latex "$@" -o - >$THIS_TEMPDIR/$texname.tex \
2>$logfile
if [ "$OUTPUT" = "-" ]; then
firstinfile="$(echo $ARGS | sed -ne '1p')"
firstinfilebase="${firstinfile%.*}"
destname="${firstinfilebase:-stdin}.pdf"
else
destname="$OUTPUT"
fi
outfile="$(sed -ne 's/^OUTPUT=//p' $logfile)"
IFS="$NEWLINE"
set -- $(sed -ne 's/^INPUT=//p' $logfile)
firstinfilebase="${1%.*}"
defaultdest="${firstinfilebase:-stdin}.pdf"
destname="${outfile:-$defaultdest}"
(
cd $THIS_TEMPDIR
if ! pdflatex -interaction=batchmode $texname.tex >/dev/null 2>&1; then
err "${THIS}: pdfLaTeX error context:"
TEXINPUTS=$TEXINPUTS:$origdir pdflatex -interaction=batchmode \
$texname.tex >/dev/null 2>&1 || {
errorcode=$?
err "${THIS}: pdfLaTeX error context:"
sed -ne '/^!/,/^[[:space:]]*$/p' \
-ne '/^[Ll]a[Tt]e[Xx] [Ww]arning/,/^[[:space:]]*$/p' \
-ne '/^[Ee]rror/,/^[[:space:]]*$/p' $texname.log >&2
@ -40,8 +39,8 @@ destname="${outfile:-$defaultdest}"
err "${THIS}: Please install the 'fancyvrb' package from CTAN:"
err " http://www.ctan.org/tex-archive/macros/latex/contrib/fancyvrb/"
fi
exit 1
fi
exit $errorcode
}
) || exit $?
is_target_exists=