git.iko.soy/typeable/pandoc
on master help sign in
Markdown writer: Support yaml title block.
commit5d01e9a117b77a0f51ce67d495b9e54881a878a5 authorJohn MacFarlane <fiddlosopher@gmail.com> dateSun 30 Jun 2013, 23:37 (13 years ago) parenta1f010d
2 files changed+37−12
src/Text/Pandoc/Writers/Markdown.hs+30−8 tests/writer.markdown+7−4
src/Text/Pandoc/Writers/Markdown.hs+30−8
@@ -46,6 +46,10 @@ import Text.Pandoc.Readers.TeXMath (readTeXMath)
4646import Text.HTML.TagSoup (renderTags, parseTags, isTagText, Tag(..))
4747import Network.URI (isAbsoluteURI)
4848import Data.Default
49+import Data.Yaml (Value(Object,String,Array,Bool,Number))
50+import qualified Data.HashMap.Strict as H
51+import qualified Data.Vector as V
52+import qualified Data.Text as T
4953
5054type Notes = [[Block]]
5155type Refs = [([Inline], Target)]
@@ -109,16 +113,42 @@ plainTitleBlock tit auths dat =
109113 (hcat (intersperse (text "; ") auths)) <> cr <>
110114 dat <> cr
111115
116+yamlTitleBlock :: Value -> Doc
117+yamlTitleBlock v = "---" $$ (jsonToYaml v) $$ "..."
118+
119+jsonToYaml :: Value -> Doc
120+jsonToYaml (Object hashmap) =
121+ vcat (map (\(k,v) ->
122+ text (T.unpack k) <> ":" <> space <> jsonToYaml v) $ H.toList hashmap)
123+jsonToYaml (Array vec) =
124+ cr <> vcat (map (\v -> hang 2 "- " (jsonToYaml v)) $ V.toList vec)
125+jsonToYaml (String s)
126+ | "\n" `T.isInfixOf` s = hang 2 ("|" <> cr) $ text $ T.unpack s
127+ | otherwise = text $ "'" ++ substitute "'" "''" (T.unpack s) ++ "'"
128+jsonToYaml (Bool b) = text $ show b
129+jsonToYaml (Number n) = text $ show n
130+jsonToYaml _ = empty
131+
112132-- | Return markdown representation of document.
113133pandocToMarkdown :: WriterOptions -> Pandoc -> State WriterState String
114134pandocToMarkdown opts (Pandoc meta blocks) = do
115135 title' <- inlineListToMarkdown opts $ docTitle meta
116136 authors' <- mapM (inlineListToMarkdown opts) $ docAuthors meta
117137 date' <- inlineListToMarkdown opts $ docDate meta
138+ let colwidth = if writerWrapText opts
139+ then Just $ writerColumns opts
140+ else Nothing
141+ metadata <- metaToJSON
142+ (fmap (render colwidth) . blockListToMarkdown opts)
143+ (fmap (render colwidth) . inlineListToMarkdown opts)
144+ (writerVariables opts)
145+ meta
118146 isPlain <- gets stPlain
119147 let titleblock = case True of
120148 _ | isPlain ->
121149 plainTitleBlock title' authors' date'
150+ | isEnabled Ext_yaml_title_block opts ->
151+ yamlTitleBlock metadata
122152 | isEnabled Ext_pandoc_title_block opts ->
123153 pandocTitleBlock title' authors' date'
124154 | isEnabled Ext_mmd_title_block opts ->
@@ -128,14 +158,6 @@ pandocToMarkdown opts (Pandoc meta blocks) = do
128158 let toc = if writerTableOfContents opts
129159 then tableOfContents opts headerBlocks
130160 else empty
131− let colwidth = if writerWrapText opts
132− then Just $ writerColumns opts
133− else Nothing
134− metadata <- metaToJSON
135− (fmap (render colwidth) . blockListToMarkdown opts)
136− (fmap (render colwidth) . inlineListToMarkdown opts)
137− (writerVariables opts)
138− meta
139161 body <- blockListToMarkdown opts blocks
140162 st <- get
141163 notes' <- notesToMarkdown opts (reverse $ stNotes st)
tests/writer.markdown+7−4
@@ -1,7 +1,10 @@
1−% Pandoc Test Suite
2−% John MacFarlane
3− Anonymous
4−% July 17, 2006
1+---
2+title: 'Pandoc Test Suite'
3+author:
4+- 'John MacFarlane'
5+- 'Anonymous'
6+date: 'July 17, 2006'
7+...
58
69This is a set of tests for pandoc. Most of them are adapted from John Gruber’s
710markdown test suite.