git.iko.soy/typeable/pandoc
on master help sign in
Moved Pandoc prettyprinting code from Shared to new Native writer.
3548bf6John MacFarlane
pandoc.cabal+1
@@ -173,6 +173,7 @@ Library
173 Text.Pandoc.Readers.Markdown,
173 Text.Pandoc.Readers.Markdown,
174 Text.Pandoc.Readers.RST,
174 Text.Pandoc.Readers.RST,
175 Text.Pandoc.Readers.TeXMath,
175 Text.Pandoc.Readers.TeXMath,
176+ Text.Pandoc.Writers.Native,
176 Text.Pandoc.Writers.Docbook,
177 Text.Pandoc.Writers.Docbook,
177 Text.Pandoc.Writers.HTML,
178 Text.Pandoc.Writers.HTML,
178 Text.Pandoc.Writers.LaTeX,
179 Text.Pandoc.Writers.LaTeX,
src/Text/Pandoc.hs+2−1
@@ -71,6 +71,7 @@ module Text.Pandoc
71 , NoteTable
71 , NoteTable
72 , HeaderType (..)
72 , HeaderType (..)
73 -- * Writers: converting /from/ Pandoc format
73 -- * Writers: converting /from/ Pandoc format
74+ , writeNative
74 , writeMarkdown
75 , writeMarkdown
75 , writePlain
76 , writePlain
76 , writeRST
77 , writeRST
@@ -88,7 +89,6 @@ module Text.Pandoc
88 , writeRTF
89 , writeRTF
89 , writeODT
90 , writeODT
90 , writeEPUB
91 , writeEPUB
91− , prettyPandoc
92 -- * Writer options used in writers
92 -- * Writer options used in writers
93 , WriterOptions (..)
93 , WriterOptions (..)
94 , HTMLMathMethod (..)
94 , HTMLMathMethod (..)
@@ -104,6 +104,7 @@ import Text.Pandoc.Readers.Markdown
104import Text.Pandoc.Readers.RST
104import Text.Pandoc.Readers.RST
105import Text.Pandoc.Readers.LaTeX
105import Text.Pandoc.Readers.LaTeX
106import Text.Pandoc.Readers.HTML
106import Text.Pandoc.Readers.HTML
107+import Text.Pandoc.Writers.Native
107import Text.Pandoc.Writers.Markdown
108import Text.Pandoc.Writers.Markdown
108import Text.Pandoc.Writers.RST
109import Text.Pandoc.Writers.RST
109import Text.Pandoc.Writers.LaTeX
110import Text.Pandoc.Writers.LaTeX
src/Text/Pandoc/Shared.hs−52
@@ -54,7 +54,6 @@ module Text.Pandoc.Shared (
54 tabFilter,
54 tabFilter,
55 -- * Prettyprinting
55 -- * Prettyprinting
56 hang',
56 hang',
57− prettyPandoc,
58 -- * Pandoc block and inline list processing
57 -- * Pandoc block and inline list processing
59 orderedListMarkers,
58 orderedListMarkers,
60 normalizeSpaces,
59 normalizeSpaces,
@@ -293,57 +292,6 @@ tabFilter tabStop =
293hang' :: Doc -> Int -> Doc -> Doc
292hang' :: Doc -> Int -> Doc -> Doc
294hang' d1 n d2 = d1 $$ (nest n d2)
293hang' d1 n d2 = d1 $$ (nest n d2)
295
294
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−
347--
295--
348-- Pandoc block and inline list processing
296-- Pandoc block and inline list processing
349--
297--
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
102
102
103-- | Association list of formats and writers.
103-- | Association list of formats and writers.
104writers :: [ ( String, WriterOptions -> Pandoc -> String ) ]
104writers :: [ ( String, WriterOptions -> Pandoc -> String ) ]
105−writers = [("native" , writeDoc)
105+writers = [("native" , writeNative)
106 ,("html" , writeHtmlString)
106 ,("html" , writeHtmlString)
107 ,("html+lhs" , writeHtmlString)
107 ,("html+lhs" , writeHtmlString)
108 ,("s5" , writeS5String)
108 ,("s5" , writeS5String)
@@ -127,10 +127,6 @@ writers = [("native" , writeDoc)
127isNonTextOutput :: String -> Bool
127isNonTextOutput :: String -> Bool
128isNonTextOutput = (`elem` ["odt","epub"])
128isNonTextOutput = (`elem` ["odt","epub"])
129
129
130−-- | Writer for Pandoc native format.
131−writeDoc :: WriterOptions -> Pandoc -> String
132−writeDoc _ = prettyPandoc
133−
134headerShift :: Int -> Pandoc -> Pandoc
130headerShift :: Int -> Pandoc -> Pandoc
135headerShift n = processWith shift
131headerShift n = processWith shift
136 where shift :: Block -> Block
132 where shift :: Block -> Block