1(* Msp -- utilities for CGI scripts and ML Server Pages *)
2
3(* Efficiently concatenable word sequences *)
4
5datatype wseq =
6    Empty                               (* The empty sequence         *)
7  | Nl                                  (* Newline                    *)
8  | $ of string                         (* A string                   *)
9  | $$ of string list                   (* A sequence of strings      *)
10  | && of wseq * wseq;                  (* Concatenation of sequences *)
11
12(* Manipulating wseqs *)
13
14val prmap    : ('a -> wseq) -> 'a list -> wseq
15val prsep    : wseq -> ('a -> wseq) -> 'a list -> wseq
16val flatten  : wseq -> string
17val printseq : wseq -> unit
18val vec2list : 'a vector -> 'a list
19
20
21(* Shorthands for accessing CGI parameters *)
22
23exception ParamMissing of string
24exception NotInt of string * string
25
26val %        : string -> string
27val %?       : string -> bool
28val %#       : string -> int
29val %%       : string * string -> string
30val %%#      : string * int -> int
31
32
33(* HTML generic marks *)
34
35val mark0    : string -> wseq
36val mark0a   : string -> string -> wseq
37val mark1    : string -> wseq -> wseq
38val mark1a   : string -> string -> wseq -> wseq
39val comment  : wseq -> wseq
40
41(* HTML documents and headers *)
42
43val html     : wseq -> wseq
44val head     : wseq -> wseq
45val title    : wseq -> wseq
46val body     : wseq -> wseq
47val bodya    : string -> wseq -> wseq
48val htmldoc  : wseq -> wseq -> wseq
49
50(* HTML headings and vertical format *)
51
52val h1       : wseq -> wseq
53val h2       : wseq -> wseq
54val h3       : wseq -> wseq
55val h4       : wseq -> wseq
56val h5       : wseq -> wseq
57val h6       : wseq -> wseq
58val p        : wseq -> wseq
59val pa       : string -> wseq -> wseq
60val br       : wseq
61val bra      : string -> wseq
62val hr       : wseq
63val hra      : string -> wseq
64
65val divi        : wseq -> wseq
66val divia       : string -> wseq -> wseq
67val blockquote  : wseq -> wseq
68val blockquotea : string -> wseq -> wseq
69val center      : wseq -> wseq
70val address     : wseq -> wseq
71val pre         : wseq -> wseq
72
73(* HTML anchors and hyperlinks *)
74
75val ahref    : string -> wseq -> wseq
76val ahrefa   : string -> string -> wseq -> wseq
77val aname    : string -> wseq -> wseq
78
79(* HTML text formats and style *)
80
81val em       : wseq -> wseq
82val strong   : wseq -> wseq
83val tt       : wseq -> wseq
84val sub      : wseq -> wseq
85val sup      : wseq -> wseq
86val fonta    : string -> wseq -> wseq
87
88(* HTML lists *)
89
90val ul       : wseq -> wseq
91val ula      : string -> wseq -> wseq
92val ol       : wseq -> wseq
93val ola      : string -> wseq -> wseq
94val li       : wseq -> wseq
95val dl       : wseq -> wseq
96val dla      : string -> wseq -> wseq
97val dt       : wseq -> wseq
98val dd       : wseq -> wseq
99
100(* HTML tables *)
101
102val table    : wseq -> wseq
103val tablea   : string -> wseq -> wseq
104val tr       : wseq -> wseq
105val tra      : string -> wseq -> wseq
106val td       : wseq -> wseq
107val tda      : string -> wseq -> wseq
108val th       : wseq -> wseq
109val tha      : string -> wseq -> wseq
110val caption  : wseq -> wseq
111val captiona : string -> wseq -> wseq
112
113(* HTML images and image maps *)
114
115val img      : string -> wseq
116val imga     : string -> string -> wseq
117val map      : string -> wseq -> wseq
118val mapa     : string -> string -> wseq -> wseq
119val area     : { alt : string option, coords : string,
120                 href : string option, shape : string} -> wseq
121
122(* HTML forms etc *)
123
124val form       : string -> wseq -> wseq
125val forma      : string -> string -> wseq -> wseq
126val input      : string -> wseq
127val inputa     : string -> string -> wseq
128val intext     : string -> string -> wseq
129val inpassword : string -> string -> wseq
130val incheckbox : {name : string, value : string} -> string -> wseq
131val inradio    : {name : string, value : string} -> string -> wseq
132val inreset    : string -> string -> wseq
133val insubmit   : string -> string -> wseq
134val inhidden   : {name : string, value : string} -> wseq
135val textarea   : string -> wseq -> wseq
136val textareaa  : string -> string -> wseq -> wseq
137val select     : string -> string -> wseq -> wseq
138val option     : string -> wseq
139
140(* HTML frames and framesets *)
141
142val frameset   : string -> wseq -> wseq
143val frame      : { src : string, name : string } -> wseq
144val framea     : { src : string, name : string } -> string -> wseq
145
146(* HTML encoding  *)
147
148val urlencode  : string -> string
149val htmlencode : string -> string
150
151
152(*
153   This module provides support functions for writing CGI scripts and
154   ML Server Page scripts.
155
156   [wseq] is the type of efficiently concatenable word sequences.
157   Building an HTML page (functionally) as a wseq is more efficient
158   than building it (functionally) as a string, and more convenient
159   and modular than building it (imperatively) by calling print.
160
161   [Empty] represents the empty string "".
162
163   [Nl] represents the string "\n" consisting of a single newline character.
164
165   [$ s] represents the string s.
166
167   [$$ ss] represents the string String.concat(ss).
168
169   [&&(ws1, ws2)] represents the concatenation of the strings
170   represented by ws1 and ws2.  The function && should be declared
171        infix &&
172
173   [prmap f xs] is f x1 && ... && f xn evaluated from left to right,
174   when xs is [x1, ..., xn].
175
176   [prsep sep f xs] is f x1 && sep && ... && sep && f xn, evaluated
177   from left to right, when xs is [x1, ..., xn].
178
179   [flatten ws] is the string represented by ws.
180
181   [printseq ws] is equivalent to print(flatten ws), but avoids
182   building any new strings.
183
184   [vec2list vec] is a list of the elements of vector vec.  Use it to
185   convert e.g. the results of a database query into a list, for
186   processing with prmap or prsep.
187
188
189   Shorthands for accessing CGI parameters:
190
191   [%? fnm] returns true if there is a string associated with CGI
192   parameter fnm; otherwise returns false.
193
194   [% fnm] returns a string associated with CGI parameter fnm if there
195   is any; raises ParamMissing(fnm) if no strings are associated with
196   fnm.  Equivalent to
197       case Mosmlcgi.cgi_field_string fnm of
198           NONE   => raise ParamMissing "fnm"
199         | SOME v => v
200   In general, multiple strings may be associated with a CGI parameter;
201   use Mosmlcgi.cgi_field_strings if you need to access all of them.
202
203   [%# fnm] returns the integer i if there is a string associated with
204   CGI parameter fnm, and that string is parsable as ML integer i.
205   Raises ParamMissing(fnm) if no string is associated with fnm.
206   Raises NotInt(fnm, s) if there is a string but it is not parsable
207   as an ML int.
208
209   [%%(fnm, dflt)] returns a string associated with CGI parameter fnm
210   if there is any; otherwise returns the string dflt.
211
212   [%%#(fnm, dflt)] returns the integer i if there is a string
213   associated with CGI parameter fnm, and that string is parsable as
214   an ML int; otherwise returns the string dflt.
215
216
217   HTML generic marks:
218
219   [mark0 t] generates the HTML tag <t> as a wseq.
220
221   [mark0a attr t] generates the attributed HTML tag <t attr> as a wseq.
222
223   [mark1 t ws] generates  <t>ws</t>  as a wseq.
224
225   [mark1a attr t ws] generates  <t attr>ws</t> as a wseq.
226
227   [comment ws] generates  <!--ws-->  as a wseq.
228
229
230   HTML documents and headers:
231
232   [html ws] generates <HTML>ws</HTML>.
233
234   [head ws] generates <HEAD>ws</HEAD>.
235
236   [title ws] generates <TITLE>ws</TITLE>.
237
238   [body ws] generates <BODY>ws</BODY>.
239
240   [bodya attr ws] generates <BODY attr>ws</BODY>.
241
242   [htmldoc titl ws] generates
243   <HTML><HEAD><TITLE>titl</TITLE></HEAD><BODY>ws</BODY></HTML>.
244
245
246   HTML headings and vertical format:
247
248   [h1 ws] generates <H1>ws</H1>.
249
250   [p ws] generates <P>ws</P>.
251
252   [pa attr ws] generates <P attr>ws</P>.
253
254   [br] generates <BR>.
255
256   [bra attr] generates <BR attr>.
257
258   [hr] generates <HR>.
259
260   [hra attr] generates <HR attr>.
261
262   [divi ws] generates <DIV>ws</DIV>.
263
264   [divia attr ws] generates <DIV attr>ws</DIV>.
265
266   [blockquote ws] generates <BLOCKQUOTE>ws</BLOCKQUOTE>.
267
268   [blockquotea attr ws] generates <BLOCKQUOTE attr>ws</BLOCKQUOTE>
269
270   [center ws] generates <CENTER>ws</CENTER>.
271
272   [address ws] generates <ADDRESS>ws</ADDRESS>.
273
274   [pre ws] generates <PRE>ws</PRE>.
275
276
277   HTML anchors and hyperlinks:
278
279   [ahref link ws] generates <A HREF="link">ws</A>.
280
281   [ahrefa link attr ws] generates <A HREF="link" attr>ws</A>.
282
283   [aname nam ws] generates <A NAME="name">ws</A>.
284
285
286   HTML text formats and style:
287
288   [em ws] generates <EM>ws</EM>.
289
290   [strong ws] generates <STRONG>ws</STRONG>.
291
292   [tt ws] generates <TT>ws</TT>.
293
294   [sub ws] generates <SUB>ws</SUB>.
295
296   [sup ws] generates <SUP>ws</SUP>.
297
298   [fonta attr ws] generates <FONT attr>ws</FONT>.
299
300
301   HTML lists:
302
303   [ul ws] generates <UL>ws</UL>.
304
305   [ula attr ws] generates <UL attr>ws</UL>.
306
307   [ol ws] generates <OL>ws</OL>.
308
309   [ola attr ws] generates <OL attr>ws</OL>.
310
311   [li ws] generates <LI>ws</LI>.
312
313   [dl ws] generates <DL>ws</DL>.
314
315   [dla attr ws] generates <DL attr>ws</DL>.
316
317   [dt ws] generates <DT>ws</DT>.
318
319   [dd ws] generates <DD>ws</DD>.
320
321
322   HTML tables:
323
324   [table ws] generates <TABLE>ws</TABLE>.
325
326   [tablea attr ws] generates <TABLE attr>ws</TABLE>.
327
328   [tr ws] generates <TR>ws</TR>.
329
330   [tra attr ws] generates <TR attr>ws</TR>.
331
332   [td ws] generates <TD>ws</TD>.
333
334   [tda attr ws] generates <TD attr>ws</TD>.
335
336   [th ws] generates <TH>ws</TH>.
337
338   [tha attr ws] generates <TH attr>ws</TH>.
339
340   [caption ws] generates <CAPTION>ws</CAPTION>.
341
342   [captiona attr ws] generates <CAPTION attr>ws</CAPTION>.
343
344
345   HTML images and image maps:
346
347   [img s] generates <IMG SRC="s">.
348
349   [imga s attr] generates <IMG SRC="s" attr>.
350
351   [map nam ws] generates <MAP NAME="name">ws</MAP>.
352
353   [mapa nam attr ws] generates <MAP NAME="name" attr>ws</MAP>.
354
355   [area { alt, coords, href, shape}] generates
356       <AREA SHAPE="shape" COORDS="coords" HREF="link" ALT="desc">
357   when href is SOME link (where HREF is replaced by NOHREF otherwise)
358   and  alt  is SOME desc (where ALT is omitted otherwise).
359
360
361   HTML forms etc:
362
363   [form act ws] generates <FORM ACTION="act">ws</FORM>.
364
365   [forma act attr ws] generates <FORM ACTION="act" attr>ws</FORM>.
366
367   [input typ] generates <INPUT TYPE=typ>.
368
369   [inputa typ attr] generates <INPUT TYPE=typ attr>.
370
371   [intext name attr] generates <INPUT TYPE=TEXT NAME="name" attr>.
372
373   [inpassword name attr] generates <INPUT TYPE=PASSWORD NAME="name" attr>.
374
375   [incheckbox {name, value} attr] generates
376   <INPUT TYPE=CHECKBOX NAME="name" VALUE="value" attr>.
377
378   [inradio {name, value} attr] generates
379   <INPUT TYPE=RADIO NAME="name" VALUE="value" attr>.
380
381   [inreset value attr] generates <INPUT TYPE=RESET VALUE="value" attr>.
382
383   [insubmit value attr] generates <INPUT TYPE=SUBMIT VALUE="value" attr>.
384
385   [inhidden {name, value}] generates
386   <INPUT TYPE=HIDDEN NAME="name" VALUE="value">.
387
388   [textarea name ws] generates <TEXTAREA NAME="name">ws</TEXTAREA>.
389
390   [textareaa name attr ws] generates
391   <TEXTAREA NAME="name" attr>ws</TEXTAREA>.
392
393   [select name attr ws] generates <SELECT NAME="name" attr>ws</SELECT>.
394
395   [option value] generates <OPTION VALUE="value">.
396
397
398   HTML frames and framesets:
399
400   [frameset attr ws] generates <FRAMESET attr>ws</FRAMESET>.
401
402   [frame { src, name }] generates <FRAME SRC="src" NAME="name">.
403
404   [framea { src, name } attr] generates <FRAME SRC="src" NAME="name" attr>.
405
406
407   HTML encoding functions:
408
409   [urlencode s] returns the url-encoding of s.  That is, space (ASCII 32)
410   is replaced by `+' and every non-alphanumeric character c except
411   the three characters hyphen (-), underscore (_) and full stop (.)
412   is replaced by %hh, where hh is the hexadecimal representation of
413   the ASCII code of c.
414
415   [htmlencode s] returns the html-encoding of s.  That is, < and >
416   are replaced by &lt; and &gt; respectively, and & is replaced by
417   &amp;
418*)
419