module Text.Pandoc.Writers.Native ( writeNative )
where
import Text.Pandoc.Shared ( WriterOptions )
import Data.List ( intercalate )
import Text.Pandoc.Definition
indentBy :: Int
-> Int
-> String
-> String
indentBy _ _ [] = ""
indentBy num first str =
let (firstLine:restLines) = lines str
firstLineIndent = num + first
in (replicate firstLineIndent ' ') ++ firstLine ++ "\n" ++
(intercalate "\n" $ map ((replicate num ' ') ++ ) restLines)
prettyBlockList :: Int
-> [Block]
-> String
prettyBlockList indent [] = indentBy indent 0 "[]"
prettyBlockList indent blocks = indentBy indent (2) $ "[ " ++
(intercalate "\n, " (map prettyBlock blocks)) ++ " ]"
prettyBlock :: Block -> String
prettyBlock (BlockQuote blocks) = "BlockQuote\n " ++
(prettyBlockList 2 blocks)
prettyBlock (OrderedList attribs blockLists) =
"OrderedList " ++ show attribs ++ "\n" ++ indentBy 2 0 ("[ " ++
(intercalate ", " $ map (\blocks -> prettyBlockList 2 blocks)
blockLists)) ++ " ]"
prettyBlock (BulletList blockLists) = "BulletList\n" ++
indentBy 2 0 ("[ " ++ (intercalate ", "
(map (\blocks -> prettyBlockList 2 blocks) blockLists))) ++ " ]"
prettyBlock (DefinitionList items) = "DefinitionList\n" ++
indentBy 2 0 ("[ " ++ (intercalate "\n, "
(map (\(term, defs) -> "(" ++ show term ++ ",\n" ++
indentBy 3 0 ("[ " ++ (intercalate ", "
(map (\blocks -> prettyBlockList 2 blocks) defs)) ++ "]") ++
")") items))) ++ " ]"
prettyBlock (Table caption aligns widths header rows) =
"Table " ++ show caption ++ " " ++ show aligns ++ " " ++
show widths ++ "\n" ++ prettyRow header ++ " [\n" ++
(intercalate ",\n" (map prettyRow rows)) ++ " ]"
where prettyRow cols = indentBy 2 0 ("[ " ++ (intercalate ", "
(map (\blocks -> prettyBlockList 2 blocks)
cols))) ++ " ]"
prettyBlock block = show block
writeNative :: WriterOptions -> Pandoc -> String
writeNative _ (Pandoc meta blocks) = "Pandoc " ++ "(" ++ show meta ++
")\n" ++ (prettyBlockList 0 blocks) ++ "\n"