2Copyright (C) 2006 John MacFarlane <jgm at berkeley dot edu>
4This program is free software; you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation; either version 2 of the License, or
7(at your option) any later version.
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
14You should have received a copy of the GNU General Public License
15along with this program; if not, write to the Free Software
16Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 Copyright : Copyright (C) 2006 John MacFarlane
22 License : GNU GPL, version 2 or above
24 Maintainer : John MacFarlane <jgm at berkeley dot edu>
26 Portability : portable
28Parses command-line options and calls the appropriate readers and
32import Text.Pandoc.UTF8 ( decodeUTF8, encodeUTF8 )
33import Text.Pandoc.Readers.Markdown ( readMarkdown )
34import Text.Pandoc.Readers.HTML ( readHtml )
35import Text.Pandoc.Writers.S5 ( s5CSS, s5Javascript, writeS5 )
36import Text.Pandoc.Writers.RST ( writeRST )
37import Text.Pandoc.Readers.RST ( readRST )
38import Text.Pandoc.ASCIIMathML ( asciiMathMLScript )
39import Text.Pandoc.Writers.HTML ( writeHtml )
40import Text.Pandoc.Writers.LaTeX ( writeLaTeX )
41import Text.Pandoc.Readers.LaTeX ( readLaTeX )
42import Text.Pandoc.Writers.RTF ( writeRTF )
43import Text.Pandoc.Writers.Markdown ( writeMarkdown )
44import Text.Pandoc.Writers.DefaultHeaders ( defaultHtmlHeader,
45 defaultRTFHeader, defaultS5Header, defaultLaTeXHeader )
46import Text.Pandoc.Definition
47import Text.Pandoc.Shared
48import Text.Regex ( mkRegex, matchRegex )
49import System ( exitWith, getArgs, getProgName )
51import System.Console.GetOpt
53import Data.Maybe ( fromMaybe )
54import Data.List ( isPrefixOf )
55import Char ( toLower )
56import Control.Monad ( (>>=) )
61copyrightMessage :: String
62copyrightMessage = "\nCopyright (C) 2006 John MacFarlane\nWeb: http://sophos.berkeley.edu/macfarlane/pandoc\nThis is free software; see the source for copying conditions. There is no\nwarranty, not even for merchantability or fitness for a particular purpose."
64-- | Association list of formats and readers.
65readers :: [(String, ParserState -> String -> Pandoc)]
66readers = [("native" , readPandoc)
67 ,("markdown" , readMarkdown)
70 ,("latex" , readLaTeX)
73-- | Reader for native Pandoc format.
74readPandoc :: ParserState -> String -> Pandoc
75readPandoc state input = read input
77-- | Association list of formats and pairs of writers and default headers.
78writers :: [ ( String, ( WriterOptions -> Pandoc -> String, String ) ) ]
79writers = [("native" , (writeDoc, ""))
80 ,("html" , (writeHtml, defaultHtmlHeader))
81 ,("s5" , (writeS5, defaultS5Header))
82 ,("latex" , (writeLaTeX, defaultLaTeXHeader))
83 ,("markdown" , (writeMarkdown, ""))
84 ,("rst" , (writeRST, ""))
85 ,("rtf" , (writeRTF, defaultRTFHeader))
88-- | Writer for Pandoc native format.
89writeDoc :: WriterOptions -> Pandoc -> String
90writeDoc options = prettyPandoc
92-- | Data structure for command line options.
94 { optPreserveTabs :: Bool -- ^ Convert tabs to spaces
95 , optTabStop :: Int -- ^ Number of spaces per tab
96 , optStandalone :: Bool -- ^ Include header, footer
97 , optReader :: String -- ^ Reader format
98 , optWriter :: String -- ^ Writer format
99 , optParseRaw :: Bool -- ^ Parse unconvertable HTML and TeX
100 , optCSS :: String -- ^ CSS file to link to
101 , optIncludeInHeader :: String -- ^ File to include in header
102 , optIncludeBeforeBody :: String -- ^ File to include at top of body
103 , optIncludeAfterBody :: String -- ^ File to include at end of body
104 , optCustomHeader :: String -- ^ Custom header to use, or "DEFAULT"
105 , optTitlePrefix :: String -- ^ Optional prefix for HTML title
106 , optOutputFile :: String -- ^ Name of output file
107 , optNumberSections :: Bool -- ^ Number sections in LaTeX
108 , optIncremental :: Bool -- ^ Use incremental lists in S5
109 , optSmart :: Bool -- ^ Use smart typography
110 , optASCIIMathML :: Bool -- ^ Use ASCIIMathML in HTML
111 , optDebug :: Bool -- ^ Output debug messages
112 , optStrict :: Bool -- ^ Use strict markdown syntax
115-- | Defaults for command-line options.
118 { optPreserveTabs = False
120 , optStandalone = False
121 , optReader = "" -- null for default reader
122 , optWriter = "" -- null for default writer
123 , optParseRaw = False
125 , optIncludeInHeader = ""
126 , optIncludeBeforeBody = ""
127 , optIncludeAfterBody = ""
128 , optCustomHeader = "DEFAULT"
129 , optTitlePrefix = ""
130 , optOutputFile = "" -- null for stdout
131 , optNumberSections = False
132 , optIncremental = False
134 , optASCIIMathML = False
139-- | A list of functions, each transforming the options data structure
140-- in response to a command-line option.
141options :: [OptDescr (Opt -> IO Opt)]
143 [ Option "fr" ["from","read"]
145 (\arg opt -> return opt { optReader = map toLower arg })
147 "" -- ("(" ++ (joinWithSep ", " (map fst readers)) ++ ")")
149 , Option "tw" ["to","write"]
151 (\arg opt -> return opt { optWriter = map toLower arg })
153 "" -- ("(" ++ (joinWithSep ", " (map fst writers)) ++ ")")
155 , Option "s" ["standalone"]
157 (\opt -> return opt { optStandalone = True }))
158 "" -- "Include needed header and footer on output"
160 , Option "o" ["output"]
162 (\arg opt -> return opt { optOutputFile = arg })
164 "" -- "Name of output file"
166 , Option "p" ["preserve-tabs"]
168 (\opt -> return opt { optPreserveTabs = True }))
169 "" -- "Preserve tabs instead of converting to spaces"
171 , Option "" ["tab-stop"]
173 (\arg opt -> return opt { optTabStop = (read arg) } )
175 "" -- "Tab stop (default 4)"
177 , Option "" ["strict"]
179 (\opt -> return opt { optStrict = True } ))
180 "" -- "Use strict markdown syntax with no extensions"
182 , Option "R" ["parse-raw"]
184 (\opt -> return opt { optParseRaw = True }))
185 "" -- "Parse untranslatable HTML codes and LaTeX environments as raw"
187 , Option "S" ["smart"]
189 (\opt -> return opt { optSmart = True }))
190 "" -- "Use smart quotes, dashes, and ellipses in HTML output"
192 , Option "m" ["asciimathml"]
194 (\opt -> return opt { optASCIIMathML = True,
195 optStandalone = True }))
196 "" -- "Use ASCIIMathML script in html output"
198 , Option "i" ["incremental"]
200 (\opt -> return opt { optIncremental = True }))
201 "" -- "Make list items display incrementally in S5"
203 , Option "N" ["number-sections"]
205 (\opt -> return opt { optNumberSections = True }))
206 "" -- "Number sections in LaTeX"
210 (\arg opt -> return opt { optCSS = arg,
211 optStandalone = True })
213 "" -- "Link to CSS style sheet"
215 , Option "H" ["include-in-header"]
219 return opt { optIncludeInHeader = text,
220 optStandalone = True })
222 "" -- "File to include at end of header (implies -s)"
224 , Option "B" ["include-before-body"]
228 return opt { optIncludeBeforeBody = text })
230 "" -- "File to include before document body"
232 , Option "A" ["include-after-body"]
236 return opt { optIncludeAfterBody = text })
238 "" -- "File to include after document body"
240 , Option "C" ["custom-header"]
244 return opt { optCustomHeader = text,
245 optStandalone = True })
247 "" -- "File to use for custom header (implies -s)"
249 , Option "T" ["title-prefix"]
251 (\arg opt -> return opt { optTitlePrefix = arg,
252 optStandalone = True })
254 "" -- "String to prefix to HTML window title"
256 , Option "D" ["print-default-header"]
259 let header = case (lookup arg writers) of
260 Just (writer, head) -> head
261 Nothing -> error ("Unknown reader: " ++ arg)
262 hPutStr stdout header
263 exitWith ExitSuccess)
265 "" -- "Print default header for FORMAT"
267 , Option "d" ["debug"]
269 (\opt -> return opt { optDebug = True }))
270 "" -- "Print debug messages to stderr, output to stdout"
272 , Option "v" ["version"]
276 hPutStrLn stderr (prg ++ " " ++ version ++
278 exitWith $ ExitFailure 2))
279 "" -- "Print version"
281 , Option "h" ["help"]
285 hPutStr stderr (usageMessage prg options)
286 exitWith $ ExitFailure 2))
290-- Returns usage message
291usageMessage :: String -> [OptDescr (Opt -> IO Opt)] -> String
292usageMessage programName options = usageInfo
293 (programName ++ " [OPTIONS] [FILES]" ++ "\nInput formats: " ++
294 joinWithSep ", " (map fst readers) ++ "\nOutput formats: " ++
295 joinWithSep ", " (map fst writers) ++ "\nOptions:")
298-- Determine default reader based on source file extensions
299defaultReaderName :: [String] -> String
300defaultReaderName [] = "markdown"
301defaultReaderName (x:xs) =
302 let x' = map toLower x in
303 case (matchRegex (mkRegex ".*\\.(.*)") x') of
304 Nothing -> defaultReaderName xs -- no extension
305 Just ["xhtml"] -> "html"
306 Just ["html"] -> "html"
307 Just ["htm"] -> "html"
308 Just ["tex"] -> "latex"
309 Just ["latex"] -> "latex"
310 Just ["ltx"] -> "latex"
311 Just ["rst"] -> "rst"
312 Just ["native"] -> "native"
315-- Determine default writer based on output file extension
316defaultWriterName :: String -> String
317defaultWriterName "" = "html" -- no output file
319 let x' = map toLower x in
320 case (matchRegex (mkRegex ".*\\.(.*)") x') of
321 Nothing -> "markdown" -- no extension
322 Just [""] -> "markdown" -- empty extension
323 Just ["tex"] -> "latex"
324 Just ["latex"] -> "latex"
325 Just ["ltx"] -> "latex"
326 Just ["rtf"] -> "rtf"
327 Just ["rst"] -> "rst"
329 Just ["native"] -> "native"
330 Just ["txt"] -> "markdown"
331 Just ["text"] -> "markdown"
332 Just ["md"] -> "markdown"
333 Just ["markdown"] -> "markdown"
339 let (actions, sources, errors) = getOpt Permute options args
341 if (not (null errors))
344 mapM (\e -> hPutStrLn stderr e) errors
345 hPutStr stderr (usageMessage name options)
346 exitWith $ ExitFailure 2
350 -- thread option data structure through all supplied option actions
351 opts <- foldl (>>=) (return defaultOpts) actions
353 let Opt { optPreserveTabs = preserveTabs
354 , optTabStop = tabStop
355 , optStandalone = standalone
356 , optReader = readerName
357 , optWriter = writerName
358 , optParseRaw = parseRaw
360 , optIncludeInHeader = includeHeader
361 , optIncludeBeforeBody = includeBefore
362 , optIncludeAfterBody = includeAfter
363 , optCustomHeader = customHeader
364 , optTitlePrefix = titlePrefix
365 , optOutputFile = outputFile
366 , optNumberSections = numberSections
367 , optIncremental = incremental
369 , optASCIIMathML = asciiMathML
374 -- assign reader and writer based on options and filenames
375 let readerName' = if null readerName
376 then defaultReaderName sources
379 let writerName' = if null writerName
380 then defaultWriterName outputFile
383 reader <- case (lookup readerName' readers) of
385 Nothing -> error ("Unknown reader: " ++ readerName')
387 (writer, defaultHeader) <- case (lookup writerName' writers) of
388 Just (w,h) -> return (w, h)
389 Nothing -> error ("Unknown writer: " ++ writerName')
391 output <- if ((null outputFile) || debug)
393 else openFile outputFile WriteMode
397 hPutStrLn stderr ("OUTPUT=" ++ outputFile)
398 hPutStr stderr $ concatMap (\s -> "INPUT=" ++ s ++ "\n") sources
401 let tabFilter = if preserveTabs then id else (tabsToSpaces tabStop)
402 let addBlank str = str ++ "\n\n"
403 let removeCRs str = filter (/= '\r') str -- remove DOS-style line endings
404 let filter = tabFilter . addBlank . removeCRs
405 let startParserState = defaultParserState { stateParseRaw = parseRaw,
406 stateTabStop = tabStop,
407 stateStandalone = standalone &&
409 stateStrict = strict }
410 let csslink = if (css == "")
412 else "<link rel=\"stylesheet\" href=\"" ++ css ++
413 "\" type=\"text/css\" media=\"all\" />\n"
414 let asciiMathMLLink = if asciiMathML then asciiMathMLScript else ""
415 let header = (if (customHeader == "DEFAULT")
417 else customHeader) ++
418 csslink ++ asciiMathMLLink ++ includeHeader
419 let writerOptions = WriterOptions { writerStandalone = standalone &&
421 writerHeader = header,
422 writerTitlePrefix = titlePrefix,
423 writerSmart = smart &&
425 writerTabStop = tabStop,
426 writerS5 = (writerName=="s5"),
427 writerIncremental = incremental,
428 writerNumberSections = numberSections,
429 writerIncludeBefore = includeBefore,
430 writerIncludeAfter = includeAfter,
431 writerStrictMarkdown = strict }
433 (readSources sources) >>= (hPutStr output . encodeUTF8 .
434 (writer writerOptions) .
435 (reader startParserState) . filter .
436 decodeUTF8 . (joinWithSep "\n")) >> hClose output
439 readSources [] = mapM readSource ["-"]
440 readSources sources = mapM readSource sources
441 readSource "-" = getContents
442 readSource source = readFile source
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list