diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/wrappers/common.sh | 16 | ||||
-rw-r--r-- | src/wrappers/html2markdown.in | 90 | ||||
-rw-r--r-- | src/wrappers/markdown2pdf.in | 35 |
3 files changed, 70 insertions, 71 deletions
diff --git a/src/wrappers/common.sh b/src/wrappers/common.sh index 2cde64157..9605f5940 100644 --- a/src/wrappers/common.sh +++ b/src/wrappers/common.sh @@ -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') + diff --git a/src/wrappers/html2markdown.in b/src/wrappers/html2markdown.in index 740d69588..6d0256274 100644 --- a/src/wrappers/html2markdown.in +++ b/src/wrappers/html2markdown.in @@ -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 diff --git a/src/wrappers/markdown2pdf.in b/src/wrappers/markdown2pdf.in index 90c5e6bb1..aaae07184 100644 --- a/src/wrappers/markdown2pdf.in +++ b/src/wrappers/markdown2pdf.in @@ -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 -fi +pandoc -s -r markdown -w latex "$@" -o - >$THIS_TEMPDIR/$texname.tex \ +2>$logfile -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}" +if [ "$OUTPUT" = "-" ]; then + firstinfile="$(echo $ARGS | sed -ne '1p')" + firstinfilebase="${firstinfile%.*}" + destname="${firstinfilebase:-stdin}.pdf" +else + destname="$OUTPUT" +fi ( 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= |