Initial revision
[bpt/mlt.git] / src / lib / web.sml
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
24 val params : string StringMap.map ref = ref StringMap.empty
25
26 val paramStack : string StringMap.map list ref = ref []
27
28 fun setParam (n, v) = params := StringMap.insert (!params, n, v)
29
30 fun getParam v =
31 (case StringMap.find (!params, v) of
32 NONE => ""
33 | SOME s => s)
34
35 fun pushParams nvs =
36 (paramStack := (!params) :: (!paramStack);
37 app setParam nvs)
38
39 fun popParams () =
40 (case !paramStack of
41 [] => raise Fail "Empty params stack in popParams"
42 | h::t => (params := h;
43 paramStack := t))
44
45 fun withParams f nvs =
46 (pushParams nvs;
47 ((f ()) handle ex => (popParams (); raise ex))
48 before popParams ())
49
50 val text = ref ([] : string list)
51
52 fun print x = text := x :: (!text)
53
54 fun output () =
55 (TextIO.print "Status: 200\nContent-type: text/html\n\n";
56 TextIO.print (String.concat (List.rev (!text))))
57 end