module Text.XHtml.Table (HtmlTable, HTMLTABLE(..),
(</>), above, (<->), beside,
aboves, besides,
simpleTable) where
import Text.XHtml.Internals
import Text.XHtml.Strict.Elements
import Text.XHtml.Strict.Attributes
import qualified Text.XHtml.BlockTable as BT
infixr 3 </>
infixr 4 <->
class HTMLTABLE ht where
cell :: ht -> HtmlTable
instance HTMLTABLE HtmlTable where
cell = id
instance HTMLTABLE Html where
cell h =
let
cellFn x y = h ! (add x colspan $ add y rowspan $ [])
add 1 _ rest = rest
add n fn rest = fn n : rest
r = BT.single cellFn
in
mkHtmlTable r
newtype HtmlTable
= HtmlTable (BT.BlockTable (Int -> Int -> Html))
mkHtmlTable :: BT.BlockTable (Int -> Int -> Html) -> HtmlTable
mkHtmlTable r = HtmlTable r
(</>),above,(<->),beside :: (HTMLTABLE ht1,HTMLTABLE ht2)
=> ht1 -> ht2 -> HtmlTable
above a b = combine BT.above (cell a) (cell b)
(</>) = above
beside a b = combine BT.beside (cell a) (cell b)
(<->) = beside
combine :: (BT.BlockTable (Int -> Int -> Html) ->
BT.BlockTable (Int -> Int -> Html) ->
BT.BlockTable (Int -> Int -> Html))
-> HtmlTable
-> HtmlTable
-> HtmlTable
combine fn (HtmlTable a) (HtmlTable b) = mkHtmlTable (a `fn` b)
aboves :: (HTMLTABLE ht) => [ht] -> HtmlTable
aboves [] = error "aboves []"
aboves xs = foldr1 (</>) (map cell xs)
besides :: (HTMLTABLE ht) => [ht] -> HtmlTable
besides [] = error "besides []"
besides xs = foldr1 (<->) (map cell xs)
renderTable :: BT.BlockTable (Int -> Int -> Html) -> Html
renderTable theTable
= concatHtml
[tr << [theCell x y | (theCell,(x,y)) <- theRow ]
| theRow <- BT.getMatrix theTable]
instance HTML HtmlTable where
toHtml (HtmlTable tab) = renderTable tab
instance Show HtmlTable where
showsPrec _ (HtmlTable tab) = shows (renderTable tab)
simpleTable :: [HtmlAttr] -> [HtmlAttr] -> [[Html]] -> Html
simpleTable attr cellAttr lst
= table ! attr
<< (aboves
. map (besides . map ((td ! cellAttr) . toHtml))
) lst