git.iko.soy/typeable/pandoc
on master help sign in
+ Removed the convenience symlinks (which don't work on Windows under

Cygwin, due to Windows' lack of true symbolic links). + Modified the wrappers to use 'pandoc' instead of the symlinks. + Modified the Makefile to remove all references to the symlinks. + Removed code from Main.hs that made pandoc's behavior depend on the name of the calling program. + Added code to Main.hs that sets default reader and writer based on extensions of input and output filenames (if provided). (Thanks to roktas for the idea.) + Modified README and man pages accordingly. + Removed WINDOWS-README target from Makefile. It is no longer needed now that we don't have the symlinks. git-svn-id: https://pandoc.googlecode.com/svn/trunk@295 788f1e2b-df1e-0410-8736-df70ead52e1b

commita1a30d69bd83e57e494cd3ca5077086077731998 authorfiddlosopher <fiddlosopher@788f1e2b-df1e-0410-8736-df70ead52e1b> dateThu 28 Dec 2006, 02:20 (20 years ago) parenta1539d9
16 files changed+198−231
Makefile+3−16 README+64−75 man/man1/html2markdown.1−1 man/man1/latex2markdown.1−1 man/man1/markdown2html.1−1 man/man1/markdown2latex.1−1 man/man1/markdown2pdf.1+11−16 man/man1/markdown2rst.1−1 man/man1/markdown2rtf.1−1 man/man1/markdown2s5.1−1 man/man1/pandoc.1+21−29 man/man1/rst2markdown.1−1 man/man1/web2markdown.1+1−2 src/Main.hs+82−75 src/wrappers/markdown2pdf.in+7−4 src/wrappers/web2markdown.in+9−6
Makefile+3−16
@@ -26,8 +26,6 @@ EXECSBASE := $(shell sed -ne 's/^[Ee]xecutable:[[:space:]]*//p' $(CABAL).in)
2626# Install targets
2727#-------------------------------------------------------------------------------
2828WRAPPERS := web2markdown markdown2pdf
29−SYMLINKS := markdown2html markdown2latex markdown2s5 markdown2rst \
30− markdown2rtf html2markdown latex2markdown rst2markdown
3129# Add .exe extensions if we're running Windows/Cygwin.
3230EXTENSION := $(shell uname | tr '[:upper:]' '[:lower:]' | \
3331 sed -ne 's/^cygwin.*$$/\.exe/p')
@@ -96,12 +94,6 @@ all: build-program
9694templates: $(SRCDIR)/templates
9795 $(MAKE) -C $(SRCDIR)/templates
9896
99−.PHONY: symlinks
100−cleanup_files+=$(SYMLINKS)
101−symlinks: $(SYMLINKS)
102−$(SYMLINKS): $(MAIN)
103− ln -sf ./$(MAIN) $@
104−
10597define generate-shell-script
10698echo "Generating $@..."; \
10799awk ' \
@@ -141,7 +133,7 @@ build: configure
141133 $(BUILDCMD) build
142134
143135.PHONY: build-exec
144−build-exec: $(PROGS) $(SYMLINKS)
136+build-exec: $(PROGS)
145137cleanup_files+=$(EXECS)
146138$(EXECS): build
147139 for f in $@; do \
@@ -201,9 +193,8 @@ install-exec: build-exec
201193 fi; \
202194 $(INSTALL_PROGRAM) $$f $(BINPATH)/; \
203195 done
204− cd $(BINPATH); for f in $(SYMLINKS); do ln -sf $(MAIN) $$f; done
205196uninstall-exec:
206− -for f in $(notdir $(PROGS) $(SYMLINKS)); do rm -f $(BINPATH)/$$f; done ;
197+ -for f in $(notdir $(PROGS)); do rm -f $(BINPATH)/$$f; done ;
207198
208199# Program + user documents installation.
209200.PHONY: install-program uninstall-program
@@ -295,15 +286,11 @@ $(osx_dmg_name): $(osx_pkg_name)
295286
296287.PHONY: win-pkg
297288win_pkg_name:=$(RELNAME).zip
298−win_docs:=COPYING.txt COPYRIGHT.txt BUGS.txt README-WINDOWS.txt README-WINDOWS.html
289+win_docs:=COPYING.txt COPYRIGHT.txt BUGS.txt README.txt README.html
299290cleanup_files+=$(win_pkg_name) $(win_docs)
300291win-pkg: $(win_pkg_name)
301292$(win_pkg_name): $(THIS).exe $(win_docs)
302293 zip -r $(win_pkg_name) $(THIS).exe $(win_docs)
303−cleanup_files+=README-WINDOWS
304−README-WINDOWS: README
305− sed -e '/^Requirements/,/^\[fancyvrb\]:/ d' \
306− -e '/^Character encodings/,/mysite.com$$/ d' $< > $@
307294
308295.PHONY: test test-markdown
309296test: $(MAIN)
README+64−75
@@ -36,14 +36,11 @@ Requirements
3636============
3737
3838The `pandoc` program itself does not depend on any external libraries
39−or programs. The convenience programs `markdown2html`, `markdown2latex`,
40−`markdown2rst`, `markdown2rtf`, `markdown2s5`, `html2markdown`,
41−`latex2markdown`, and `rst2markdown` are implemented as symbolic links to
42−`pandoc`.
39+or programs.
4340
4441The wrapper script `web2markdown` requires
4542
46− - `html2markdown` (included with Pandoc)
43+ - `pandoc` (which must be in the PATH)
4744 - a POSIX-compliant shell (installed by default on all linux and unix
4845 systems, including Mac OS X, and in [Cygwin] for Windows),
4946 - `HTML Tidy`
@@ -56,7 +53,7 @@ The wrapper script `web2markdown` requires
5653
5754The wrapper script `markdown2pdf` requires
5855
59− - `markdown2latex` (included with Pandoc)
56+ - `pandoc` (which must be in the PATH)
6057 - a POSIX-compliant shell
6158 - `pdflatex`, which should be part of any [LaTeX] distribution
6259 - the [unicode] and [fancyvrb] LaTeX packages, which are included
@@ -80,47 +77,11 @@ Using Pandoc
8077
8178If you run `pandoc` without arguments, it will accept input from
8279STDIN. If you run it with file names as arguments, it will take input
83−from those files. It accepts several command-line options. For a
84−list, type
85−
86− pandoc -h
87−
88−The most important options specify the format of the source file and
89−the output. The default reader is markdown; the default writer is
90−HTML. So if you don't specify a reader or writer, `pandoc` will
91−convert markdown to HTML. For example,
92−
93− pandoc hello.txt
94−
95−will convert `hello.txt` from markdown to HTML. For other conversions,
96−you must specify a reader and/or a writer using the `-r` and `-w`
97−flags. To convert markdown to LaTeX, you would write:
98−
99− pandoc -w latex hello.txt
100−
101−To convert html to markdown:
102−
103− pandoc -r html -w markdown hello.txt
104−
105−Supported writers include `markdown`, `latex`, `html`, `rtf` (rich text
106−format), `rst` (reStructuredText), and `s5` (which produces an HTML
107−file that acts like powerpoint). Supported readers include `markdown`,
108−`html`, `latex`, and `rst`. Note that the `rst` reader only parses
109−a subset of reStructuredText syntax. For example, it doesn't handle
110−tables, definition lists, option lists, or footnotes. It handles only the
111−constructs expressible in unextended markdown. But for simple documents
112−it should be adequate. The `latex` and `html` readers are also limited
113−in what they can do. Because the `html` reader is picky about the HTML
114−it parses, it is recommended that you pipe HTML through [HTML Tidy] before
115−sending it to `pandoc`, or use the `web2markdown` script described below.
116−
117−By default, `pandoc` writes its output to STDOUT. If you want to
118−write to a file, use the `-o` option or shell redirection:
80+from those files. By default, `pandoc` writes its output to STDOUT.
81+If you want to write to a file, use the `-o` option:
11982
12083 pandoc -o hello.html hello.txt
12184
122− pandoc hello.txt > hello.html
123−
12485Note that you can specify multiple input files on the command line.
12586`pandoc` will concatenate them all (with blank lines between them)
12687before parsing:
@@ -131,6 +92,44 @@ before parsing:
13192with a proper header, rather than a fragment. For more details on this
13293and many other command-line options, see below.)
13394
95+The format of the input and output can be specified explicitly using
96+command-line options. The input format can be specified using the
97+`-r/--read` or `-f/--from` options, the output format using the
98+`-w/--write` or `-t/--to` options. Thus, to convert `hello.txt` from
99+markdown to LaTeX, you could type:
100+
101+ pandoc -f markdown -t latex hello.txt
102+
103+To convert `hello.html` from html to markdown:
104+
105+ pandoc -f html -t markdown hello.html
106+
107+Supported output formats include `markdown`, `latex`, `html`, `rtf`
108+(rich text format), `rst` (reStructuredText), and `s5` (which produces
109+an HTML file that acts like powerpoint). Supported input formats
110+include `markdown`, `html`, `latex`, and `rst`. Note that the `rst`
111+reader only parses a subset of reStructuredText syntax. For example,
112+it doesn't handle tables, definition lists, option lists, or footnotes.
113+It handles only the constructs expressible in unextended markdown.
114+But for simple documents it should be adequate. The `latex` and `html`
115+readers are also limited in what they can do. Because the `html`
116+reader is picky about the HTML it parses, it is recommended that you
117+pipe HTML through [HTML Tidy] before sending it to `pandoc`, or use the
118+`web2markdown` script described below.
119+
120+If you don't specify a reader or writer explicitly, `pandoc` will
121+try to determine the input and output format from the extensions of
122+the input and output filenames. Thus, for example,
123+
124+ pandoc -o hello.tex hello.txt
125+
126+will convert `hello.txt` from markdown to LaTeX. If no output file
127+is specified (so that output goes to STDOUT), or if the output file's
128+extension is unknown, the output format will default to HTML.
129+If no input file is specified (so that input comes from STDIN), or
130+if the input files' extensions are unknown, the input format will
131+be assumed to be markdown unless explicitly specified.
132+
134133Character encodings
135134-------------------
136135
@@ -150,31 +149,16 @@ The shell scripts (described below) automatically convert the input
150149from the local encoding to UTF-8 before running them through `pandoc`,
151150then convert the output back to the local encoding.
152151
153−Convenience programs and wrapper scripts
154−========================================
155−
156−For convenience, eight variant programs are included with Pandoc:
157−`markdown2html` (which is equivalent to `pandoc -w html`),
158−`markdown2latex` (equivalent to `pandoc -w latex`), `markdown2rst`
159−(equivalent to `pandoc -w rst`), `markdown2rtf` (equivalent to
160−`pandoc -w rtf`), `markdown2s5` (equivalent to `pandoc -w s5`),
161−`html2markdown` (equivalent to `pandoc -r html -w markdown`),
162−`latex2markdown` (equivalent to `pandoc -r latex -w markdown`), and
163−`rst2markdown` (equivalent to `pandoc -r rst -w markdown`). These
164−programs take an appropriately restricted subset of `pandoc`'s
165−options. (Run them with the `-h` flag for a full list of allowed
166−options.)
167−
168−Like `pandoc`, all of these programs produce fragments by default.
169−If you want to produce a standalone file, complete with a header
170−and footer appropriate to the format, use the `-s` option:
152+`markdown2pdf` and `web2markdown`
153+=================================
171154
172− markdown2latex -s sample.txt > sample.tex
173−
174−Two shell scripts have also been included:
155+Two shell scripts, `markdown2pdf` and `web2markdown`, are included in
156+the standard Pandoc installation. (They are not included in the Windows
157+binary package, as they require a POSIX shell, but they may be used
158+in Windows under Cygwin.)
175159
1761601. `markdown2pdf` produces a PDF file from markdown-formatted
177− text, using `markdown2latex` and `pdflatex`. The default
161+ text, using `pandoc` and `pdflatex`. The default
178162 behavior of `markdown2pdf` is to create a file with the same
179163 base name as the first argument and the extension `pdf`; thus,
180164 for example,
@@ -190,7 +174,7 @@ Two shell scripts have also been included:
190174 If no input file is specified, input will be taken from STDIN.
191175
1921762. `web2markdown` grabs a web page from a file or URL and converts
193− it to markdown-formatted text, using `tidy` and `html2markdown`.
177+ it to markdown-formatted text, using `tidy` and `pandoc`.
194178 Unless input is from STDIN, an attempt is made to determine the
195179 character encoding of the page from the "Content-type" meta tag.
196180 If this is not present, UTF-8 is assumed. Alternatively, a character
@@ -207,9 +191,20 @@ Command-line options
207191====================
208192
209193Various command-line options can be used to customize the output.
210−For a complete list, type
211194
212− pandoc --help
195+`-f`, `--from`, `-r`, or `--read` can be used to specify the input
196+format -- the format Pandoc will be converting *from*. Available
197+formats are `native`, `markdown`, `rst`, `html`, and `latex`.
198+
199+`-t`, `--to`, `-w`, or `--write` can be used to specify the output
200+format -- the format Pandoc will be converting *to*. Available formats
201+are `native`, `html`, `s5`, `latex`, `markdown`, `rst`, and `rtf`.
202+
203+`-s` or `--standalone` indicates that a standalone document is to be
204+produced (with appropriate headers and footers), rather than a fragment.
205+
206+`-o` or `--output` specifies the name of the output file. If no output
207+filename is given, output will be sent to STDOUT.
213208
214209`-p` or `--preserve-tabs` causes tabs in the source text to be
215210preserved, rather than converted to spaces (the default).
@@ -225,12 +220,6 @@ untranslatable HTML codes and LaTeX environments. (The LaTeX reader
225220does pass through untranslatable LaTeX commands, even if `-R` is not
226221specified.)
227222
228−`-s` or `--standalone` causes `pandoc` to produce a standalone file,
229−complete with appropriate document headers. By default, `pandoc`
230−produces a fragment.
231−
232−`-o` or `--output-file` can be used to specify an output file.
233−
234223`-C` or `--custom-header` can be used to specify a custom document
235224header. To see the headers used by default, use the `-D` option:
236225for example, `pandoc -D html` prints the default HTML header.
man/man1/html2markdown.1deleted−1
@@ -1 +0,0 @@
1−.so man1/pandoc.1
man/man1/latex2markdown.1deleted−1
@@ -1 +0,0 @@
1−.so man1/pandoc.1
man/man1/markdown2html.1deleted−1
@@ -1 +0,0 @@
1−.so man1/pandoc.1
man/man1/markdown2latex.1deleted−1
@@ -1 +0,0 @@
1−.so man1/pandoc.1
man/man1/markdown2pdf.1+11−16
@@ -6,14 +6,13 @@ markdown2pdf \- converts markdown-formatted text to PDF, using pdflatex
66.SH DESCRIPTION
77\fBmarkdown2pdf\fR converts \fIinput\-file\fR (or text from standard
88input) from markdown\-formatted plain text to PDF, using \fBpdflatex\fR.
9−If no output filename is specified, the name of the output file is
10−derived from the input file; thus, for example, if the input file
11−is \fIhello.txt\fR, the output file will be \fIhello.pdf\fR. If
12−the input is read from STDIN and no output filename is
13−specified, the output file will be named \fIstdin.pdf\fR. If
14−multiple input files are specified, they will be concatenated before
15−conversion, and the name of the output file will be derived from
16−the first input file.
9+If no output filename is specified (using the \fB\-o\fR option),
10+the name of the output file is derived from the input file; thus, for
11+example, if the input file is \fIhello.txt\fR, the output file will be
12+\fIhello.pdf\fR. If the input is read from STDIN and no output filename
13+is specified, the output file will be named \fIstdin.pdf\fR. If multiple
14+input files are specified, they will be concatenated before conversion,
15+and the name of the output file will be derived from the first input file.
1716.PP
1817Input is assumed to be in the UTF\-8 character encoding. If your
1918local character encoding is not UTF\-8, you should pipe input and
@@ -21,11 +20,11 @@ output through \fBiconv\fR:
2120.IP
2221.B iconv \-t utf\-8 input.txt | pandoc | iconv \-f utf\-8
2322.PP
24−\fBmarkdown2pdf\fR assumes that the 'unicode' package
25−is in latex's search path. If this package is not included in your
26−latex setup, it can be obtained from <http://ctan.org>.
23+\fBmarkdown2pdf\fR assumes that the 'unicode' and 'fancyvrb' packages
24+are in latex's search path. If these packages are not included in your
25+latex setup, they can be obtained from <http://ctan.org>.
2726.PP
28−\fBmarkdown2pdf\fR is a wrapper around \fBmarkdown2latex\fR.
27+\fBmarkdown2pdf\fR is a wrapper around \fBpandoc\fR.
2928.SH OPTIONS
3029.TP
3130.B \-o FILE, \-\-output=FILE
@@ -37,10 +36,6 @@ Preserve tabs instead of converting them to spaces.
3736.B \-\-tab-stop=\fITABSTOP\fB
3837Specify tab stop (default is 4).
3938.TP
40−.B \-R, \-\-parse-raw
41−Parse untranslatable LaTeX environments as raw LaTeX,
42−instead of ignoring them.
43−.TP
4439.B \-N, \-\-number-sections
4540Number section headings in LaTeX output. (Default is not to number them.)
4641.TP
man/man1/markdown2rst.1deleted−1
@@ -1 +0,0 @@
1−.so man1/pandoc.1
man/man1/markdown2rtf.1deleted−1
@@ -1 +0,0 @@
1−.so man1/pandoc.1
man/man1/markdown2s5.1deleted−1
@@ -1 +0,0 @@
1−.so man1/pandoc.1
man/man1/pandoc.1+21−29
@@ -1,8 +1,6 @@
11.TH PANDOC 1 "December 15, 2006" Pandoc "User Manuals"
22.SH NAME
3−pandoc, markdown2html, markdown2latex, markdown2rst, markdown2rtf,
4−markdown2s5, html2markdown2, latex2markdown, rst2markdown \- general
5−markup converter
3+pandoc \- general markup converter
64.SH SYNOPSIS
75\fBpandoc\fR [\fIoptions\fR] [\fIinput\-file\fR]...
86.SH DESCRIPTION
@@ -13,41 +11,37 @@ slide shows.
1311.PP
1412If no \fIinput\-file\fR is specified, input is read from STDIN.
1513Otherwise, the \fIinput\-files\fR are concatenated (with a blank
16−line between each) and used as input. Output goes to standard
17−output. If you want output to a file, use the \fB\-o\fR option or
18−shell redirection:
14+line between each) and used as input. Output goes to STDOUT by
15+default. For output to a file, use the \fB\-o\fR option:
1916.IP
2017.B pandoc \-o output.html input.txt
21−.IP
22−.B pandoc input.txt > output.html
2318.PP
24−The default behavior of \fIPandoc\fR is to convert the input from
25−markdown\-formatted plain text to HTML. Different input and output
26−formats can be specified using command\-line options. For example,
19+The input and output formats may be specified using command-line options
20+(see \fBOPTIONS\fR, below, for details). If these formats are not
21+specified explicitly, \fIPandoc\fR will attempt to determine them
22+from the extensions of the input and output filenames. If input comes
23+from STDIN or from a file with an unknown extension, the input is assumed
24+to be markdown. If no output filename is specified using the \fB\-o\fR
25+option, or if a filename is specified but its extension is unknown,
26+the output will default to HTML. Thus, for example,
2727.IP
28−.B pandoc \-f latex \-t markdown chap1.tex > chap1.txt
28+.B pandoc -o chap1.tex chap1.txt
2929.PP
30−converts \fIchap1.tex\fR from LaTeX to markdown\-formatted plain text.
31−See below for a detailed list of command\-line options.
30+converts \fIchap1.txt\fR from markdown to LaTeX. And
31+.IP
32+.B pandoc README
3233.PP
33−For convenience, eight variant programs are available:
34−\fBmarkdown2html\fR (same as \fBpandoc \-w html\fR),
35−\fBmarkdown2latex\fR (same as \fBpandoc \-w latex\fR),
36−\fBmarkdown2rst\fR (same as \fBpandoc \-w rst\fR),
37−\fBmarkdown2rtf\fR (same as \fBpandoc \-w rtf\fR),
38−\fBmarkdown2s5\fR (same as \fBpandoc \-w s5\fR),
39−\fBhtml2markdown\fR (same as \fBpandoc \-r html \-w markdown\fR),
40−\fBlatex2markdown\fR (same as \fBpandoc \-r latex \-w markdown\fR),
41−and \fBrst2markdown\fR (same as \fBpandoc \-r rst \-w markdown\fR).
42−These programs take an appropriately restricted subset of \fBpandoc\fR's
43−options. (Run them with the \fB-h\fR flag for a full list of allowed
44−options.)
34+converts \fIREADME\fR from markdown to HTML.
4535.PP
4636\fIPandoc\fR uses the UTF\-8 character encoding for both input and output.
4737If your local character encoding is not UTF\-8, you should pipe input
4838and output through \fBiconv\fR:
4939.IP
5040.B iconv \-t utf\-8 input.txt | pandoc | iconv \-f utf\-8
41+.PP
42+\fIPandoc\fR's HTML parser is not very forgiving. If your input is
43+HTML, consider running it through \fBtidy\fR(1) before passing it
44+to Pandoc. Or use \fBweb2markdown\fR(1), a wrapper around \fBpandoc\fR.
5145
5246.SH OPTIONS
5347.TP
@@ -158,9 +152,7 @@ Show usage message.
158152
159153.SH "SEE ALSO"
160154\fBweb2markdown\fR(1),
161−\fBmarkdown2pdf\fR(1),
162−\fBiconv\fR(1)
163−
155+\fBmarkdown2pdf\fR(1).
164156The
165157.I README
166158file distributed with Pandoc contains full documentation.
man/man1/rst2markdown.1deleted−1
@@ -1 +0,0 @@
1−.so man1/pandoc.1
man/man1/web2markdown.1+1−2
@@ -16,7 +16,7 @@ option.
1616from STDIN, UTF-8 is assumed. A character encoding may be specified
1717explicitly using the \fB\-e\fR option.
1818.PP
19−\fBweb2markdown\fR is a wrapper for \fBhtml2markdown\fR.
19+\fBweb2markdown\fR is a wrapper for \fBpandoc\fR.
2020.SH OPTIONS
2121.TP
2222.B \-s, \-\-standalone
@@ -76,7 +76,6 @@ web2markdown \-g 'wget \-\-user=foo \-\-password=bar' mysite.com
7676
7777.SH "SEE ALSO"
7878\fBpandoc\fR(1),
79−\fBhtml2markdown\fR(1),
8079\fBiconv\fR(1)
8180.SH AUTHOR
8281John MacFarlane and Recai Oktas
src/Main.hs+82−75
@@ -45,7 +45,7 @@ import Text.Pandoc.Writers.DefaultHeaders ( defaultHtmlHeader,
4545 defaultRTFHeader, defaultS5Header, defaultLaTeXHeader )
4646import Text.Pandoc.Definition
4747import Text.Pandoc.Shared
48−import Text.Regex ( mkRegex, splitRegex )
48+import Text.Regex ( mkRegex, matchRegex )
4949import System ( exitWith, getArgs, getProgName )
5050import System.Exit
5151import System.Console.GetOpt
@@ -94,8 +94,8 @@ data Opt = Opt
9494 { optPreserveTabs :: Bool -- ^ If @False@, convert tabs to spaces
9595 , optTabStop :: Int -- ^ Number of spaces per tab
9696 , optStandalone :: Bool -- ^ If @True@, include header, footer
97− , optReader :: ParserState -> String -> Pandoc -- ^ Read format
98− , optWriter :: WriterOptions -> Pandoc -> String -- ^ Write fmt
97+ , optReader :: String -- ^ Reader format
98+ , optWriter :: String -- ^ Writer format
9999 , optParseRaw :: Bool -- ^ If @True@, parse unconvertable
100100 -- HTML and TeX
101101 , optCSS :: String -- ^ CSS file to link to
@@ -103,64 +103,55 @@ data Opt = Opt
103103 , optIncludeBeforeBody :: String -- ^ File to include at top of body
104104 , optIncludeAfterBody :: String -- ^ File to include at end of body
105105 , optCustomHeader :: String -- ^ Custom header to use, or "DEFAULT"
106− , optDefaultHeader :: String -- ^ Default header
107106 , optTitlePrefix :: String -- ^ Optional prefix for HTML title
108107 , optOutputFile :: String -- ^ Name of output file
109108 , optNumberSections :: Bool -- ^ If @True@, number sections in LaTeX
110109 , optIncremental :: Bool -- ^ If @True@, incremental lists in S5
111110 , optSmart :: Bool -- ^ If @True@, use smart typography
112111 , optASCIIMathML :: Bool -- ^ If @True@, use ASCIIMathML in HTML
113− , optShowUsage :: Bool -- ^ If @True@, show usage message
114112 , optDebug :: Bool -- ^ If @True@, output debug messages
115113 }
116114
117115-- | Defaults for command-line options.
118−startOpt :: Opt
119−startOpt = Opt
116+defaultOpts :: Opt
117+defaultOpts = Opt
120118 { optPreserveTabs = False
121119 , optTabStop = 4
122120 , optStandalone = False
123− , optReader = readMarkdown
124− , optWriter = writeHtml
121+ , optReader = "" -- null for default reader
122+ , optWriter = "" -- null for default writer
125123 , optParseRaw = False
126124 , optCSS = ""
127125 , optIncludeInHeader = ""
128126 , optIncludeBeforeBody = ""
129127 , optIncludeAfterBody = ""
130128 , optCustomHeader = "DEFAULT"
131− , optDefaultHeader = defaultHtmlHeader
132129 , optTitlePrefix = ""
133130 , optOutputFile = "" -- null for stdout
134131 , optNumberSections = False
135132 , optIncremental = False
136133 , optSmart = False
137134 , optASCIIMathML = False
138− , optShowUsage = False
139135 , optDebug = False
140136 }
141137
142−-- | A list of functions, each transforming the options data structure in response
143−-- to a command-line option.
144−allOptions :: [OptDescr (Opt -> IO Opt)]
145−allOptions =
138+-- | A list of functions, each transforming the options data structure
139+-- in response to a command-line option.
140+options :: [OptDescr (Opt -> IO Opt)]
141+options =
146142 [ Option "fr" ["from","read"]
147143 (ReqArg
148− (\arg opt -> case (lookup (map toLower arg) readers) of
149− Just reader -> return opt { optReader = reader }
150− Nothing -> error ("Unknown reader: " ++ arg) )
144+ (\arg opt -> return opt { optReader = map toLower arg })
151145 "FORMAT")
152− ("Source format (" ++
153− (concatMap (\(name, fn) -> " " ++ name) readers) ++ " )")
146+ ("Input format (" ++ (joinWithSep ", " (map fst readers)) ++
147+ ")")
154148
155149 , Option "tw" ["to","write"]
156150 (ReqArg
157− (\arg opt -> case (lookup (map toLower arg) writers) of
158− Just (writer, defaultHeader) ->
159− return opt { optWriter = writer,
160− optDefaultHeader = defaultHeader }
161− Nothing -> error ("Unknown writer: " ++ arg) )
151+ (\arg opt -> return opt { optWriter = map toLower arg })
162152 "FORMAT")
163− ("Output format (" ++ (concatMap (\(name, fn) -> " " ++ name) writers) ++ " )")
153+ ("Output format (" ++ (joinWithSep ", " (map fst writers)) ++
154+ ")")
164155
165156 , Option "s" ["standalone"]
166157 (NoArg
@@ -169,8 +160,7 @@ allOptions =
169160
170161 , Option "o" ["output"]
171162 (ReqArg
172− (\arg opt -> do
173− return opt { optOutputFile = arg })
163+ (\arg opt -> return opt { optOutputFile = arg })
174164 "FILENAME")
175165 "Name of output file"
176166
@@ -286,57 +276,66 @@ allOptions =
286276
287277 , Option "h" ["help"]
288278 (NoArg
289− (\opt -> return opt { optShowUsage = True }))
279+ (\_ -> do
280+ prg <- getProgName
281+ hPutStr stderr (reformatUsageInfo $
282+ usageInfo (prg ++ " [OPTIONS] [FILES]") options)
283+ exitWith $ ExitFailure 2))
290284 "Show help"
291285 ]
292286
293−-- parse name of calling program and return default reader and writer descriptions
294−parseProgName name =
295− case (splitRegex (mkRegex "2") (map toLower name)) of
296− [from, to] -> (from, to)
297− _ -> ("markdown", "html")
298−
299−-- set default options based on reader and writer descriptions; start is starting options
300−setDefaultOpts from to start =
301− case ((lookup from readers), (lookup to writers)) of
302− (Just reader, Just (writer, header)) -> start {optReader = reader,
303− optWriter = writer,
304− optDefaultHeader = header}
305− _ -> start
306−
307−-- True if single-letter option is in option list
308−inOptList :: [Char] -> OptDescr (Opt -> IO Opt) -> Bool
309−inOptList list desc =
310− let (Option letters _ _ _) = desc in
311− any (\x -> x `elem` list) letters
312−
313287-- Reformat usage message so it doesn't wrap illegibly
288+reformatUsageInfo :: String -> String
314289reformatUsageInfo = gsub " *--" " --" .
315290 gsub "(-[A-Za-z0-9]) *--" "\\1, --" .
316291 gsub " *([^- ])" "\n\t\\1"
317292
318−main = do
319−
320− name <- getProgName
321− let (from, to) = parseProgName name
293+-- Determine default reader based on source file extensions
294+defaultReaderName :: [String] -> String
295+defaultReaderName [] = "markdown"
296+defaultReaderName (x:xs) =
297+ let x' = map toLower x in
298+ case (matchRegex (mkRegex ".*\\.(.*)") x') of
299+ Nothing -> defaultReaderName xs -- no extension
300+ Just ["xhtml"] -> "html"
301+ Just ["html"] -> "html"
302+ Just ["htm"] -> "html"
303+ Just ["tex"] -> "latex"
304+ Just ["latex"] -> "latex"
305+ Just ["ltx"] -> "latex"
306+ Just ["rst"] -> "rst"
307+ Just ["native"] -> "native"
308+ Just _ -> "markdown"
309+
310+-- Determine default writer based on output file extension
311+defaultWriterName :: String -> String
312+defaultWriterName "" = "html" -- no output file
313+defaultWriterName x =
314+ let x' = map toLower x in
315+ case (matchRegex (mkRegex ".*\\.(.*)") x') of
316+ Nothing -> "markdown" -- no extension
317+ Just [""] -> "markdown" -- empty extension
318+ Just ["tex"] -> "latex"
319+ Just ["latex"] -> "latex"
320+ Just ["ltx"] -> "latex"
321+ Just ["rtf"] -> "rtf"
322+ Just ["rst"] -> "rst"
323+ Just ["s5"] -> "s5"
324+ Just ["native"] -> "native"
325+ Just ["txt"] -> "markdown"
326+ Just ["text"] -> "markdown"
327+ Just ["md"] -> "markdown"
328+ Just ["markdown"] -> "markdown"
329+ Just _ -> "html"
322330
323− let irrelevantOptions = if not ('2' `elem` name)
324− then ""
325− else "frtwD" ++
326− (if (to /= "html" && to /= "s5") then "SmcT" else "") ++
327− (if (to /= "latex") then "N" else "") ++
328− (if (to /= "s5") then "i" else "") ++
329− (if (from /= "html" && from /= "latex") then "R" else "")
330−
331− let options = filter (not . inOptList irrelevantOptions) allOptions
332−
333− let defaultOpts = setDefaultOpts from to startOpt
331+main = do
334332
335333 args <- getArgs
336334 let (actions, sources, errors) = getOpt Permute options args
337335
338336 if (not (null errors))
339337 then do
338+ name <- getProgName
340339 mapM (\e -> hPutStrLn stderr e) errors
341340 hPutStrLn stderr (reformatUsageInfo $
342341 usageInfo (name ++ " [OPTIONS] [FILES]") options)
@@ -350,30 +349,39 @@ main = do
350349 let Opt { optPreserveTabs = preserveTabs
351350 , optTabStop = tabStop
352351 , optStandalone = standalone
353− , optReader = reader
354− , optWriter = writer
352+ , optReader = readerName
353+ , optWriter = writerName
355354 , optParseRaw = parseRaw
356355 , optCSS = css
357356 , optIncludeInHeader = includeHeader
358357 , optIncludeBeforeBody = includeBefore
359358 , optIncludeAfterBody = includeAfter
360359 , optCustomHeader = customHeader
361− , optDefaultHeader = defaultHeader
362360 , optTitlePrefix = titlePrefix
363361 , optOutputFile = outputFile
364362 , optNumberSections = numberSections
365363 , optIncremental = incremental
366364 , optSmart = smart
367365 , optASCIIMathML = asciiMathML
368− , optShowUsage = showUsage
369366 , optDebug = debug
370367 } = opts
371368
372− if showUsage
373− then do
374− hPutStr stderr (reformatUsageInfo $ usageInfo (name ++ " [OPTIONS] [FILES]") options)
375− exitWith $ ExitFailure 2
376− else return ()
369+ -- assign reader and writer based on options and filenames
370+ let readerName' = if null readerName
371+ then defaultReaderName sources
372+ else readerName
373+
374+ let writerName' = if null writerName
375+ then defaultWriterName outputFile
376+ else writerName
377+
378+ reader <- case (lookup readerName' readers) of
379+ Just r -> return r
380+ Nothing -> error ("Unknown reader: " ++ readerName')
381+
382+ (writer, defaultHeader) <- case (lookup writerName' writers) of
383+ Just (w,h) -> return (w, h)
384+ Nothing -> error ("Unknown writer: " ++ writerName')
377385
378386 output <- if ((null outputFile) || debug)
379387 then return stdout
@@ -385,7 +393,6 @@ main = do
385393 hPutStr stderr $ concatMap (\s -> "INPUT=" ++ s ++ "\n") sources
386394 else return ()
387395
388− let writingS5 = (defaultHeader == defaultS5Header)
389396 let tabFilter = if preserveTabs then id else (tabsToSpaces tabStop)
390397 let addBlank str = str ++ "\n\n"
391398 let removeCRs str = filter (/= '\r') str -- remove DOS-style line endings
@@ -407,7 +414,7 @@ main = do
407414 writerTitlePrefix = titlePrefix,
408415 writerSmart = smart,
409416 writerTabStop = tabStop,
410− writerS5 = writingS5,
417+ writerS5 = (writerName=="s5"),
411418 writerIncremental = incremental,
412419 writerNumberSections = numberSections,
413420 writerIncludeBefore = includeBefore,
src/wrappers/markdown2pdf.in+7−4
@@ -1,6 +1,6 @@
11#!/bin/sh -e
22
3−REQUIRED="markdown2latex pdflatex"
3+REQUIRED="pdflatex"
44
55### common.sh
66
@@ -9,9 +9,12 @@ REQUIRED="markdown2latex pdflatex"
99texname=output
1010logfile=$THIS_TEMPDIR/log
1111
12−if ! markdown2latex -s -d "$@" >$THIS_TEMPDIR/$texname.tex 2>$logfile; then
13− [ -f $logfile ] && sed -e 's/markdown2latex/markdown2pdf/g' \
14− -e '/^INPUT=/d' -e '/^OUTPUT=/d' $logfile >&2
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
1518 exit 1
1619fi
1720
src/wrappers/web2markdown.in+9−6
@@ -2,7 +2,7 @@
22# converts HTML from a URL, file, or stdin to markdown
33# uses an available program to fetch URL and tidy to normalize it first
44
5−REQUIRED="tidy html2markdown"
5+REQUIRED="tidy"
66
77### common.sh
88
@@ -72,14 +72,16 @@ grabber=
7272while [ $# -gt 0 ]; do
7373 case "$1" in
7474 -h|--help)
75− html2markdown -h 2>&1 | sed -e 's/html2markdown/web2markdown/' 1>&2
75+ pandoc -h 2>&1 | sed -e 's/pandoc/web2markdown/' \
76+ -e '/^[[:space:]]*\(-f\|-t\|-S\|-N\|-m\|-i\|-c\|-T\|-D\|-d\)/,/./d'\
77+ 1>&2
7678 err " -e ENCODING, --encoding=ENCODING"
7779 err " Specify character encoding of input"
7880 err " -g COMMAND, --grabber=COMMAND"
7981 err " Specify command to be used to grab contents of URL"
8082 exit 0 ;;
8183 -v|--version)
82− html2markdown -v
84+ pandoc -v 2>&1 | sed -e 's/pandoc/web2markdown/' 1>&2
8385 exit 0 ;;
8486 -e)
8587 shift
@@ -112,7 +114,7 @@ while [ $# -gt 0 ]; do
112114 shift
113115done
114116
115−# Unpack options. Now "$@" will hold the html2markdown options.
117+# Unpack options. Now "$@" will hold the pandoc options.
116118oldifs="$IFS"; IFS="$NEWLINE"; set -- $options; IFS="$oldifs"
117119
118120inurl=
@@ -162,10 +164,11 @@ else # assume UTF-8
162164fi
163165
164166if [ -z "$argument" ]; then
165− tidy -utf8 2>/dev/null | html2markdown "$@"
167+ tidy -utf8 2>/dev/null | pandoc -r html -w markdown "$@"
166168else
167169 if [ -f "$argument" ]; then
168− to_utf8 "$argument" | tidy -utf8 2>/dev/null | html2markdown "$@"
170+ to_utf8 "$argument" |
171+ tidy -utf8 2>/dev/null | pandoc -r html -w markdown "$@"
169172 else
170173 err "File '$argument' not found."
171174 exit 1