git.iko.soy/typeable/pandoc
on master help sign in
Added 'beamer' as an output format.

Beamer output uses the default LaTeX template, with some customizations via variables. Added `writerBeamer` to `WriterOptions`. Added `--beamer` option to `markdown2pdf`.

commitea39a607eda7ea45906db44ccab4dc36bd43be89 authorJohn MacFarlane <fiddlosopher@gmail.com> dateThu 29 Dec 2011, 13:24 (15 years ago) parent012405e
10 files changed+123−41
README+29−18 src/Text/Pandoc.hs+2 src/Text/Pandoc/Shared.hs+2 src/Text/Pandoc/Templates.hs+1 src/Text/Pandoc/Writers/LaTeX.hs+75−13 src/markdown2pdf.hs+10−6 templates+1−1 tests/lhs-test.latex+1−1 tests/lhs-test.latex+lhs+1−1 tests/writer.latex+1−1
README+29−18
@@ -14,10 +14,10 @@ Pandoc is a [Haskell] library for converting from one markup format to
1414another, and a command-line tool that uses this library. It can read
1515[markdown] and (subsets of) [Textile], [reStructuredText], [HTML],
1616and [LaTeX]; and it can write plain text, [markdown], [reStructuredText],
17−[HTML], [LaTeX], [ConTeXt], [RTF], [DocBook XML], [OpenDocument XML], [ODT],
18−[GNU Texinfo], [MediaWiki markup], [EPUB], [Textile], [groff man] pages,
19−[Emacs Org-Mode], [AsciiDoc], and [Slidy], [DZSlides], or [S5] HTML
20−slide shows.
17+[HTML], [LaTeX], [LaTeX beamer], [ConTeXt], [RTF], [DocBook XML],
18+[OpenDocument XML], [ODT], [GNU Texinfo], [MediaWiki markup], [EPUB],
19+[Textile], [groff man] pages, [Emacs Org-Mode], [AsciiDoc], and [Slidy],
20+[DZSlides], or [S5] HTML slide shows.
2121
2222Pandoc's enhanced version of markdown includes syntax for footnotes,
2323tables, flexible ordered lists, definition lists, delimited code blocks,
@@ -151,20 +151,19 @@ Options
151151`-t` *FORMAT*, `-w` *FORMAT*, `--to=`*FORMAT*, `--write=`*FORMAT*
152152: Specify output format. *FORMAT* can be `native` (native Haskell),
153153 `json` (JSON version of native AST), `plain` (plain text),
154− `markdown` (markdown), `rst` (reStructuredText),
155− `html` (HTML), `latex` (LaTeX), `context` (ConTeXt), `man` (groff man),
156− `mediawiki` (MediaWiki markup), `textile` (Textile), `org` (Emacs
157− Org-Mode), `texinfo` (GNU Texinfo), `docbook` (DocBook XML),
154+ `markdown` (markdown), `rst` (reStructuredText), `html` (HTML), `latex`
155+ (LaTeX), `beamer` (LaTeX beamer), `context` (ConTeXt), `man` (groff
156+ man), `mediawiki` (MediaWiki markup), `textile` (Textile), `org`
157+ (Emacs Org-Mode), `texinfo` (GNU Texinfo), `docbook` (DocBook XML),
158158 `opendocument` (OpenDocument XML), `odt` (OpenOffice text document),
159159 `epub` (EPUB book), `asciidoc` (AsciiDoc), `slidy` (Slidy HTML and
160160 javascript slide show), `dzslides` (HTML5 + javascript slide show),
161− `s5` (S5 HTML and javascript slide show), or `rtf` (rich text
162− format). Note that `odt` and `epub` output will not be directed to
163− *stdout*; an output filename must be specified using the `-o/--output`
164− option. If `+lhs` is appended to `markdown`, `rst`, `latex`, or `html`,
165− the output will be rendered as literate Haskell source:
166− see [Literate Haskell support](#literate-haskell-support),
167− below.
161+ `s5` (S5 HTML and javascript slide show), or `rtf` (rich text format).
162+ Note that `odt` and `epub` output will not be directed to *stdout*; an
163+ output filename must be specified using the `-o/--output` option. If
164+ `+lhs` is appended to `markdown`, `rst`, `latex`, or `html`, the output
165+ will be rendered as literate Haskell source: see [Literate Haskell
166+ support](#literate-haskell-support), below.
168167
169168`-s`, `--standalone`
170169: Produce output with an appropriate header and footer (e.g. a
@@ -580,6 +579,14 @@ depending on the output format, but include:
580579 `http://www.w3.org/Talks/Tools/Slidy2`)
581580`s5-url`
582581: base URL for S5 documents (defaults to `ui/default`)
582+`font-size`
583+: font size (10pt, 11pt, 12pt) for LaTeX and beamer documents
584+`documentclass`
585+: document class for LaTeX documents
586+`theme`
587+: theme for beamer documents
588+`colortheme`
589+: colortheme for beamer documents
583590
584591Variables may be set at the command line using the `-V/--variable`
585592option. This allows users to include custom variables in their
@@ -1903,12 +1910,14 @@ document with an appropriate header:
19031910The bibliography will be inserted after this header.
19041911
19051912
1906−Producing HTML slide shows with Pandoc
1907−======================================
1913+Producing slide shows with Pandoc
1914+=================================
19081915
19091916You can use Pandoc to produce an HTML + javascript slide presentation
19101917that can be viewed via a web browser. There are three ways to do this,
1911−using [S5], [DZSlides], or [Slidy].
1918+using [S5], [DZSlides], or [Slidy]. You can also produce a PDF slide
1919+show using [LaTeX beamer]: just pass the `--beamer` option to
1920+`markdown2pdf`.
19121921
19131922Here's the markdown source for a simple slide show, `eating.txt`:
19141923
@@ -1946,6 +1955,7 @@ for DZSlides.
19461955
19471956A title page is constructed automatically from the document's title
19481957block. Each level-one header and horizontal rule begins a new slide.
1958+(If beamer is used, all headers begin a new slide.)
19491959
19501960For Slidy and S5, the file produced by pandoc with the `-s/--standalone`
19511961option embeds a link to javascripts and CSS files, which are assumed to
@@ -2043,6 +2053,7 @@ Christopher Sawicki, Kelsey Hightower.
20432053[Slidy]: http://www.w3.org/Talks/Tools/Slidy/
20442054[HTML]: http://www.w3.org/TR/html40/
20452055[LaTeX]: http://www.latex-project.org/
2056+[LaTeX beamer]: http://www.tex.ac.uk/CTAN/macros/latex/contrib/beamer
20462057[ConTeXt]: http://www.pragma-ade.nl/
20472058[RTF]: http://en.wikipedia.org/wiki/Rich_Text_Format
20482059[DocBook XML]: http://www.docbook.org/
src/Text/Pandoc.hs+2
@@ -181,6 +181,8 @@ writers = [("native" , writeNative)
181181 ,("latex" , writeLaTeX)
182182 ,("latex+lhs" , \o ->
183183 writeLaTeX o{ writerLiterateHaskell = True })
184+ ,("beamer" , \o ->
185+ writeLaTeX o{ writerBeamer = True })
184186 ,("context" , writeConTeXt)
185187 ,("texinfo" , writeTexinfo)
186188 ,("man" , writeMan)
src/Text/Pandoc/Shared.hs+2
@@ -477,6 +477,7 @@ data WriterOptions = WriterOptions
477477 , writerCiteMethod :: CiteMethod -- ^ How to print cites
478478 , writerBiblioFiles :: [FilePath] -- ^ Biblio files to use for citations
479479 , writerHtml5 :: Bool -- ^ Produce HTML5
480+ , writerBeamer :: Bool -- ^ Produce beamer LaTeX slide show
480481 , writerChapters :: Bool -- ^ Use "chapter" for top-level sects
481482 , writerListings :: Bool -- ^ Use listings package for code
482483 , writerHighlight :: Bool -- ^ Highlight source code
@@ -512,6 +513,7 @@ defaultWriterOptions =
512513 , writerCiteMethod = Citeproc
513514 , writerBiblioFiles = []
514515 , writerHtml5 = False
516+ , writerBeamer = False
515517 , writerChapters = False
516518 , writerListings = False
517519 , writerHighlight = False
src/Text/Pandoc/Templates.hs+1
@@ -85,6 +85,7 @@ getDefaultTemplate _ "native" = return $ Right ""
8585getDefaultTemplate _ "json" = return $ Right ""
8686getDefaultTemplate user "odt" = getDefaultTemplate user "opendocument"
8787getDefaultTemplate user "epub" = getDefaultTemplate user "html"
88+getDefaultTemplate user "beamer" = getDefaultTemplate user "latex"
8889getDefaultTemplate user writer = do
8990 let format = takeWhile (/='+') writer -- strip off "+lhs" if present
9091 let fname = "templates" </> "default" <.> format
src/Text/Pandoc/Writers/LaTeX.hs+75−13
@@ -62,6 +62,8 @@ data WriterState =
6262 , stBook :: Bool -- true if document uses book or memoir class
6363 , stCsquotes :: Bool -- true if document uses csquotes
6464 , stHighlighting :: Bool -- true if document has highlighted code
65+ , stFirstFrame :: Bool -- true til we've written first beamer frame
66+ , stIncremental :: Bool -- true if beamer lists should be displayed bit by bit
6567 }
6668
6769-- | Convert Pandoc to LaTeX.
@@ -74,23 +76,24 @@ writeLaTeX options document =
7476 stTable = False, stStrikeout = False, stSubscript = False,
7577 stUrl = False, stGraphics = False,
7678 stLHS = False, stBook = writerChapters options,
77− stCsquotes = False, stHighlighting = False }
79+ stCsquotes = False, stHighlighting = False,
80+ stFirstFrame = True, stIncremental = writerIncremental options }
7881
7982pandocToLaTeX :: WriterOptions -> Pandoc -> State WriterState String
8083pandocToLaTeX options (Pandoc (Meta title authors date) blocks) = do
8184 let template = writerTemplate options
85+ let templateLines = lines template
8286 let usesBookClass x = "\\documentclass" `isPrefixOf` x &&
8387 ("{memoir}" `isSuffixOf` x || "{book}" `isSuffixOf` x ||
8488 "{report}" `isSuffixOf` x)
85− when (any usesBookClass (lines template)) $
89+ when (any usesBookClass templateLines) $
8690 modify $ \s -> s{stBook = True}
8791 -- check for \usepackage...{csquotes}; if present, we'll use
8892 -- \enquote{...} for smart quotes:
8993 when ("{csquotes}" `isInfixOf` template) $
9094 modify $ \s -> s{stCsquotes = True}
91− opts <- liftM stOptions get
92− let colwidth = if writerWrapText opts
93− then Just $ writerColumns opts
95+ let colwidth = if writerWrapText options
96+ then Just $ writerColumns options
9497 else Nothing
9598 titletext <- liftM (render colwidth) $ inlineListToLaTeX title
9699 authorsText <- mapM (liftM (render colwidth) . inlineListToLaTeX) authors
@@ -100,7 +103,10 @@ pandocToLaTeX options (Pandoc (Meta title authors date) blocks) = do
100103 else case last blocks of
101104 Header 1 il -> (init blocks, il)
102105 _ -> (blocks, [])
103− body <- blockListToLaTeX blocks'
106+ blocks'' <- if writerBeamer options
107+ then toSlides blocks'
108+ else return blocks'
109+ body <- blockListToLaTeX blocks''
104110 biblioTitle <- liftM (render colwidth) $ inlineListToLaTeX lastHeader
105111 let main = render colwidth body
106112 st <- get
@@ -119,7 +125,12 @@ pandocToLaTeX options (Pandoc (Meta title authors date) blocks) = do
119125 [ ("toc", if writerTableOfContents options then "yes" else "")
120126 , ("body", main)
121127 , ("title", titletext)
122− , ("date", dateText) ] ++
128+ , ("date", dateText)
129+ , ("documentclass", if writerBeamer options
130+ then "beamer"
131+ else if writerChapters options
132+ then "book"
133+ else "article") ] ++
123134 [ ("author", a) | a <- authorsText ] ++
124135 [ ("verbatim-in-note", "yes") | stVerbInNote st ] ++
125136 [ ("fancy-enums", "yes") | stEnumerate st ] ++
@@ -132,8 +143,9 @@ pandocToLaTeX options (Pandoc (Meta title authors date) blocks) = do
132143 [ ("graphics", "yes") | stGraphics st ] ++
133144 [ ("book-class", "yes") | stBook st] ++
134145 [ ("listings", "yes") | writerListings options || stLHS st ] ++
146+ [ ("beamer", "yes") | writerBeamer options ] ++
135147 [ ("highlighting-macros", styleToLaTeX
136− $ writerHighlightStyle opts ) | stHighlighting st ] ++
148+ $ writerHighlightStyle options ) | stHighlighting st ] ++
137149 citecontext
138150 return $ if writerStandalone options
139151 then renderTemplate context template
@@ -171,6 +183,42 @@ stringToLaTeX isUrl = escapeStringUsing latexEscapes
171183inCmd :: String -> Doc -> Doc
172184inCmd cmd contents = char '\\' <> text cmd <> braces contents
173185
186+toSlides :: [Block] -> State WriterState [Block]
187+toSlides (Header n ils : bs) = do
188+ tit <- inlineListToLaTeX ils
189+ firstFrame <- gets stFirstFrame
190+ modify $ \s -> s{ stFirstFrame = False }
191+ -- note: [fragile] is required or verbatim breaks
192+ result <- ((Header n ils :) .
193+ (RawBlock "latex" ("\\begin{frame}[fragile]\n" ++
194+ "\\frametitle{" ++ render Nothing tit ++ "}") :))
195+ `fmap` toSlides bs
196+ if firstFrame
197+ then return result
198+ else return $ RawBlock "latex" "\\end{frame}" : result
199+toSlides (HorizontalRule : Header n ils : bs) =
200+ toSlides (Header n ils : bs)
201+toSlides (HorizontalRule : bs) = do
202+ firstFrame <- gets stFirstFrame
203+ modify $ \s -> s{ stFirstFrame = False }
204+ result <- (RawBlock "latex" "\\begin{frame}[fragile]" :)
205+ `fmap` toSlides bs
206+ if firstFrame
207+ then return result
208+ else return $ RawBlock "latex" "\\end{frame}" : result
209+toSlides (b:bs) = (b:) `fmap` toSlides bs
210+toSlides [] = do
211+ firstFrame <- gets stFirstFrame
212+ if firstFrame
213+ then return []
214+ else return [RawBlock "latex" "\\end{frame}"]
215+
216+isListBlock :: Block -> Bool
217+isListBlock (BulletList _) = True
218+isListBlock (OrderedList _ _) = True
219+isListBlock (DefinitionList _) = True
220+isListBlock _ = False
221+
174222-- | Convert Pandoc block element to LaTeX.
175223blockToLaTeX :: Block -- ^ Block to convert
176224 -> State WriterState Doc
@@ -185,8 +233,17 @@ blockToLaTeX (Para lst) = do
185233 result <- inlineListToLaTeX lst
186234 return $ result <> blankline
187235blockToLaTeX (BlockQuote lst) = do
188− contents <- blockListToLaTeX lst
189− return $ "\\begin{quote}" $$ contents $$ "\\end{quote}"
236+ beamer <- writerBeamer `fmap` gets stOptions
237+ case lst of
238+ [b] | beamer && isListBlock b -> do
239+ oldIncremental <- gets stIncremental
240+ modify $ \s -> s{ stIncremental = True }
241+ result <- blockToLaTeX b
242+ modify $ \s -> s{ stIncremental = oldIncremental }
243+ return result
244+ _ -> do
245+ contents <- blockListToLaTeX lst
246+ return $ "\\begin{quote}" $$ contents $$ "\\end{quote}"
190247blockToLaTeX (CodeBlock (_,classes,keyvalAttr) str) = do
191248 opts <- gets stOptions
192249 case () of
@@ -243,10 +300,13 @@ blockToLaTeX (CodeBlock (_,classes,keyvalAttr) str) = do
243300blockToLaTeX (RawBlock "latex" x) = return $ text x <> blankline
244301blockToLaTeX (RawBlock _ _) = return empty
245302blockToLaTeX (BulletList lst) = do
303+ incremental <- gets stIncremental
304+ let inc = if incremental then "[<+->]" else ""
246305 items <- mapM listItemToLaTeX lst
247− return $ "\\begin{itemize}" $$ vcat items $$ "\\end{itemize}"
306+ return $ text ("\\begin{itemize}" ++ inc) $$ vcat items $$ "\\end{itemize}"
248307blockToLaTeX (OrderedList (start, numstyle, numdelim) lst) = do
249308 st <- get
309+ let inc = if stIncremental st then "[<+->]" else ""
250310 let oldlevel = stOLLevel st
251311 put $ st {stOLLevel = oldlevel + 1}
252312 items <- mapM listItemToLaTeX lst
@@ -263,11 +323,13 @@ blockToLaTeX (OrderedList (start, numstyle, numdelim) lst) = do
263323 map toLower (toRomanNumeral oldlevel) ++
264324 "}{" ++ show (start - 1) ++ "}"
265325 else empty
266− return $ "\\begin{enumerate}" <> exemplar $$ resetcounter $$
326+ return $ text ("\\begin{enumerate}" ++ inc) <> exemplar $$ resetcounter $$
267327 vcat items $$ "\\end{enumerate}"
268328blockToLaTeX (DefinitionList lst) = do
329+ incremental <- gets stIncremental
330+ let inc = if incremental then "[<+->]" else ""
269331 items <- mapM defListItemToLaTeX lst
270− return $ "\\begin{description}" $$ vcat items $$ "\\end{description}"
332+ return $ text ("\\begin{description}" ++ inc) $$ vcat items $$ "\\end{description}"
271333blockToLaTeX HorizontalRule = return $
272334 "\\begin{center}\\rule{3in}{0.4pt}\\end{center}" $$ blankline
273335blockToLaTeX (Header level lst) = do
src/markdown2pdf.hs+10−6
@@ -78,11 +78,11 @@ parsePandocArgs args = do
7878 --trim = reverse . dropWhile isSpace . reverse . dropWhile isSpace
7979 trim = takeWhile (/='\r') . dropWhile (=='\r')
8080
81−runPandoc :: [String] -> FilePath -> IO (Either String FilePath)
82−runPandoc inputsAndArgs output = do
81+runPandoc :: String -> [String] -> FilePath -> IO (Either String FilePath)
82+runPandoc outputFormat inputsAndArgs output = do
8383 let texFile = addExtension output "tex"
8484 result <- run "pandoc" $
85− ["-s", "--no-wrap", "-r", "markdown", "-w", "latex"]
85+ ["-s", "--no-wrap", "-r", "markdown", "-w", outputFormat]
8686 ++ inputsAndArgs ++ ["-o", texFile]
8787 return $ either Left (const $ Right texFile) result
8888
@@ -207,14 +207,15 @@ main = withTempDir "pandoc"
207207 "--custom-header","--output",
208208 "--template", "--variable",
209209 "--no-highlight", "--highlight-style",
210− "--csl", "--bibliography", "--data-dir", "--listings"]
210+ "--csl", "--bibliography", "--data-dir", "--listings",
211+ "--beamer"]
211212 let isOpt ('-':_) = True
212213 isOpt _ = False
213214 let opts = filter isOpt args
214215 -- note that a long option can come in this form: --opt=val
215216 let isGoodopt x = x `elem` (goodopts ++ goodoptslong) ||
216217 any (\o -> (o ++ "=") `isPrefixOf` x) goodoptslong
217− let markdown2pdfOpts = ["--xetex","--luatex"]
218+ let markdown2pdfOpts = ["--xetex","--luatex", "--beamer"]
218219 unless (all isGoodopt opts) $ do
219220 (code, out, _err) <- readProcessWithExitCode "pandoc" ["--help"] ""
220221 UTF8.putStrLn "markdown2pdf [OPTIONS] [FILES]\nOptions:"
@@ -231,6 +232,9 @@ main = withTempDir "pandoc"
231232 else if "--luatex" `elem` opts
232233 then "lualatex"
233234 else "pdflatex"
235+ let outputFormat = if "--beamer" `elem` opts
236+ then "beamer"
237+ else "latex"
234238 let execs = ["pandoc", latexProgram, "bibtex"]
235239 paths <- mapM findExecutable execs
236240 let miss = map snd $ filter (isNothing . fst) $ zip paths execs
@@ -249,7 +253,7 @@ main = withTempDir "pandoc"
249253 -- no need because we'll pass all arguments to pandoc
250254 Just (_ ,out) -> return ([], out)
251255 -- run pandoc
252− pandocRes <- runPandoc (input ++ args') $ replaceDirectory output tmp
256+ pandocRes <- runPandoc outputFormat (input ++ args') $ replaceDirectory output tmp
253257 case pandocRes of
254258 Left err -> exit err
255259 Right texFile -> do
templates+1−1

Submodule changed.

tests/lhs-test.latex+1−1
@@ -1,4 +1,4 @@
1−\documentclass{article}
1+\documentclass[]{article}
22\usepackage{amssymb,amsmath}
33\usepackage{ifxetex,ifluatex}
44\ifxetex
tests/lhs-test.latex+lhs+1−1
@@ -1,4 +1,4 @@
1−\documentclass{article}
1+\documentclass[]{article}
22\usepackage{amssymb,amsmath}
33\usepackage{ifxetex,ifluatex}
44\ifxetex
tests/writer.latex+1−1
@@ -1,4 +1,4 @@
1−\documentclass{article}
1+\documentclass[]{article}
22\usepackage{amssymb,amsmath}
33\usepackage{ifxetex,ifluatex}
44\ifxetex