]> git.immae.eu Git - github/fretlink/blazeT.git/blame - src/Text/BlazeT/Internal.hs
Initial
[github/fretlink/blazeT.git] / src / Text / BlazeT / Internal.hs
CommitLineData
675085c2
JG
1{-# LANGUAGE DeriveFunctor #-}
2{-# LANGUAGE GeneralizedNewtypeDeriving #-}
3{-# LANGUAGE FlexibleInstances #-}
4{-# LANGUAGE RankNTypes #-}
5{-# LANGUAGE CPP #-}
6#if MIN_VERSION_blaze_markup(0,7,1)
7#define PRE_BUILDER
8#endif
9module Text.BlazeT.Internal
10 (
11 -- * Important types.
12 B.ChoiceString (..)
13 , B.StaticString (..)
14 , MarkupM
15 , Markup
16 , B.Tag
17 , B.Attribute
18 , B.AttributeValue
19
20 -- * Creating custom tags and attributes.
21 , customParent
22 , customLeaf
23 , B.attribute
24 , B.dataAttribute
25 , B.customAttribute
26
27 -- * Converting values to Markup.
28 , text
29 , preEscapedText
30 , lazyText
31 , preEscapedLazyText
32 , textBuilder
33 , preEscapedTextBuilder
34 , string
35 , preEscapedString
36 , unsafeByteString
37 , unsafeLazyByteString
38
39 -- * Comments
40 , B.textComment
41 , B.lazyTextComment
42 , B.stringComment
43 , B.unsafeByteStringComment
44 , B.unsafeLazyByteStringComment
45
46 -- * Converting values to tags.
47 , B.textTag
48 , B.stringTag
49
50 -- * Converting values to attribute values.
51 , B.textValue
52 , B.preEscapedTextValue
53 , B.lazyTextValue
54 , B.preEscapedLazyTextValue
55 , B.textBuilderValue
56 , B.preEscapedTextBuilderValue
57 , B.stringValue
58 , B.preEscapedStringValue
59 , B.unsafeByteStringValue
60 , B.unsafeLazyByteStringValue
61
62 -- * Setting attributes
63 , B.Attributable
64 , (B.!)
65 , (B.!?)
66
67 -- * Modifying Markup elements
68 , contents
69 , external
70
71 -- * Querying Markup elements
72 , null
73
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
87
88import Control.Monad.Identity
89import Control.Monad.Trans.Class
90import Control.Monad.Writer.Strict
91import qualified Data.ByteString as BS
92import qualified Data.ByteString.Lazy as BL
93import Data.String
94import qualified Data.Text as T
95import qualified Data.Text.Lazy as LT
96import qualified Data.Text.Lazy.Builder as LTB
97import qualified Text.Blaze as B
98import qualified Text.Blaze.Internal as B
99
100newtype MarkupT m a= MarkupT { fromMarkupT :: WriterT B.Markup m a }
101 deriving (Functor
102#if MIN_VERSION_base(4,8,0)
103 ,Applicative
104#endif
105 ,Monad
106 ,MonadWriter B.Markup
107 ,MonadTrans
108 )
109
110-- | Map both the return value and markup of a computation using the
111-- given function
112mapMarkupT :: (m (a,B.Markup) -> n (b,B.Markup)) -> MarkupT m a -> MarkupT n b
113mapMarkupT f = MarkupT . mapWriterT f . fromMarkupT
114{-# INLINE mapMarkupT #-}
115
116type MarkupM = MarkupT Identity
117type Markup = forall m . Monad m => MarkupT m ()
118type Markup2 = forall m . Monad m => MarkupT m () -> MarkupT m ()
119
120runMarkupT :: MarkupT m a -> m (a,B.Markup)
121runMarkupT = runWriterT . fromMarkupT
122{-# INLINE runMarkupT #-}
123
124execMarkupT :: Monad m => MarkupT m a -> m B.Markup
125execMarkupT = liftM snd . runMarkupT
126{-# INLINE execMarkupT #-}
127
128runMarkup :: MarkupM a -> (a,B.Markup)
129runMarkup = runIdentity . runMarkupT
130{-# INLINE runMarkup #-}
131
132execMarkup :: MarkupM a -> B.Markup
133execMarkup = snd . runMarkup
134{-# INLINE execMarkup #-}
135
136-- instance MonadTrans MarkupT where
137
138
139instance (Monad m,Monoid a) => Monoid (MarkupT m a) where
140 mempty = return mempty
141 {-# INLINE mempty #-}
142 a `mappend` b = do {a' <- a; b >>= return . (mappend a')}
143 {-# INLINE mappend #-}
144
145
146instance Monad m => B.Attributable (MarkupT m a) where
147 h ! a = wrapMarkupT2 (B.! a) h
148 {-# INLINE (!) #-}
149
150instance Monad m => B.Attributable (a -> MarkupT m b) where
151 h ! a = \x -> wrapMarkupT2 (B.! a) $ h x
152 {-# INLINE (!) #-}
153
154instance Monad m => IsString (MarkupT m ()) where
155 fromString = wrapMarkup . fromString
156 {-# INLINE fromString #-}
157
158wrapMarkupT :: Monad m => B.Markup -> MarkupT m ()
159wrapMarkupT = tell
160{-# INLINE wrapMarkupT #-}
161
162wrapMarkup :: B.Markup -> Markup
163wrapMarkup = wrapMarkupT
164{-# INLINE wrapMarkup #-}
165
166wrapMarkupT2 :: Monad m => (B.Markup -> B.Markup)
167 -> MarkupT m a -> MarkupT m a
168wrapMarkupT2 = censor
169{-# INLINE wrapMarkupT2 #-}
170
171wrapMarkup2 :: (B.Markup -> B.Markup) -> Markup2
172wrapMarkup2 = wrapMarkupT2
173{-# INLINE wrapMarkup2 #-}
174
175unsafeByteString :: BS.ByteString -> Markup
176unsafeByteString = wrapMarkup . B.unsafeByteString
177{-# INLINE unsafeByteString #-}
178
179-- | Insert a lazy 'BL.ByteString'. See 'unsafeByteString' for reasons why this
180-- is an unsafe operation.
181--
182unsafeLazyByteString :: BL.ByteString -- ^ Value to insert
183 -> Markup -- ^ Resulting HTML fragment
184unsafeLazyByteString = wrapMarkup . B.unsafeLazyByteString
185{-# INLINE unsafeLazyByteString #-}
186
187external :: Monad m => MarkupT m a -> MarkupT m a
188external = wrapMarkupT2 B.external
189{-# INLINE external #-}
190
191contents :: Monad m => MarkupT m a -> MarkupT m a
192contents = wrapMarkupT2 B.contents
193{-# INLINE contents #-}
194
195customParent ::B.Tag -> Markup2
196customParent = wrapMarkup2 . B.customParent
197{-# INLINE customParent #-}
198
199customLeaf :: B.Tag -> Bool -> Markup
200customLeaf = fmap wrapMarkup . B.customLeaf
201{-# INLINE customLeaf #-}
202
203preEscapedText :: T.Text -> Markup
204preEscapedText = wrapMarkup . B.preEscapedText
205{-# INLINE preEscapedText #-}
206
207preEscapedLazyText :: LT.Text -> Markup
208preEscapedLazyText = wrapMarkup . B.preEscapedLazyText
209{-# INLINE preEscapedLazyText #-}
210
211preEscapedTextBuilder :: LTB.Builder -> Markup
212textBuilder :: LTB.Builder -> Markup
213
214#ifdef PRE_BUILDER
215preEscapedTextBuilder = wrapMarkup . B.preEscapedTextBuilder
216textBuilder = wrapMarkup . B.textBuilder
217{-# INLINE preEscapedTextBuilder #-}
218{-# INLINE textBuilder #-}
219#else
220preEscapedTextBuilder = error "This function needs blaze-markup 0.7.1.0"
221textBuilder = error "This function needs blaze-markup 0.7.1.0"
222#endif
223
224preEscapedString :: String -> Markup
225preEscapedString = wrapMarkup . B.preEscapedString
226{-# INLINE preEscapedString #-}
227
228string :: String -> Markup
229string = wrapMarkup . B.string
230{-# INLINE string #-}
231
232text :: T.Text -> Markup
233text = wrapMarkup . B.text
234{-# INLINE text #-}
235
236lazyText :: LT.Text -> Markup
237lazyText = wrapMarkup . B.lazyText
238{-# INLINE lazyText #-}