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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
|
{-# OPTIONS_CYMAKE -Wno-incomplete-patterns #-}
module HTML.Base
( HtmlExp(..), textOf,
HtmlPage(..), PageParam(..),
HtmlFormDef(..), formDefWithID, formDefId,
CookieParam(..),
CgiRef, idOfCgiRef, instCgiRefs, CgiEnv, HtmlHandler,
defaultEncoding,
answerText, answerEncText,
getCookies,
page, standardPage,
pageEnc, pageCookie, pageCSS, pageMetaInfo,
pageLinkInfo, pageBodyAttr, addPageParam, addCookies, addHttpHeader,
htxt, htxts, hempty, nbsp, h1, h2, h3, h4, h5, h6,
par, section, header, footer, emphasize, strong, bold, italic, nav, code,
center, blink, teletype, pre, verbatim, address, href, anchor,
ulist, ulistWithClass, ulistWithItemClass,
olist, olistWithClass, olistWithItemClass,
litem, dlist,
table, tableWithClass, headedTable, addHeadings,
hrule, breakline, image,
styleSheet, style, textstyle, blockstyle, inline, block,
redirectPage, expires,
formExp,
button, resetButton, imageButton, coordinates,
textField, password, textArea, checkBox, checkedBox,
radioMain, radioMainOff, radioOther,
selection, selectionInitial, multipleSelection,
hiddenField, htmlQuote, htmlIsoUmlauts, addAttr, addAttrs, addClass,
showHtmlExps, showHtmlExp, showHtmlPage,
htmlPrelude, htmlTagAttrs,
getUrlParameter, urlencoded2string, string2urlencoded,
formatCookie
) where
import Char ( isAlphaNum, isSpace )
import ReadNumeric ( readNat, readHex )
import System ( getEnviron )
import Time ( CalendarTime(..), ClockTime, toTimeString, toUTCTime )
infixl 0 `addAttr`
infixl 0 `addAttrs`
infixl 0 `addClass`
infixl 0 `addPageParam`
defaultEncoding :: String
defaultEncoding = "utf-8"
data CgiRef = CgiRef String
idOfCgiRef :: CgiRef -> String
idOfCgiRef (CgiRef i) = i
type CgiEnv = CgiRef -> String
type HtmlHandler = CgiEnv -> IO HtmlPage
data HtmlExp =
HtmlText String
| HtmlStruct String [(String,String)] [HtmlExp]
| HtmlCRef HtmlExp CgiRef
| HtmlEvent HtmlExp CgiRef HtmlHandler
| HtmlAction (IO HtmlExp)
textOf :: [HtmlExp] -> String
textOf = unwords . filter (not . null) . map textOfHtmlExp
where
textOfHtmlExp (HtmlText s) = s
textOfHtmlExp (HtmlStruct _ _ hs) = textOf hs
textOfHtmlExp (HtmlCRef hexp _) = textOf [hexp]
textOfHtmlExp (HtmlEvent hexp _ _) = textOf [hexp]
textOfHtmlExp (HtmlAction _) = ""
data HtmlFormDef a = HtmlFormDef String (IO a) (a -> [HtmlExp])
formDefWithID :: String -> IO a -> (a -> [HtmlExp]) -> HtmlFormDef a
formDefWithID = HtmlFormDef
formDefId :: HtmlFormDef a -> String
formDefId (HtmlFormDef s _ _) = s
genInitForm :: HtmlFormDef a -> IO [HtmlExp]
genInitForm (HtmlFormDef _ readact formgen) = readact >>= return . formgen
instCgiRefs :: [HtmlExp] -> Int -> ([HtmlExp],Int)
instCgiRefs [] i = ([],i)
instCgiRefs (HtmlText s : hexps) i =
case instCgiRefs hexps i of
(nhexps,j) -> (HtmlText s : nhexps, j)
instCgiRefs (HtmlStruct tag attrs hexps1 : hexps2) i =
case instCgiRefs hexps1 i of
(nhexps1,j) -> case instCgiRefs hexps2 j of
(nhexps2,k) -> (HtmlStruct tag attrs nhexps1 : nhexps2, k)
instCgiRefs (HtmlEvent (HtmlStruct tag attrs hes) cgiref handler : hexps) i
| idOfCgiRef cgiref =:= ("FIELD_" ++ show i)
= case instCgiRefs hexps (i+1) of
(nhexps,j) ->
(HtmlEvent (HtmlStruct tag attrs hes) cgiref handler : nhexps, j)
instCgiRefs (HtmlCRef hexp cgiref : hexps) i
| idOfCgiRef cgiref =:= ("FIELD_" ++ show i)
= case instCgiRefs [hexp] (i+1) of
([nhexp],j) -> case instCgiRefs hexps j of
(nhexps,k) -> (nhexp : nhexps, k)
instCgiRefs (HtmlAction _ : _) _ =
error "HTML.Base.instCgiRefs: HtmlAction occurred"
data HtmlPage = HtmlPage String [PageParam] [HtmlExp]
| HtmlAnswer String String
data PageParam = PageEnc String
| PageCookie String String [CookieParam]
| PageCSS String
| String String
| PageJScript String
| PageMeta [(String,String)]
| PageLink [(String,String)]
| PageHeadInclude HtmlExp
| PageBodyAttr (String,String)
pageEnc :: String -> PageParam
pageEnc enc = PageEnc enc
pageCookie :: (String,String) -> PageParam
pageCookie (n,v) = PageCookie n v []
pageCSS :: String -> PageParam
pageCSS css = PageCSS css
httpHeader :: String -> String -> PageParam
k v = HttpHeader k v
pageMetaInfo :: [(String,String)] -> PageParam
pageMetaInfo attrs = PageMeta attrs
pageLinkInfo :: [(String,String)] -> PageParam
pageLinkInfo attrs = PageLink attrs
pageBodyAttr :: (String,String) -> PageParam
pageBodyAttr attr = PageBodyAttr attr
page :: String -> [HtmlExp] -> HtmlPage
page title hexps = HtmlPage title [PageEnc defaultEncoding] hexps
standardPage :: String -> [HtmlExp] -> HtmlPage
standardPage title hexps = page title (h1 [htxt title] : hexps)
addPageParam :: HtmlPage -> PageParam -> HtmlPage
addPageParam (HtmlPage title params hexps) param =
HtmlPage title (param:params) hexps
addPageParam hexp@(HtmlAnswer _ _) _ = hexp
addCookies :: [(String,String)] -> HtmlPage -> HtmlPage
addCookies cs (HtmlPage title params hexps) =
HtmlPage title (map pageCookie cs ++ params) hexps
addCookies _ (HtmlAnswer _ _) =
error "addCookies: cannot add cookie to HTML answer"
addHttpHeader :: String -> String -> HtmlPage -> HtmlPage
key value (HtmlPage t fas hs) =
HtmlPage t (HttpHeader key value : fas) hs
addHttpHeader _ _ (HtmlAnswer _ _) =
error "addHttpHeader: cannot add HTTP header to HTML answer"
data CookieParam = CookieExpire ClockTime
| CookieDomain String
| CookiePath String
| CookieSecure
formatCookie :: (String,String,[CookieParam]) -> String
formatCookie (name,value,params) =
"Set-Cookie: " ++ name ++ "=" ++ string2urlencoded value ++
concatMap (\p->"; "++formatCookieParam p) params
formatCookieParam :: CookieParam -> String
formatCookieParam (CookieExpire e) = "expires=" ++ toCookieDateString e
formatCookieParam (CookieDomain d) = "domain=" ++ d
formatCookieParam (CookiePath p) = "path=" ++ p
formatCookieParam CookieSecure = "secure"
toCookieDateString :: ClockTime -> String
toCookieDateString time =
let (CalendarTime y mo d h mi s tz) = toUTCTime time
in (show d ++ "-" ++ shortMonths!!(mo-1) ++ "-" ++ show y ++ " " ++
toTimeString (CalendarTime y mo d h mi s tz) ++ " UTC")
where shortMonths = ["Jan","Feb","Mar","Apr","May","Jun",
"Jul","Aug","Sep","Oct","Nov","Dec"]
answerText :: String -> HtmlPage
answerText = HtmlAnswer "text/plain"
answerEncText :: String -> String -> HtmlPage
answerEncText enc = HtmlAnswer ("text/plain; charset="++enc)
redirectPage :: String -> HtmlPage
redirectPage url = addHttpHeader "Location" url $ page "Redirect" []
expires :: Int -> HtmlPage -> HtmlPage
expires secs hpage =
hpage `addPageParam`
PageHeadInclude (HtmlStruct "meta" [("http-equiv","expires"),
("content",show secs)] [])
htxt :: String -> HtmlExp
htxt s = HtmlText (htmlQuote s)
htxts :: [String] -> [HtmlExp]
htxts = map htxt
hempty :: HtmlExp
hempty = HtmlText ""
nbsp :: HtmlExp
nbsp = HtmlText " "
h1 :: [HtmlExp] -> HtmlExp
h1 hexps = HtmlStruct "h1" [] hexps
h2 :: [HtmlExp] -> HtmlExp
h2 hexps = HtmlStruct "h2" [] hexps
h3 :: [HtmlExp] -> HtmlExp
h3 hexps = HtmlStruct "h3" [] hexps
h4 :: [HtmlExp] -> HtmlExp
h4 hexps = HtmlStruct "h4" [] hexps
h5 :: [HtmlExp] -> HtmlExp
h5 hexps = HtmlStruct "h5" [] hexps
h6 :: [HtmlExp] -> HtmlExp
h6 hexps = HtmlStruct "h6" [] hexps
par :: [HtmlExp] -> HtmlExp
par hexps = HtmlStruct "p" [] hexps
section :: [HtmlExp] -> HtmlExp
section hexps = HtmlStruct "section" [] hexps
header :: [HtmlExp] -> HtmlExp
hexps = HtmlStruct "header" [] hexps
footer :: [HtmlExp] -> HtmlExp
hexps = HtmlStruct "footer" [] hexps
emphasize :: [HtmlExp] -> HtmlExp
emphasize hexps = HtmlStruct "em" [] hexps
strong :: [HtmlExp] -> HtmlExp
strong hexps = HtmlStruct "strong" [] hexps
bold :: [HtmlExp] -> HtmlExp
bold hexps = HtmlStruct "b" [] hexps
italic :: [HtmlExp] -> HtmlExp
italic hexps = HtmlStruct "i" [] hexps
nav :: [HtmlExp] -> HtmlExp
nav doc = HtmlStruct "nav" [] doc
code :: [HtmlExp] -> HtmlExp
code hexps = HtmlStruct "code" [] hexps
center :: [HtmlExp] -> HtmlExp
center hexps = HtmlStruct "center" [] hexps
blink :: [HtmlExp] -> HtmlExp
blink hexps = HtmlStruct "blink" [] hexps
teletype :: [HtmlExp] -> HtmlExp
teletype hexps = HtmlStruct "tt" [] hexps
pre :: [HtmlExp] -> HtmlExp
pre hexps = HtmlStruct "pre" [] hexps
verbatim :: String -> HtmlExp
verbatim s = HtmlStruct "pre" [] [HtmlText (htmlQuote s)]
address :: [HtmlExp] -> HtmlExp
address hexps = HtmlStruct "address" [] hexps
href :: String -> [HtmlExp] -> HtmlExp
href ref hexps = HtmlStruct "a" [("href",ref)] hexps
anchor :: String -> [HtmlExp] -> HtmlExp
anchor anc hexps = HtmlStruct "span" [("id",anc)] hexps
ulist :: [[HtmlExp]] -> HtmlExp
ulist items = HtmlStruct "ul" [] (map litem items)
ulistWithClass :: String -> String -> [[HtmlExp]] -> HtmlExp
ulistWithClass listclass itemclass items =
HtmlStruct "ul" [] (map litemWC items) `addClass` listclass
where
litemWC i = litem i `addClass` itemclass
ulistWithItemClass :: String -> [(String,[HtmlExp])] -> HtmlExp
ulistWithItemClass listclass classeditems =
HtmlStruct "ul" [] (map litemWC classeditems) `addClass` listclass
where
litemWC (c,i) = litem i `addClass` c
olist :: [[HtmlExp]] -> HtmlExp
olist items = HtmlStruct "ol" [] (map litem items)
olistWithClass :: String -> String -> [[HtmlExp]] -> HtmlExp
olistWithClass listclass itemclass items =
HtmlStruct "ol" [] (map litemWC items) `addClass` listclass
where
litemWC i = litem i `addClass` itemclass
olistWithItemClass :: String -> [(String,[HtmlExp])] -> HtmlExp
olistWithItemClass listclass classeditems =
HtmlStruct "ol" [] (map litemWC classeditems) `addClass` listclass
where
litemWC (c,i) = litem i `addClass` c
litem :: [HtmlExp] -> HtmlExp
litem hexps = HtmlStruct "li" [] hexps
dlist :: [([HtmlExp],[HtmlExp])] -> HtmlExp
dlist items = HtmlStruct "dl" [] (concatMap ditem items)
where
ditem (hexps1,hexps2) = [HtmlStruct "dt" [] hexps1,
HtmlStruct "dd" [] hexps2]
table :: [[[HtmlExp]]] -> HtmlExp
table items = HtmlStruct "table" []
(map (\row->HtmlStruct "tr" []
(map (\item -> HtmlStruct "td" [] item) row)) items)
tableWithClass :: String -> String -> String -> [[[HtmlExp]]] -> HtmlExp
tableWithClass tableclass rowclass dataclass items =
HtmlStruct "table" []
(map (\row -> HtmlStruct "tr" []
(map (\d -> HtmlStruct "td" [] d `addClass` dataclass) row)
`addClass` rowclass)
items) `addClass` tableclass
headedTable :: [[[HtmlExp]]] -> HtmlExp
headedTable = withinTable . table
where
withinTable (HtmlStruct "table" attrs (HtmlStruct "tr" rowAttrs row:rows)) =
HtmlStruct "table" attrs
(HtmlStruct "tr" rowAttrs (map addTh row):rows)
addTh x = case x of
(HtmlStruct "td" attrs conts) -> HtmlStruct "th" attrs conts
other -> other
addHeadings :: HtmlExp -> [[HtmlExp]] -> HtmlExp
addHeadings htable headings = case htable of
HtmlStruct "table" attrs rows ->
HtmlStruct "table" attrs
(HtmlStruct "tr" [] (map (\item->HtmlStruct "th" [] item) headings):rows)
_ -> htable
hrule :: HtmlExp
hrule = HtmlStruct "hr" [] []
breakline :: HtmlExp
breakline = HtmlStruct "br" [] []
image :: String -> String -> HtmlExp
image src alt = HtmlStruct "img" [("src",src),("alt",htmlQuote alt)] []
styleSheet :: String -> HtmlExp
styleSheet css = HtmlStruct "style" [("type","text/css")] [HtmlText css]
style :: String -> [HtmlExp] -> HtmlExp
style st hexps = HtmlStruct "span" [("class",st)] hexps
textstyle :: String -> String -> HtmlExp
textstyle st txt = HtmlStruct "span" [("class",st)] [htxt txt]
blockstyle :: String -> [HtmlExp] -> HtmlExp
blockstyle st hexps = HtmlStruct "div" [("class",st)] hexps
inline :: [HtmlExp] -> HtmlExp
inline hexps = HtmlStruct "span" [] hexps
block :: [HtmlExp] -> HtmlExp
block hexps = HtmlStruct "div" [] hexps
formExp :: HtmlFormDef a -> HtmlExp
formExp formspec = HtmlAction htmlAction
where
htmlAction = do
urlparam <- getUrlParameter
he <- genInitForm formspec
return $
HtmlStruct "form" [("method","post"),("action",'?' : urlparam)]
(hiddenField "FORMID" (formDefId formspec) : fst (instCgiRefs he 0))
button :: String -> HtmlHandler -> HtmlExp
button label handler
| cref =:= CgiRef ref
= HtmlEvent (HtmlStruct "input" [("type","submit"), ("name",ref),
("value",htmlQuote label)] [])
cref handler
where
cref,ref free
resetButton :: String -> HtmlExp
resetButton label =
HtmlStruct "input" [("type","reset"),("value",htmlQuote label)] []
imageButton :: String -> HtmlHandler -> HtmlExp
imageButton src handler
| cref =:= CgiRef ref
= HtmlEvent
(HtmlStruct "input" [("type","image"),("name",ref),("src",src)] [])
cref handler
where
cref,ref free
textField :: CgiRef -> String -> HtmlExp
textField cref contents
| cref =:= CgiRef ref
= HtmlCRef
(HtmlStruct "input" [("type","text"),("name",ref),
("value",htmlQuote contents)] [])
cref
where ref free
password :: CgiRef -> HtmlExp
password cref
| cref =:= CgiRef ref
= HtmlCRef
(HtmlStruct "input" [("type","password"),("name",ref)] [])
cref
where
ref free
textArea :: CgiRef -> (Int,Int) -> String -> HtmlExp
textArea cref (height,width) contents
| cref =:= CgiRef ref
= HtmlCRef
(HtmlStruct "textarea" [("name",ref),
("rows",show height),("cols",show width)]
[htxt contents])
cref
where
ref free
checkBox :: CgiRef -> String -> HtmlExp
checkBox cref value
| cref =:= CgiRef ref
= HtmlCRef
(HtmlStruct "input" [("type","checkbox"),("name",ref),
("value",htmlQuote value)] [])
cref
where
ref free
checkedBox :: CgiRef -> String -> HtmlExp
checkedBox cref value
| cref =:= CgiRef ref
= HtmlCRef
(HtmlStruct "input" [("type","checkbox"),("name",ref),
("value",htmlQuote value),("checked","checked")] [])
cref
where
ref free
radioMain :: CgiRef -> String -> HtmlExp
radioMain cref value
| cref =:= CgiRef ref
= HtmlCRef
(HtmlStruct "input" [("type","radio"),("name",ref),
("value",htmlQuote value),("checked","yes")] [])
cref
where
ref free
radioMainOff :: CgiRef -> String -> HtmlExp
radioMainOff cref value
| cref =:= CgiRef ref
= HtmlCRef
(HtmlStruct "input" [("type","radio"),("name",ref),
("value",htmlQuote value)] [])
cref
where
ref free
radioOther :: CgiRef -> String -> HtmlExp
radioOther cref value
| cref =:= CgiRef ref
= HtmlStruct "input"
[("type","radio"),("name",ref),("value",htmlQuote value)] []
where
ref free
selection :: CgiRef -> [(String,String)] -> HtmlExp
selection cref menue
| cref =:= CgiRef ref
= HtmlCRef
(HtmlStruct "select" [("name",ref)]
((concat . map (\(n,v)->[HtmlStruct "option" [("value",v)] [htxt n]]))
menue))
cref
where
ref free
selectionInitial :: CgiRef -> [(String,String)] -> Int -> HtmlExp
selectionInitial cref sellist sel
| cref =:= CgiRef ref
= HtmlCRef (HtmlStruct "select" [("name",ref)] (selOption sellist sel))
cref
where
ref free
selOption [] _ = []
selOption ((n,v):nvs) i =
HtmlStruct "option"
([("value",v)] ++ if i==0 then [("selected","selected")] else [])
[htxt n] : selOption nvs (i-1)
multipleSelection :: CgiRef -> [(String,String,Bool)] -> HtmlExp
multipleSelection cref sellist
| cref =:= CgiRef ref
= HtmlCRef (HtmlStruct "select" [("name",ref),("multiple","multiple")]
(map selOption sellist))
cref
where
ref free
selOption (n,v,flag) =
HtmlStruct "option"
([("value",v)] ++ if flag then [("selected","selected")] else [])
[htxt n]
hiddenField :: String -> String -> HtmlExp
hiddenField name value =
HtmlStruct "input" [("type","hidden"),("name",name),("value",value)] []
htmlQuote :: String -> String
htmlQuote [] = []
htmlQuote (c:cs) | c=='<' = "<" ++ htmlQuote cs
| c=='>' = ">" ++ htmlQuote cs
| c=='&' = "&" ++ htmlQuote cs
| c=='"' = """ ++ htmlQuote cs
| otherwise = htmlIsoUmlauts [c] ++ htmlQuote cs
htmlIsoUmlauts :: String -> String
htmlIsoUmlauts [] = []
htmlIsoUmlauts (c:cs) | oc==228 = "ä" ++ htmlIsoUmlauts cs
| oc==246 = "ö" ++ htmlIsoUmlauts cs
| oc==252 = "ü" ++ htmlIsoUmlauts cs
| oc==196 = "Ä" ++ htmlIsoUmlauts cs
| oc==214 = "Ö" ++ htmlIsoUmlauts cs
| oc==220 = "Ü" ++ htmlIsoUmlauts cs
| oc==223 = "ß" ++ htmlIsoUmlauts cs
| oc==197 = "Å" ++ htmlIsoUmlauts cs
| oc==250 = "ú"++ htmlIsoUmlauts cs
| oc==237 = "í"++ htmlIsoUmlauts cs
| oc==225 = "á"++ htmlIsoUmlauts cs
| otherwise = c : htmlIsoUmlauts cs
where oc = ord c
addAttr :: HtmlExp -> (String,String) -> HtmlExp
addAttr hexp attr = addAttrs hexp [attr]
addAttrs :: HtmlExp -> [(String,String)] -> HtmlExp
addAttrs (HtmlText s) _ = HtmlText s
addAttrs (HtmlStruct tag attrs hexps) newattrs =
HtmlStruct tag (attrs ++ newattrs) hexps
addAttrs (HtmlEvent hexp cref handler) attrs =
HtmlEvent (addAttrs hexp attrs) cref handler
addAttrs (HtmlCRef hexp cref) attrs =
HtmlCRef (addAttrs hexp attrs) cref
addAttrs (HtmlAction act) _ = HtmlAction act
addClass :: HtmlExp -> String -> HtmlExp
addClass hexp cls | null cls = hexp
| otherwise = addAttr hexp ("class",cls)
type ShowS = String -> String
showString :: String -> String -> String
showString s = (s++)
showChar :: Char -> String -> String
showChar c = (c:)
nl :: String -> String
nl = showChar '\n'
concatS :: [a -> a] -> a -> a
concatS [] = id
concatS xs@(_:_) = foldr1 (\ f g -> f . g) xs
showHtmlExps :: [HtmlExp] -> String
showHtmlExps hexps = showsHtmlExps 0 hexps ""
getText :: HtmlExp -> String
getText (HtmlText s) = s
getText (HtmlStruct _ _ _) = ""
getText (HtmlEvent he _ _) = getText he
getText (HtmlCRef he _) = getText he
getText (HtmlAction _) = ""
getTag :: HtmlExp -> String
getTag (HtmlText _) = ""
getTag (HtmlStruct tag _ _) = tag
getTag (HtmlEvent he _ _) = getTag he
getTag (HtmlCRef he _) = getTag he
getTag (HtmlAction _) = ""
tagWithLn :: String -> Bool
tagWithLn t = t/="" &&
t `elem` ["br","p","li","ul","ol","dl","dt","dd","hr",
"h1","h2","h3","h4","h5","h6","div",
"html","title","head","body","link","meta","script",
"form","table","tr","td"]
showHtmlExp :: HtmlExp -> String
showHtmlExp hexp = showsHtmlExp 0 hexp ""
noEndTags :: [String]
noEndTags = ["img","input","link","meta"]
showsHtmlExp :: Int -> HtmlExp -> ShowS
showsHtmlExp _ (HtmlText s) = showString s
showsHtmlExp i (HtmlStruct tag attrs hexps) =
let maybeLn j = if tagWithLn tag then nl . showTab j else id
in maybeLn i .
(if null hexps && (null attrs || tag `elem` noEndTags)
then showsHtmlOpenTag tag attrs "/>"
else showsHtmlOpenTag tag attrs ">" . maybeLn (i+2) . showExps hexps .
maybeLn i . showString "</" . showString tag . showChar '>'
) . maybeLn i
where
showExps = if tag=="pre"
then concatS . map (showsHtmlExp 0)
else showsHtmlExps (i+2)
showsHtmlExp i (HtmlEvent hexp _ _) = showsHtmlExp i hexp
showsHtmlExp i (HtmlCRef hexp _) = showsHtmlExp i hexp
showsHtmlExp _ (HtmlAction _) =
error "HTML.Base.showsHtmlExp: HtmlAction occurred"
showsHtmlExps :: Int -> [HtmlExp] -> ShowS
showsHtmlExps _ [] = id
showsHtmlExps i (he:hes) = showsWithLnPrefix he . showsHtmlExps i hes
where
showsWithLnPrefix hexp = let s = getText hexp
in if s/="" && isSpace (head s)
then nl . showTab i . showString (tail s)
else showsHtmlExp i hexp
showTab :: Int -> String -> String
showTab n = showString (take n (repeat ' '))
showsHtmlOpenTag :: String -> [(String,String)] -> String -> ShowS
showsHtmlOpenTag tag attrs close =
showChar '<' . showString tag .
concatS (map attr2string attrs) . showString close
where
attr2string (attr,value) = showChar ' ' . showString attr .
showString "=\"" . encodeQuotes value . showChar '"'
encodeQuotes [] = id
encodeQuotes (c:cs) | c=='"' = showString """ . encodeQuotes cs
| otherwise = showChar c . encodeQuotes cs
showHtmlPage :: HtmlPage -> String
showHtmlPage (HtmlAnswer _ cont) = cont
showHtmlPage (HtmlPage title params html) =
htmlPrelude ++
showHtmlExp (HtmlStruct "html" htmlTagAttrs
[HtmlStruct "head" []
([HtmlStruct "title" [] [HtmlText (htmlQuote title)]] ++
concatMap param2html params),
HtmlStruct "body" bodyattrs html])
where
param2html (PageEnc enc) =
[HtmlStruct "meta" [("http-equiv","Content-Type"),
("content","text/html; charset="++enc)] []]
param2html (PageCookie _ _ _) = []
param2html (PageCSS css) =
[HtmlStruct "link" [("rel","stylesheet"),("type","text/css"),("href",css)]
[]]
param2html (HttpHeader _ _) = []
param2html (PageJScript js) =
[HtmlStruct "script" [("type","text/javascript"),("src",js)] []]
param2html (PageMeta attrs) = [HtmlStruct "meta" attrs []]
param2html (PageLink attrs) = [HtmlStruct "link" attrs []]
param2html (PageHeadInclude hexp) = [hexp]
param2html (PageBodyAttr _) = []
bodyattrs = [attr | (PageBodyAttr attr) <- params]
htmlPrelude :: String
htmlPrelude = "<!DOCTYPE html>\n"
htmlTagAttrs :: [(String,String)]
htmlTagAttrs = [("lang","en")]
getUrlParameter :: IO String
getUrlParameter = getEnviron "QUERY_STRING"
urlencoded2string :: String -> String
urlencoded2string [] = []
urlencoded2string (c:cs)
| c == '+' = ' ' : urlencoded2string cs
| c == '%' = chr (maybe 0 fst (readHex (take 2 cs)))
: urlencoded2string (drop 2 cs)
| otherwise = c : urlencoded2string cs
string2urlencoded :: String -> String
string2urlencoded [] = []
string2urlencoded (c:cs)
| isAlphaNum c = c : string2urlencoded cs
| c == ' ' = '+' : string2urlencoded cs
| otherwise = let oc = ord c
in '%' : int2hex(oc `div` 16) : int2hex(oc `mod` 16) : string2urlencoded cs
where
int2hex i = if i<10 then chr (ord '0' + i)
else chr (ord 'A' + i - 10)
getCookies :: IO [(String,String)]
getCookies = do
cookiestring <- getEnviron "HTTP_COOKIE"
return $ parseCookies cookiestring
parseCookies :: String -> [(String,String)]
parseCookies str =
if null str
then []
else let (c1,cs) = break (==';') str
in parseCookie c1 :
parseCookies (dropWhile (==' ') (if cs=="" then "" else tail cs))
where
parseCookie s = let (name,evalue) = break (=='=') s in
(name, if evalue=="" then "" else urlencoded2string (tail evalue))
coordinates :: CgiEnv -> Maybe (Int,Int)
coordinates env = let x = env (CgiRef "x")
y = env (CgiRef "y")
in if x/="" && y/=""
then Just (tryReadNat 0 x, tryReadNat 0 y)
else Nothing
where
tryReadNat d s = maybe d (\ (i,rs) -> if null rs then i else d) (readNat s)
|