git.iko.soy/typeable/pandoc
on master help sign in
Moved XML-formatting functions to new unexported module Text.Pandoc.XML.

git-svn-id: https://pandoc.googlecode.com/svn/trunk@1253 788f1e2b-df1e-0410-8736-df70ead52e1b

commitdcb37d32d1992fdff4baa50a62fa75f47dd31bac authorfiddlosopher <fiddlosopher@788f1e2b-df1e-0410-8736-df70ead52e1b> dateWed 19 Mar 2008, 18:46 (19 years ago) parent2baf6d0
4 files changed+91−116
Text/Pandoc/Writers/Docbook.hs+1−60 Text/Pandoc/Writers/OpenDocument.hs+1−56 Text/Pandoc/XML.hs+88 pandoc.cabal+1
Text/Pandoc/Writers/Docbook.hs+1−60
@@ -29,71 +29,12 @@ Conversion of 'Pandoc' documents to Docbook XML.
2929-}
3030module Text.Pandoc.Writers.Docbook ( writeDocbook) where
3131import Text.Pandoc.Definition
32+import Text.Pandoc.XML
3233import Text.Pandoc.Shared
3334import Text.Pandoc.Readers.TeXMath
3435import Data.List ( isPrefixOf, drop )
3536import Text.PrettyPrint.HughesPJ hiding ( Str )
3637
37−--
38−-- code to format XML
39−--
40−
41−-- | Escape one character as needed for XML.
42−escapeCharForXML :: Char -> String
43−escapeCharForXML x = case x of
44− '&' -> "&amp;"
45− '<' -> "&lt;"
46− '>' -> "&gt;"
47− '"' -> "&quot;"
48− '\160' -> "&nbsp;"
49− c -> [c]
50−
51−-- | True if the character needs to be escaped.
52−needsEscaping :: Char -> Bool
53−needsEscaping c = c `elem` "&<>\"\160"
54−
55−-- | Escape string as needed for XML. Entity references are not preserved.
56−escapeStringForXML :: String -> String
57−escapeStringForXML "" = ""
58−escapeStringForXML str =
59− case break needsEscaping str of
60− (okay, "") -> okay
61− (okay, (c:cs)) -> okay ++ escapeCharForXML c ++ escapeStringForXML cs
62−
63−-- | Return a text object with a string of formatted XML attributes.
64−attributeList :: [(String, String)] -> Doc
65−attributeList = text . concatMap
66− (\(a, b) -> " " ++ escapeStringForXML a ++ "=\"" ++
67− escapeStringForXML b ++ "\"")
68−
69−-- | Put the supplied contents between start and end tags of tagType,
70−-- with specified attributes and (if specified) indentation.
71−inTags:: Bool -> String -> [(String, String)] -> Doc -> Doc
72−inTags isIndented tagType attribs contents =
73− let openTag = char '<' <> text tagType <> attributeList attribs <>
74− char '>'
75− closeTag = text "</" <> text tagType <> char '>'
76− in if isIndented
77− then openTag $$ nest 2 contents $$ closeTag
78− else openTag <> contents <> closeTag
79−
80−-- | Return a self-closing tag of tagType with specified attributes
81−selfClosingTag :: String -> [(String, String)] -> Doc
82−selfClosingTag tagType attribs =
83− char '<' <> text tagType <> attributeList attribs <> text " />"
84−
85−-- | Put the supplied contents between start and end tags of tagType.
86−inTagsSimple :: String -> Doc -> Doc
87−inTagsSimple tagType = inTags False tagType []
88−
89−-- | Put the supplied contents in indented block btw start and end tags.
90−inTagsIndented :: String -> Doc -> Doc
91−inTagsIndented tagType = inTags True tagType []
92−
93−--
94−-- Docbook writer
95−--
96−
9738-- | Convert list of authors to a docbook <author> section
9839authorToDocbook :: [Char] -> Doc
9940authorToDocbook name = inTagsIndented "author" $
Text/Pandoc/Writers/OpenDocument.hs+1−56
@@ -32,6 +32,7 @@ Conversion of 'Pandoc' documents to OpenDocument XML.
3232module Text.Pandoc.Writers.OpenDocument ( writeOpenDocument ) where
3333import Text.Pandoc.Definition
3434import Text.Pandoc.Shared
35+import Text.Pandoc.XML
3536import Text.Pandoc.Readers.TeXMath
3637import Text.PrettyPrint.HughesPJ hiding ( Str )
3738
@@ -39,62 +40,6 @@ import Control.Applicative ((<$>))
3940import Control.Monad.State
4041import Data.Char (chr)
4142
42−--
43−-- code to format XML
44−--
45−
46−-- | Escape one character as needed for XML.
47−escapeCharForXML :: Char -> String
48−escapeCharForXML x = case x of
49− '&' -> "&amp;"
50− '<' -> "&lt;"
51− '>' -> "&gt;"
52− '"' -> "&quot;"
53− '\160' -> "&nbsp;"
54− c -> [c]
55−
56−-- | True if the character needs to be escaped.
57−needsEscaping :: Char -> Bool
58−needsEscaping c = c `elem` "&<>\"\160"
59−
60−-- | Escape string as needed for XML. Entity references are not preserved.
61−escapeStringForXML :: String -> String
62−escapeStringForXML "" = ""
63−escapeStringForXML str =
64− case break needsEscaping str of
65− (okay, "") -> okay
66− (okay, (c:cs)) -> okay ++ escapeCharForXML c ++ escapeStringForXML cs
67−
68−-- | Return a text object with a string of formatted XML attributes.
69−attributeList :: [(String, String)] -> Doc
70−attributeList = text . concatMap
71− (\(a, b) -> " " ++ escapeStringForXML a ++ "=\"" ++
72− escapeStringForXML b ++ "\"")
73−
74−-- | Put the supplied contents between start and end tags of tagType,
75−-- with specified attributes and (if specified) indentation.
76−inTags:: Bool -> String -> [(String, String)] -> Doc -> Doc
77−inTags isIndented tagType attribs contents =
78− let openTag = char '<' <> text tagType <> attributeList attribs <>
79− char '>'
80− closeTag = text "</" <> text tagType <> char '>'
81− in if isIndented
82− then openTag $$ nest 2 contents $$ closeTag
83− else openTag <> contents <> closeTag
84−
85−-- | Return a self-closing tag of tagType with specified attributes
86−selfClosingTag :: String -> [(String, String)] -> Doc
87−selfClosingTag tagType attribs =
88− char '<' <> text tagType <> attributeList attribs <> text " />"
89−
90−-- | Put the supplied contents between start and end tags of tagType.
91−inTagsSimple :: String -> Doc -> Doc
92−inTagsSimple tagType = inTags False tagType []
93−
94−-- | Put the supplied contents in indented block btw start and end tags.
95−inTagsIndented :: String -> Doc -> Doc
96−inTagsIndented tagType = inTags True tagType []
97−
9843-- | Auxiliary function to convert Plain block to Para.
9944plainToPara :: Block -> Block
10045plainToPara (Plain x) = Para x
Text/Pandoc/XML.hsnew file+88
@@ -0,0 +1,88 @@
1+{-
2+Copyright (C) 2006-7 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.XML
21+ Copyright : Copyright (C) 2006-7 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+Functions for escaping and formatting XML.
29+-}
30+module Text.Pandoc.XML ( escapeCharForXML,
31+ escapeStringForXML,
32+ inTags,
33+ selfClosingTag,
34+ inTagsSimple,
35+ inTagsIndented ) where
36+import Text.PrettyPrint.HughesPJ
37+
38+-- | Escape one character as needed for XML.
39+escapeCharForXML :: Char -> String
40+escapeCharForXML x = case x of
41+ '&' -> "&amp;"
42+ '<' -> "&lt;"
43+ '>' -> "&gt;"
44+ '"' -> "&quot;"
45+ '\160' -> "&nbsp;"
46+ c -> [c]
47+
48+-- | True if the character needs to be escaped.
49+needsEscaping :: Char -> Bool
50+needsEscaping c = c `elem` "&<>\"\160"
51+
52+-- | Escape string as needed for XML. Entity references are not preserved.
53+escapeStringForXML :: String -> String
54+escapeStringForXML "" = ""
55+escapeStringForXML str =
56+ case break needsEscaping str of
57+ (okay, "") -> okay
58+ (okay, (c:cs)) -> okay ++ escapeCharForXML c ++ escapeStringForXML cs
59+
60+-- | Return a text object with a string of formatted XML attributes.
61+attributeList :: [(String, String)] -> Doc
62+attributeList = text . concatMap
63+ (\(a, b) -> " " ++ escapeStringForXML a ++ "=\"" ++
64+ escapeStringForXML b ++ "\"")
65+
66+-- | Put the supplied contents between start and end tags of tagType,
67+-- with specified attributes and (if specified) indentation.
68+inTags:: Bool -> String -> [(String, String)] -> Doc -> Doc
69+inTags isIndented tagType attribs contents =
70+ let openTag = char '<' <> text tagType <> attributeList attribs <>
71+ char '>'
72+ closeTag = text "</" <> text tagType <> char '>'
73+ in if isIndented
74+ then openTag $$ nest 2 contents $$ closeTag
75+ else openTag <> contents <> closeTag
76+
77+-- | Return a self-closing tag of tagType with specified attributes
78+selfClosingTag :: String -> [(String, String)] -> Doc
79+selfClosingTag tagType attribs =
80+ char '<' <> text tagType <> attributeList attribs <> text " />"
81+
82+-- | Put the supplied contents between start and end tags of tagType.
83+inTagsSimple :: String -> Doc -> Doc
84+inTagsSimple tagType = inTags False tagType []
85+
86+-- | Put the supplied contents in indented block btw start and end tags.
87+inTagsIndented :: String -> Doc -> Doc
88+inTagsIndented tagType = inTags True tagType []
pandoc.cabal+1
@@ -73,6 +73,7 @@ Library
7373 Text.Pandoc.Writers.RST,
7474 Text.Pandoc.Writers.RTF,
7575 Text.Pandoc.Writers.S5
76+ Other-Modules: Text.Pandoc.XML
7677 Ghc-Options: -O2
7778 Ghc-Prof-Options: -auto-all
7879