{-# LANGUAGE RecordWildCards #-} {-# LANGUAGE OverloadedStrings #-} {-# OPTIONS -fno-warn-name-shadowing -fno-warn-unused-do-bind #-} 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) -- | Add an class to an element. (!.) :: (Attributable h) => h -> AttributeValue -> h elem !. className = elem ! class_ className -- | Add an id to an element. (!#) :: (Attributable h) => h -> AttributeValue -> h elem !# idName = elem ! A.id idName -- | Render a list of lines to HTML. linesToHtml :: String -> Html linesToHtml str = forM_ (lines str) $ \line -> do toHtml line; br -- | Intercalate some HTML. htmlIntercalate :: Html -> [Html] -> Html htmlIntercalate _ [x] = x htmlIntercalate sep (x:xs) = do x; sep; htmlIntercalate sep xs htmlIntercalate _ [] = mempty -- | Make a list of html into a comma separated html. htmlCommas :: [Html] -> Html htmlCommas = htmlIntercalate ", " -- | Set a parameter of a URI, as an attribute. hrefSet :: URI -> String -> String -> Attribute hrefSet uri key value = hrefURI updated where updated = updateUrlParam key value uri -- | Provide a URI as an attribute for href. hrefURI :: URI -> Attribute hrefURI uri = href (toValue (showURI uri)) where showURI URI{..} = uriPath ++ uriQuery -- | Create a href from a a path and association list of parameters. hrefAssoc :: String -> [(String,String)] -> Attribute hrefAssoc path qs = href (toValue uri) where uri = "/" ++ path ++ "?" ++ intercalate "&" (map (uncurry (printf "%s=%s")) qs)