Commit | Line | Data |
---|---|---|
c0a3b488 AC |
1 | (* |
2 | * Dynamic web page generation with Standard ML | |
3 | * Copyright (C) 2003 Adam Chlipala | |
4 | * | |
5 | * This library is free software; you can redistribute it and/or | |
6 | * modify it under the terms of the GNU Lesser General Public | |
7 | * License as published by the Free Software Foundation; either | |
8 | * version 2.1 of the License, or (at your option) any later version. | |
9 | * | |
10 | * This library is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | * Lesser General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU Lesser General Public | |
16 | * License along with this library; if not, write to the Free Software | |
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 | *) | |
19 | ||
20 | (* CGI interaction *) | |
21 | ||
22 | structure Web :> WEB = | |
23 | struct | |
8291a2b9 AC |
24 | fun for f (r1, r2) = |
25 | if r1 < r2 then | |
26 | let | |
27 | fun loop i = | |
28 | if i > r2 then | |
29 | () | |
30 | else | |
31 | (f i; | |
32 | loop (i+1)) | |
33 | in | |
34 | loop r1 | |
35 | end | |
36 | else | |
37 | let | |
38 | fun loop i = | |
39 | if i < r2 then | |
40 | () | |
41 | else | |
42 | (f i; | |
43 | loop (i-1)) | |
44 | in | |
45 | loop r1 | |
46 | end | |
c0a3b488 | 47 | |
8291a2b9 AC |
48 | val params : string list StringMap.map ref = ref StringMap.empty |
49 | ||
50 | val paramStack : string list StringMap.map list ref = ref [] | |
c0a3b488 AC |
51 | |
52 | fun setParam (n, v) = params := StringMap.insert (!params, n, v) | |
8291a2b9 | 53 | fun setSingleParam (n, v) = setParam (n, [v]) |
c0a3b488 AC |
54 | |
55 | fun getParam v = | |
56 | (case StringMap.find (!params, v) of | |
57 | NONE => "" | |
8291a2b9 AC |
58 | | SOME [] => "" |
59 | | SOME (s::_) => s) | |
60 | ||
61 | fun getMultiParam v = | |
62 | (case StringMap.find (!params, v) of | |
63 | NONE => [] | |
64 | | SOME l => l) | |
c0a3b488 AC |
65 | |
66 | fun pushParams nvs = | |
67 | (paramStack := (!params) :: (!paramStack); | |
68 | app setParam nvs) | |
69 | ||
70 | fun popParams () = | |
71 | (case !paramStack of | |
72 | [] => raise Fail "Empty params stack in popParams" | |
73 | | h::t => (params := h; | |
74 | paramStack := t)) | |
75 | ||
76 | fun withParams f nvs = | |
77 | (pushParams nvs; | |
78 | ((f ()) handle ex => (popParams (); raise ex)) | |
79 | before popParams ()) | |
80 | ||
81 | val text = ref ([] : string list) | |
82 | ||
83 | fun print x = text := x :: (!text) | |
b26ce3d9 AC |
84 | fun clear () = text := [] |
85 | fun noOutput () = !text = [] | |
c0a3b488 AC |
86 | fun output () = |
87 | (TextIO.print "Status: 200\nContent-type: text/html\n\n"; | |
88 | TextIO.print (String.concat (List.rev (!text)))) | |
8291a2b9 AC |
89 | |
90 | val getCgi = OS.Process.getEnv | |
91 | ||
92 | fun html s = | |
93 | let | |
94 | fun xch #"<" = "<" | |
95 | | xch #">" = ">" | |
96 | | xch #"&" = "&" | |
97 | | xch #"\"" = """ | |
98 | | xch ch = str ch | |
99 | in | |
100 | foldr op^ "" (map xch (String.explode s)) | |
101 | end | |
102 | ||
103 | fun htmlNl s = | |
104 | let | |
105 | fun xch #"<" = "<" | |
106 | | xch #">" = ">" | |
107 | | xch #"&" = "&" | |
108 | | xch #"\"" = """ | |
109 | | xch #"\n" = "<br />" | |
110 | | xch ch = str ch | |
111 | in | |
112 | foldr op^ "" (map xch (String.explode s)) | |
113 | end | |
114 | ||
115 | exception Format of string | |
116 | ||
117 | fun stoiOpt s = Int.fromString s | |
118 | fun stoi s = | |
119 | (case Int.fromString s of | |
120 | NONE => raise Format s | |
121 | | SOME i => i) | |
122 | ||
123 | fun storOpt s = Real.fromString s | |
124 | fun stor s = | |
125 | (case Real.fromString s of | |
126 | NONE => raise Format s | |
127 | | SOME r => r) | |
128 | ||
129 | fun summary () = | |
130 | StringMap.foldli (fn (n, vs, s) => foldl (fn (v, s) => s ^ " VALUE: " ^ v) (s ^ " NAME: " ^ n) vs) | |
131 | "" (!params) | |
b26ce3d9 AC |
132 | |
133 | val exn = ref (NONE : exn option) | |
134 | fun setExn ex = exn := SOME ex | |
135 | fun getExn () = valOf (!exn) | |
c0a3b488 | 136 | end |