git.iko.soy/typeable/pandoc
on master help sign in
Moved Pandoc prettyprinting code from Shared to new Native writer.

+ Text.Pandoc.Writers.Native + The function prettyPandoc is now gone. Use writeNative instead.

commit3548bf6d9cdc09c59b36e8d77a1834e1d0619b4a authorJohn MacFarlane <jgm@berkeley.edu> dateSun 4 Jul 2010, 13:55 (16 years ago) parent998fd09
5 files changed+90−58
pandoc.cabal+1 src/Text/Pandoc.hs+2−1 src/Text/Pandoc/Shared.hs−52 src/Text/Pandoc/Writers/Native.hs+86 src/pandoc.hs+1−5
pandoc.cabal+1
@@ -173,6 +173,7 @@ Library
173173 Text.Pandoc.Readers.Markdown,
174174 Text.Pandoc.Readers.RST,
175175 Text.Pandoc.Readers.TeXMath,
176+ Text.Pandoc.Writers.Native,
176177 Text.Pandoc.Writers.Docbook,
177178 Text.Pandoc.Writers.HTML,
178179 Text.Pandoc.Writers.LaTeX,
src/Text/Pandoc.hs+2−1
@@ -71,6 +71,7 @@ module Text.Pandoc
7171 , NoteTable
7272 , HeaderType (..)
7373 -- * Writers: converting /from/ Pandoc format
74+ , writeNative
7475 , writeMarkdown
7576 , writePlain
7677 , writeRST
@@ -88,7 +89,6 @@ module Text.Pandoc
8889 , writeRTF
8990 , writeODT
9091 , writeEPUB
91− , prettyPandoc
9292 -- * Writer options used in writers
9393 , WriterOptions (..)
9494 , HTMLMathMethod (..)
@@ -104,6 +104,7 @@ import Text.Pandoc.Readers.Markdown
104104import Text.Pandoc.Readers.RST
105105import Text.Pandoc.Readers.LaTeX
106106import Text.Pandoc.Readers.HTML
107+import Text.Pandoc.Writers.Native
107108import Text.Pandoc.Writers.Markdown
108109import Text.Pandoc.Writers.RST
109110import Text.Pandoc.Writers.LaTeX
src/Text/Pandoc/Shared.hs−52
@@ -54,7 +54,6 @@ module Text.Pandoc.Shared (
5454 tabFilter,
5555 -- * Prettyprinting
5656 hang',
57− prettyPandoc,
5857 -- * Pandoc block and inline list processing
5958 orderedListMarkers,
6059 normalizeSpaces,
@@ -293,57 +292,6 @@ tabFilter tabStop =
293292hang' :: Doc -> Int -> Doc -> Doc
294293hang' d1 n d2 = d1 $$ (nest n d2)
295294
296−-- | Indent string as a block.
297−indentBy :: Int -- ^ Number of spaces to indent the block
298− -> Int -- ^ Number of spaces (rel to block) to indent first line
299− -> String -- ^ Contents of block to indent
300− -> String
301−indentBy _ _ [] = ""
302−indentBy num first str =
303− let (firstLine:restLines) = lines str
304− firstLineIndent = num + first
305− in (replicate firstLineIndent ' ') ++ firstLine ++ "\n" ++
306− (intercalate "\n" $ map ((replicate num ' ') ++ ) restLines)
307−
308−-- | Prettyprint list of Pandoc blocks elements.
309−prettyBlockList :: Int -- ^ Number of spaces to indent list of blocks
310− -> [Block] -- ^ List of blocks
311− -> String
312−prettyBlockList indent [] = indentBy indent 0 "[]"
313−prettyBlockList indent blocks = indentBy indent (-2) $ "[ " ++
314− (intercalate "\n, " (map prettyBlock blocks)) ++ " ]"
315−
316−-- | Prettyprint Pandoc block element.
317−prettyBlock :: Block -> String
318−prettyBlock (BlockQuote blocks) = "BlockQuote\n " ++
319− (prettyBlockList 2 blocks)
320−prettyBlock (OrderedList attribs blockLists) =
321− "OrderedList " ++ show attribs ++ "\n" ++ indentBy 2 0 ("[ " ++
322− (intercalate ", " $ map (\blocks -> prettyBlockList 2 blocks)
323− blockLists)) ++ " ]"
324−prettyBlock (BulletList blockLists) = "BulletList\n" ++
325− indentBy 2 0 ("[ " ++ (intercalate ", "
326− (map (\blocks -> prettyBlockList 2 blocks) blockLists))) ++ " ]"
327−prettyBlock (DefinitionList items) = "DefinitionList\n" ++
328− indentBy 2 0 ("[ " ++ (intercalate "\n, "
329− (map (\(term, defs) -> "(" ++ show term ++ ",\n" ++
330− indentBy 3 0 ("[ " ++ (intercalate ", "
331− (map (\blocks -> prettyBlockList 2 blocks) defs)) ++ "]") ++
332− ")") items))) ++ " ]"
333−prettyBlock (Table caption aligns widths header rows) =
334− "Table " ++ show caption ++ " " ++ show aligns ++ " " ++
335− show widths ++ "\n" ++ prettyRow header ++ " [\n" ++
336− (intercalate ",\n" (map prettyRow rows)) ++ " ]"
337− where prettyRow cols = indentBy 2 0 ("[ " ++ (intercalate ", "
338− (map (\blocks -> prettyBlockList 2 blocks)
339− cols))) ++ " ]"
340−prettyBlock block = show block
341−
342−-- | Prettyprint Pandoc document.
343−prettyPandoc :: Pandoc -> String
344−prettyPandoc (Pandoc meta blocks) = "Pandoc " ++ "(" ++ show meta ++
345− ")\n" ++ (prettyBlockList 0 blocks) ++ "\n"
346−
347295--
348296-- Pandoc block and inline list processing
349297--
src/Text/Pandoc/Writers/Native.hsnew file+86
@@ -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+
src/pandoc.hs+1−5
@@ -102,7 +102,7 @@ readPandoc _ = read
102102
103103-- | Association list of formats and writers.
104104writers :: [ ( String, WriterOptions -> Pandoc -> String ) ]
105−writers = [("native" , writeDoc)
105+writers = [("native" , writeNative)
106106 ,("html" , writeHtmlString)
107107 ,("html+lhs" , writeHtmlString)
108108 ,("s5" , writeS5String)
@@ -127,10 +127,6 @@ writers = [("native" , writeDoc)
127127isNonTextOutput :: String -> Bool
128128isNonTextOutput = (`elem` ["odt","epub"])
129129
130−-- | Writer for Pandoc native format.
131−writeDoc :: WriterOptions -> Pandoc -> String
132−writeDoc _ = prettyPandoc
133−
134130headerShift :: Int -> Pandoc -> Pandoc
135131headerShift n = processWith shift
136132 where shift :: Block -> Block