1 -----------------------------------------------------------------------------
3 -- Module : Text.Html.BlockTable
4 -- Copyright : (c) Andy Gill and OGI, 1999-2001
5 -- License : BSD-style (see the file libraries/base/LICENSE)
7 -- Maintainer : Andy Gill <andy@galconn.com>
8 -- Stability : experimental
9 -- Portability : portable
11 -- An Html combinator library
13 -----------------------------------------------------------------------------
15 module Text.Html.BlockTable (
19 BlockTable, -- abstract
21 -- Contruction Functions:
28 -- Investigation Functions:
41 -- These combinators can be used to build formated 2D tables.
42 -- The specific target useage is for HTML table generation.
47 > table1 :: BlockTable String
48 > table1 = single "Hello" +-----+
50 This is a 1x1 cell +-----+
53 single :: a -> BlockTable a
55 So the cells can contain anything.
57 > table2 :: BlockTable String
58 > table2 = single "World" +-----+
63 > table3 :: BlockTable String
64 > table3 = table1 %-% table2 +-----%-----+
66 % is used to indicate +-----%-----+
70 > table4 :: BlockTable String
71 > table4 = table3 %/% table2 +-----+-----+
73 Notice the padding on the %%%%%%%%%%%%%
74 smaller (bottom) cell to |World |
75 force the table to be a +-----------+
78 > table5 :: BlockTable String
79 > table5 = table1 %-% table4 +-----%-----+-----+
81 Notice the padding on the | %-----+-----+
82 leftmost cell, again to | %World |
83 force the table to be a +-----%-----------+
86 Now the table can be rendered with processTable, for example:
87 Main> processTable table5
91 [("World",(2,1))]] :: [[([Char],(Int,Int))]]
95 -- ---------------------------------------------------------------------------
96 -- Contruction Functions
98 -- Perhaps one day I'll write the Show instance
99 -- to show boxes aka the above ascii renditions.
101 instance (Show a) => Show (BlockTable a) where
102 showsPrec _ = showsTable
104 type TableI a = [[(a,(Int,Int))]] -> [[(a,(Int,Int))]]
106 data BlockTable a = Table (Int -> Int -> TableI a) Int Int
109 -- You can create a (1x1) table entry
111 single :: a -> BlockTable a
112 single a = Table (\ x y r -> [(a,(x+1,y+1))] : r) 1 1
114 empty :: BlockTable a
115 empty = Table (\ _ _ r -> r) 0 0
118 -- You can compose tables, horizonally and vertically
120 above :: BlockTable a -> BlockTable a -> BlockTable a
121 beside :: BlockTable a -> BlockTable a -> BlockTable a
123 t1 `above` t2 = trans (combine (trans t1) (trans t2) (.))
125 t1 `beside` t2 = combine t1 t2 (\ lst1 lst2 r ->
127 -- Note this depends on the fact that
128 -- that the result has the same number
129 -- of lines as the y dimention; one list
130 -- per line. This is not true in general
131 -- but is always true for these combinators.
132 -- I should assert this!
133 -- I should even prove this.
134 beside' (x:xs) (y:ys) = (x ++ y) : beside' xs ys
135 beside' (x:xs) [] = x : xs ++ r
136 beside' [] (y:ys) = y : ys ++ r
139 beside' (lst1 []) (lst2 []))
141 -- trans flips (transposes) over the x and y axis of
142 -- the table. It is only used internally, and typically
143 -- in pairs, ie. (flip ... munge ... (un)flip).
145 trans :: BlockTable a -> BlockTable a
146 trans (Table f1 x1 y1) = Table (flip f1) y1 x1
148 combine :: BlockTable a
150 -> (TableI a -> TableI b -> TableI c)
152 combine (Table f1 x1 y1) (Table f2 x2 y2) comb = Table new_fn (x1+x2) max_y
156 case compare y1 y2 of
157 EQ -> comb (f1 0 y) (f2 x y)
158 GT -> comb (f1 0 y) (f2 x (y + y1 - y2))
159 LT -> comb (f1 0 (y + y2 - y1)) (f2 x y)
161 -- ---------------------------------------------------------------------------
162 -- Investigation Functions
164 -- This is the other thing you can do with a Table;
165 -- turn it into a 2D list, tagged with the (x,y)
166 -- sizes of each cell in the table.
168 getMatrix :: BlockTable a -> [[(a,(Int,Int))]]
169 getMatrix (Table r _ _) = r 0 0 []
171 -- You can also look at a table
173 showsTable :: (Show a) => BlockTable a -> ShowS
174 showsTable table = shows (getMatrix table)
176 showTable :: (Show a) => BlockTable a -> String
177 showTable table = showsTable table ""