{-# LANGUAGE OverloadedStrings #-}

module Blaze.Html5
  (module Text.Blaze
  ,module Text.Blaze.Html5
  ,Attributable
  ,(!.)
  ,(!#)
  ,linesToHtml)
  where

import           Data.Monoid
import           Text.Blaze
import           Text.Blaze.Html5 hiding (map)
import qualified Text.Blaze.Html5.Attributes as A
import           Text.Blaze.Internal (Attributable)

-- | Class attribute.
(!.) :: (Attributable h) => h -> AttributeValue -> h
elem !. className = elem ! A.class_ className

-- | Id attribute.
(!#) :: (Attributable h) => h -> AttributeValue -> h
elem !# idName = elem ! A.id idName

-- | Render the lines as HTML lines.
linesToHtml :: [Html] -> Html
linesToHtml = htmlIntercalate br

-- | Intercalate the given things.
htmlIntercalate :: Html -> [Html] -> Html
htmlIntercalate _ [x] = x
htmlIntercalate sep (x:xs) = do x; sep; htmlIntercalate sep xs
htmlIntercalate _ []  = mempty

-- | Show some HTML comma-separated with “and” inbetween to be grammatical.
htmlCommasAnd :: [Html] -> Html
htmlCommasAnd [x] = x
htmlCommasAnd [x,y] = do x; " and "; y
htmlCommasAnd (x:xs) = do x; ", "; htmlCommasAnd xs
htmlCommasAnd []  = mempty

-- | Comma-separate some HTML.
htmlCommas :: [Html] -> Html
htmlCommas = htmlIntercalate ", "