{-#LANGUAGE GeneralizedNewtypeDeriving #-}
{-#LANGUAGE OverloadedStrings #-}
{-#LANGUAGE FlexibleInstances #-}
module Text.Ginger.Html
( Html
, unsafeRawHtml
, html
, htmlSource
, ToHtml (..)
)
where
import Data.Text (Text)
import qualified Data.Text as Text
import Data.Semigroup as Semigroup
newtype Html = Html { unHtml :: Text }
deriving (Semigroup.Semigroup, Monoid, Show, Eq, Ord)
class ToHtml s where
toHtml :: s -> Html
instance ToHtml Text where
toHtml = html
instance ToHtml [Char] where
toHtml = mconcat . map htmlEncodeChar
instance ToHtml Html where
toHtml = id
htmlSource :: Html -> Text
htmlSource = unHtml
unsafeRawHtml :: Text -> Html
unsafeRawHtml = Html
html :: Text -> Html
html = mconcat . map htmlEncodeChar . Text.unpack
htmlEncodeChar :: Char -> Html
htmlEncodeChar '&' = Html "&"
htmlEncodeChar '"' = Html """
htmlEncodeChar '\'' = Html "'"
htmlEncodeChar '<' = Html "<"
htmlEncodeChar '>' = Html ">"
htmlEncodeChar c = Html $ Text.singleton c