-- | 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 0 String -> String
forall a. a -> a
id
  where
    go :: Int -> (String -> String) -> MarkupM b -> String -> String
    go :: Int -> (String -> String) -> MarkupM b -> String -> String
go i :: Int
i attrs :: String -> String
attrs (Parent _ open :: StaticString
open close :: StaticString
close content :: 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
. (">\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
.  ('\n' Char -> String -> String
forall a. a -> [a] -> [a]
:)
    go i :: Int
i attrs :: String -> String
attrs (CustomParent tag :: ChoiceString
tag content :: 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 -> 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
. (">\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
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
.
        (">\n" String -> String -> String
forall a. [a] -> [a] -> [a]
++)
    go i :: Int
i attrs :: String -> String
attrs (Leaf _ begin :: StaticString
begin end :: StaticString
end _) =
        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
. ('\n' Char -> String -> String
forall a. a -> [a] -> [a]
:)
    go i :: Int
i attrs :: String -> String
attrs (CustomLeaf tag :: ChoiceString
tag close :: Bool
close _) =
        Int -> String -> String
ind Int
i (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ('<' 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 " />\n" else ">\n") String -> String -> String
forall a. [a] -> [a] -> [a]
++)
    go i :: Int
i attrs :: String -> String
attrs (AddAttribute _ key :: StaticString
key value :: ChoiceString
value h :: 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 -> 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 i :: Int
i attrs :: String -> String
attrs (AddCustomAttribute key :: ChoiceString
key value :: ChoiceString
value h :: 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 -> 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
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 -> 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 i :: Int
i _ (Content content :: ChoiceString
content _) = 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
. ('\n' Char -> String -> String
forall a. a -> [a] -> [a]
:)
    go i :: Int
i _ (Comment comment :: ChoiceString
comment _) = 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
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
. (" -->\n" String -> String -> String
forall a. [a] -> [a] -> [a]
++)
    go i :: Int
i attrs :: String -> String
attrs (Append h1 :: MarkupM b
h1 h2 :: 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 _ _ (Empty _) = 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
(+) 4

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