-- | A renderer that produces pretty HTML, mostly meant for debugging purposes.
--
module Text.Blaze.Renderer.Pretty
    ( renderMarkup
    , renderHtml
    ) where

import Text.Blaze.Internal
import Text.Blaze.Renderer.String (fromChoiceString)

-- | Render some 'Markup' to an appending 'String'.
--
renderString :: Markup  -- ^ Markup to render
             -> String  -- ^ String to append
             -> String  -- ^ Resulting String
renderString :: Markup -> String -> String
renderString = forall b.
Int -> (String -> String) -> MarkupM b -> String -> String
go Int
0 forall a. a -> a
id
  where
    go :: Int -> (String -> String) -> MarkupM b -> String -> String
    go :: forall b.
Int -> (String -> String) -> MarkupM b -> String -> String
go Int
i String -> String
attrs (Parent StaticString
_ StaticString
open StaticString
close MarkupM b
content) =
        Int -> String -> String
ind Int
i forall b c a. (b -> c) -> (a -> b) -> a -> c
. StaticString -> String -> String
getString StaticString
open forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
attrs forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String
">\n" forall a. [a] -> [a] -> [a]
++) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall b.
Int -> (String -> String) -> MarkupM b -> String -> String
go (Int -> Int
inc Int
i) forall a. a -> a
id MarkupM b
content
              forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> String -> String
ind Int
i forall b c a. (b -> c) -> (a -> b) -> a -> c
. StaticString -> String -> String
getString StaticString
close forall b c a. (b -> c) -> (a -> b) -> a -> c
.  (Char
'\n' forall a. a -> [a] -> [a]
:)
    go Int
i String -> String
attrs (CustomParent ChoiceString
tag MarkupM b
content) =
        Int -> String -> String
ind Int
i forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char
'<' forall a. a -> [a] -> [a]
:) forall b c a. (b -> c) -> (a -> b) -> a -> c
. ChoiceString -> String -> String
fromChoiceString ChoiceString
tag forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
attrs forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String
">\n" forall a. [a] -> [a] -> [a]
++) forall b c a. (b -> c) -> (a -> b) -> a -> c
.
        forall b.
Int -> (String -> String) -> MarkupM b -> String -> String
go (Int -> Int
inc Int
i) forall a. a -> a
id MarkupM b
content forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> String -> String
ind Int
i forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String
"</" forall a. [a] -> [a] -> [a]
++) forall b c a. (b -> c) -> (a -> b) -> a -> c
. ChoiceString -> String -> String
fromChoiceString ChoiceString
tag forall b c a. (b -> c) -> (a -> b) -> a -> c
.
        (String
">\n" forall a. [a] -> [a] -> [a]
++)
    go Int
i String -> String
attrs (Leaf StaticString
_ StaticString
begin StaticString
end b
_) =
        Int -> String -> String
ind Int
i forall b c a. (b -> c) -> (a -> b) -> a -> c
. StaticString -> String -> String
getString StaticString
begin forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
attrs forall b c a. (b -> c) -> (a -> b) -> a -> c
. StaticString -> String -> String
getString StaticString
end forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char
'\n' forall a. a -> [a] -> [a]
:)
    go Int
i String -> String
attrs (CustomLeaf ChoiceString
tag Bool
close b
_) =
        Int -> String -> String
ind Int
i forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char
'<' forall a. a -> [a] -> [a]
:) forall b c a. (b -> c) -> (a -> b) -> a -> c
. ChoiceString -> String -> String
fromChoiceString ChoiceString
tag forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
attrs forall b c a. (b -> c) -> (a -> b) -> a -> c
.
        ((if Bool
close then String
" />\n" else String
">\n") forall a. [a] -> [a] -> [a]
++)
    go Int
i String -> String
attrs (AddAttribute StaticString
_ StaticString
key ChoiceString
value MarkupM b
h) = forall a b c. (a -> b -> c) -> b -> a -> c
flip (forall b.
Int -> (String -> String) -> MarkupM b -> String -> String
go Int
i) MarkupM b
h forall a b. (a -> b) -> a -> b
$
        StaticString -> String -> String
getString StaticString
key forall b c a. (b -> c) -> (a -> b) -> a -> c
. ChoiceString -> String -> String
fromChoiceString ChoiceString
value forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char
'"' forall a. a -> [a] -> [a]
:) forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
attrs
    go Int
i String -> String
attrs (AddCustomAttribute ChoiceString
key ChoiceString
value MarkupM b
h) = forall a b c. (a -> b -> c) -> b -> a -> c
flip (forall b.
Int -> (String -> String) -> MarkupM b -> String -> String
go Int
i) MarkupM b
h forall a b. (a -> b) -> a -> b
$
        (Char
' ' forall a. a -> [a] -> [a]
: ) forall b c a. (b -> c) -> (a -> b) -> a -> c
. ChoiceString -> String -> String
fromChoiceString ChoiceString
key forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String
"=\"" forall a. [a] -> [a] -> [a]
++) forall b c a. (b -> c) -> (a -> b) -> a -> c
. ChoiceString -> String -> String
fromChoiceString ChoiceString
value forall b c a. (b -> c) -> (a -> b) -> a -> c
.
        (Char
'"' forall a. a -> [a] -> [a]
:) forall b c a. (b -> c) -> (a -> b) -> a -> c
.  String -> String
attrs
    go Int
i String -> String
_ (Content ChoiceString
content b
_) = Int -> String -> String
ind Int
i forall b c a. (b -> c) -> (a -> b) -> a -> c
. ChoiceString -> String -> String
fromChoiceString ChoiceString
content forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char
'\n' forall a. a -> [a] -> [a]
:)
    go Int
i String -> String
_ (Comment ChoiceString
comment b
_) = Int -> String -> String
ind Int
i forall b c a. (b -> c) -> (a -> b) -> a -> c
.
        (String
"<!-- " forall a. [a] -> [a] -> [a]
++) forall b c a. (b -> c) -> (a -> b) -> a -> c
. ChoiceString -> String -> String
fromChoiceString ChoiceString
comment forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String
" -->\n" forall a. [a] -> [a] -> [a]
++)
    go Int
i String -> String
attrs (Append MarkupM b
h1 MarkupM b
h2) = forall b.
Int -> (String -> String) -> MarkupM b -> String -> String
go Int
i String -> String
attrs MarkupM b
h1 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall b.
Int -> (String -> String) -> MarkupM b -> String -> String
go Int
i String -> String
attrs MarkupM b
h2
    go Int
_ String -> String
_ (Empty b
_) = forall a. a -> a
id
    {-# NOINLINE go #-}

    -- Increase the indentation
    inc :: Int -> Int
inc = forall a. Num a => a -> a -> a
(+) Int
4

    -- Produce appending indentation
    ind :: Int -> String -> String
ind Int
i = (forall a. Int -> a -> [a]
replicate Int
i Char
' ' forall a. [a] -> [a] -> [a]
++)
{-# INLINE renderString #-}

-- | Render markup to a lazy 'String'. The result is prettified.
--
renderMarkup :: Markup -> String
renderMarkup :: Markup -> String
renderMarkup Markup
html = Markup -> String -> String
renderString Markup
html String
""
{-# INLINE renderMarkup #-}

renderHtml :: Markup -> String
renderHtml :: Markup -> String
renderHtml = Markup -> String
renderMarkup
{-# INLINE renderHtml #-}
{-# DEPRECATED renderHtml
    "Use renderHtml from Text.Blaze.Html.Renderer.Pretty instead" #-}