{-# LANGUAGE CPP #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE UndecidableInstances #-}
module Lucid.Base
(
renderText
,renderBS
,renderTextT
,renderBST
,renderToFile
,execHtmlT
,evalHtmlT
,runHtmlT
,relaxHtmlT
,commuteHtmlT
,makeElement
,makeElementNoEnd
,makeXmlElementNoEnd
,makeAttribute
,Html
,HtmlT(HtmlT)
,Attribute(..)
,Term(..)
,TermRaw(..)
,ToHtml(..)
,With(..))
where
import Blaze.ByteString.Builder (Builder)
import qualified Blaze.ByteString.Builder as Blaze
import qualified Blaze.ByteString.Builder.Html.Utf8 as Blaze
import Control.Applicative
import Control.Monad
import Control.Monad.Morph
import Control.Monad.Reader
import Control.Monad.Error.Class (MonadError(..))
import Control.Monad.State.Class (MonadState(..))
import Control.Monad.Writer.Class (MonadWriter(..))
import Data.ByteString.Lazy (ByteString)
import qualified Data.ByteString.Lazy as L
import qualified Data.ByteString as S
import Data.Functor.Identity
import Data.HashMap.Strict (HashMap)
import qualified Data.HashMap.Strict as M
import Data.Hashable (Hashable(..))
import Data.Semigroup (Semigroup (..))
import Data.Monoid (Monoid (..))
import Data.String
import Data.Text (Text)
import qualified Data.Text.Lazy as LT
import qualified Data.Text.Lazy.Encoding as LT
import qualified Data.Text.Encoding as T
import Data.Typeable (Typeable)
import Prelude
data Attribute = Attribute !Text !Text
deriving (Int -> Attribute -> ShowS
[Attribute] -> ShowS
Attribute -> String
(Int -> Attribute -> ShowS)
-> (Attribute -> String)
-> ([Attribute] -> ShowS)
-> Show Attribute
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Attribute] -> ShowS
$cshowList :: [Attribute] -> ShowS
show :: Attribute -> String
$cshow :: Attribute -> String
showsPrec :: Int -> Attribute -> ShowS
$cshowsPrec :: Int -> Attribute -> ShowS
Show,Attribute -> Attribute -> Bool
(Attribute -> Attribute -> Bool)
-> (Attribute -> Attribute -> Bool) -> Eq Attribute
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Attribute -> Attribute -> Bool
$c/= :: Attribute -> Attribute -> Bool
== :: Attribute -> Attribute -> Bool
$c== :: Attribute -> Attribute -> Bool
Eq,Typeable)
instance Hashable Attribute where
hashWithSalt :: Int -> Attribute -> Int
hashWithSalt Int
salt (Attribute Text
a Text
b) = Int
salt Int -> Text -> Int
forall a. Hashable a => Int -> a -> Int
`hashWithSalt` Text
a Int -> Text -> Int
forall a. Hashable a => Int -> a -> Int
`hashWithSalt` Text
b
type Html = HtmlT Identity
newtype HtmlT m a =
HtmlT {HtmlT m a -> m (HashMap Text Text -> Builder, a)
runHtmlT :: m (HashMap Text Text -> Builder,a)
}
#if __GLASGOW_HASKELL__ >= 707
deriving (Typeable)
#endif
instance MFunctor HtmlT where
hoist :: (forall a. m a -> n a) -> HtmlT m b -> HtmlT n b
hoist forall a. m a -> n a
f (HtmlT m (HashMap Text Text -> Builder, b)
xs) = n (HashMap Text Text -> Builder, b) -> HtmlT n b
forall (m :: * -> *) a.
m (HashMap Text Text -> Builder, a) -> HtmlT m a
HtmlT (m (HashMap Text Text -> Builder, b)
-> n (HashMap Text Text -> Builder, b)
forall a. m a -> n a
f m (HashMap Text Text -> Builder, b)
xs)
instance (a ~ (),Applicative m) => Semigroup (HtmlT m a) where
<> :: HtmlT m a -> HtmlT m a -> HtmlT m a
(<>) = (a -> a -> a) -> HtmlT m a -> HtmlT m a -> HtmlT m a
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 a -> a -> a
forall a. Semigroup a => a -> a -> a
(<>)
instance (a ~ (),Applicative m) => Monoid (HtmlT m a) where
mempty :: HtmlT m a
mempty = a -> HtmlT m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
forall a. Monoid a => a
mempty
mappend :: HtmlT m a -> HtmlT m a -> HtmlT m a
mappend = (a -> a -> a) -> HtmlT m a -> HtmlT m a -> HtmlT m a
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 a -> a -> a
forall a. Monoid a => a -> a -> a
mappend
instance Applicative m => Applicative (HtmlT m) where
pure :: a -> HtmlT m a
pure a
a = m (HashMap Text Text -> Builder, a) -> HtmlT m a
forall (m :: * -> *) a.
m (HashMap Text Text -> Builder, a) -> HtmlT m a
HtmlT ((HashMap Text Text -> Builder, a)
-> m (HashMap Text Text -> Builder, a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (HashMap Text Text -> Builder
forall a. Monoid a => a
mempty,a
a))
{-# INLINE pure #-}
HtmlT m (a -> b)
f <*> :: HtmlT m (a -> b) -> HtmlT m a -> HtmlT m b
<*> HtmlT m a
x = m (HashMap Text Text -> Builder, b) -> HtmlT m b
forall (m :: * -> *) a.
m (HashMap Text Text -> Builder, a) -> HtmlT m a
HtmlT (m (HashMap Text Text -> Builder, b) -> HtmlT m b)
-> m (HashMap Text Text -> Builder, b) -> HtmlT m b
forall a b. (a -> b) -> a -> b
$ (HashMap Text Text -> Builder, a -> b)
-> (HashMap Text Text -> Builder, a)
-> (HashMap Text Text -> Builder, b)
forall a t b. Semigroup a => (a, t -> b) -> (a, t) -> (a, b)
mk ((HashMap Text Text -> Builder, a -> b)
-> (HashMap Text Text -> Builder, a)
-> (HashMap Text Text -> Builder, b))
-> m (HashMap Text Text -> Builder, a -> b)
-> m ((HashMap Text Text -> Builder, a)
-> (HashMap Text Text -> Builder, b))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> HtmlT m (a -> b) -> m (HashMap Text Text -> Builder, a -> b)
forall (m :: * -> *) a.
HtmlT m a -> m (HashMap Text Text -> Builder, a)
runHtmlT HtmlT m (a -> b)
f m ((HashMap Text Text -> Builder, a)
-> (HashMap Text Text -> Builder, b))
-> m (HashMap Text Text -> Builder, a)
-> m (HashMap Text Text -> Builder, b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> HtmlT m a -> m (HashMap Text Text -> Builder, a)
forall (m :: * -> *) a.
HtmlT m a -> m (HashMap Text Text -> Builder, a)
runHtmlT HtmlT m a
x
where mk :: (a, t -> b) -> (a, t) -> (a, b)
mk ~(a
g, t -> b
f') ~(a
h, t
x') = (a
g a -> a -> a
forall a. Semigroup a => a -> a -> a
<> a
h, t -> b
f' t
x')
{-# INLINE (<*>) #-}
HtmlT m a
m *> :: HtmlT m a -> HtmlT m b -> HtmlT m b
*> HtmlT m b
n = m (HashMap Text Text -> Builder, b) -> HtmlT m b
forall (m :: * -> *) a.
m (HashMap Text Text -> Builder, a) -> HtmlT m a
HtmlT (m (HashMap Text Text -> Builder, b) -> HtmlT m b)
-> m (HashMap Text Text -> Builder, b) -> HtmlT m b
forall a b. (a -> b) -> a -> b
$ (HashMap Text Text -> Builder, a)
-> (HashMap Text Text -> Builder, b)
-> (HashMap Text Text -> Builder, b)
forall a b b. Semigroup a => (a, b) -> (a, b) -> (a, b)
mk ((HashMap Text Text -> Builder, a)
-> (HashMap Text Text -> Builder, b)
-> (HashMap Text Text -> Builder, b))
-> m (HashMap Text Text -> Builder, a)
-> m ((HashMap Text Text -> Builder, b)
-> (HashMap Text Text -> Builder, b))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> HtmlT m a -> m (HashMap Text Text -> Builder, a)
forall (m :: * -> *) a.
HtmlT m a -> m (HashMap Text Text -> Builder, a)
runHtmlT HtmlT m a
m m ((HashMap Text Text -> Builder, b)
-> (HashMap Text Text -> Builder, b))
-> m (HashMap Text Text -> Builder, b)
-> m (HashMap Text Text -> Builder, b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> HtmlT m b -> m (HashMap Text Text -> Builder, b)
forall (m :: * -> *) a.
HtmlT m a -> m (HashMap Text Text -> Builder, a)
runHtmlT HtmlT m b
n
where mk :: (a, b) -> (a, b) -> (a, b)
mk ~(a
g, b
_) ~(a
h, b
b) = (a
g a -> a -> a
forall a. Semigroup a => a -> a -> a
<> a
h, b
b)
{-# INLINE (*>) #-}
HtmlT m a
m <* :: HtmlT m a -> HtmlT m b -> HtmlT m a
<* HtmlT m b
n = m (HashMap Text Text -> Builder, a) -> HtmlT m a
forall (m :: * -> *) a.
m (HashMap Text Text -> Builder, a) -> HtmlT m a
HtmlT (m (HashMap Text Text -> Builder, a) -> HtmlT m a)
-> m (HashMap Text Text -> Builder, a) -> HtmlT m a
forall a b. (a -> b) -> a -> b
$ (HashMap Text Text -> Builder, a)
-> (HashMap Text Text -> Builder, b)
-> (HashMap Text Text -> Builder, a)
forall a b b. Semigroup a => (a, b) -> (a, b) -> (a, b)
mk ((HashMap Text Text -> Builder, a)
-> (HashMap Text Text -> Builder, b)
-> (HashMap Text Text -> Builder, a))
-> m (HashMap Text Text -> Builder, a)
-> m ((HashMap Text Text -> Builder, b)
-> (HashMap Text Text -> Builder, a))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> HtmlT m a -> m (HashMap Text Text -> Builder, a)
forall (m :: * -> *) a.
HtmlT m a -> m (HashMap Text Text -> Builder, a)
runHtmlT HtmlT m a
m m ((HashMap Text Text -> Builder, b)
-> (HashMap Text Text -> Builder, a))
-> m (HashMap Text Text -> Builder, b)
-> m (HashMap Text Text -> Builder, a)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> HtmlT m b -> m (HashMap Text Text -> Builder, b)
forall (m :: * -> *) a.
HtmlT m a -> m (HashMap Text Text -> Builder, a)
runHtmlT HtmlT m b
n
where mk :: (a, b) -> (a, b) -> (a, b)
mk ~(a
g, b
a) ~(a
h, b
_) = (a
g a -> a -> a
forall a. Semigroup a => a -> a -> a
<> a
h, b
a)
{-# INLINE (<*) #-}
instance Functor m => Functor (HtmlT m) where
fmap :: (a -> b) -> HtmlT m a -> HtmlT m b
fmap a -> b
f = m (HashMap Text Text -> Builder, b) -> HtmlT m b
forall (m :: * -> *) a.
m (HashMap Text Text -> Builder, a) -> HtmlT m a
HtmlT (m (HashMap Text Text -> Builder, b) -> HtmlT m b)
-> (HtmlT m a -> m (HashMap Text Text -> Builder, b))
-> HtmlT m a
-> HtmlT m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((HashMap Text Text -> Builder, a)
-> (HashMap Text Text -> Builder, b))
-> m (HashMap Text Text -> Builder, a)
-> m (HashMap Text Text -> Builder, b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((a -> b)
-> (HashMap Text Text -> Builder, a)
-> (HashMap Text Text -> Builder, b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f) (m (HashMap Text Text -> Builder, a)
-> m (HashMap Text Text -> Builder, b))
-> (HtmlT m a -> m (HashMap Text Text -> Builder, a))
-> HtmlT m a
-> m (HashMap Text Text -> Builder, b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HtmlT m a -> m (HashMap Text Text -> Builder, a)
forall (m :: * -> *) a.
HtmlT m a -> m (HashMap Text Text -> Builder, a)
runHtmlT
<$ :: a -> HtmlT m b -> HtmlT m a
(<$) = (b -> a) -> HtmlT m b -> HtmlT m a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((b -> a) -> HtmlT m b -> HtmlT m a)
-> (a -> b -> a) -> a -> HtmlT m b -> HtmlT m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b -> a
forall a b. a -> b -> a
const
{-# INLINE (<$) #-}
instance Monad m => Monad (HtmlT m) where
return :: a -> HtmlT m a
return a
a = m (HashMap Text Text -> Builder, a) -> HtmlT m a
forall (m :: * -> *) a.
m (HashMap Text Text -> Builder, a) -> HtmlT m a
HtmlT ((HashMap Text Text -> Builder, a)
-> m (HashMap Text Text -> Builder, a)
forall (m :: * -> *) a. Monad m => a -> m a
return (HashMap Text Text -> Builder
forall a. Monoid a => a
mempty,a
a))
{-# INLINE return #-}
HtmlT m a
m >>= :: HtmlT m a -> (a -> HtmlT m b) -> HtmlT m b
>>= a -> HtmlT m b
f = m (HashMap Text Text -> Builder, b) -> HtmlT m b
forall (m :: * -> *) a.
m (HashMap Text Text -> Builder, a) -> HtmlT m a
HtmlT (m (HashMap Text Text -> Builder, b) -> HtmlT m b)
-> m (HashMap Text Text -> Builder, b) -> HtmlT m b
forall a b. (a -> b) -> a -> b
$ do
~(HashMap Text Text -> Builder
g,a
a) <- HtmlT m a -> m (HashMap Text Text -> Builder, a)
forall (m :: * -> *) a.
HtmlT m a -> m (HashMap Text Text -> Builder, a)
runHtmlT HtmlT m a
m
~(HashMap Text Text -> Builder
h,b
b) <- HtmlT m b -> m (HashMap Text Text -> Builder, b)
forall (m :: * -> *) a.
HtmlT m a -> m (HashMap Text Text -> Builder, a)
runHtmlT (a -> HtmlT m b
f a
a)
(HashMap Text Text -> Builder, b)
-> m (HashMap Text Text -> Builder, b)
forall (m :: * -> *) a. Monad m => a -> m a
return (HashMap Text Text -> Builder
g (HashMap Text Text -> Builder)
-> (HashMap Text Text -> Builder) -> HashMap Text Text -> Builder
forall a. Semigroup a => a -> a -> a
<> HashMap Text Text -> Builder
h,b
b)
{-# INLINE (>>=) #-}
HtmlT m a
m >> :: HtmlT m a -> HtmlT m b -> HtmlT m b
>> HtmlT m b
n = m (HashMap Text Text -> Builder, b) -> HtmlT m b
forall (m :: * -> *) a.
m (HashMap Text Text -> Builder, a) -> HtmlT m a
HtmlT (m (HashMap Text Text -> Builder, b) -> HtmlT m b)
-> m (HashMap Text Text -> Builder, b) -> HtmlT m b
forall a b. (a -> b) -> a -> b
$ do
~(HashMap Text Text -> Builder
g, a
_) <- HtmlT m a -> m (HashMap Text Text -> Builder, a)
forall (m :: * -> *) a.
HtmlT m a -> m (HashMap Text Text -> Builder, a)
runHtmlT HtmlT m a
m
~(HashMap Text Text -> Builder
h, b
b) <- HtmlT m b -> m (HashMap Text Text -> Builder, b)
forall (m :: * -> *) a.
HtmlT m a -> m (HashMap Text Text -> Builder, a)
runHtmlT HtmlT m b
n
(HashMap Text Text -> Builder, b)
-> m (HashMap Text Text -> Builder, b)
forall (m :: * -> *) a. Monad m => a -> m a
return (HashMap Text Text -> Builder
g (HashMap Text Text -> Builder)
-> (HashMap Text Text -> Builder) -> HashMap Text Text -> Builder
forall a. Semigroup a => a -> a -> a
<> HashMap Text Text -> Builder
h, b
b)
{-# INLINE (>>) #-}
instance MonadTrans HtmlT where
lift :: m a -> HtmlT m a
lift m a
m =
m (HashMap Text Text -> Builder, a) -> HtmlT m a
forall (m :: * -> *) a.
m (HashMap Text Text -> Builder, a) -> HtmlT m a
HtmlT (do a
a <- m a
m
(HashMap Text Text -> Builder, a)
-> m (HashMap Text Text -> Builder, a)
forall (m :: * -> *) a. Monad m => a -> m a
return (\HashMap Text Text
_ -> Builder
forall a. Monoid a => a
mempty,a
a))
instance MonadFix m => MonadFix (HtmlT m) where
mfix :: (a -> HtmlT m a) -> HtmlT m a
mfix a -> HtmlT m a
m = m (HashMap Text Text -> Builder, a) -> HtmlT m a
forall (m :: * -> *) a.
m (HashMap Text Text -> Builder, a) -> HtmlT m a
HtmlT (m (HashMap Text Text -> Builder, a) -> HtmlT m a)
-> m (HashMap Text Text -> Builder, a) -> HtmlT m a
forall a b. (a -> b) -> a -> b
$ ((HashMap Text Text -> Builder, a)
-> m (HashMap Text Text -> Builder, a))
-> m (HashMap Text Text -> Builder, a)
forall (m :: * -> *) a. MonadFix m => (a -> m a) -> m a
mfix (((HashMap Text Text -> Builder, a)
-> m (HashMap Text Text -> Builder, a))
-> m (HashMap Text Text -> Builder, a))
-> ((HashMap Text Text -> Builder, a)
-> m (HashMap Text Text -> Builder, a))
-> m (HashMap Text Text -> Builder, a)
forall a b. (a -> b) -> a -> b
$ \ ~(HashMap Text Text -> Builder
_, a
a) -> HtmlT m a -> m (HashMap Text Text -> Builder, a)
forall (m :: * -> *) a.
HtmlT m a -> m (HashMap Text Text -> Builder, a)
runHtmlT (HtmlT m a -> m (HashMap Text Text -> Builder, a))
-> HtmlT m a -> m (HashMap Text Text -> Builder, a)
forall a b. (a -> b) -> a -> b
$ a -> HtmlT m a
m a
a
instance MonadReader r m => MonadReader r (HtmlT m) where
ask :: HtmlT m r
ask = m r -> HtmlT m r
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m r
forall r (m :: * -> *). MonadReader r m => m r
ask
local :: (r -> r) -> HtmlT m a -> HtmlT m a
local r -> r
f (HtmlT m (HashMap Text Text -> Builder, a)
a) = m (HashMap Text Text -> Builder, a) -> HtmlT m a
forall (m :: * -> *) a.
m (HashMap Text Text -> Builder, a) -> HtmlT m a
HtmlT ((r -> r)
-> m (HashMap Text Text -> Builder, a)
-> m (HashMap Text Text -> Builder, a)
forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local r -> r
f m (HashMap Text Text -> Builder, a)
a)
instance MonadState s m => MonadState s (HtmlT m) where
get :: HtmlT m s
get = m s -> HtmlT m s
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m s
forall s (m :: * -> *). MonadState s m => m s
get
put :: s -> HtmlT m ()
put = m () -> HtmlT m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m () -> HtmlT m ()) -> (s -> m ()) -> s -> HtmlT m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> m ()
forall s (m :: * -> *). MonadState s m => s -> m ()
put
state :: (s -> (a, s)) -> HtmlT m a
state = m a -> HtmlT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> HtmlT m a)
-> ((s -> (a, s)) -> m a) -> (s -> (a, s)) -> HtmlT m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (s -> (a, s)) -> m a
forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state
instance MonadError e m => MonadError e (HtmlT m) where
throwError :: e -> HtmlT m a
throwError = m a -> HtmlT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> HtmlT m a) -> (e -> m a) -> e -> HtmlT m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. e -> m a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError
catchError :: HtmlT m a -> (e -> HtmlT m a) -> HtmlT m a
catchError (HtmlT m (HashMap Text Text -> Builder, a)
m) e -> HtmlT m a
h = m (HashMap Text Text -> Builder, a) -> HtmlT m a
forall (m :: * -> *) a.
m (HashMap Text Text -> Builder, a) -> HtmlT m a
HtmlT (m (HashMap Text Text -> Builder, a) -> HtmlT m a)
-> m (HashMap Text Text -> Builder, a) -> HtmlT m a
forall a b. (a -> b) -> a -> b
$ m (HashMap Text Text -> Builder, a)
-> (e -> m (HashMap Text Text -> Builder, a))
-> m (HashMap Text Text -> Builder, a)
forall e (m :: * -> *) a.
MonadError e m =>
m a -> (e -> m a) -> m a
catchError m (HashMap Text Text -> Builder, a)
m (HtmlT m a -> m (HashMap Text Text -> Builder, a)
forall (m :: * -> *) a.
HtmlT m a -> m (HashMap Text Text -> Builder, a)
runHtmlT (HtmlT m a -> m (HashMap Text Text -> Builder, a))
-> (e -> HtmlT m a) -> e -> m (HashMap Text Text -> Builder, a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. e -> HtmlT m a
h)
instance MonadWriter w m => MonadWriter w (HtmlT m) where
tell :: w -> HtmlT m ()
tell = m () -> HtmlT m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m () -> HtmlT m ()) -> (w -> m ()) -> w -> HtmlT m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. w -> m ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell
listen :: HtmlT m a -> HtmlT m (a, w)
listen (HtmlT m (HashMap Text Text -> Builder, a)
x) = m (HashMap Text Text -> Builder, (a, w)) -> HtmlT m (a, w)
forall (m :: * -> *) a.
m (HashMap Text Text -> Builder, a) -> HtmlT m a
HtmlT (m (HashMap Text Text -> Builder, (a, w)) -> HtmlT m (a, w))
-> m (HashMap Text Text -> Builder, (a, w)) -> HtmlT m (a, w)
forall a b. (a -> b) -> a -> b
$ (((HashMap Text Text -> Builder, a), w)
-> (HashMap Text Text -> Builder, (a, w)))
-> m ((HashMap Text Text -> Builder, a), w)
-> m (HashMap Text Text -> Builder, (a, w))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((HashMap Text Text -> Builder, a), w)
-> (HashMap Text Text -> Builder, (a, w))
forall a a b. ((a, a), b) -> (a, (a, b))
reassoc (m ((HashMap Text Text -> Builder, a), w)
-> m (HashMap Text Text -> Builder, (a, w)))
-> m ((HashMap Text Text -> Builder, a), w)
-> m (HashMap Text Text -> Builder, (a, w))
forall a b. (a -> b) -> a -> b
$ m (HashMap Text Text -> Builder, a)
-> m ((HashMap Text Text -> Builder, a), w)
forall w (m :: * -> *) a. MonadWriter w m => m a -> m (a, w)
listen m (HashMap Text Text -> Builder, a)
x
where reassoc :: ((a, a), b) -> (a, (a, b))
reassoc ((a
a, a
b), b
c) = (a
a, (a
b, b
c))
pass :: HtmlT m (a, w -> w) -> HtmlT m a
pass (HtmlT m (HashMap Text Text -> Builder, (a, w -> w))
p) = m (HashMap Text Text -> Builder, a) -> HtmlT m a
forall (m :: * -> *) a.
m (HashMap Text Text -> Builder, a) -> HtmlT m a
HtmlT (m (HashMap Text Text -> Builder, a) -> HtmlT m a)
-> m (HashMap Text Text -> Builder, a) -> HtmlT m a
forall a b. (a -> b) -> a -> b
$ m ((HashMap Text Text -> Builder, a), w -> w)
-> m (HashMap Text Text -> Builder, a)
forall w (m :: * -> *) a. MonadWriter w m => m (a, w -> w) -> m a
pass (m ((HashMap Text Text -> Builder, a), w -> w)
-> m (HashMap Text Text -> Builder, a))
-> m ((HashMap Text Text -> Builder, a), w -> w)
-> m (HashMap Text Text -> Builder, a)
forall a b. (a -> b) -> a -> b
$ ((HashMap Text Text -> Builder, (a, w -> w))
-> ((HashMap Text Text -> Builder, a), w -> w))
-> m (HashMap Text Text -> Builder, (a, w -> w))
-> m ((HashMap Text Text -> Builder, a), w -> w)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (HashMap Text Text -> Builder, (a, w -> w))
-> ((HashMap Text Text -> Builder, a), w -> w)
forall a b b. (a, (b, b)) -> ((a, b), b)
assoc m (HashMap Text Text -> Builder, (a, w -> w))
p
where assoc :: (a, (b, b)) -> ((a, b), b)
assoc (a
a, (b
b, b
c)) = ((a
a, b
b), b
c)
instance MonadIO m => MonadIO (HtmlT m) where
liftIO :: IO a -> HtmlT m a
liftIO = m a -> HtmlT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> HtmlT m a) -> (IO a -> m a) -> IO a -> HtmlT m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO
instance (Monad m,a ~ ()) => IsString (HtmlT m a) where
fromString :: String -> HtmlT m a
fromString = String -> HtmlT m a
forall a (m :: * -> *). (ToHtml a, Monad m) => a -> HtmlT m ()
toHtml
instance (m ~ Identity) => Show (HtmlT m a) where
show :: HtmlT m a -> String
show = Text -> String
LT.unpack (Text -> String) -> (Html a -> Text) -> Html a -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Html a -> Text
forall a. Html a -> Text
renderText
class ToHtml a where
toHtml :: Monad m => a -> HtmlT m ()
toHtmlRaw :: Monad m => a -> HtmlT m ()
instance (a ~ (), m ~ Identity) => ToHtml (HtmlT m a) where
toHtml :: HtmlT m a -> HtmlT m ()
toHtml = HtmlT m a -> HtmlT m ()
forall (m :: * -> *) a. Monad m => HtmlT Identity a -> HtmlT m a
relaxHtmlT
toHtmlRaw :: HtmlT m a -> HtmlT m ()
toHtmlRaw = HtmlT m a -> HtmlT m ()
forall (m :: * -> *) a. Monad m => HtmlT Identity a -> HtmlT m a
relaxHtmlT
instance ToHtml String where
toHtml :: String -> HtmlT m ()
toHtml = Builder -> HtmlT m ()
forall (m :: * -> *). Monad m => Builder -> HtmlT m ()
build (Builder -> HtmlT m ())
-> (String -> Builder) -> String -> HtmlT m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Builder
Blaze.fromHtmlEscapedString
toHtmlRaw :: String -> HtmlT m ()
toHtmlRaw = Builder -> HtmlT m ()
forall (m :: * -> *). Monad m => Builder -> HtmlT m ()
build (Builder -> HtmlT m ())
-> (String -> Builder) -> String -> HtmlT m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Builder
Blaze.fromString
instance ToHtml Text where
toHtml :: Text -> HtmlT m ()
toHtml = Builder -> HtmlT m ()
forall (m :: * -> *). Monad m => Builder -> HtmlT m ()
build (Builder -> HtmlT m ()) -> (Text -> Builder) -> Text -> HtmlT m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Builder
Blaze.fromHtmlEscapedText
toHtmlRaw :: Text -> HtmlT m ()
toHtmlRaw = Builder -> HtmlT m ()
forall (m :: * -> *). Monad m => Builder -> HtmlT m ()
build (Builder -> HtmlT m ()) -> (Text -> Builder) -> Text -> HtmlT m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Builder
Blaze.fromText
instance ToHtml LT.Text where
toHtml :: Text -> HtmlT m ()
toHtml = Builder -> HtmlT m ()
forall (m :: * -> *). Monad m => Builder -> HtmlT m ()
build (Builder -> HtmlT m ()) -> (Text -> Builder) -> Text -> HtmlT m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Builder
Blaze.fromHtmlEscapedLazyText
toHtmlRaw :: Text -> HtmlT m ()
toHtmlRaw = Builder -> HtmlT m ()
forall (m :: * -> *). Monad m => Builder -> HtmlT m ()
build (Builder -> HtmlT m ()) -> (Text -> Builder) -> Text -> HtmlT m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Builder
Blaze.fromLazyText
instance ToHtml S.ByteString where
toHtml :: ByteString -> HtmlT m ()
toHtml = Builder -> HtmlT m ()
forall (m :: * -> *). Monad m => Builder -> HtmlT m ()
build (Builder -> HtmlT m ())
-> (ByteString -> Builder) -> ByteString -> HtmlT m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Builder
Blaze.fromHtmlEscapedText (Text -> Builder) -> (ByteString -> Text) -> ByteString -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Text
T.decodeUtf8
toHtmlRaw :: ByteString -> HtmlT m ()
toHtmlRaw = Builder -> HtmlT m ()
forall (m :: * -> *). Monad m => Builder -> HtmlT m ()
build (Builder -> HtmlT m ())
-> (ByteString -> Builder) -> ByteString -> HtmlT m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Builder
Blaze.fromByteString
instance ToHtml L.ByteString where
toHtml :: ByteString -> HtmlT m ()
toHtml = Builder -> HtmlT m ()
forall (m :: * -> *). Monad m => Builder -> HtmlT m ()
build (Builder -> HtmlT m ())
-> (ByteString -> Builder) -> ByteString -> HtmlT m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Builder
Blaze.fromHtmlEscapedLazyText (Text -> Builder) -> (ByteString -> Text) -> ByteString -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Text
LT.decodeUtf8
toHtmlRaw :: ByteString -> HtmlT m ()
toHtmlRaw = Builder -> HtmlT m ()
forall (m :: * -> *). Monad m => Builder -> HtmlT m ()
build (Builder -> HtmlT m ())
-> (ByteString -> Builder) -> ByteString -> HtmlT m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Builder
Blaze.fromLazyByteString
build :: Monad m => Builder -> HtmlT m ()
build :: Builder -> HtmlT m ()
build Builder
b = m (HashMap Text Text -> Builder, ()) -> HtmlT m ()
forall (m :: * -> *) a.
m (HashMap Text Text -> Builder, a) -> HtmlT m a
HtmlT ((HashMap Text Text -> Builder, ())
-> m (HashMap Text Text -> Builder, ())
forall (m :: * -> *) a. Monad m => a -> m a
return (Builder -> HashMap Text Text -> Builder
forall a b. a -> b -> a
const Builder
b,()))
{-# INLINE build #-}
class Term arg result | result -> arg where
term :: Text
-> arg
-> result
term = (Text -> [Attribute] -> arg -> result)
-> [Attribute] -> Text -> arg -> result
forall a b c. (a -> b -> c) -> b -> a -> c
flip Text -> [Attribute] -> arg -> result
forall arg result.
Term arg result =>
Text -> [Attribute] -> arg -> result
termWith []
{-# INLINE term #-}
termWith :: Text
-> [Attribute]
-> arg
-> result
instance (Applicative m,f ~ HtmlT m a) => Term [Attribute] (f -> HtmlT m a) where
termWith :: Text -> [Attribute] -> [Attribute] -> f -> HtmlT m a
termWith Text
name [Attribute]
f = (HtmlT m a -> HtmlT m a) -> [Attribute] -> HtmlT m a -> HtmlT m a
forall a. With a => a -> [Attribute] -> a
with (Text -> HtmlT m a -> HtmlT m a
forall (m :: * -> *) a. Functor m => Text -> HtmlT m a -> HtmlT m a
makeElement Text
name) ([Attribute] -> HtmlT m a -> HtmlT m a)
-> ([Attribute] -> [Attribute])
-> [Attribute]
-> HtmlT m a
-> HtmlT m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([Attribute] -> [Attribute] -> [Attribute]
forall a. Semigroup a => a -> a -> a
<> [Attribute]
f)
instance (Applicative m) => Term (HtmlT m a) (HtmlT m a) where
termWith :: Text -> [Attribute] -> HtmlT m a -> HtmlT m a
termWith Text
name [Attribute]
f = (HtmlT m a -> HtmlT m a) -> [Attribute] -> HtmlT m a -> HtmlT m a
forall a. With a => a -> [Attribute] -> a
with (Text -> HtmlT m a -> HtmlT m a
forall (m :: * -> *) a. Functor m => Text -> HtmlT m a -> HtmlT m a
makeElement Text
name) [Attribute]
f
{-# INLINE termWith #-}
instance Term Text Attribute where
termWith :: Text -> [Attribute] -> Text -> Attribute
termWith Text
key [Attribute]
_ Text
value = Text -> Text -> Attribute
makeAttribute Text
key Text
value
class TermRaw arg result | result -> arg where
termRaw :: Text
-> arg
-> result
termRaw = (Text -> [Attribute] -> arg -> result)
-> [Attribute] -> Text -> arg -> result
forall a b c. (a -> b -> c) -> b -> a -> c
flip Text -> [Attribute] -> arg -> result
forall arg result.
TermRaw arg result =>
Text -> [Attribute] -> arg -> result
termRawWith []
termRawWith :: Text
-> [Attribute]
-> arg
-> result
instance (Monad m,ToHtml f, a ~ ()) => TermRaw [Attribute] (f -> HtmlT m a) where
termRawWith :: Text -> [Attribute] -> [Attribute] -> f -> HtmlT m a
termRawWith Text
name [Attribute]
f [Attribute]
attrs = (HtmlT m () -> HtmlT m ())
-> [Attribute] -> HtmlT m () -> HtmlT m ()
forall a. With a => a -> [Attribute] -> a
with (Text -> HtmlT m () -> HtmlT m ()
forall (m :: * -> *) a. Functor m => Text -> HtmlT m a -> HtmlT m a
makeElement Text
name) ([Attribute]
attrs [Attribute] -> [Attribute] -> [Attribute]
forall a. Semigroup a => a -> a -> a
<> [Attribute]
f) (HtmlT m () -> HtmlT m ()) -> (f -> HtmlT m ()) -> f -> HtmlT m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f -> HtmlT m ()
forall a (m :: * -> *). (ToHtml a, Monad m) => a -> HtmlT m ()
toHtmlRaw
instance (Monad m,a ~ ()) => TermRaw Text (HtmlT m a) where
termRawWith :: Text -> [Attribute] -> Text -> HtmlT m a
termRawWith Text
name [Attribute]
f = (HtmlT m () -> HtmlT m ())
-> [Attribute] -> HtmlT m () -> HtmlT m ()
forall a. With a => a -> [Attribute] -> a
with (Text -> HtmlT m () -> HtmlT m ()
forall (m :: * -> *) a. Functor m => Text -> HtmlT m a -> HtmlT m a
makeElement Text
name) [Attribute]
f (HtmlT m () -> HtmlT m ())
-> (Text -> HtmlT m ()) -> Text -> HtmlT m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> HtmlT m ()
forall a (m :: * -> *). (ToHtml a, Monad m) => a -> HtmlT m ()
toHtmlRaw
instance TermRaw Text Attribute where
termRawWith :: Text -> [Attribute] -> Text -> Attribute
termRawWith Text
key [Attribute]
_ Text
value = Text -> Text -> Attribute
makeAttribute Text
key Text
value
class With a where
with :: a
-> [Attribute]
-> a
instance (Functor m) => With (HtmlT m a) where
with :: HtmlT m a -> [Attribute] -> HtmlT m a
with HtmlT m a
f = \[Attribute]
attr -> m (HashMap Text Text -> Builder, a) -> HtmlT m a
forall (m :: * -> *) a.
m (HashMap Text Text -> Builder, a) -> HtmlT m a
HtmlT ([Attribute]
-> (HashMap Text Text -> Builder, a)
-> (HashMap Text Text -> Builder, a)
forall t b.
[Attribute]
-> (HashMap Text Text -> t, b) -> (HashMap Text Text -> t, b)
mk [Attribute]
attr ((HashMap Text Text -> Builder, a)
-> (HashMap Text Text -> Builder, a))
-> m (HashMap Text Text -> Builder, a)
-> m (HashMap Text Text -> Builder, a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> HtmlT m a -> m (HashMap Text Text -> Builder, a)
forall (m :: * -> *) a.
HtmlT m a -> m (HashMap Text Text -> Builder, a)
runHtmlT HtmlT m a
f)
where
mk :: [Attribute]
-> (HashMap Text Text -> t, b) -> (HashMap Text Text -> t, b)
mk [Attribute]
attr ~(HashMap Text Text -> t
f',b
a) = (\HashMap Text Text
attr' -> HashMap Text Text -> t
f' (HashMap Text Text -> HashMap Text Text -> HashMap Text Text
unionArgs ((Text -> Text -> Text) -> [(Text, Text)] -> HashMap Text Text
forall k v.
(Eq k, Hashable k) =>
(v -> v -> v) -> [(k, v)] -> HashMap k v
M.fromListWith Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
(<>) ((Attribute -> (Text, Text)) -> [Attribute] -> [(Text, Text)]
forall a b. (a -> b) -> [a] -> [b]
map Attribute -> (Text, Text)
toPair [Attribute]
attr)) HashMap Text Text
attr')
,b
a)
toPair :: Attribute -> (Text, Text)
toPair (Attribute Text
x Text
y) = (Text
x,Text
y)
instance (Functor m) => With (HtmlT m a -> HtmlT m a) where
with :: (HtmlT m a -> HtmlT m a) -> [Attribute] -> HtmlT m a -> HtmlT m a
with HtmlT m a -> HtmlT m a
f = \[Attribute]
attr HtmlT m a
inner -> m (HashMap Text Text -> Builder, a) -> HtmlT m a
forall (m :: * -> *) a.
m (HashMap Text Text -> Builder, a) -> HtmlT m a
HtmlT ([Attribute]
-> (HashMap Text Text -> Builder, a)
-> (HashMap Text Text -> Builder, a)
forall t b.
[Attribute]
-> (HashMap Text Text -> t, b) -> (HashMap Text Text -> t, b)
mk [Attribute]
attr ((HashMap Text Text -> Builder, a)
-> (HashMap Text Text -> Builder, a))
-> m (HashMap Text Text -> Builder, a)
-> m (HashMap Text Text -> Builder, a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> HtmlT m a -> m (HashMap Text Text -> Builder, a)
forall (m :: * -> *) a.
HtmlT m a -> m (HashMap Text Text -> Builder, a)
runHtmlT (HtmlT m a -> HtmlT m a
f HtmlT m a
inner))
where
mk :: [Attribute]
-> (HashMap Text Text -> t, b) -> (HashMap Text Text -> t, b)
mk [Attribute]
attr ~(HashMap Text Text -> t
f',b
a) = (\HashMap Text Text
attr' -> HashMap Text Text -> t
f' (HashMap Text Text -> HashMap Text Text -> HashMap Text Text
unionArgs ((Text -> Text -> Text) -> [(Text, Text)] -> HashMap Text Text
forall k v.
(Eq k, Hashable k) =>
(v -> v -> v) -> [(k, v)] -> HashMap k v
M.fromListWith Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
(<>) ((Attribute -> (Text, Text)) -> [Attribute] -> [(Text, Text)]
forall a b. (a -> b) -> [a] -> [b]
map Attribute -> (Text, Text)
toPair [Attribute]
attr)) HashMap Text Text
attr')
,b
a)
toPair :: Attribute -> (Text, Text)
toPair (Attribute Text
x Text
y) = (Text
x,Text
y)
unionArgs :: HashMap Text Text -> HashMap Text Text -> HashMap Text Text
unionArgs :: HashMap Text Text -> HashMap Text Text -> HashMap Text Text
unionArgs = (Text -> Text -> Text)
-> HashMap Text Text -> HashMap Text Text -> HashMap Text Text
forall k v.
(Eq k, Hashable k) =>
(v -> v -> v) -> HashMap k v -> HashMap k v -> HashMap k v
M.unionWith Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
(<>)
renderToFile :: FilePath -> Html a -> IO ()
renderToFile :: String -> Html a -> IO ()
renderToFile String
fp = String -> ByteString -> IO ()
L.writeFile String
fp (ByteString -> IO ()) -> (Html a -> ByteString) -> Html a -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> ByteString
Blaze.toLazyByteString (Builder -> ByteString)
-> (Html a -> Builder) -> Html a -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Identity Builder -> Builder
forall a. Identity a -> a
runIdentity (Identity Builder -> Builder)
-> (Html a -> Identity Builder) -> Html a -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Html a -> Identity Builder
forall (m :: * -> *) a. Monad m => HtmlT m a -> m Builder
execHtmlT
renderBS :: Html a -> ByteString
renderBS :: Html a -> ByteString
renderBS = Builder -> ByteString
Blaze.toLazyByteString (Builder -> ByteString)
-> (Html a -> Builder) -> Html a -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Identity Builder -> Builder
forall a. Identity a -> a
runIdentity (Identity Builder -> Builder)
-> (Html a -> Identity Builder) -> Html a -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Html a -> Identity Builder
forall (m :: * -> *) a. Monad m => HtmlT m a -> m Builder
execHtmlT
renderText :: Html a -> LT.Text
renderText :: Html a -> Text
renderText = ByteString -> Text
LT.decodeUtf8 (ByteString -> Text) -> (Html a -> ByteString) -> Html a -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> ByteString
Blaze.toLazyByteString (Builder -> ByteString)
-> (Html a -> Builder) -> Html a -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Identity Builder -> Builder
forall a. Identity a -> a
runIdentity (Identity Builder -> Builder)
-> (Html a -> Identity Builder) -> Html a -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Html a -> Identity Builder
forall (m :: * -> *) a. Monad m => HtmlT m a -> m Builder
execHtmlT
renderBST :: Monad m => HtmlT m a -> m ByteString
renderBST :: HtmlT m a -> m ByteString
renderBST = (Builder -> ByteString) -> m Builder -> m ByteString
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Builder -> ByteString
Blaze.toLazyByteString (m Builder -> m ByteString)
-> (HtmlT m a -> m Builder) -> HtmlT m a -> m ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HtmlT m a -> m Builder
forall (m :: * -> *) a. Monad m => HtmlT m a -> m Builder
execHtmlT
renderTextT :: Monad m => HtmlT m a -> m LT.Text
renderTextT :: HtmlT m a -> m Text
renderTextT = (Builder -> Text) -> m Builder -> m Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (ByteString -> Text
LT.decodeUtf8 (ByteString -> Text) -> (Builder -> ByteString) -> Builder -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> ByteString
Blaze.toLazyByteString) (m Builder -> m Text)
-> (HtmlT m a -> m Builder) -> HtmlT m a -> m Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HtmlT m a -> m Builder
forall (m :: * -> *) a. Monad m => HtmlT m a -> m Builder
execHtmlT
execHtmlT :: Monad m
=> HtmlT m a
-> m Builder
execHtmlT :: HtmlT m a -> m Builder
execHtmlT HtmlT m a
m =
do (HashMap Text Text -> Builder
f,a
_) <- HtmlT m a -> m (HashMap Text Text -> Builder, a)
forall (m :: * -> *) a.
HtmlT m a -> m (HashMap Text Text -> Builder, a)
runHtmlT HtmlT m a
m
Builder -> m Builder
forall (m :: * -> *) a. Monad m => a -> m a
return (HashMap Text Text -> Builder
f HashMap Text Text
forall a. Monoid a => a
mempty)
relaxHtmlT :: Monad m
=> HtmlT Identity a
-> HtmlT m a
relaxHtmlT :: HtmlT Identity a -> HtmlT m a
relaxHtmlT = (forall a. Identity a -> m a) -> HtmlT Identity a -> HtmlT m a
forall k (t :: (* -> *) -> k -> *) (m :: * -> *) (n :: * -> *)
(b :: k).
(MFunctor t, Monad m) =>
(forall a. m a -> n a) -> t m b -> t n b
hoist forall a. Identity a -> m a
forall (m :: * -> *) a. Monad m => Identity a -> m a
go
where
go :: Monad m => Identity a -> m a
go :: Identity a -> m a
go = a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> m a) -> (Identity a -> a) -> Identity a -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Identity a -> a
forall a. Identity a -> a
runIdentity
commuteHtmlT :: (Functor m, Monad n)
=> HtmlT m a
-> m (HtmlT n a)
commuteHtmlT :: HtmlT m a -> m (HtmlT n a)
commuteHtmlT (HtmlT m (HashMap Text Text -> Builder, a)
xs) = ((HashMap Text Text -> Builder, a) -> HtmlT n a)
-> m (HashMap Text Text -> Builder, a) -> m (HtmlT n a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (n (HashMap Text Text -> Builder, a) -> HtmlT n a
forall (m :: * -> *) a.
m (HashMap Text Text -> Builder, a) -> HtmlT m a
HtmlT (n (HashMap Text Text -> Builder, a) -> HtmlT n a)
-> ((HashMap Text Text -> Builder, a)
-> n (HashMap Text Text -> Builder, a))
-> (HashMap Text Text -> Builder, a)
-> HtmlT n a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (HashMap Text Text -> Builder, a)
-> n (HashMap Text Text -> Builder, a)
forall (m :: * -> *) a. Monad m => a -> m a
return) m (HashMap Text Text -> Builder, a)
xs
evalHtmlT :: Monad m
=> HtmlT m a
-> m a
evalHtmlT :: HtmlT m a -> m a
evalHtmlT HtmlT m a
m =
do (HashMap Text Text -> Builder
_,a
a) <- HtmlT m a -> m (HashMap Text Text -> Builder, a)
forall (m :: * -> *) a.
HtmlT m a -> m (HashMap Text Text -> Builder, a)
runHtmlT HtmlT m a
m
a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
a
makeAttribute :: Text
-> Text
-> Attribute
makeAttribute :: Text -> Text -> Attribute
makeAttribute Text
x Text
y = Text -> Text -> Attribute
Attribute Text
x Text
y
makeElement :: Functor m
=> Text
-> HtmlT m a
-> HtmlT m a
{-# INLINE[1] makeElement #-}
makeElement :: Text -> HtmlT m a -> HtmlT m a
makeElement Text
name = \HtmlT m a
m' -> m (HashMap Text Text -> Builder, a) -> HtmlT m a
forall (m :: * -> *) a.
m (HashMap Text Text -> Builder, a) -> HtmlT m a
HtmlT ((HashMap Text Text -> Builder, a)
-> (HashMap Text Text -> Builder, a)
mk ((HashMap Text Text -> Builder, a)
-> (HashMap Text Text -> Builder, a))
-> m (HashMap Text Text -> Builder, a)
-> m (HashMap Text Text -> Builder, a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> HtmlT m a -> m (HashMap Text Text -> Builder, a)
forall (m :: * -> *) a.
HtmlT m a -> m (HashMap Text Text -> Builder, a)
runHtmlT HtmlT m a
m')
where
mk :: (HashMap Text Text -> Builder, a)
-> (HashMap Text Text -> Builder, a)
mk ~(HashMap Text Text -> Builder
f,a
a) =
(\HashMap Text Text
attr ->
String -> Builder
s String
"<" Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Text -> Builder
Blaze.fromText Text
name
Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> (Text -> Text -> Builder) -> HashMap Text Text -> Builder
forall m k v. Monoid m => (k -> v -> m) -> HashMap k v -> m
foldlMapWithKey Text -> Text -> Builder
buildAttr HashMap Text Text
attr Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> String -> Builder
s String
">"
Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> HashMap Text Text -> Builder
f HashMap Text Text
forall a. Monoid a => a
mempty
Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> String -> Builder
s String
"</" Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Text -> Builder
Blaze.fromText Text
name Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> String -> Builder
s String
">"
,a
a)
makeElementNoEnd :: Applicative m
=> Text
-> HtmlT m ()
makeElementNoEnd :: Text -> HtmlT m ()
makeElementNoEnd Text
name =
m (HashMap Text Text -> Builder, ()) -> HtmlT m ()
forall (m :: * -> *) a.
m (HashMap Text Text -> Builder, a) -> HtmlT m a
HtmlT ((HashMap Text Text -> Builder, ())
-> m (HashMap Text Text -> Builder, ())
forall (f :: * -> *) a. Applicative f => a -> f a
pure (\HashMap Text Text
attr -> String -> Builder
s String
"<" Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Text -> Builder
Blaze.fromText Text
name
Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> (Text -> Text -> Builder) -> HashMap Text Text -> Builder
forall m k v. Monoid m => (k -> v -> m) -> HashMap k v -> m
foldlMapWithKey Text -> Text -> Builder
buildAttr HashMap Text Text
attr Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> String -> Builder
s String
">",
()))
makeXmlElementNoEnd :: Applicative m
=> Text
-> HtmlT m ()
makeXmlElementNoEnd :: Text -> HtmlT m ()
makeXmlElementNoEnd Text
name =
m (HashMap Text Text -> Builder, ()) -> HtmlT m ()
forall (m :: * -> *) a.
m (HashMap Text Text -> Builder, a) -> HtmlT m a
HtmlT ((HashMap Text Text -> Builder, ())
-> m (HashMap Text Text -> Builder, ())
forall (f :: * -> *) a. Applicative f => a -> f a
pure (\HashMap Text Text
attr -> String -> Builder
s String
"<" Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Text -> Builder
Blaze.fromText Text
name
Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> (Text -> Text -> Builder) -> HashMap Text Text -> Builder
forall m k v. Monoid m => (k -> v -> m) -> HashMap k v -> m
foldlMapWithKey Text -> Text -> Builder
buildAttr HashMap Text Text
attr Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> String -> Builder
s String
"/>",
()))
buildAttr :: Text -> Text -> Builder
buildAttr :: Text -> Text -> Builder
buildAttr Text
key Text
val =
String -> Builder
s String
" " Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>
Text -> Builder
Blaze.fromText Text
key Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>
if Text
val Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
forall a. Monoid a => a
mempty
then Builder
forall a. Monoid a => a
mempty
else String -> Builder
s String
"=\"" Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Text -> Builder
Blaze.fromHtmlEscapedText Text
val Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> String -> Builder
s String
"\""
foldlMapWithKey :: Monoid m => (k -> v -> m) -> HashMap k v -> m
foldlMapWithKey :: (k -> v -> m) -> HashMap k v -> m
foldlMapWithKey k -> v -> m
f = (m -> k -> v -> m) -> m -> HashMap k v -> m
forall a k v. (a -> k -> v -> a) -> a -> HashMap k v -> a
M.foldlWithKey' (\m
m k
k v
v -> m
m m -> m -> m
forall a. Monoid a => a -> a -> a
`mappend` k -> v -> m
f k
k v
v) m
forall a. Monoid a => a
mempty
s :: String -> Builder
s :: String -> Builder
s = String -> Builder
Blaze.fromString
{-# INLINE s #-}