{-# LANGUAGE  UndecidableInstances #-}
{-# LANGUAGE  FlexibleInstances #-}

{-|
Module: IHP.HSX.ToHtml
Description: Provides a few helper instances that convert data structures to HTML
Copyright: (c) digitally induced GmbH, 2022
-}
module IHP.HSX.ToHtml where

import Prelude
import qualified Text.Blaze.Html5 as Html5
import qualified Text.Blaze.Internal
import Data.Text
import Data.ByteString
import Data.String.Conversions (cs)
import IHP.HSX.ConvertibleStrings ()

class ToHtml a where
    toHtml :: a -> Html5.Html

instance ToHtml (Text.Blaze.Internal.MarkupM ()) where
    {-# INLINE toHtml #-}
    toHtml :: MarkupM () -> MarkupM ()
toHtml MarkupM ()
a = MarkupM ()
a

instance ToHtml Text where
    {-# INLINE toHtml #-}
    toHtml :: Text -> MarkupM ()
toHtml = Text -> MarkupM ()
Html5.text

instance ToHtml String where
    {-# INLINE toHtml #-}
    toHtml :: String -> MarkupM ()
toHtml = String -> MarkupM ()
Html5.string

instance ToHtml ByteString where
    {-# INLINE toHtml #-}
    toHtml :: ByteString -> MarkupM ()
toHtml ByteString
value = forall a. ToHtml a => a -> MarkupM ()
toHtml (forall a b. ConvertibleStrings a b => a -> b
cs ByteString
value :: Text)

instance {-# OVERLAPPABLE #-} ToHtml a => ToHtml (Maybe a) where
    {-# INLINE toHtml #-}
    toHtml :: Maybe a -> MarkupM ()
toHtml Maybe a
maybeValue = forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. Monoid a => a
mempty forall a. ToHtml a => a -> MarkupM ()
toHtml Maybe a
maybeValue

instance {-# OVERLAPPABLE #-} Show a => ToHtml a where
    {-# INLINE toHtml #-}
    toHtml :: a -> MarkupM ()
toHtml a
value = String -> MarkupM ()
Html5.string (forall a. Show a => a -> String
show a
value)