1 -----------------------------------------------------------------------------
3 -- Module : Text.Html.BlockTable
4 -- Copyright : (c) Andy Gill, and the Oregon Graduate Institute of
5 -- Science and Technology, 1999-2001
6 -- License : BSD-style (see the file libraries/base/LICENSE)
8 -- Maintainer : Andy Gill <andy@galconn.com>
9 -- Stability : experimental
10 -- Portability : portable
12 -- An Html combinator library
14 -----------------------------------------------------------------------------
16 module Text.Html.BlockTable (
20 BlockTable, -- abstract
22 -- 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 p = 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 z -> [(a,(x+1,y+1))] : z) 1 1
115 -- You can compose tables, horizonally and vertically
117 above :: BlockTable a -> BlockTable a -> BlockTable a
118 beside :: BlockTable a -> BlockTable a -> BlockTable a
120 t1 `above` t2 = trans (combine (trans t1) (trans t2) (.))
122 t1 `beside` t2 = combine t1 t2 (\ lst1 lst2 r ->
124 -- Note this depends on the fact that
125 -- that the result has the same number
126 -- of lines as the y dimention; one list
127 -- per line. This is not true in general
128 -- but is always true for these combinators.
129 -- I should assert this!
130 -- I should even prove this.
131 beside (x:xs) (y:ys) = (x ++ y) : beside xs ys
132 beside (x:xs) [] = x : xs ++ r
133 beside [] (y:ys) = y : ys ++ r
136 beside (lst1 []) (lst2 []))
138 -- trans flips (transposes) over the x and y axis of
139 -- the table. It is only used internally, and typically
140 -- in pairs, ie. (flip ... munge ... (un)flip).
142 trans :: BlockTable a -> BlockTable a
143 trans (Table f1 x1 y1) = Table (flip f1) y1 x1
145 combine :: BlockTable a
147 -> (TableI a -> TableI b -> TableI c)
149 combine (Table f1 x1 y1) (Table f2 x2 y2) comb = Table new_fn (x1+x2) max_y
153 case compare y1 y2 of
154 EQ -> comb (f1 0 y) (f2 x y)
155 GT -> comb (f1 0 y) (f2 x (y + y1 - y2))
156 LT -> comb (f1 0 (y + y2 - y1)) (f2 x y)
158 -- ---------------------------------------------------------------------------
159 -- Investigation Functions
161 -- This is the other thing you can do with a Table;
162 -- turn it into a 2D list, tagged with the (x,y)
163 -- sizes of each cell in the table.
165 getMatrix :: BlockTable a -> [[(a,(Int,Int))]]
166 getMatrix (Table r _ _) = r 0 0 []
168 -- You can also look at a table
170 showsTable :: (Show a) => BlockTable a -> ShowS
171 showsTable table = shows (getMatrix table)
173 showTable :: (Show a) => BlockTable a -> String
174 showTable table = showsTable table ""