From b7784aa4115bb93d7f513c79ed029a5a41f3b4d5 Mon Sep 17 00:00:00 2001
From: roktas <roktas@788f1e2b-df1e-0410-8736-df70ead52e1b>
Date: Fri, 27 Oct 2006 11:43:14 +0000
Subject: [PATCH] Add "-o | --output" option to markdown2pdf, update man file.

git-svn-id: https://pandoc.googlecode.com/svn/trunk@18 788f1e2b-df1e-0410-8736-df70ead52e1b
---
 man/man1/markdown2pdf.1 |  8 +++---
 markdown2pdf            | 62 +++++++++++++++++++++++++++++++++--------
 2 files changed, 54 insertions(+), 16 deletions(-)

diff --git a/man/man1/markdown2pdf.1 b/man/man1/markdown2pdf.1
index 685f0af81..894d05ec3 100644
--- a/man/man1/markdown2pdf.1
+++ b/man/man1/markdown2pdf.1
@@ -2,14 +2,14 @@
 .SH NAME
 markdown2pdf \- converts markdown-formatted text to PDF, using pdflatex 
 .SH SYNOPSIS
-.B markdown2pdf [input-file]
+.B markdown2pdf [-o|--output output-file] [input-file]
 .SH DESCRIPTION
 .B markdown2pdf 
 converts input-file (or text from STDIN) from markdown-formatted
 plain text to PDF, using pdflatex.  The name of the output file is derived
-from the input file; thus, for example, if the input file is 'hello.txt',
-the output file will be 'hello.pdf'.  If the input is read from STDIN, the
-output file will be named 'stdin.pdf'.
+from the input file, if it is not specified; thus, for example, if the input
+file is 'hello.txt', the output file will be 'hello.pdf'.  If the input is
+read from STDIN, the output file will be named 'stdin.pdf'.
 .SH AUTHOR
 John MacFarlane <jgm at berkeley.edu>
 .SH "SEE ALSO"
diff --git a/markdown2pdf b/markdown2pdf
index 6a76aab7d..bea336a07 100644
--- a/markdown2pdf
+++ b/markdown2pdf
@@ -1,4 +1,4 @@
-#!/bin/sh -e
+#!/bin/sh
 # converts markdown to latex, then uses latex to make a PDF
 
 [ -n "$(which pandoc)" ] || {
@@ -10,17 +10,55 @@
     exit 1
 }
 
-TEMP=${TMPDIR-/tmp}/markdown2pdf.$$
-trap "status=$?; rm -rf $TEMP; exit $status" 0 INT
+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=yes
+	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
 
-if [ -z "$1" ]; then
-    BASE='stdin' # input is STDIN, since no argument given
-else
-    filename=${1##*/}
-    BASE=${filename%\.*}
+if [ "$help" = "yes" ]; then
+    echo "Usage: $0 [-o|--output output-file] [input-file]"
+    exit 0
 fi
 
-mkdir -p $TEMP && iconv -t utf-8 $* | pandoc -w latex -s > $TEMP/$BASE.tex  
+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 -w latex -s > $TEMP/$BASE.tex  
 (
     cd $TEMP
     if ! pdflatex -interaction=batchmode $BASE.tex >/dev/null 2>&1; then
@@ -35,10 +73,10 @@ if [ -f $BASE.pdf ]; then
     is_target_exists=1
 fi
 
-cp --suffix=~ --backup $TEMP/$BASE.pdf .
+mv --suffix=~ --backup $TEMP/$BASE.pdf $outfile
 
-echo -n >&2 "Created $BASE.pdf"
+echo -n >&2 "Created $outfile"
 [ -z "$is_target_exists" ] || {
-    echo -n >&2 " (previous file has been backed up as '$BASE.pdf~')"
+    echo -n >&2 " (previous file has been backed up as '$outfile~')"
 }
 echo >&2 .