module Text.HTML.Tagchup.Format (
xml, xmlCondensed, html, xhtml, htmlOrXhtml,
) where
import qualified Text.HTML.Tagchup.Tag as Tag
import qualified Text.HTML.Basic.Tag as TagH
import qualified Text.XML.Basic.Name as Name
import qualified Text.XML.Basic.Format as Fmt
import Data.List.HT (viewL, )
import Data.Maybe (fromMaybe, )
import Control.Monad (guard, )
xml :: (Name.Tag name, Name.Attribute name, Fmt.C string) =>
[Tag.T name string] -> ShowS
xml = Fmt.many Fmt.run
xmlCondensed :: (Name.Tag name, Name.Attribute name, Fmt.C string) =>
[Tag.T name string] -> ShowS
xmlCondensed = xmlCondensedGen (==)
html :: (Name.Tag name, Name.Attribute name, Fmt.C string) =>
[Tag.T name string] -> ShowS
html =
Fmt.many Fmt.run .
filter (maybe True (not . TagH.isEmpty) . Tag.maybeClose)
xhtml :: (Name.Tag name, Name.Attribute name, Fmt.C string) =>
[Tag.T name string] -> ShowS
xhtml =
xmlCondensedGen
(\nameOpen nameClose ->
nameOpen==nameClose && TagH.isEmpty nameOpen)
htmlOrXhtml :: (Name.Tag name, Name.Attribute name, Fmt.C string) =>
[Tag.T name string] -> ShowS
htmlOrXhtml tags =
fromMaybe (html tags) $
do (tag,_) <- viewL tags
(name,_) <- Tag.maybeProcessing tag
guard (Name.match "xml" name)
return (xhtml tags)
xmlCondensedGen :: (Name.Tag name, Name.Attribute name, Fmt.C string) =>
(Tag.Name name -> Tag.Name name -> Bool) ->
[Tag.T name string] -> ShowS
xmlCondensedGen check =
let recourse (Tag.Open nameOpen attrs : Tag.Close nameClose : ts) =
(if check nameOpen nameClose
then Tag.formatOpen True nameOpen attrs
else Tag.formatOpen False nameOpen attrs .
Tag.formatClose nameClose)
. recourse ts
recourse (t : ts) = Fmt.run t . recourse ts
recourse [] = id
in recourse