@@ -118,13 +118,23 @@ yamlTitleBlock v = "---" $$ (jsonToYaml v) $$ "..."
118118
119119jsonToYaml :: Value -> Doc
120120jsonToYaml (Object hashmap) =
121− vcat (map (\(k,v) ->
122− text (T.unpack k) <> ":" <> space <> jsonToYaml v) $ H.toList hashmap)
121+ vcat $ map (\(k,v) ->
122+ case (text (T.unpack k), v, jsonToYaml v) of
123+ (k', Array vec, x)
124+ | V.null vec -> empty
125+ | otherwise -> (k' <> ":") $$ x
126+ (k', Object _, x) -> (k' <> ":") $$ nest 2 x
127+ (_, String "", _) -> empty
128+ (k', _, x) -> k' <> ":" <> space <> x)
129+ $ H.toList hashmap
123130jsonToYaml (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) ++ "'"
131+ vcat $ map (\v -> hang 2 "- " (jsonToYaml v)) $ V.toList vec
132+jsonToYaml (String "") = empty
133+jsonToYaml (String s) =
134+ case T.unpack s of
135+ x | '\n' `elem` x -> hang 2 ("|" <> cr) $ text x
136+ | not (any (`elem` x) "\"'#:[]{},?-") -> text x
137+ | otherwise -> text $ "'" ++ substitute "'" "''" x ++ "'"
128138jsonToYaml (Bool b) = text $ show b
129139jsonToYaml (Number n) = text $ show n
130140jsonToYaml _ = empty
@@ -132,28 +142,31 @@ jsonToYaml _ = empty
132142-- | Return markdown representation of document.
133143pandocToMarkdown :: WriterOptions -> Pandoc -> State WriterState String
134144pandocToMarkdown opts (Pandoc meta blocks) = do
135− title' <- inlineListToMarkdown opts $ docTitle meta
136− authors' <- mapM (inlineListToMarkdown opts) $ docAuthors meta
137− date' <- inlineListToMarkdown opts $ docDate meta
138145 let colwidth = if writerWrapText opts
139146 then Just $ writerColumns opts
140147 else Nothing
141− metadata <- metaToJSON
142− (fmap (render colwidth) . blockListToMarkdown opts)
143− (fmap (render colwidth) . inlineListToMarkdown opts)
144− (writerVariables opts)
145− meta
148+ metadata <- if writerStandalone opts
149+ then metaToJSON
150+ (fmap (render colwidth) . blockListToMarkdown opts)
151+ (fmap (render colwidth) . inlineListToMarkdown opts)
152+ (writerVariables opts)
153+ meta
154+ else return $ Object H.empty
155+ let title' = maybe empty text $ getField "title" metadata
156+ let authors' = maybe [] (map text) $ getField "author" metadata
157+ let date' = maybe empty text $ getField "date" metadata
146158 isPlain <- gets stPlain
147− let titleblock = case True of
148− _ | isPlain ->
149− plainTitleBlock title' authors' date'
150− | isEnabled Ext_yaml_title_block opts ->
151− yamlTitleBlock metadata
152− | isEnabled Ext_pandoc_title_block opts ->
153− pandocTitleBlock title' authors' date'
154− | isEnabled Ext_mmd_title_block opts ->
155− mmdTitleBlock title' authors' date'
156− | otherwise -> empty
159+ let titleblock = case writerStandalone opts of
160+ True | isPlain ->
161+ plainTitleBlock title' authors' date'
162+ | isEnabled Ext_yaml_title_block opts ->
163+ yamlTitleBlock metadata
164+ | isEnabled Ext_pandoc_title_block opts ->
165+ pandocTitleBlock title' authors' date'
166+ | isEnabled Ext_mmd_title_block opts ->
167+ mmdTitleBlock title' authors' date'
168+ | otherwise -> empty
169+ False -> empty
157170 let headerBlocks = filter isHeaderBlock blocks
158171 let toc = if writerTableOfContents opts
159172 then tableOfContents opts headerBlocks