diff options
author | Johannes Gerer <oss@johannesgerer.com> | 2016-10-27 02:18:13 +0200 |
---|---|---|
committer | Johannes Gerer <oss@johannesgerer.com> | 2016-10-27 02:18:13 +0200 |
commit | 95eb4d6a041305a27dc8fcd42ff1831d9961b7a3 (patch) | |
tree | 3433db574dc8d074354ae70232d29a9cb81ba136 /src/Text/BlazeT/Internal.hs | |
parent | 675085c2e0b0b851378da08b7d73024766107c87 (diff) | |
download | blazeT-95eb4d6a041305a27dc8fcd42ff1831d9961b7a3.tar.gz blazeT-95eb4d6a041305a27dc8fcd42ff1831d9961b7a3.tar.zst blazeT-95eb4d6a041305a27dc8fcd42ff1831d9961b7a3.zip |
Docsv0.0.1
Diffstat (limited to 'src/Text/BlazeT/Internal.hs')
-rw-r--r-- | src/Text/BlazeT/Internal.hs | 234 |
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 |
9 | module Text.BlazeT.Internal | 9 | module 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 | ||
102 | import Control.Arrow | ||
88 | import Control.Monad.Identity | 103 | import Control.Monad.Identity |
89 | import Control.Monad.Trans.Class | 104 | import Control.Monad.Trans.Class |
90 | import Control.Monad.Writer.Strict | 105 | import Control.Monad.Writer.Strict |
@@ -94,47 +109,63 @@ import Data.String | |||
94 | import qualified Data.Text as T | 109 | import qualified Data.Text as T |
95 | import qualified Data.Text.Lazy as LT | 110 | import qualified Data.Text.Lazy as LT |
96 | import qualified Data.Text.Lazy.Builder as LTB | 111 | import qualified Data.Text.Lazy.Builder as LTB |
97 | import qualified Text.Blaze as B | 112 | import qualified Text.Blaze |
98 | import qualified Text.Blaze.Internal as B | 113 | import qualified Text.Blaze.Internal as Text.Blaze |
99 | 114 | ||
100 | newtype 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 | -} | ||
120 | newtype 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 | ||
130 | type 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 |
112 | mapMarkupT :: (m (a,B.Markup) -> n (b,B.Markup)) -> MarkupT m a -> MarkupT n b | 134 | mapMarkupT :: (m (a,Text.Blaze.Markup) -> n (b,Text.Blaze.Markup)) -> MarkupT m a -> MarkupT n b |
113 | mapMarkupT f = MarkupT . mapWriterT f . fromMarkupT | 135 | mapMarkupT f = MarkupT . mapWriterT f . fromMarkupT |
114 | {-# INLINE mapMarkupT #-} | 136 | {-# INLINE mapMarkupT #-} |
115 | 137 | ||
116 | type MarkupM = MarkupT Identity | 138 | type MarkupM a = forall m . Monad m => MarkupT m a |
117 | type Markup = forall m . Monad m => MarkupT m () | 139 | type Markup = MarkupM () |
118 | type Markup2 = forall m . Monad m => MarkupT m () -> MarkupT m () | 140 | type Markup2 = forall m . Monad m => MarkupT m () -> MarkupT m () |
119 | 141 | ||
120 | runMarkupT :: MarkupT m a -> m (a,B.Markup) | 142 | runMarkupT :: MarkupT m a -> m (a,Text.Blaze.Markup) |
121 | runMarkupT = runWriterT . fromMarkupT | 143 | runMarkupT = runWriterT . fromMarkupT |
122 | {-# INLINE runMarkupT #-} | 144 | {-# INLINE runMarkupT #-} |
123 | 145 | ||
124 | execMarkupT :: 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' | ||
149 | runWith :: Monad m => (Markup -> c) -> MarkupT m a -> m (a, c) | ||
150 | runWith renderer = liftM (second $ \x -> renderer $ wrapMarkup x) . runMarkupT | ||
151 | {-# INLINE runWith #-} | ||
152 | |||
153 | execMarkupT :: Monad m => MarkupT m a -> m Text.Blaze.Markup | ||
125 | execMarkupT = liftM snd . runMarkupT | 154 | execMarkupT = liftM snd . runMarkupT |
126 | {-# INLINE execMarkupT #-} | 155 | {-# INLINE execMarkupT #-} |
127 | 156 | ||
128 | runMarkup :: MarkupM a -> (a,B.Markup) | 157 | execWith :: Monad m => (Markup -> c) -> MarkupT m a -> m c |
158 | execWith renderer = liftM snd . runWith renderer | ||
159 | {-# INLINE execWith #-} | ||
160 | |||
161 | runMarkup :: MarkupI a -> (a, Text.Blaze.Markup) | ||
129 | runMarkup = runIdentity . runMarkupT | 162 | runMarkup = runIdentity . runMarkupT |
130 | {-# INLINE runMarkup #-} | 163 | {-# INLINE runMarkup #-} |
131 | 164 | ||
132 | execMarkup :: MarkupM a -> B.Markup | 165 | execMarkup :: MarkupI a -> Text.Blaze.Markup |
133 | execMarkup = snd . runMarkup | 166 | execMarkup = snd . runMarkup |
134 | {-# INLINE execMarkup #-} | 167 | {-# INLINE execMarkup #-} |
135 | 168 | ||
136 | -- instance MonadTrans MarkupT where | ||
137 | |||
138 | 169 | ||
139 | instance (Monad m,Monoid a) => Monoid (MarkupT m a) where | 170 | instance (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 | ||
146 | instance Monad m => B.Attributable (MarkupT m a) where | 177 | instance 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 | ||
150 | instance Monad m => B.Attributable (a -> MarkupT m b) where | 181 | instance 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 | ||
154 | instance Monad m => IsString (MarkupT m ()) where | 185 | instance Monad m => IsString (MarkupT m ()) where |
155 | fromString = wrapMarkup . fromString | 186 | fromString = wrapMarkup . fromString |
156 | {-# INLINE fromString #-} | 187 | {-# INLINE fromString #-} |
157 | 188 | ||
158 | wrapMarkupT :: Monad m => B.Markup -> MarkupT m () | 189 | -- | Wrapper for 'Text.Blaze.Markup' is simply |
190 | -- 'tell' | ||
191 | wrapMarkupT :: Monad m => Text.Blaze.Markup -> MarkupT m () | ||
159 | wrapMarkupT = tell | 192 | wrapMarkupT = tell |
160 | {-# INLINE wrapMarkupT #-} | 193 | {-# INLINE wrapMarkupT #-} |
161 | 194 | ||
162 | wrapMarkup :: B.Markup -> Markup | 195 | wrapMarkup :: Text.Blaze.Markup -> Markup |
163 | wrapMarkup = wrapMarkupT | 196 | wrapMarkup = wrapMarkupT |
164 | {-# INLINE wrapMarkup #-} | 197 | {-# INLINE wrapMarkup #-} |
165 | 198 | ||
166 | wrapMarkupT2 :: Monad m => (B.Markup -> B.Markup) | 199 | |
200 | -- | Wrapper for functions that modify 'Text.Blaze.Markup' is simply | ||
201 | -- 'censor' | ||
202 | wrapMarkupT2 :: Monad m => (Text.Blaze.Markup -> Text.Blaze.Markup) | ||
167 | -> MarkupT m a -> MarkupT m a | 203 | -> MarkupT m a -> MarkupT m a |
168 | wrapMarkupT2 = censor | 204 | wrapMarkupT2 = censor |
169 | {-# INLINE wrapMarkupT2 #-} | 205 | {-# INLINE wrapMarkupT2 #-} |
170 | 206 | ||
171 | wrapMarkup2 :: (B.Markup -> B.Markup) -> Markup2 | 207 | wrapMarkup2 :: (Text.Blaze.Markup -> Text.Blaze.Markup) -> Markup2 |
172 | wrapMarkup2 = wrapMarkupT2 | 208 | wrapMarkup2 = wrapMarkupT2 |
173 | {-# INLINE wrapMarkup2 #-} | 209 | {-# INLINE wrapMarkup2 #-} |
174 | 210 | ||
175 | unsafeByteString :: BS.ByteString -> Markup | 211 | unsafeByteString :: BS.ByteString -> Markup |
176 | unsafeByteString = wrapMarkup . B.unsafeByteString | 212 | unsafeByteString = 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 | -- |
182 | unsafeLazyByteString :: BL.ByteString -- ^ Value to insert | 218 | unsafeLazyByteString :: BL.ByteString -- ^ Value to insert |
183 | -> Markup -- ^ Resulting HTML fragment | 219 | -> Markup -- ^ Resulting HTML fragment |
184 | unsafeLazyByteString = wrapMarkup . B.unsafeLazyByteString | 220 | unsafeLazyByteString = wrapMarkup . Text.Blaze.unsafeLazyByteString |
185 | {-# INLINE unsafeLazyByteString #-} | 221 | {-# INLINE unsafeLazyByteString #-} |
186 | 222 | ||
187 | external :: Monad m => MarkupT m a -> MarkupT m a | 223 | external :: Monad m => MarkupT m a -> MarkupT m a |
188 | external = wrapMarkupT2 B.external | 224 | external = wrapMarkupT2 Text.Blaze.external |
189 | {-# INLINE external #-} | 225 | {-# INLINE external #-} |
190 | 226 | ||
191 | contents :: Monad m => MarkupT m a -> MarkupT m a | 227 | contents :: Monad m => MarkupT m a -> MarkupT m a |
192 | contents = wrapMarkupT2 B.contents | 228 | contents = wrapMarkupT2 Text.Blaze.contents |
193 | {-# INLINE contents #-} | 229 | {-# INLINE contents #-} |
194 | 230 | ||
195 | customParent ::B.Tag -> Markup2 | 231 | customParent ::Text.Blaze.Tag -> Markup2 |
196 | customParent = wrapMarkup2 . B.customParent | 232 | customParent = wrapMarkup2 . Text.Blaze.customParent |
197 | {-# INLINE customParent #-} | 233 | {-# INLINE customParent #-} |
198 | 234 | ||
199 | customLeaf :: B.Tag -> Bool -> Markup | 235 | customLeaf :: Text.Blaze.Tag -> Bool -> Markup |
200 | customLeaf = fmap wrapMarkup . B.customLeaf | 236 | customLeaf = fmap wrapMarkup . Text.Blaze.customLeaf |
201 | {-# INLINE customLeaf #-} | 237 | {-# INLINE customLeaf #-} |
202 | 238 | ||
203 | preEscapedText :: T.Text -> Markup | 239 | preEscapedText :: T.Text -> Markup |
204 | preEscapedText = wrapMarkup . B.preEscapedText | 240 | preEscapedText = wrapMarkup . Text.Blaze.preEscapedText |
205 | {-# INLINE preEscapedText #-} | 241 | {-# INLINE preEscapedText #-} |
206 | 242 | ||
207 | preEscapedLazyText :: LT.Text -> Markup | 243 | preEscapedLazyText :: LT.Text -> Markup |
208 | preEscapedLazyText = wrapMarkup . B.preEscapedLazyText | 244 | preEscapedLazyText = wrapMarkup . Text.Blaze.preEscapedLazyText |
209 | {-# INLINE preEscapedLazyText #-} | 245 | {-# INLINE preEscapedLazyText #-} |
210 | 246 | ||
211 | preEscapedTextBuilder :: LTB.Builder -> Markup | 247 | preEscapedTextBuilder :: LTB.Builder -> Markup |
212 | textBuilder :: LTB.Builder -> Markup | 248 | textBuilder :: LTB.Builder -> Markup |
213 | 249 | ||
214 | #ifdef PRE_BUILDER | 250 | #ifdef PRE_BUILDER |
215 | preEscapedTextBuilder = wrapMarkup . B.preEscapedTextBuilder | 251 | preEscapedTextBuilder = wrapMarkup . Text.Blaze.preEscapedTextBuilder |
216 | textBuilder = wrapMarkup . B.textBuilder | 252 | textBuilder = 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 | ||
224 | preEscapedString :: String -> Markup | 260 | preEscapedString :: String -> Markup |
225 | preEscapedString = wrapMarkup . B.preEscapedString | 261 | preEscapedString = wrapMarkup . Text.Blaze.preEscapedString |
226 | {-# INLINE preEscapedString #-} | 262 | {-# INLINE preEscapedString #-} |
227 | 263 | ||
228 | string :: String -> Markup | 264 | string :: String -> Markup |
229 | string = wrapMarkup . B.string | 265 | string = wrapMarkup . Text.Blaze.string |
230 | {-# INLINE string #-} | 266 | {-# INLINE string #-} |
231 | 267 | ||
232 | text :: T.Text -> Markup | 268 | text :: T.Text -> Markup |
233 | text = wrapMarkup . B.text | 269 | text = wrapMarkup . Text.Blaze.text |
234 | {-# INLINE text #-} | 270 | {-# INLINE text #-} |
235 | 271 | ||
236 | lazyText :: LT.Text -> Markup | 272 | lazyText :: LT.Text -> Markup |
237 | lazyText = wrapMarkup . B.lazyText | 273 | lazyText = 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. | ||