module Text.Blaze.Extra where
import Control.Monad
import Data.Monoid
import Prelude hiding (head,div)
import Text.Blaze.Html5 as H hiding (map)
import Text.Blaze.Html5.Attributes as A
import Text.Blaze.Internal (Attributable)
import Network.URI.Params
import Network.URI
import Text.Printf
import Data.List (intercalate)
(!.) :: (Attributable h) => h -> AttributeValue -> h
elem !. className = elem ! class_ className
(!#) :: (Attributable h) => h -> AttributeValue -> h
elem !# idName = elem ! A.id idName
linesToHtml :: String -> Html
linesToHtml str = forM_ (lines str) $ \line -> do toHtml line; br
htmlIntercalate :: Html -> [Html] -> Html
htmlIntercalate _ [x] = x
htmlIntercalate sep (x:xs) = do x; sep; htmlIntercalate sep xs
htmlIntercalate _ [] = mempty
htmlCommas :: [Html] -> Html
htmlCommas = htmlIntercalate ", "
hrefSet :: URI -> String -> String -> Attribute
hrefSet uri key value = hrefURI updated where
updated = updateUrlParam key value uri
hrefURI :: URI -> Attribute
hrefURI uri = href (toValue (showURI uri)) where
showURI URI{..} = uriPath ++ uriQuery
hrefAssoc :: String -> [(String,String)] -> Attribute
hrefAssoc path qs = href (toValue uri) where
uri = "/" ++ path ++ "?" ++ intercalate "&" (map (uncurry (printf "%s=%s")) qs)
css :: H.AttributeValue -> H.Html
css link = H.link ! A.rel "stylesheet" ! A.type_ "text/css" ! A.href link
js :: H.AttributeValue -> H.Html
js link = H.script ! A.type_ "text/JavaScript" ! A.src link $ ""
linkTo :: H.AttributeValue -> H.Html -> H.Html
linkTo url = H.a ! A.href url
postForm :: String -> H.Html -> H.Html
postForm uri = H.form ! A.action (H.toValue uri) ! A.method "post"