aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Text/BlazeT/Internal.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Text/BlazeT/Internal.hs')
-rw-r--r--src/Text/BlazeT/Internal.hs234
1 files changed, 140 insertions, 94 deletions
diff --git a/src/Text/BlazeT/Internal.hs b/src/Text/BlazeT/Internal.hs
index 24ef1fe..aa56347 100644
--- a/src/Text/BlazeT/Internal.hs
+++ b/src/Text/BlazeT/Internal.hs
@@ -8,23 +8,49 @@
8#endif 8#endif
9module Text.BlazeT.Internal 9module Text.BlazeT.Internal
10 ( 10 (
11 -- * Important types. 11 -- * Entities exported only by the @blazeT@ version of this module
12 B.ChoiceString (..) 12 MarkupT(..)
13 , B.StaticString (..) 13 ,MarkupI
14 , MarkupM 14 ,mapMarkupT
15 , Markup 15 -- ** Specializations for @blaze-markup@ backwards compatibility
16 , B.Tag 16 ,MarkupM
17 , B.Attribute 17 ,Markup
18 , B.AttributeValue 18 ,Markup2
19 19 -- ** Running
20 -- * Creating custom tags and attributes. 20 ,runMarkupT
21 ,runMarkup
22 ,runWith
23 -- ** Executing
24 ,execMarkupT
25 ,execMarkup
26 ,execWith
27 -- ** Wrappers
28 ,wrapMarkupT
29 ,wrapMarkupT2
30 ,wrapMarkup
31 ,wrapMarkup2
32 ,
33
34 -- * Entities exported also by "Text.Blaze.Internal"
35 -- $descr1
36
37 -- ** Important types.
38 Text.Blaze.ChoiceString (..)
39 , Text.Blaze.StaticString (..)
40 -- , MarkupM
41 -- , Markup
42 , Text.Blaze.Tag
43 , Text.Blaze.Attribute
44 , Text.Blaze.AttributeValue
45
46 -- ** Creating custom tags and attributes.
21 , customParent 47 , customParent
22 , customLeaf 48 , customLeaf
23 , B.attribute 49 , Text.Blaze.attribute
24 , B.dataAttribute 50 , Text.Blaze.dataAttribute
25 , B.customAttribute 51 , Text.Blaze.customAttribute
26 52
27 -- * Converting values to Markup. 53 -- ** Converting values to Markup.
28 , text 54 , text
29 , preEscapedText 55 , preEscapedText
30 , lazyText 56 , lazyText
@@ -36,55 +62,44 @@ module Text.BlazeT.Internal
36 , unsafeByteString 62 , unsafeByteString
37 , unsafeLazyByteString 63 , unsafeLazyByteString
38 64
39 -- * Comments 65 -- ** Comments
40 , B.textComment 66 , Text.Blaze.textComment
41 , B.lazyTextComment 67 , Text.Blaze.lazyTextComment
42 , B.stringComment 68 , Text.Blaze.stringComment
43 , B.unsafeByteStringComment 69 , Text.Blaze.unsafeByteStringComment
44 , B.unsafeLazyByteStringComment 70 , Text.Blaze.unsafeLazyByteStringComment
45 71
46 -- * Converting values to tags. 72 -- ** Converting values to tags.
47 , B.textTag 73 , Text.Blaze.textTag
48 , B.stringTag 74 , Text.Blaze.stringTag
49 75
50 -- * Converting values to attribute values. 76 -- ** Converting values to attribute values.
51 , B.textValue 77 , Text.Blaze.textValue
52 , B.preEscapedTextValue 78 , Text.Blaze.preEscapedTextValue
53 , B.lazyTextValue 79 , Text.Blaze.lazyTextValue
54 , B.preEscapedLazyTextValue 80 , Text.Blaze.preEscapedLazyTextValue
55 , B.textBuilderValue 81 , Text.Blaze.textBuilderValue
56 , B.preEscapedTextBuilderValue 82 , Text.Blaze.preEscapedTextBuilderValue
57 , B.stringValue 83 , Text.Blaze.stringValue
58 , B.preEscapedStringValue 84 , Text.Blaze.preEscapedStringValue
59 , B.unsafeByteStringValue 85 , Text.Blaze.unsafeByteStringValue
60 , B.unsafeLazyByteStringValue 86 , Text.Blaze.unsafeLazyByteStringValue
61 87
62 -- * Setting attributes 88 -- ** Setting attributes
63 , B.Attributable 89 , Text.Blaze.Attributable
64 , (B.!) 90 , (Text.Blaze.!)
65 , (B.!?) 91 , (Text.Blaze.!?)
66 92
67 -- * Modifying Markup elements 93 -- ** Modifying Markup elements
68 , contents 94 , contents
69 , external 95 , external
70 96
71 -- * Querying Markup elements 97 -- ** Querying Markup elements
72 , null 98 , null
73 99
74 -- * BlazeT new stuff
75 ,Markup2
76 ,mapMarkupT
77 ,MarkupT
78 ,runMarkup
79 ,runMarkupT
80 ,execMarkup
81 ,execMarkupT
82 ,wrapMarkup
83 ,wrapMarkupT
84 ,wrapMarkup2
85 ,wrapMarkupT2
86 ) where 100 ) where
87 101
102import Control.Arrow
88import Control.Monad.Identity 103import Control.Monad.Identity
89import Control.Monad.Trans.Class 104import Control.Monad.Trans.Class
90import Control.Monad.Writer.Strict 105import Control.Monad.Writer.Strict
@@ -94,47 +109,63 @@ import Data.String
94import qualified Data.Text as T 109import qualified Data.Text as T
95import qualified Data.Text.Lazy as LT 110import qualified Data.Text.Lazy as LT
96import qualified Data.Text.Lazy.Builder as LTB 111import qualified Data.Text.Lazy.Builder as LTB
97import qualified Text.Blaze as B 112import qualified Text.Blaze
98import qualified Text.Blaze.Internal as B 113import qualified Text.Blaze.Internal as Text.Blaze
99 114
100newtype MarkupT m a= MarkupT { fromMarkupT :: WriterT B.Markup m a } 115{- | Everything is build around the simple @newtype@ definition of the
116'MarkupT' transformer, which makes use the 'Monoid' instance of Blaze
117'Text.Blaze.Markup' and is basically a 'WriterT' writing Blaze
118'Text.Blaze.Markup':
119-}
120newtype MarkupT m a= MarkupT { fromMarkupT :: WriterT Text.Blaze.Markup m a }
101 deriving (Functor 121 deriving (Functor
102#if MIN_VERSION_base(4,8,0) 122#if MIN_VERSION_base(4,8,0)
103 ,Applicative 123 ,Applicative
104#endif 124#endif
105 ,Monad 125 ,Monad
106 ,MonadWriter B.Markup 126 ,MonadWriter Text.Blaze.Markup
107 ,MonadTrans 127 ,MonadTrans
108 ) 128 )
109 129
130type MarkupI a = MarkupT Identity a
131
110-- | Map both the return value and markup of a computation using the 132-- | Map both the return value and markup of a computation using the
111-- given function 133-- given function
112mapMarkupT :: (m (a,B.Markup) -> n (b,B.Markup)) -> MarkupT m a -> MarkupT n b 134mapMarkupT :: (m (a,Text.Blaze.Markup) -> n (b,Text.Blaze.Markup)) -> MarkupT m a -> MarkupT n b
113mapMarkupT f = MarkupT . mapWriterT f . fromMarkupT 135mapMarkupT f = MarkupT . mapWriterT f . fromMarkupT
114{-# INLINE mapMarkupT #-} 136{-# INLINE mapMarkupT #-}
115 137
116type MarkupM = MarkupT Identity 138type MarkupM a = forall m . Monad m => MarkupT m a
117type Markup = forall m . Monad m => MarkupT m () 139type Markup = MarkupM ()
118type Markup2 = forall m . Monad m => MarkupT m () -> MarkupT m () 140type Markup2 = forall m . Monad m => MarkupT m () -> MarkupT m ()
119 141
120runMarkupT :: MarkupT m a -> m (a,B.Markup) 142runMarkupT :: MarkupT m a -> m (a,Text.Blaze.Markup)
121runMarkupT = runWriterT . fromMarkupT 143runMarkupT = runWriterT . fromMarkupT
122{-# INLINE runMarkupT #-} 144{-# INLINE runMarkupT #-}
123 145
124execMarkupT :: Monad m => MarkupT m a -> m B.Markup 146-- | run the MarkupT and return a pair consisting of the result of the
147-- computation and the blaze markup rendered with a blaze renderer
148-- like 'Text.BlazeT.Renderer.Text.renderHtml'
149runWith :: Monad m => (Markup -> c) -> MarkupT m a -> m (a, c)
150runWith renderer = liftM (second $ \x -> renderer $ wrapMarkup x) . runMarkupT
151{-# INLINE runWith #-}
152
153execMarkupT :: Monad m => MarkupT m a -> m Text.Blaze.Markup
125execMarkupT = liftM snd . runMarkupT 154execMarkupT = liftM snd . runMarkupT
126{-# INLINE execMarkupT #-} 155{-# INLINE execMarkupT #-}
127 156
128runMarkup :: MarkupM a -> (a,B.Markup) 157execWith :: Monad m => (Markup -> c) -> MarkupT m a -> m c
158execWith renderer = liftM snd . runWith renderer
159{-# INLINE execWith #-}
160
161runMarkup :: MarkupI a -> (a, Text.Blaze.Markup)
129runMarkup = runIdentity . runMarkupT 162runMarkup = runIdentity . runMarkupT
130{-# INLINE runMarkup #-} 163{-# INLINE runMarkup #-}
131 164
132execMarkup :: MarkupM a -> B.Markup 165execMarkup :: MarkupI a -> Text.Blaze.Markup
133execMarkup = snd . runMarkup 166execMarkup = snd . runMarkup
134{-# INLINE execMarkup #-} 167{-# INLINE execMarkup #-}
135 168
136-- instance MonadTrans MarkupT where
137
138 169
139instance (Monad m,Monoid a) => Monoid (MarkupT m a) where 170instance (Monad m,Monoid a) => Monoid (MarkupT m a) where
140 mempty = return mempty 171 mempty = return mempty
@@ -143,37 +174,42 @@ instance (Monad m,Monoid a) => Monoid (MarkupT m a) where
143 {-# INLINE mappend #-} 174 {-# INLINE mappend #-}
144 175
145 176
146instance Monad m => B.Attributable (MarkupT m a) where 177instance Monad m => Text.Blaze.Attributable (MarkupT m a) where
147 h ! a = wrapMarkupT2 (B.! a) h 178 h ! a = wrapMarkupT2 (Text.Blaze.! a) h
148 {-# INLINE (!) #-} 179 {-# INLINE (!) #-}
149 180
150instance Monad m => B.Attributable (a -> MarkupT m b) where 181instance Monad m => Text.Blaze.Attributable (a -> MarkupT m b) where
151 h ! a = \x -> wrapMarkupT2 (B.! a) $ h x 182 h ! a = \x -> wrapMarkupT2 (Text.Blaze.! a) $ h x
152 {-# INLINE (!) #-} 183 {-# INLINE (!) #-}
153 184
154instance Monad m => IsString (MarkupT m ()) where 185instance Monad m => IsString (MarkupT m ()) where
155 fromString = wrapMarkup . fromString 186 fromString = wrapMarkup . fromString
156 {-# INLINE fromString #-} 187 {-# INLINE fromString #-}
157 188
158wrapMarkupT :: Monad m => B.Markup -> MarkupT m () 189-- | Wrapper for 'Text.Blaze.Markup' is simply
190-- 'tell'
191wrapMarkupT :: Monad m => Text.Blaze.Markup -> MarkupT m ()
159wrapMarkupT = tell 192wrapMarkupT = tell
160{-# INLINE wrapMarkupT #-} 193{-# INLINE wrapMarkupT #-}
161 194
162wrapMarkup :: B.Markup -> Markup 195wrapMarkup :: Text.Blaze.Markup -> Markup
163wrapMarkup = wrapMarkupT 196wrapMarkup = wrapMarkupT
164{-# INLINE wrapMarkup #-} 197{-# INLINE wrapMarkup #-}
165 198
166wrapMarkupT2 :: Monad m => (B.Markup -> B.Markup) 199
200-- | Wrapper for functions that modify 'Text.Blaze.Markup' is simply
201-- 'censor'
202wrapMarkupT2 :: Monad m => (Text.Blaze.Markup -> Text.Blaze.Markup)
167 -> MarkupT m a -> MarkupT m a 203 -> MarkupT m a -> MarkupT m a
168wrapMarkupT2 = censor 204wrapMarkupT2 = censor
169{-# INLINE wrapMarkupT2 #-} 205{-# INLINE wrapMarkupT2 #-}
170 206
171wrapMarkup2 :: (B.Markup -> B.Markup) -> Markup2 207wrapMarkup2 :: (Text.Blaze.Markup -> Text.Blaze.Markup) -> Markup2
172wrapMarkup2 = wrapMarkupT2 208wrapMarkup2 = wrapMarkupT2
173{-# INLINE wrapMarkup2 #-} 209{-# INLINE wrapMarkup2 #-}
174 210
175unsafeByteString :: BS.ByteString -> Markup 211unsafeByteString :: BS.ByteString -> Markup
176unsafeByteString = wrapMarkup . B.unsafeByteString 212unsafeByteString = wrapMarkup . Text.Blaze.unsafeByteString
177{-# INLINE unsafeByteString #-} 213{-# INLINE unsafeByteString #-}
178 214
179-- | Insert a lazy 'BL.ByteString'. See 'unsafeByteString' for reasons why this 215-- | Insert a lazy 'BL.ByteString'. See 'unsafeByteString' for reasons why this
@@ -181,39 +217,39 @@ unsafeByteString = wrapMarkup . B.unsafeByteString
181-- 217--
182unsafeLazyByteString :: BL.ByteString -- ^ Value to insert 218unsafeLazyByteString :: BL.ByteString -- ^ Value to insert
183 -> Markup -- ^ Resulting HTML fragment 219 -> Markup -- ^ Resulting HTML fragment
184unsafeLazyByteString = wrapMarkup . B.unsafeLazyByteString 220unsafeLazyByteString = wrapMarkup . Text.Blaze.unsafeLazyByteString
185{-# INLINE unsafeLazyByteString #-} 221{-# INLINE unsafeLazyByteString #-}
186 222
187external :: Monad m => MarkupT m a -> MarkupT m a 223external :: Monad m => MarkupT m a -> MarkupT m a
188external = wrapMarkupT2 B.external 224external = wrapMarkupT2 Text.Blaze.external
189{-# INLINE external #-} 225{-# INLINE external #-}
190 226
191contents :: Monad m => MarkupT m a -> MarkupT m a 227contents :: Monad m => MarkupT m a -> MarkupT m a
192contents = wrapMarkupT2 B.contents 228contents = wrapMarkupT2 Text.Blaze.contents
193{-# INLINE contents #-} 229{-# INLINE contents #-}
194 230
195customParent ::B.Tag -> Markup2 231customParent ::Text.Blaze.Tag -> Markup2
196customParent = wrapMarkup2 . B.customParent 232customParent = wrapMarkup2 . Text.Blaze.customParent
197{-# INLINE customParent #-} 233{-# INLINE customParent #-}
198 234
199customLeaf :: B.Tag -> Bool -> Markup 235customLeaf :: Text.Blaze.Tag -> Bool -> Markup
200customLeaf = fmap wrapMarkup . B.customLeaf 236customLeaf = fmap wrapMarkup . Text.Blaze.customLeaf
201{-# INLINE customLeaf #-} 237{-# INLINE customLeaf #-}
202 238
203preEscapedText :: T.Text -> Markup 239preEscapedText :: T.Text -> Markup
204preEscapedText = wrapMarkup . B.preEscapedText 240preEscapedText = wrapMarkup . Text.Blaze.preEscapedText
205{-# INLINE preEscapedText #-} 241{-# INLINE preEscapedText #-}
206 242
207preEscapedLazyText :: LT.Text -> Markup 243preEscapedLazyText :: LT.Text -> Markup
208preEscapedLazyText = wrapMarkup . B.preEscapedLazyText 244preEscapedLazyText = wrapMarkup . Text.Blaze.preEscapedLazyText
209{-# INLINE preEscapedLazyText #-} 245{-# INLINE preEscapedLazyText #-}
210 246
211preEscapedTextBuilder :: LTB.Builder -> Markup 247preEscapedTextBuilder :: LTB.Builder -> Markup
212textBuilder :: LTB.Builder -> Markup 248textBuilder :: LTB.Builder -> Markup
213 249
214#ifdef PRE_BUILDER 250#ifdef PRE_BUILDER
215preEscapedTextBuilder = wrapMarkup . B.preEscapedTextBuilder 251preEscapedTextBuilder = wrapMarkup . Text.Blaze.preEscapedTextBuilder
216textBuilder = wrapMarkup . B.textBuilder 252textBuilder = wrapMarkup . Text.Blaze.textBuilder
217{-# INLINE preEscapedTextBuilder #-} 253{-# INLINE preEscapedTextBuilder #-}
218{-# INLINE textBuilder #-} 254{-# INLINE textBuilder #-}
219#else 255#else
@@ -222,17 +258,27 @@ textBuilder = error "This function needs blaze-markup 0.7.1.0"
222#endif 258#endif
223 259
224preEscapedString :: String -> Markup 260preEscapedString :: String -> Markup
225preEscapedString = wrapMarkup . B.preEscapedString 261preEscapedString = wrapMarkup . Text.Blaze.preEscapedString
226{-# INLINE preEscapedString #-} 262{-# INLINE preEscapedString #-}
227 263
228string :: String -> Markup 264string :: String -> Markup
229string = wrapMarkup . B.string 265string = wrapMarkup . Text.Blaze.string
230{-# INLINE string #-} 266{-# INLINE string #-}
231 267
232text :: T.Text -> Markup 268text :: T.Text -> Markup
233text = wrapMarkup . B.text 269text = wrapMarkup . Text.Blaze.text
234{-# INLINE text #-} 270{-# INLINE text #-}
235 271
236lazyText :: LT.Text -> Markup 272lazyText :: LT.Text -> Markup
237lazyText = wrapMarkup . B.lazyText 273lazyText = wrapMarkup . Text.Blaze.lazyText
238{-# INLINE lazyText #-} 274{-# INLINE lazyText #-}
275
276-- $descr1
277-- The following is an adaptation of all "Text.Blaze.Internal" exports to
278-- @blazeT@ types.
279--
280-- Entities that are reexported from "Text.Blaze.Internal" have the original
281-- documentation attached to them.
282--
283-- Entities that had to be adapted are tagged with \"(Adapted)\". For
284-- their documentation consult the "Text.Blaze.Internal" documentation.