module Text.XHtmlCombinators.Render
( render, renderPretty
) where
import Control.Applicative hiding (empty)
import Data.Foldable
import qualified Data.Sequence as Seq
import Data.Text.Lazy (Text)
import qualified Data.Text.Lazy as T
import qualified Data.Text.Lazy.IO as T
import qualified Text.XML.Light as XML
import Text.XHtmlCombinators.Internal
lt = T.singleton '<'
gt = T.singleton '>'
space = T.singleton ' '
lts = T.pack "</"
nl = T.singleton '\n'
renderAttrs :: Attrs -> Text
renderAttrs [] = T.empty
renderAttrs attrs = T.concat (space : fmap renderAttr attrs)
where renderAttr (Attr name val) =
T.concat [name, T.pack "=\"", val, T.pack "\""]
renderNode (TextNode t) = t
renderNode (Node name rattrs attrs c)
| Seq.null c = T.concat [lt, name, a, b, gt, lts, name, gt]
| otherwise = T.concat
[lt, name, a, b, gt, fold (fmap renderNode c), lts, name, gt]
where a = renderAttrs rattrs
b = renderAttrs attrs
render :: Content c => XHtml c -> Text
render page = fold $ renderNode . toContent <$> execXHtml page
renderPretty :: Content c => XHtml c -> Text
renderPretty = T.pack . unlines . fmap XML.ppContent . XML.parseXML . T.unpack . render