@@ -0,0 +1,86 @@
1+{-
2+Copyright (C) 2006-2010 John MacFarlane <jgm@berkeley.edu>
3+
4+This program is free software; you can redistribute it and/or modify
5+it under the terms of the GNU General Public License as published by
6+the Free Software Foundation; either version 2 of the License, or
7+(at your option) any later version.
8+
9+This program is distributed in the hope that it will be useful,
10+but WITHOUT ANY WARRANTY; without even the implied warranty of
11+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+GNU General Public License for more details.
13+
14+You should have received a copy of the GNU General Public License
15+along with this program; if not, write to the Free Software
16+Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17+-}
18+
19+{- |
20+ Module : Text.Pandoc.Writers.Native
21+ Copyright : Copyright (C) 2006-2010 John MacFarlane
22+ License : GNU GPL, version 2 or above
23+
24+ Maintainer : John MacFarlane <jgm@berkeley.edu>
25+ Stability : alpha
26+ Portability : portable
27+
28+Utility functions and definitions used by the various Pandoc modules.
29+-}
30+module Text.Pandoc.Writers.Native ( writeNative )
31+where
32+import Text.Pandoc.Shared ( WriterOptions )
33+import Data.List ( intercalate )
34+import Text.Pandoc.Definition
35+
36+-- | Indent string as a block.
37+indentBy :: Int -- ^ Number of spaces to indent the block
38+ -> Int -- ^ Number of spaces (rel to block) to indent first line
39+ -> String -- ^ Contents of block to indent
40+ -> String
41+indentBy _ _ [] = ""
42+indentBy num first str =
43+ let (firstLine:restLines) = lines str
44+ firstLineIndent = num + first
45+ in (replicate firstLineIndent ' ') ++ firstLine ++ "\n" ++
46+ (intercalate "\n" $ map ((replicate num ' ') ++ ) restLines)
47+
48+-- | Prettyprint list of Pandoc blocks elements.
49+prettyBlockList :: Int -- ^ Number of spaces to indent list of blocks
50+ -> [Block] -- ^ List of blocks
51+ -> String
52+prettyBlockList indent [] = indentBy indent 0 "[]"
53+prettyBlockList indent blocks = indentBy indent (-2) $ "[ " ++
54+ (intercalate "\n, " (map prettyBlock blocks)) ++ " ]"
55+
56+-- | Prettyprint Pandoc block element.
57+prettyBlock :: Block -> String
58+prettyBlock (BlockQuote blocks) = "BlockQuote\n " ++
59+ (prettyBlockList 2 blocks)
60+prettyBlock (OrderedList attribs blockLists) =
61+ "OrderedList " ++ show attribs ++ "\n" ++ indentBy 2 0 ("[ " ++
62+ (intercalate ", " $ map (\blocks -> prettyBlockList 2 blocks)
63+ blockLists)) ++ " ]"
64+prettyBlock (BulletList blockLists) = "BulletList\n" ++
65+ indentBy 2 0 ("[ " ++ (intercalate ", "
66+ (map (\blocks -> prettyBlockList 2 blocks) blockLists))) ++ " ]"
67+prettyBlock (DefinitionList items) = "DefinitionList\n" ++
68+ indentBy 2 0 ("[ " ++ (intercalate "\n, "
69+ (map (\(term, defs) -> "(" ++ show term ++ ",\n" ++
70+ indentBy 3 0 ("[ " ++ (intercalate ", "
71+ (map (\blocks -> prettyBlockList 2 blocks) defs)) ++ "]") ++
72+ ")") items))) ++ " ]"
73+prettyBlock (Table caption aligns widths header rows) =
74+ "Table " ++ show caption ++ " " ++ show aligns ++ " " ++
75+ show widths ++ "\n" ++ prettyRow header ++ " [\n" ++
76+ (intercalate ",\n" (map prettyRow rows)) ++ " ]"
77+ where prettyRow cols = indentBy 2 0 ("[ " ++ (intercalate ", "
78+ (map (\blocks -> prettyBlockList 2 blocks)
79+ cols))) ++ " ]"
80+prettyBlock block = show block
81+
82+-- | Prettyprint Pandoc document.
83+writeNative :: WriterOptions -> Pandoc -> String
84+writeNative _ (Pandoc meta blocks) = "Pandoc " ++ "(" ++ show meta ++
85+ ")\n" ++ (prettyBlockList 0 blocks) ++ "\n"
86+