git.iko.soy/typeable/pandoc
on master help sign in
Implemented Ext_mmd_title_block in markdown reader & writer.
e8e8468John MacFarlane
README+18−2
@@ -1534,7 +1534,7 @@ you'll need to add colons as above.
1534Title block
1534Title block
1535-----------
1535-----------
1536
1536
1537−**Extension: `pandoc_title_blocks`**
1537+**Extension: `pandoc_title_block`**
1538
1538
1539If the file begins with a title block
1539If the file begins with a title block
1540
1540
@@ -2207,12 +2207,28 @@ Causes anything between `\\(` and `\\)` to be interpreted as inline
2207TeX math, and anything between `\\[` and `\\]` to be interpreted
2207TeX math, and anything between `\\[` and `\\]` to be interpreted
2208as display TeX math.
2208as display TeX math.
2209
2209
2210−+**Extension: `markdown_attribute`**\
2210+**Extension: `markdown_attribute`**\
2211Causes the attribute `markdown=1` to be added to all block-level
2211Causes the attribute `markdown=1` to be added to all block-level
2212HTML tags that might contain markdown. In pandoc, material inside
2212HTML tags that might contain markdown. In pandoc, material inside
2213block-level tags is interpreted a markdown by default, but in some
2213block-level tags is interpreted a markdown by default, but in some
2214other implementations, the `markdown=1` tag is needed.
2214other implementations, the `markdown=1` tag is needed.
2215
2215
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/
2216
2232
2217Producing slide shows with Pandoc
2233Producing slide shows with Pandoc
2218=================================
2234=================================
src/Text/Pandoc/Options.hs+3−2
@@ -48,7 +48,8 @@ import Text.Pandoc.Highlighting (Style, pygments)
48-- | Individually selectable syntax extensions.
48-- | Individually selectable syntax extensions.
49data Extension = Ext_footnotes
49data Extension = Ext_footnotes
50 | Ext_inline_notes
50 | Ext_inline_notes
51− | Ext_pandoc_title_blocks
51+ | Ext_pandoc_title_block
52+ | Ext_mmd_title_block
52 | Ext_table_captions
53 | Ext_table_captions
53 -- | Ext_image_captions
54 -- | Ext_image_captions
54 | Ext_simple_tables
55 | Ext_simple_tables
@@ -87,7 +88,7 @@ pandocExtensions :: Set Extension
87pandocExtensions = Set.fromList
88pandocExtensions = Set.fromList
88 [ Ext_footnotes
89 [ Ext_footnotes
89 , Ext_inline_notes
90 , Ext_inline_notes
90− , Ext_pandoc_title_blocks
91+ , Ext_pandoc_title_block
91 , Ext_table_captions
92 , Ext_table_captions
92 -- , Ext_image_captions
93 -- , Ext_image_captions
93 , Ext_simple_tables
94 , Ext_simple_tables
src/Text/Pandoc/Readers/Markdown.hs+25−3
@@ -35,7 +35,7 @@ module Text.Pandoc.Readers.Markdown ( readMarkdown ) where
35import Data.List ( transpose, sortBy, findIndex, intercalate )
35import Data.List ( transpose, sortBy, findIndex, intercalate )
36import qualified Data.Map as M
36import qualified Data.Map as M
37import Data.Ord ( comparing )
37import Data.Ord ( comparing )
38−import Data.Char ( isAlphaNum )
38+import Data.Char ( isAlphaNum, toLower )
39import Data.Maybe
39import Data.Maybe
40import Text.Pandoc.Definition
40import Text.Pandoc.Definition
41import qualified Text.Pandoc.Builder as B
41import qualified Text.Pandoc.Builder as B
@@ -175,14 +175,36 @@ dateLine = try $ do
175 trimInlinesF . mconcat <$> manyTill inline newline
175 trimInlinesF . mconcat <$> manyTill inline newline
176
176
177titleBlock :: Parser [Char] ParserState (F Inlines, F [Inlines], F Inlines)
177titleBlock :: Parser [Char] ParserState (F Inlines, F [Inlines], F Inlines)
178−titleBlock = try $ do
178+titleBlock = pandocTitleBlock <|> mmdTitleBlock
179− guardEnabled Ext_pandoc_title_blocks
179+
180+pandocTitleBlock :: Parser [Char] ParserState (F Inlines, F [Inlines], F Inlines)
181+pandocTitleBlock = try $ do
182+ guardEnabled Ext_pandoc_title_block
180 title <- option mempty titleLine
183 title <- option mempty titleLine
181 author <- option (return []) authorsLine
184 author <- option (return []) authorsLine
182 date <- option mempty dateLine
185 date <- option mempty dateLine
183 optional blanklines
186 optional blanklines
184 return (title, author, date)
187 return (title, author, date)
185
188
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+
186parseMarkdown :: Parser [Char] ParserState Pandoc
208parseMarkdown :: Parser [Char] ParserState Pandoc
187parseMarkdown = do
209parseMarkdown = do
188 -- markdown allows raw HTML
210 -- markdown allows raw HTML
src/Text/Pandoc/Writers/Markdown.hs+29−6
@@ -87,14 +87,39 @@ plainify = bottomUp go
87 go (Cite _ cits) = SmallCaps cits
87 go (Cite _ cits) = SmallCaps cits
88 go x = x
88 go x = x
89
89
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+
90-- | Return markdown representation of document.
108-- | Return markdown representation of document.
91pandocToMarkdown :: WriterOptions -> Pandoc -> State WriterState String
109pandocToMarkdown :: WriterOptions -> Pandoc -> State WriterState String
92pandocToMarkdown opts (Pandoc (Meta title authors date) blocks) = do
110pandocToMarkdown opts (Pandoc (Meta title authors date) blocks) = do
93 title' <- inlineListToMarkdown opts title
111 title' <- inlineListToMarkdown opts title
94 authors' <- mapM (inlineListToMarkdown opts) authors
112 authors' <- mapM (inlineListToMarkdown opts) authors
95 date' <- inlineListToMarkdown opts date
113 date' <- inlineListToMarkdown opts date
96− let titleblock = isEnabled Ext_pandoc_title_blocks opts &&
114+ isPlain <- gets stPlain
97− not (null title && null authors && null date)
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
98 let headerBlocks = filter isHeaderBlock blocks
123 let headerBlocks = filter isHeaderBlock blocks
99 let toc = if writerTableOfContents opts
124 let toc = if writerTableOfContents opts
100 then tableOfContents opts headerBlocks
125 then tableOfContents opts headerBlocks
@@ -113,11 +138,9 @@ pandocToMarkdown opts (Pandoc (Meta title authors date) blocks) = do
113 let context = writerVariables opts ++
138 let context = writerVariables opts ++
114 [ ("toc", render colwidth toc)
139 [ ("toc", render colwidth toc)
115 , ("body", main)
140 , ("body", main)
116− , ("title", render colwidth title')
117− , ("date", render colwidth date')
118 ] ++
141 ] ++
119− [ ("titleblock", "yes") | titleblock ] ++
120− [ ("author", render colwidth a) | a <- authors' ]
142+ [ ("titleblock", render colwidth titleblock)
143+ | not (null title && null authors && null date) ]
121 if writerStandalone opts
144 if writerStandalone opts
122 then return $ renderTemplate context $ writerTemplate opts
145 then return $ renderTemplate context $ writerTemplate opts
123 else return main
146 else return main