-- | 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 = Int -> (String -> String) -> Markup -> String -> String
forall b.
Int -> (String -> String) -> MarkupM b -> String -> String
go Int
0 String -> String
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 (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StaticString -> String -> String
getString StaticString
open (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
attrs (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String
">\n" String -> String -> String
forall a. [a] -> [a] -> [a]
++) (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> (String -> String) -> MarkupM b -> String -> String
forall b.
Int -> (String -> String) -> MarkupM b -> String -> String
go (Int -> Int
inc Int
i) String -> String
forall a. a -> a
id MarkupM b
content
              (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> String -> String
ind Int
i (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StaticString -> String -> String
getString StaticString
close (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
.  (Char
'\n' Char -> String -> String
forall a. a -> [a] -> [a]
:)
    go Int
i String -> String
attrs (CustomParent ChoiceString
tag MarkupM b
content) =
        Int -> String -> String
ind Int
i (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char
'<' Char -> String -> String
forall a. a -> [a] -> [a]
:) (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ChoiceString -> String -> String
fromChoiceString ChoiceString
tag (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
attrs (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String
">\n" String -> String -> String
forall a. [a] -> [a] -> [a]
++) (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
        Int -> (String -> String) -> MarkupM b -> String -> String
forall b.
Int -> (String -> String) -> MarkupM b -> String -> String
go (Int -> Int
inc Int
i) String -> String
forall a. a -> a
id MarkupM b
content (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> String -> String
ind Int
i (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String
"</" String -> String -> String
forall a. [a] -> [a] -> [a]
++) (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ChoiceString -> String -> String
fromChoiceString ChoiceString
tag (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
        (String
">\n" String -> String -> String
forall a. [a] -> [a] -> [a]
++)
    go Int
i String -> String
attrs (Leaf StaticString
_ StaticString
begin StaticString
end b
_) =
        Int -> String -> String
ind Int
i (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StaticString -> String -> String
getString StaticString
begin (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
attrs (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StaticString -> String -> String
getString StaticString
end (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char
'\n' Char -> String -> String
forall a. a -> [a] -> [a]
:)
    go Int
i String -> String
attrs (CustomLeaf ChoiceString
tag Bool
close b
_) =
        Int -> String -> String
ind Int
i (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char
'<' Char -> String -> String
forall a. a -> [a] -> [a]
:) (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ChoiceString -> String -> String
fromChoiceString ChoiceString
tag (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
attrs (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
        ((if Bool
close then String
" />\n" else String
">\n") String -> String -> String
forall a. [a] -> [a] -> [a]
++)
    go Int
i String -> String
attrs (AddAttribute StaticString
_ StaticString
key ChoiceString
value MarkupM b
h) = ((String -> String) -> MarkupM b -> String -> String)
-> MarkupM b -> (String -> String) -> String -> String
forall a b c. (a -> b -> c) -> b -> a -> c
flip (Int -> (String -> String) -> MarkupM b -> String -> String
forall b.
Int -> (String -> String) -> MarkupM b -> String -> String
go Int
i) MarkupM b
h ((String -> String) -> String -> String)
-> (String -> String) -> String -> String
forall a b. (a -> b) -> a -> b
$
        StaticString -> String -> String
getString StaticString
key (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ChoiceString -> String -> String
fromChoiceString ChoiceString
value (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char
'"' Char -> String -> String
forall a. a -> [a] -> [a]
:) (String -> String) -> (String -> String) -> String -> String
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) = ((String -> String) -> MarkupM b -> String -> String)
-> MarkupM b -> (String -> String) -> String -> String
forall a b c. (a -> b -> c) -> b -> a -> c
flip (Int -> (String -> String) -> MarkupM b -> String -> String
forall b.
Int -> (String -> String) -> MarkupM b -> String -> String
go Int
i) MarkupM b
h ((String -> String) -> String -> String)
-> (String -> String) -> String -> String
forall a b. (a -> b) -> a -> b
$
        (Char
' ' Char -> String -> String
forall a. a -> [a] -> [a]
: ) (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ChoiceString -> String -> String
fromChoiceString ChoiceString
key (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String
"=\"" String -> String -> String
forall a. [a] -> [a] -> [a]
++) (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ChoiceString -> String -> String
fromChoiceString ChoiceString
value (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
        (Char
'"' Char -> String -> String
forall a. a -> [a] -> [a]
:) (String -> String) -> (String -> String) -> String -> String
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 (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ChoiceString -> String -> String
fromChoiceString ChoiceString
content (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char
'\n' Char -> String -> String
forall a. a -> [a] -> [a]
:)
    go Int
i String -> String
_ (Comment ChoiceString
comment b
_) = Int -> String -> String
ind Int
i (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
        (String
"<!-- " String -> String -> String
forall a. [a] -> [a] -> [a]
++) (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ChoiceString -> String -> String
fromChoiceString ChoiceString
comment (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String
" -->\n" String -> String -> String
forall a. [a] -> [a] -> [a]
++)
    go Int
i String -> String
attrs (Append MarkupM b
h1 MarkupM b
h2) = Int -> (String -> String) -> MarkupM b -> String -> String
forall b.
Int -> (String -> String) -> MarkupM b -> String -> String
go Int
i String -> String
attrs MarkupM b
h1 (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> (String -> String) -> MarkupM b -> String -> String
forall b.
Int -> (String -> String) -> MarkupM b -> String -> String
go Int
i String -> String
attrs MarkupM b
h2
    go Int
_ String -> String
_ (Empty b
_) = String -> String
forall a. a -> a
id
    {-# NOINLINE go #-}

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

    -- Produce appending indentation
    ind :: Int -> String -> String
ind Int
i = (Int -> Char -> String
forall a. Int -> a -> [a]
replicate Int
i Char
' ' String -> String -> String
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" #-}