{-# LANGUAGE CPP #-}
#if __GLASGOW_HASKELL__ >= 704
{-# OPTIONS_GHC -fsimpl-tick-factor=40000 #-}
#endif
module Blaze.ByteString.Builder.Html.Utf8
(
module Blaze.ByteString.Builder.Char.Utf8
, writeHtmlEscapedChar
, fromHtmlEscapedChar
, fromHtmlEscapedString
, fromHtmlEscapedShow
, fromHtmlEscapedText
, fromHtmlEscapedLazyText
) where
import Data.ByteString.Char8 ()
import qualified Data.Text as TS
import qualified Data.Text.Lazy as TL
import Blaze.ByteString.Builder.Compat.Write ( Write, writePrimBounded )
import qualified Data.ByteString.Builder as B
import Data.ByteString.Builder.Prim ((>*<), (>$<), condB)
import qualified Data.ByteString.Builder.Prim as P
import Blaze.ByteString.Builder.Char.Utf8
writeHtmlEscapedChar :: Char -> Write
writeHtmlEscapedChar = writePrimBounded charUtf8HtmlEscaped
{-# INLINE writeHtmlEscapedChar #-}
fromHtmlEscapedChar :: Char -> B.Builder
fromHtmlEscapedChar = P.primBounded charUtf8HtmlEscaped
{-# INLINE fromHtmlEscapedChar #-}
{-# INLINE charUtf8HtmlEscaped #-}
charUtf8HtmlEscaped :: P.BoundedPrim Char
charUtf8HtmlEscaped =
condB (> '>' ) (condB (== '\DEL') P.emptyB P.charUtf8) $
condB (== '<' ) (fixed4 ('&',('l',('t',';')))) $
condB (== '>' ) (fixed4 ('&',('g',('t',';')))) $
condB (== '&' ) (fixed5 ('&',('a',('m',('p',';'))))) $
condB (== '"' ) (fixed6 ('&',('q',('u',('o',('t',';')))))) $
condB (== '\'') (fixed5 ('&',('#',('3',('9',';'))))) $
condB (\c -> c >= ' ' || c == '\t' || c == '\n' || c == '\r')
(P.liftFixedToBounded P.char7) $
P.emptyB
where
{-# INLINE fixed4 #-}
fixed4 x = P.liftFixedToBounded $ const x >$<
P.char7 >*< P.char7 >*< P.char7 >*< P.char7
{-# INLINE fixed5 #-}
fixed5 x = P.liftFixedToBounded $ const x >$<
P.char7 >*< P.char7 >*< P.char7 >*< P.char7 >*< P.char7
{-# INLINE fixed6 #-}
fixed6 x = P.liftFixedToBounded $ const x >$<
P.char7 >*< P.char7 >*< P.char7 >*< P.char7 >*< P.char7 >*< P.char7
fromHtmlEscapedString :: String -> B.Builder
fromHtmlEscapedString = P.primMapListBounded charUtf8HtmlEscaped
fromHtmlEscapedShow :: Show a => a -> B.Builder
fromHtmlEscapedShow = fromHtmlEscapedString . show
fromHtmlEscapedText :: TS.Text -> B.Builder
fromHtmlEscapedText = fromHtmlEscapedString . TS.unpack
fromHtmlEscapedLazyText :: TL.Text -> B.Builder
fromHtmlEscapedLazyText = fromHtmlEscapedString . TL.unpack