DataHtml.hs 8.67 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
-------------------------------------------------------------------------------
-- $Id: DataHtml.hs,v 1.1 1999/11/12 11:54:17 simonmar Exp $
--
-- Copyright (c) 1999 Andy Gill
-------------------------------------------------------------------------------

module DataHtml (
	Html, HtmlName, HtmlAttr, HtmlTable,
	(+++), verbatim, {- tag, atag, -} noHtml, primHtml, 
	concatHtml, htmlStr, htmlLine,
	h1,h2,h3,h4,h5,h6, 	
	font, bold, anchor, header, body, theTitle, paragraph, italics,
	ul, tt,
	bar, meta, li,
	{- tr, int, percent -}
	color, bgcolor, href, name, title, height, width, align, valign,
	border, size, cellpadding, cellspacing,
	p, hr, copyright, spaceHtml, 
	renderHtml, 
	cellHtml, (+/+), above, (+-+), beside, aboves, besides,		
	renderTable, simpleTable, 
	) where

import qualified OptTable as OT

infixr 5 +++	-- appending Html
infixr 3 +/+	-- combining HtmlTable
infixr 4 +-+	-- combining HtmlTable

data Html
	= HtmlAppend Html Html		  -- Some Html, followed by more text
	| HtmlVerbatim Html		  -- Turn on or off smart formating
	| HtmlEmpty			  -- Nothing!
	| HtmlNestingTag HtmlName [HtmlAttr] Html
	| HtmlSimpleTag  HtmlName [HtmlAttr]
	| HtmlString String
		deriving (Show)

{-
 - A important property of Html is all strings inside the
 - structure are already in Html friendly format.
 - For example, use of >,etc.
 -}

type HtmlName	= String
type HtmlAttr	= (HtmlName,Either Int String)
type HtmlTable	= OT.OptTable (Int -> Int -> Html)

------------------------------------------------------------------------------
-- Interface
------------------------------------------------------------------------------

-- primitive combinators
(+++) 		:: Html -> Html 		-> Html
verbatim	:: Html 			-> Html
tag 		:: String -> [HtmlAttr] -> Html -> Html
atag 		:: String -> [HtmlAttr] 	-> Html
noHtml  	:: 				   Html
primHtml 	:: String 			-> Html

-- useful combinators
concatHtml 		:: [Html] 			-> Html
htmlStr, htmlLine 	:: String 			-> Html

-- html constructors
h1,h2,h3,h4,h5,h6 	:: [HtmlAttr] -> Html		-> Html
font, bold, anchor, 
 header, body, 
 theTitle, paragraph,
 italics, ul, tt	:: [HtmlAttr] -> Html		-> Html
bar, meta, li		:: [HtmlAttr]			-> Html

-- html attributes
str 			:: String -> String 		-> HtmlAttr
int 			:: String -> Int 		-> HtmlAttr
percent 		:: String -> Int 		-> HtmlAttr

color, bgcolor, href,
 name, title, height,
 width, align, valign	:: String			-> HtmlAttr

border, size,
 cellpadding,
 cellspacing		:: Int				-> HtmlAttr 

-- abbriviations

p			:: Html				-> Html
hr			::				   Html
copyright		::				   Html
spaceHtml		::				   Html

-- rendering
renderHtml 		:: Html -> String

-- html tables
cellHtml		:: [HtmlAttr] -> Html 		-> HtmlTable
(+/+),above,
 (+-+),beside 		:: HtmlTable -> HtmlTable 	-> HtmlTable
aboves, besides		:: [HtmlTable] 			-> HtmlTable
renderTable 		:: [HtmlAttr] -> HtmlTable 	-> Html
simpleTable 		:: [HtmlAttr] -> [HtmlAttr] -> [[Html]] 
							-> Html

------------------------------------------------------------------------------
-- Basic, primitive combinators

-- This is intentionally lazy in the second argument.
(HtmlAppend x y) +++ z = x +++ (y +++ z)
(HtmlEmpty)      +++ z = z
x                +++ z = HtmlAppend x z

verbatim 	= HtmlVerbatim
tag 		= HtmlNestingTag
atag 		= HtmlSimpleTag
noHtml 		= HtmlEmpty

-- This is not processed for special chars. 
-- It is used to output them, though!
primHtml 	= HtmlString

------------------------------------------------------------------------------
-- Useful Combinators

concatHtml = foldr (+++) noHtml
-- Processing Strings into Html friendly things.
-- This converts a string to an Html.
htmlStr = primHtml . htmlizeStr

-- This converts a string, but keeps spaces as non-line-breakable
htmlLine = primHtml . concat . map htmlizeChar2
   where 
	htmlizeChar2 ' ' = " "
	htmlizeChar2 c   = htmlizeChar c

-- Local Utilites
htmlizeStr :: String -> String
htmlizeStr = concat . map htmlizeChar

htmlizeChar :: Char -> String
htmlizeChar '<' = "&gt;"
htmlizeChar '>' = "&lt;"
htmlizeChar '&' = "&amb;"
htmlizeChar '"' = "&quot;"
htmlizeChar c   = [c]

------------------------------------------------------------------------------
-- Html Constructors
h n = tag ("h" ++ show n)

-- Isn't Haskell great!
[h1,h2,h3,h4,h5,h6] = map h [1..6]

-- tags
font   		= tag "font"
bold		= tag "b"
anchor		= tag "a"
header 		= tag "header"
body   		= tag "body"
theTitle	= tag "title"
paragraph	= tag "p"
italics		= tag "i"
ul		= tag "ul"
tt		= tag "tt"

bar		= atag "hr"
meta		= atag "meta"
li		= atag "li"

------------------------------------------------------------------------------
-- Html Attributes

-- note: the string is presumed to be formated for output
--str :: String -> String -> HtmlAttr
str n s = (n,Right s)

--int :: String -> Int -> HtmlAttr
int n v = (n,Left v)

--percent :: String -> Int -> HtmlAttr
percent n v = str n (show v ++ "%")

-- attributes
color   	= str "color"
bgcolor 	= str "bgcolor"
href		= str "href"
name		= str "name"
title		= str "tile"
height  	= str "height" 
width   	= str "width"
align   	= str "align"
valign  	= str "valign"

border  	= int "border" 
size    	= int "size"
cellpadding    	= int "cellpadding"
cellspacing    	= int "cellspacing"

------------------------------------------------------------------------------
-- abbriviations
p 		= paragraph []
hr		= atag "hr" []
copyright	= primHtml "&copy;"
spaceHtml 	= primHtml "&nbsp;"

------------------------------------------------------------------------------
-- Rendering

renderHtml html = renderHtml' html (Just 0) ++ footerMessage

footerMessage 
   = "\n<!-- Generated using the Haskell HTML generator package HaskHTML -->\n"

renderHtml' (HtmlAppend html1 html2) d
	= renderHtml' html1 d ++ renderHtml' html2 d
renderHtml' (HtmlVerbatim html1) d
	= renderHtml' html1 Nothing
renderHtml' (HtmlEmpty) d = ""
renderHtml' (HtmlSimpleTag name attr) d
	= renderTag True name attr d
renderHtml' (HtmlNestingTag name attr html) d
	= renderTag True name attr d ++ renderHtml' html (incDepth d) ++
	  renderTag False name [] d
renderHtml' (HtmlString str) _ = str

incDepth :: Maybe Int -> Maybe Int
incDepth = fmap (+4)

-- This prints the tags in 
renderTag :: Bool -> HtmlName -> [HtmlAttr] -> Maybe Int -> String
renderTag x name attrs n = start ++ base_spaces ++ open ++ name ++ rest attrs ++ ">"
  where
	open = if x then "<" else "</"
	(start,base_spaces,sep) = case n of
		 	      Nothing -> ("",""," ")
			      Just n ->  ("\n",replicate n ' ',"\n")
			
	rest []            = ""
	rest [(tag,val)]   = " " ++ tag ++ "=" ++ myShow val 
	rest (hd:tl)       = " " ++ showPair hd ++ sep ++
		  foldr1 (\ x y -> x ++ sep ++ y)
			 [ base_spaces ++ replicate (1 + length name + 1) ' ' 
				++ showPair p | p <- tl ]

	showPair :: HtmlAttr -> String
	showPair (tag,val) = tag ++ replicate (tagsz - length tag) ' ' ++ 
			" = " ++ myShow val 
	myShow (Left n) = show n
	myShow (Right s) = "\"" ++ s ++ "\""

	tagsz = maximum (map (length.fst) attrs)

------------------------------------------------------------------------------
-- Html table related things

cellHtml attr html = OT.single cellFn
    where
  	cellFn x y = tag "td" (addX x (addY y attr)) html
	addX 1 rest = rest
	addX n rest = int "colspan" n : rest
	addY 1 rest = rest
	addY n rest = int "rowspan" n : rest

above 	= OT.above
(+/+) 	= above
beside	= OT.beside
(+-+)	= beside

{-
 - Note: Both aboves and besides presume a non-empty list.
 -}

aboves = foldl1 (+/+)
besides = foldl1 (+-+)

-- renderTable takes the HtmlTable, and renders it back into
-- and Html object. The attributes are added to the outside
-- table tag.

renderTable attr theTable
 	= table [row [theCell x y | (theCell,(x,y)) <- theRow ] 
			| theRow <- OT.getMatrix theTable]
   where
	row :: [Html] -> Html
	row  = tag "tr" [] . concatHtml

	table :: [Html] -> Html
	table = tag "table" attr . concatHtml

-- If you cant be bothered with the above, then you
-- can build simple tables with this.
-- Just provide the attributes for the whole table,
-- attributes for the cells (same for every cell),
-- and a list of list of cell contents,
-- and this function will build the table for you.
-- It does presume that all the lists are non-empty,
-- and there is at least one list.
--  
-- Different length lists means that the last cell
-- gets padded. If you want more power, then
-- use the system above.

simpleTable attr cellAttr
	= renderTable attr 
	. aboves
	. map (besides . map (cellHtml cellAttr))

	
------------------------------------------------------------------------------