module Html.Reify where
import Html.Type.Internal
import Html.Type.Internal.GHC
import Html.Convert
import GHC.TypeLits
import Data.Proxy
import Data.Semigroup ((<>))
import qualified Data.Text.Lazy as T
import qualified Data.Text.Lazy.Encoding as T
import qualified Data.ByteString.Lazy as B
import qualified Data.ByteString.Builder as B
renderBuilder :: Document a => a -> B.Builder
renderBuilder = renderchunks . tag
tag :: a -> Tagged (ToTypeList a) a
tag = Tagged
renderString :: Document a => a -> String
renderString = T.unpack . renderText
renderText :: Document a => a -> T.Text
renderText = T.decodeUtf8 . renderByteString
renderByteString :: Document a => a -> B.ByteString
renderByteString = B.toLazyByteString . renderBuilder
type Document a = Document' a
type Document' a = Renderchunks (Tagged (ToTypeList a) a)
class Renderchunks a where
renderchunks :: a -> B.Builder
instance KnownSymbol a => Renderchunks (Tagged (prox :: [k]) (Proxy a)) where
renderchunks _ = mempty
instance Renderchunks (Tagged prox ()) where
renderchunks _ = mempty
instance
( Convert val
) => Renderchunks (Tagged '[ EmptySym ] val) where
renderchunks (Tagged x)
= unConv (convert x)
instance
Renderchunks (Tagged '[] val) where
renderchunks _ = mempty
instance
( Convert val
, Convert (Proxy s)
) => Renderchunks (Tagged '[s] val) where
renderchunks (Tagged x)
= unConv (convert (Proxy @ s))
<> unConv (convert x)
instance
( Renderchunks (Tagged xs val)
) => Renderchunks (Tagged (NoTail xs) val) where
renderchunks (Tagged t)
= renderchunks (Tagged t :: Tagged xs val)
instance
( Renderchunks (Tagged xs val)
, Convert (Proxy x)
) => Renderchunks (Tagged ('FingerTree xs x) val) where
renderchunks (Tagged t)
= renderchunks (Tagged t :: Tagged xs val)
<> unConv (convert (Proxy @ x))
instance
( Renderchunks (Tagged (Take (CountContent b) prox) b)
, Renderchunks (Tagged (Drop (CountContent b) prox) c)
) => Renderchunks (Tagged prox ((a :@: b) c)) where
renderchunks (Tagged ~(WithAttributes b c))
= renderchunks (Tagged b :: Tagged (Take (CountContent b) prox) b)
<> renderchunks (Tagged c :: Tagged (Drop (CountContent b) prox) c)
instance
( Renderchunks (Tagged (Take (CountContent a) prox) a)
, Renderchunks (Tagged (Drop (CountContent a) prox) b)
) => Renderchunks (Tagged prox (a # b)) where
renderchunks (Tagged ~(a :#: b))
= renderchunks (Tagged a :: Tagged (Take (CountContent a) prox) a)
<> renderchunks (Tagged b :: Tagged (Drop (CountContent a) prox) b)
instance
( Renderchunks (Tagged (ToTypeList (a `f` b)) (a `f` b))
, Convert (Proxy s)
) => Renderchunks (Tagged (s ': ss) [a `f` b]) where
renderchunks (Tagged xs)
= unConv (convert (Proxy @ s))
<> foldMap (renderchunks . tag) xs