Template language overhaul & misc. improvements
[bpt/mlt.git] / src / lib / web.sml
CommitLineData
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
22structure Web :> WEB =
23struct
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)
84
85 fun output () =
86 (TextIO.print "Status: 200\nContent-type: text/html\n\n";
87 TextIO.print (String.concat (List.rev (!text))))
8291a2b9
AC
88
89 val getCgi = OS.Process.getEnv
90
91 fun html s =
92 let
93 fun xch #"<" = "&lt;"
94 | xch #">" = "&gt;"
95 | xch #"&" = "&amp;"
96 | xch #"\"" = "&quot;"
97 | xch ch = str ch
98 in
99 foldr op^ "" (map xch (String.explode s))
100 end
101
102 fun htmlNl s =
103 let
104 fun xch #"<" = "&lt;"
105 | xch #">" = "&gt;"
106 | xch #"&" = "&amp;"
107 | xch #"\"" = "&quot;"
108 | xch #"\n" = "<br />"
109 | xch ch = str ch
110 in
111 foldr op^ "" (map xch (String.explode s))
112 end
113
114 exception Format of string
115
116 fun stoiOpt s = Int.fromString s
117 fun stoi s =
118 (case Int.fromString s of
119 NONE => raise Format s
120 | SOME i => i)
121
122 fun storOpt s = Real.fromString s
123 fun stor s =
124 (case Real.fromString s of
125 NONE => raise Format s
126 | SOME r => r)
127
128 fun summary () =
129 StringMap.foldli (fn (n, vs, s) => foldl (fn (v, s) => s ^ " VALUE: " ^ v) (s ^ " NAME: " ^ n) vs)
130 "" (!params)
c0a3b488 131end