Debian Codegraph @

src/Text/XmlHtml/XML/Render.hs

raw
1
2 {-# LANGUAGE CPP #-}
3 {-# LANGUAGE OverloadedStrings #-}
4
5 module Text.XmlHtml.XML.Render where
6
7 import Blaze.ByteString.Builder
8 import Data.Char
9 import Data.Maybe
10 import Text.XmlHtml.Common
11
12 import Data.Text (Text)
13 import qualified Data.Text as T
14
15 #if !MIN_VERSION_base(4,8,0)
16 import Data.Monoid
17 #endif
18
19
20 ------------------------------------------------------------------------------
21 renderWithOptions :: RenderOptions -> Encoding -> Maybe DocType -> [Node] -> Builder
22 renderWithOptions opts e dt ns = byteOrder
23 `mappend` xmlDecl e
24 `mappend` docTypeDecl e dt
25 `mappend` nodes
26 where byteOrder | isUTF16 e = fromText e "\xFEFF" -- byte order mark
27 | otherwise = mempty
28 nodes | null ns = mempty
29 | otherwise = firstNode opts e (head ns)
30 `mappend` (mconcat $ map (node opts e) (tail ns))
31
32
33 render :: Encoding -> Maybe DocType -> [Node] -> Builder
34 render = renderWithOptions defaultRenderOptions
35
36 ------------------------------------------------------------------------------
37 -- | Function for rendering XML nodes without the overhead of creating a
38 -- Document structure.
39 renderXmlFragmentWithOptions :: RenderOptions -> Encoding -> [Node] -> Builder
40 renderXmlFragmentWithOptions _ _ [] = mempty
41 renderXmlFragmentWithOptions opts e (n:ns) =
42 firstNode opts e n `mappend` (mconcat $ map (node opts e) ns)
43
44 renderXmlFragment :: Encoding -> [Node] -> Builder
45 renderXmlFragment = renderXmlFragmentWithOptions defaultRenderOptions
46
47 ------------------------------------------------------------------------------
48 xmlDecl :: Encoding -> Builder
49 xmlDecl e = fromText e "<?xml version=\"1.0\" encoding=\""
50 `mappend` fromText e (encodingName e)
51 `mappend` fromText e "\"?>\n"
52
53
54 ------------------------------------------------------------------------------
55 docTypeDecl :: Encoding -> Maybe DocType -> Builder
56 docTypeDecl _ Nothing = mempty
57 docTypeDecl e (Just (DocType tag ext int)) = fromText e "<!DOCTYPE "
58 `mappend` fromText e tag
59 `mappend` externalID e ext
60 `mappend` internalSubset e int
61 `mappend` fromText e ">\n"
62
63
64 ------------------------------------------------------------------------------
65 externalID :: Encoding -> ExternalID -> Builder
66 externalID _ NoExternalID = mempty
67 externalID e (System sid) = fromText e " SYSTEM "
68 `mappend` sysID e sid
69 externalID e (Public pid sid) = fromText e " PUBLIC "
70 `mappend` pubID e pid
71 `mappend` fromText e " "
72 `mappend` sysID e sid
73
74
75 ------------------------------------------------------------------------------
76 internalSubset :: Encoding -> InternalSubset -> Builder
77 internalSubset _ NoInternalSubset = mempty
78 internalSubset e (InternalText t) = fromText e " " `mappend` fromText e t
79
80
81 ------------------------------------------------------------------------------
82 sysID :: Encoding -> Text -> Builder
83 sysID e sid | not ("\'" `T.isInfixOf` sid) = fromText e "\'"
84 `mappend` fromText e sid
85 `mappend` fromText e "\'"
86 | not ("\"" `T.isInfixOf` sid) = fromText e "\""
87 `mappend` fromText e sid
88 `mappend` fromText e "\""
89 | otherwise = error "SYSTEM id is invalid"
90
91
92 ------------------------------------------------------------------------------
93 pubID :: Encoding -> Text -> Builder
94 pubID e sid | not ("\"" `T.isInfixOf` sid) = fromText e "\""
95 `mappend` fromText e sid
96 `mappend` fromText e "\""
97 | otherwise = error "PUBLIC id is invalid"
98
99
100 ------------------------------------------------------------------------------
101 node :: RenderOptions -> Encoding -> Node -> Builder
102 node _ e (TextNode t) = escaped "<>&" e t
103 node _ e (Comment t) | "--" `T.isInfixOf` t = error "Invalid comment"
104 | "-" `T.isSuffixOf` t = error "Invalid comment"
105 | otherwise = fromText e "<!--"
106 `mappend` fromText e t
107 `mappend` fromText e "-->"
108 node opts e (Element t a c) = element opts e t a c
109
110
111 ------------------------------------------------------------------------------
112 -- | Process the first node differently to encode leading whitespace. This
113 -- lets us be sure that @parseXML@ is a left inverse to @render@.
114 firstNode :: RenderOptions -> Encoding -> Node -> Builder
115 firstNode opts e (Comment t) = node opts e (Comment t)
116 firstNode opts e (Element t a c) = node opts e (Element t a c)
117 firstNode _ _ (TextNode "") = mempty
118 firstNode opts e (TextNode t) = let (c,t') = fromJust $ T.uncons t
119 in escaped "<>& \t\r" e (T.singleton c)
120 `mappend` node opts e (TextNode t')
121
122
123 ------------------------------------------------------------------------------
124 escaped :: [Char] -> Encoding -> Text -> Builder
125 escaped _ _ "" = mempty
126 escaped bad e t = let (p,s) = T.break (`elem` bad) t
127 r = T.uncons s
128 in fromText e p `mappend` case r of
129 Nothing -> mempty
130 Just (c,ss) -> entity e c `mappend` escaped bad e ss
131
132
133 ------------------------------------------------------------------------------
134 entity :: Encoding -> Char -> Builder
135 entity e '&' = fromText e "&amp;"
136 entity e '<' = fromText e "&lt;"
137 entity e '>' = fromText e "&gt;"
138 entity e '\"' = fromText e "&quot;"
139 entity e c = fromText e "&#"
140 `mappend` fromText e (T.pack (show (ord c)))
141 `mappend` fromText e ";"
142
143
144 ------------------------------------------------------------------------------
145 element :: RenderOptions -> Encoding -> Text -> [(Text, Text)] -> [Node] -> Builder
146 element opts e t a [] = fromText e "<"
147 `mappend` fromText e t
148 `mappend` (mconcat $ map (attribute opts e) a)
149 `mappend` fromText e "/>"
150 element opts e t a c = fromText e "<"
151 `mappend` fromText e t
152 `mappend` (mconcat $ map (attribute opts e) a)
153 `mappend` fromText e ">"
154 `mappend` (mconcat $ map (node opts e) c)
155 `mappend` fromText e "</"
156 `mappend` fromText e t
157 `mappend` fromText e ">"
158
159
160 ------------------------------------------------------------------------------
161 attribute :: RenderOptions -> Encoding -> (Text, Text) -> Builder
162 attribute opts e (n,v)
163 | roAttributeResolveInternal opts == AttrResolveAvoidEscape
164 && surround `T.isInfixOf` v
165 && not (alternative `T.isInfixOf` v) =
166 fromText e " "
167 `mappend` fromText e n
168 `mappend` fromText e (T.cons '=' alternative)
169 `mappend` escaped "<&" e v
170 `mappend` fromText e alternative
171 | otherwise =
172 fromText e " "
173 `mappend` fromText e n
174 `mappend` fromText e (T.cons '=' surround)
175 `mappend` bmap (T.replace surround ent) (escaped "<&" e v)
176 `mappend` fromText e surround
177 where
178 (surround, alternative, ent) = case roAttributeSurround opts of
179 SurroundSingleQuote -> ("'" , "\"", "&apos;")
180 SurroundDoubleQuote -> ("\"", "'" , "&quot;")