mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-04-09 04:59:31 -06:00
XML module: added CData support. Escape stuff that needs to be escaped in XML.
This commit is contained in:
@@ -12,8 +12,8 @@ module GF.Data.XML (XML(..), Attr, comments, showsXMLDoc, showsXML) where
|
||||
|
||||
import GF.Data.Utilities
|
||||
|
||||
data XML = Data String | Tag String [Attr] [XML] | Comment String
|
||||
deriving (Eq,Show)
|
||||
data XML = Data String | CData String | Tag String [Attr] [XML] | Comment String
|
||||
deriving (Ord,Eq,Show)
|
||||
|
||||
type Attr = (String,String)
|
||||
|
||||
@@ -26,6 +26,7 @@ showsXMLDoc xml = showString header . showsXML xml
|
||||
|
||||
showsXML :: XML -> ShowS
|
||||
showsXML (Data s) = showString s
|
||||
showsXML (CData s) = showString "<![CDATA[" . showString s .showString "]]>"
|
||||
showsXML (Tag t as []) = showChar '<' . showString t . showsAttrs as . showString "/>"
|
||||
showsXML (Tag t as cs) =
|
||||
showChar '<' . showString t . showsAttrs as . showChar '>'
|
||||
@@ -38,9 +39,11 @@ showsAttrs = concatS . map (showChar ' ' .) . map showsAttr
|
||||
showsAttr :: Attr -> ShowS
|
||||
showsAttr (n,v) = showString n . showString "=\"" . showString (escape v) . showString "\""
|
||||
|
||||
-- FIXME: escape strange charachters with &#xxx;
|
||||
escape :: String -> String
|
||||
escape = concatMap escChar
|
||||
where
|
||||
escChar c | c `elem` ['"','\\'] = '\\':[c]
|
||||
| otherwise = [c]
|
||||
escChar '<' = "<"
|
||||
escChar '>' = ">"
|
||||
escChar '&' = "&"
|
||||
escChar '"' = """
|
||||
escChar c = [c]
|
||||
|
||||
Reference in New Issue
Block a user