git.iko.soy/typeable/pandoc
on master help sign in
Implemented Ext_mmd_title_block in markdown reader & writer.
commite8e8468d69b885a9e84435f29c4b553bb4d2417d authorJohn MacFarlane <jgm@berkeley.edu> dateSun 12 Aug 2012, 19:27 (14 years ago) parent5d83751
4 files changed+75−13
README+18−2 src/Text/Pandoc/Options.hs+3−2 src/Text/Pandoc/Readers/Markdown.hs+25−3 src/Text/Pandoc/Writers/Markdown.hs+29−6
README+18−2
@@ -1534,7 +1534,7 @@ you'll need to add colons as above.
15341534Title block
15351535-----------
15361536
1537−**Extension: `pandoc_title_blocks`**
1537+**Extension: `pandoc_title_block`**
15381538
15391539If the file begins with a title block
15401540
@@ -2207,12 +2207,28 @@ Causes anything between `\\(` and `\\)` to be interpreted as inline
22072207TeX math, and anything between `\\[` and `\\]` to be interpreted
22082208as display TeX math.
22092209
2210−+**Extension: `markdown_attribute`**\
2210+**Extension: `markdown_attribute`**\
22112211Causes the attribute `markdown=1` to be added to all block-level
22122212HTML tags that might contain markdown. In pandoc, material inside
22132213block-level tags is interpreted a markdown by default, but in some
22142214other implementations, the `markdown=1` tag is needed.
22152215
2216+**Extension: `mmd_title_block`**\
2217+Enables a [MultiMarkdown] style title block at the top of
2218+the document, for example:
2219+
2220+ Title: My title
2221+ Author: John Doe
2222+ Date: September 1, 2008
2223+ Comment: This is a sample mmd title block, with
2224+ a field spanning multiple lines.
2225+
2226+See the MultiMarkdown documentation for details. Note that only title,
2227+author, and date are recognized; other fields are simply ignored by
2228+pandoc. If `pandoc_title_block` is enabled, it will take precedence over
2229+`mmd_title_block`.
2230+
2231+ [MultiMarkdown]: http://fletcherpenney.net/multimarkdown/
22162232
22172233Producing slide shows with Pandoc
22182234=================================
src/Text/Pandoc/Options.hs+3−2
@@ -48,7 +48,8 @@ import Text.Pandoc.Highlighting (Style, pygments)
4848-- | Individually selectable syntax extensions.
4949data Extension = Ext_footnotes
5050 | Ext_inline_notes
51− | Ext_pandoc_title_blocks
51+ | Ext_pandoc_title_block
52+ | Ext_mmd_title_block
5253 | Ext_table_captions
5354 -- | Ext_image_captions
5455 | Ext_simple_tables
@@ -87,7 +88,7 @@ pandocExtensions :: Set Extension
8788pandocExtensions = Set.fromList
8889 [ Ext_footnotes
8990 , Ext_inline_notes
90− , Ext_pandoc_title_blocks
91+ , Ext_pandoc_title_block
9192 , Ext_table_captions
9293 -- , Ext_image_captions
9394 , Ext_simple_tables
src/Text/Pandoc/Readers/Markdown.hs+25−3
@@ -35,7 +35,7 @@ module Text.Pandoc.Readers.Markdown ( readMarkdown ) where
3535import Data.List ( transpose, sortBy, findIndex, intercalate )
3636import qualified Data.Map as M
3737import Data.Ord ( comparing )
38−import Data.Char ( isAlphaNum )
38+import Data.Char ( isAlphaNum, toLower )
3939import Data.Maybe
4040import Text.Pandoc.Definition
4141import qualified Text.Pandoc.Builder as B
@@ -175,14 +175,36 @@ dateLine = try $ do
175175 trimInlinesF . mconcat <$> manyTill inline newline
176176
177177titleBlock :: Parser [Char] ParserState (F Inlines, F [Inlines], F Inlines)
178−titleBlock = try $ do
179− guardEnabled Ext_pandoc_title_blocks
178+titleBlock = pandocTitleBlock <|> mmdTitleBlock
179+
180+pandocTitleBlock :: Parser [Char] ParserState (F Inlines, F [Inlines], F Inlines)
181+pandocTitleBlock = try $ do
182+ guardEnabled Ext_pandoc_title_block
180183 title <- option mempty titleLine
181184 author <- option (return []) authorsLine
182185 date <- option mempty dateLine
183186 optional blanklines
184187 return (title, author, date)
185188
189+mmdTitleBlock :: Parser [Char] ParserState (F Inlines, F [Inlines], F Inlines)
190+mmdTitleBlock = try $ do
191+ guardEnabled Ext_mmd_title_block
192+ kvPairs <- many1 kvPair
193+ blanklines
194+ let title = maybe mempty return $ lookup "title" kvPairs
195+ let author = maybe mempty (\x -> return [x]) $ lookup "author" kvPairs
196+ let date = maybe mempty return $ lookup "date" kvPairs
197+ return (title, author, date)
198+
199+kvPair :: Parser [Char] ParserState (String, Inlines)
200+kvPair = try $ do
201+ key <- many1Till (alphaNum <|> oneOf "_- ") (char ':')
202+ val <- manyTill anyChar
203+ (try $ newline >> lookAhead (blankline <|> nonspaceChar))
204+ let key' = concat $ words $ map toLower key
205+ let val' = trimInlines $ B.text val
206+ return (key',val')
207+
186208parseMarkdown :: Parser [Char] ParserState Pandoc
187209parseMarkdown = do
188210 -- markdown allows raw HTML
src/Text/Pandoc/Writers/Markdown.hs+29−6
@@ -87,14 +87,39 @@ plainify = bottomUp go
8787 go (Cite _ cits) = SmallCaps cits
8888 go x = x
8989
90+pandocTitleBlock :: Doc -> [Doc] -> Doc -> Doc
91+pandocTitleBlock tit auths dat =
92+ hang 2 (text "% ") tit <> cr <>
93+ hang 2 (text "% ") (hcat (intersperse (text "; ") auths)) <> cr <>
94+ hang 2 (text "% ") dat <> cr
95+
96+mmdTitleBlock :: Doc -> [Doc] -> Doc -> Doc
97+mmdTitleBlock tit auths dat =
98+ hang 8 (text "Title: ") tit <> cr <>
99+ hang 8 (text "Author: ") (hcat (intersperse (text "; ") auths)) <> cr <>
100+ hang 8 (text "Date: ") dat <> cr
101+
102+plainTitleBlock :: Doc -> [Doc] -> Doc -> Doc
103+plainTitleBlock tit auths dat =
104+ tit <> cr <>
105+ (hcat (intersperse (text "; ") auths)) <> cr <>
106+ dat <> cr
107+
90108-- | Return markdown representation of document.
91109pandocToMarkdown :: WriterOptions -> Pandoc -> State WriterState String
92110pandocToMarkdown opts (Pandoc (Meta title authors date) blocks) = do
93111 title' <- inlineListToMarkdown opts title
94112 authors' <- mapM (inlineListToMarkdown opts) authors
95113 date' <- inlineListToMarkdown opts date
96− let titleblock = isEnabled Ext_pandoc_title_blocks opts &&
97− not (null title && null authors && null date)
114+ isPlain <- gets stPlain
115+ let titleblock = case True of
116+ _ | isPlain ->
117+ plainTitleBlock title' authors' date'
118+ | isEnabled Ext_pandoc_title_block opts ->
119+ pandocTitleBlock title' authors' date'
120+ | isEnabled Ext_mmd_title_block opts ->
121+ mmdTitleBlock title' authors' date'
122+ | otherwise -> empty
98123 let headerBlocks = filter isHeaderBlock blocks
99124 let toc = if writerTableOfContents opts
100125 then tableOfContents opts headerBlocks
@@ -113,11 +138,9 @@ pandocToMarkdown opts (Pandoc (Meta title authors date) blocks) = do
113138 let context = writerVariables opts ++
114139 [ ("toc", render colwidth toc)
115140 , ("body", main)
116− , ("title", render colwidth title')
117− , ("date", render colwidth date')
118141 ] ++
119− [ ("titleblock", "yes") | titleblock ] ++
120− [ ("author", render colwidth a) | a <- authors' ]
142+ [ ("titleblock", render colwidth titleblock)
143+ | not (null title && null authors && null date) ]
121144 if writerStandalone opts
122145 then return $ renderTemplate context $ writerTemplate opts
123146 else return main