{-# LANGUAGE OverloadedStrings #-}
module Text.Blaze.Renderer.Utf8
( renderMarkupBuilder
, renderMarkup
, renderMarkupToByteStringIO
, renderHtmlBuilder
, renderHtml
, renderHtmlToByteStringIO
) where
import Data.Monoid (mappend, mempty)
import Data.List (isInfixOf)
import qualified Data.ByteString.Lazy as L
import qualified Data.Text as T (isInfixOf)
import qualified Data.ByteString as S (ByteString, isInfixOf)
import Text.Blaze.Internal
import Blaze.ByteString.Builder (Builder)
import qualified Blaze.ByteString.Builder as B
import qualified Blaze.ByteString.Builder.Html.Utf8 as B
fromChoiceString :: ChoiceString
-> Builder
fromChoiceString :: ChoiceString -> Builder
fromChoiceString (Static StaticString
s) = ByteString -> Builder
B.copyByteString forall a b. (a -> b) -> a -> b
$ StaticString -> ByteString
getUtf8ByteString StaticString
s
fromChoiceString (String [Char]
s) = [Char] -> Builder
B.fromHtmlEscapedString [Char]
s
fromChoiceString (Text Text
s) = Text -> Builder
B.fromHtmlEscapedText Text
s
fromChoiceString (ByteString ByteString
s) = ByteString -> Builder
B.fromByteString ByteString
s
fromChoiceString (PreEscaped ChoiceString
x) = case ChoiceString
x of
String [Char]
s -> [Char] -> Builder
B.fromString [Char]
s
Text Text
s -> Text -> Builder
B.fromText Text
s
ChoiceString
s -> ChoiceString -> Builder
fromChoiceString ChoiceString
s
fromChoiceString (External ChoiceString
x) = case ChoiceString
x of
String [Char]
s -> if [Char]
"</" forall a. Eq a => [a] -> [a] -> Bool
`isInfixOf` [Char]
s then forall a. Monoid a => a
mempty else [Char] -> Builder
B.fromString [Char]
s
Text Text
s -> if Text
"</" Text -> Text -> Bool
`T.isInfixOf` Text
s then forall a. Monoid a => a
mempty else Text -> Builder
B.fromText Text
s
ByteString ByteString
s -> if ByteString
"</" ByteString -> ByteString -> Bool
`S.isInfixOf` ByteString
s then forall a. Monoid a => a
mempty else ByteString -> Builder
B.fromByteString ByteString
s
ChoiceString
s -> ChoiceString -> Builder
fromChoiceString ChoiceString
s
fromChoiceString (AppendChoiceString ChoiceString
x ChoiceString
y) =
ChoiceString -> Builder
fromChoiceString ChoiceString
x forall a. Monoid a => a -> a -> a
`mappend` ChoiceString -> Builder
fromChoiceString ChoiceString
y
fromChoiceString ChoiceString
EmptyChoiceString = forall a. Monoid a => a
mempty
{-# INLINE fromChoiceString #-}
renderMarkupBuilder, renderHtmlBuilder :: Markup
-> Builder
renderMarkupBuilder :: Markup -> Builder
renderMarkupBuilder = forall b. Builder -> MarkupM b -> Builder
go forall a. Monoid a => a
mempty
where
go :: Builder -> MarkupM b -> Builder
go :: forall b. Builder -> MarkupM b -> Builder
go Builder
attrs (Parent StaticString
_ StaticString
open StaticString
close MarkupM b
content) =
ByteString -> Builder
B.copyByteString (StaticString -> ByteString
getUtf8ByteString StaticString
open)
forall a. Monoid a => a -> a -> a
`mappend` Builder
attrs
forall a. Monoid a => a -> a -> a
`mappend` Char -> Builder
B.fromChar Char
'>'
forall a. Monoid a => a -> a -> a
`mappend` forall b. Builder -> MarkupM b -> Builder
go forall a. Monoid a => a
mempty MarkupM b
content
forall a. Monoid a => a -> a -> a
`mappend` ByteString -> Builder
B.copyByteString (StaticString -> ByteString
getUtf8ByteString StaticString
close)
go Builder
attrs (CustomParent ChoiceString
tag MarkupM b
content) =
Char -> Builder
B.fromChar Char
'<'
forall a. Monoid a => a -> a -> a
`mappend` ChoiceString -> Builder
fromChoiceString ChoiceString
tag
forall a. Monoid a => a -> a -> a
`mappend` Builder
attrs
forall a. Monoid a => a -> a -> a
`mappend` Char -> Builder
B.fromChar Char
'>'
forall a. Monoid a => a -> a -> a
`mappend` forall b. Builder -> MarkupM b -> Builder
go forall a. Monoid a => a
mempty MarkupM b
content
forall a. Monoid a => a -> a -> a
`mappend` ByteString -> Builder
B.fromByteString ByteString
"</"
forall a. Monoid a => a -> a -> a
`mappend` ChoiceString -> Builder
fromChoiceString ChoiceString
tag
forall a. Monoid a => a -> a -> a
`mappend` Char -> Builder
B.fromChar Char
'>'
go Builder
attrs (Leaf StaticString
_ StaticString
begin StaticString
end b
_) =
ByteString -> Builder
B.copyByteString (StaticString -> ByteString
getUtf8ByteString StaticString
begin)
forall a. Monoid a => a -> a -> a
`mappend` Builder
attrs
forall a. Monoid a => a -> a -> a
`mappend` ByteString -> Builder
B.copyByteString (StaticString -> ByteString
getUtf8ByteString StaticString
end)
go Builder
attrs (CustomLeaf ChoiceString
tag Bool
close b
_) =
Char -> Builder
B.fromChar Char
'<'
forall a. Monoid a => a -> a -> a
`mappend` ChoiceString -> Builder
fromChoiceString ChoiceString
tag
forall a. Monoid a => a -> a -> a
`mappend` Builder
attrs
forall a. Monoid a => a -> a -> a
`mappend` (if Bool
close then ByteString -> Builder
B.fromByteString ByteString
" />" else Char -> Builder
B.fromChar Char
'>')
go Builder
attrs (AddAttribute StaticString
_ StaticString
key ChoiceString
value MarkupM b
h) =
forall b. Builder -> MarkupM b -> Builder
go (ByteString -> Builder
B.copyByteString (StaticString -> ByteString
getUtf8ByteString StaticString
key)
forall a. Monoid a => a -> a -> a
`mappend` ChoiceString -> Builder
fromChoiceString ChoiceString
value
forall a. Monoid a => a -> a -> a
`mappend` Char -> Builder
B.fromChar Char
'"'
forall a. Monoid a => a -> a -> a
`mappend` Builder
attrs) MarkupM b
h
go Builder
attrs (AddCustomAttribute ChoiceString
key ChoiceString
value MarkupM b
h) =
forall b. Builder -> MarkupM b -> Builder
go (Char -> Builder
B.fromChar Char
' '
forall a. Monoid a => a -> a -> a
`mappend` ChoiceString -> Builder
fromChoiceString ChoiceString
key
forall a. Monoid a => a -> a -> a
`mappend` ByteString -> Builder
B.fromByteString ByteString
"=\""
forall a. Monoid a => a -> a -> a
`mappend` ChoiceString -> Builder
fromChoiceString ChoiceString
value
forall a. Monoid a => a -> a -> a
`mappend` Char -> Builder
B.fromChar Char
'"'
forall a. Monoid a => a -> a -> a
`mappend` Builder
attrs) MarkupM b
h
go Builder
_ (Content ChoiceString
content b
_) = ChoiceString -> Builder
fromChoiceString ChoiceString
content
go Builder
_ (Comment ChoiceString
comment b
_) =
ByteString -> Builder
B.fromByteString ByteString
"<!-- "
forall a. Monoid a => a -> a -> a
`mappend` ChoiceString -> Builder
fromChoiceString ChoiceString
comment
forall a. Monoid a => a -> a -> a
`mappend` ByteString -> Builder
B.fromByteString ByteString
" -->"
go Builder
attrs (Append MarkupM b
h1 MarkupM b
h2) = forall b. Builder -> MarkupM b -> Builder
go Builder
attrs MarkupM b
h1 forall a. Monoid a => a -> a -> a
`mappend` forall b. Builder -> MarkupM b -> Builder
go Builder
attrs MarkupM b
h2
go Builder
_ (Empty b
_) = forall a. Monoid a => a
mempty
{-# NOINLINE go #-}
{-# INLINE renderMarkupBuilder #-}
renderHtmlBuilder :: Markup -> Builder
renderHtmlBuilder = Markup -> Builder
renderMarkupBuilder
{-# INLINE renderHtmlBuilder #-}
{-# DEPRECATED renderHtmlBuilder
"Use renderHtmlBuilder from Text.Blaze.Html.Renderer.Utf8 instead" #-}
renderMarkup, renderHtml :: Markup
-> L.ByteString
renderMarkup :: Markup -> ByteString
renderMarkup = Builder -> ByteString
B.toLazyByteString forall b c a. (b -> c) -> (a -> b) -> a -> c
. Markup -> Builder
renderMarkupBuilder
{-# INLINE renderMarkup #-}
renderHtml :: Markup -> ByteString
renderHtml = Markup -> ByteString
renderMarkup
{-# INLINE renderHtml #-}
{-# DEPRECATED renderHtml
"Use renderHtml from Text.Blaze.Html.Renderer.Utf8 instead" #-}
renderMarkupToByteStringIO, renderHtmlToByteStringIO :: (S.ByteString -> IO ())
-> Markup
-> IO ()
renderMarkupToByteStringIO :: (ByteString -> IO ()) -> Markup -> IO ()
renderMarkupToByteStringIO ByteString -> IO ()
io = (ByteString -> IO ()) -> Builder -> IO ()
B.toByteStringIO ByteString -> IO ()
io forall b c a. (b -> c) -> (a -> b) -> a -> c
. Markup -> Builder
renderMarkupBuilder
{-# INLINE renderMarkupToByteStringIO #-}
renderHtmlToByteStringIO :: (ByteString -> IO ()) -> Markup -> IO ()
renderHtmlToByteStringIO = (ByteString -> IO ()) -> Markup -> IO ()
renderMarkupToByteStringIO
{-# INLINE renderHtmlToByteStringIO #-}
{-# DEPRECATED renderHtmlToByteStringIO
"Use renderMarkupToByteStringIO from Text.Blaze.Html.Renderer.Utf8 instead" #-}