module Text.CxML.Output
(showNonCxmlStrict)
where
import Data.List (intercalate, nub)
import Text.CxML.Types
import Text.CxML.Tags
import Text.CxML.HTML
import Text.CxML.CSS (csslink)
strictDocType = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\""
++ " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n"
fullInlineDoc :: String -> CxML a -> CxML a
fullInlineDoc titl bod
= (tag "html")!("xmlns","http://www.w3.org/1999/xhtml") /-
[
header /- (theTitle : staticCSSLinks ++ [inlineParts]),
body /- [htmlPart bod, inlineJS $ js bod ]
]
where
theTitle = title /- [t titl]
css' = css bod
staticCSSLinks = map csslink $ nub $ [url | CSSLink url <- css']
cssRls = nub $ [(sel,declBlock) | CSSRule sel declBlock <- css']
inlineParts = inlineCSS cssRls
inlineCSS :: [([String], [(String,String)])]-> CxML a
inlineCSS [] = noElem
inlineCSS cssRls = style!("type", "text/css")!("media","screen") /- [t $ concatMap showRule cssRls]
where
showRule (sel,declBlock) = concat [ intercalate "," sel, " {",
concatMap (\(at,vl)->at++":"++vl++";") declBlock,
"}\n" ]
inlineJS :: [JSDecl]-> CxML a
inlineJS [] = noElem
inlineJS js = script!("type","text/javascript")/- [t $ show js]
instance Show JSDecl where
show (OnLoad s) = s
showNonCx :: CxML () -> String
showNonCx (CxML (h,ts,c,j)) = concatMap (showNice' 0) $ h ()
showNice = showNice' 0
showNice' :: Int -> HElem -> String
showNice' i (HText str) = concat [spaces i,str, "\n" ]
showNice' i (HTag nm@"div" ats chs) = concat [spaces i, "<", nm,
concatMap (\(an, av)->" "++an++"=\""++av++"\"") ats, ">\n",
concatMap (showNice' (i+1)) chs, spaces i, "</", nm, ">\n"]
showNice' i (HTag nm [] []) = concat [spaces i, "<", nm, " />\n" ]
showNice' i (HTag nm ats chs) = concat [spaces i, "<", nm,
concatMap (\(an, av)->" "++an++"=\""++av++"\"") ats, ">\n",
concatMap (showNice' (i+1)) chs, spaces i, "</", nm, ">\n"]
spaces i = replicate i ' '
showNonCxmlStrict :: String -> CxML () -> String
showNonCxmlStrict titl = (strictDocType ++) . showNonCx . fullInlineDoc titl