adamc@380
|
1 (* Copyright (c) 2008, Adam Chlipala
|
adamc@380
|
2 * All rights reserved.
|
adamc@380
|
3 *
|
adamc@380
|
4 * Redistribution and use in source and binary forms, with or without
|
adamc@380
|
5 * modification, are permitted provided that the following conditions are met:
|
adamc@380
|
6 *
|
adamc@380
|
7 * - Redistributions of source code must retain the above copyright notice,
|
adamc@380
|
8 * this list of conditions and the following disclaimer.
|
adamc@380
|
9 * - Redistributions in binary form must reproduce the above copyright notice,
|
adamc@380
|
10 * this list of conditions and the following disclaimer in the documentation
|
adamc@380
|
11 * and/or other materials provided with the distribution.
|
adamc@380
|
12 * - The names of contributors may not be used to endorse or promote products
|
adamc@380
|
13 * derived from this software without specific prior written permission.
|
adamc@380
|
14 *
|
adamc@380
|
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
adamc@380
|
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
adamc@380
|
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
adamc@380
|
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
adamc@380
|
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
adamc@380
|
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
adamc@380
|
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
adamc@380
|
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
adamc@380
|
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
adamc@380
|
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
adamc@380
|
25 * POSSIBILITY OF SUCH DAMAGE.
|
adamc@380
|
26 *)
|
adamc@380
|
27
|
adamc@380
|
28 structure Demo :> DEMO = struct
|
adamc@380
|
29
|
adamc@501
|
30 fun make {prefix, dirname, guided} =
|
adamc@380
|
31 let
|
adamc@380
|
32 val prose = OS.Path.joinDirFile {dir = dirname,
|
adamc@380
|
33 file = "prose"}
|
adamc@380
|
34 val inf = TextIO.openIn prose
|
adamc@380
|
35
|
adamc@380
|
36 val outDir = OS.Path.concat (dirname, "out")
|
adamc@380
|
37
|
adamc@380
|
38 val () = if OS.FileSys.access (outDir, []) then
|
adamc@380
|
39 ()
|
adamc@380
|
40 else
|
adamc@380
|
41 OS.FileSys.mkDir outDir
|
adamc@380
|
42
|
adamc@380
|
43 val fname = OS.Path.joinDirFile {dir = outDir,
|
adamc@380
|
44 file = "index.html"}
|
adamc@380
|
45
|
adamc@380
|
46 val out = TextIO.openOut fname
|
adamc@381
|
47 val () = (TextIO.output (out, "<frameset cols=\"10%,90%\">\n");
|
adamc@380
|
48 TextIO.output (out, "<frame src=\"demos.html\">\n");
|
adamc@380
|
49 TextIO.output (out, "<frame src=\"intro.html\" name=\"staging\">\n");
|
adamc@380
|
50 TextIO.output (out, "</frameset>\n");
|
adamc@380
|
51 TextIO.closeOut out)
|
adamc@380
|
52
|
adamc@380
|
53 val fname = OS.Path.joinDirFile {dir = outDir,
|
adamc@380
|
54 file = "demos.html"}
|
adamc@380
|
55
|
adamc@380
|
56 val demosOut = TextIO.openOut fname
|
adamc@381
|
57 val () = (TextIO.output (demosOut, "<html><body>\n\n");
|
adamc@380
|
58 TextIO.output (demosOut, "<li> <a target=\"staging\" href=\"intro.html\">Intro</a></li>\n\n"))
|
adamc@380
|
59
|
adamc@384
|
60 val fname = OS.Path.joinDirFile {dir = dirname,
|
adamc@384
|
61 file = "demo.urs"}
|
adamc@384
|
62 val ursOut = TextIO.openOut fname
|
adamc@384
|
63 val () = (TextIO.output (ursOut, "val main : unit -> transaction page\n");
|
adamc@384
|
64 TextIO.closeOut ursOut)
|
adamc@384
|
65
|
adamc@384
|
66 val fname = OS.Path.joinDirFile {dir = dirname,
|
adamc@384
|
67 file = "demo.ur"}
|
adamc@384
|
68 val urOut = TextIO.openOut fname
|
adamc@384
|
69 val () = TextIO.output (urOut, "fun main () = return <xml><body>\n")
|
adamc@384
|
70
|
adamc@380
|
71 fun mergeWith f (o1, o2) =
|
adamc@380
|
72 case (o1, o2) of
|
adamc@380
|
73 (NONE, _) => o2
|
adamc@380
|
74 | (_, NONE) => o1
|
adamc@380
|
75 | (SOME v1, SOME v2) => SOME (f (v1, v2))
|
adamc@380
|
76
|
adamc@380
|
77 fun combiner (combined : Compiler.job, urp : Compiler.job) = {
|
adamc@385
|
78 prefix = prefix,
|
adamc@380
|
79 database = mergeWith (fn (v1, v2) =>
|
adamc@380
|
80 if v1 = v2 then
|
adamc@380
|
81 v1
|
adamc@380
|
82 else
|
adamc@380
|
83 raise Fail "Different demos want to use different database strings")
|
adamc@380
|
84 (#database combined, #database urp),
|
adamc@380
|
85 sources = foldl (fn (file, files) =>
|
adamc@380
|
86 if List.exists (fn x => x = file) files then
|
adamc@380
|
87 files
|
adamc@380
|
88 else
|
adamc@380
|
89 files @ [file])
|
adamc@380
|
90 (#sources combined) (#sources urp),
|
adamc@380
|
91 exe = OS.Path.joinDirFile {dir = dirname,
|
adamc@380
|
92 file = "demo.exe"},
|
adamc@380
|
93 sql = SOME (OS.Path.joinDirFile {dir = dirname,
|
adamc@380
|
94 file = "demo.sql"}),
|
adamc@502
|
95 debug = false,
|
adamc@673
|
96 timeout = Int.max (#timeout combined, #timeout urp),
|
adamc@764
|
97 profile = false,
|
adamc@764
|
98 ffi = [],
|
adamc@764
|
99 link = [],
|
adamc@765
|
100 headers = [],
|
adamc@766
|
101 scripts = [],
|
adamc@765
|
102 clientToServer = [],
|
adamc@765
|
103 effectful = [],
|
adamc@765
|
104 clientOnly = [],
|
adamc@765
|
105 serverOnly = [],
|
adamc@765
|
106 jsFuncs = []
|
adamc@380
|
107 }
|
adamc@380
|
108
|
adamc@380
|
109 val parse = Compiler.run (Compiler.transform Compiler.parseUrp "Demo parseUrp")
|
adamc@380
|
110
|
adamc@380
|
111 fun capitalize "" = ""
|
adamc@380
|
112 | capitalize s = str (Char.toUpper (String.sub (s, 0)))
|
adamc@380
|
113 ^ String.extract (s, 1, NONE)
|
adamc@380
|
114
|
adamc@380
|
115 fun startUrp urp =
|
adamc@380
|
116 let
|
adamc@380
|
117 val base = OS.Path.base urp
|
adamc@380
|
118 val name = capitalize base
|
adamc@380
|
119
|
adamc@380
|
120 val () = (TextIO.output (demosOut, "<li> <a target=\"staging\" href=\"");
|
adamc@380
|
121 TextIO.output (demosOut, base);
|
adamc@380
|
122 TextIO.output (demosOut, ".html\">");
|
adamc@380
|
123 TextIO.output (demosOut, name);
|
adamc@380
|
124 TextIO.output (demosOut, "</a></li>\n"))
|
adamc@380
|
125
|
adamc@384
|
126 val () = (TextIO.output (urOut, " <li> <a link={");
|
adamc@384
|
127 TextIO.output (urOut, name);
|
adamc@384
|
128 TextIO.output (urOut, ".main ()}>");
|
adamc@384
|
129 TextIO.output (urOut, name);
|
adamc@384
|
130 TextIO.output (urOut, "</a></li>\n"))
|
adamc@384
|
131
|
adamc@380
|
132 val urp_file = OS.Path.joinDirFile {dir = dirname,
|
adamc@380
|
133 file = urp}
|
adamc@380
|
134
|
adamc@380
|
135 val out = OS.Path.joinBaseExt {base = base,
|
adamc@380
|
136 ext = SOME "html"}
|
adamc@380
|
137 val out = OS.Path.joinDirFile {dir = outDir,
|
adamc@380
|
138 file = out}
|
adamc@380
|
139 val out = TextIO.openOut out
|
adamc@380
|
140
|
adamc@501
|
141 val () = (TextIO.output (out, "<frameset rows=\"");
|
adamc@501
|
142 TextIO.output (out, if guided then
|
adamc@501
|
143 "*,100"
|
adamc@501
|
144 else
|
adamc@501
|
145 "50%,*");
|
adamc@501
|
146 TextIO.output (out, "\">\n");
|
adamc@380
|
147 TextIO.output (out, "<frame src=\"");
|
adamc@380
|
148 TextIO.output (out, prefix);
|
adamc@380
|
149 TextIO.output (out, "/");
|
adamc@380
|
150 TextIO.output (out, name);
|
adamc@380
|
151 TextIO.output (out, "/main\" name=\"showcase\">\n");
|
adamc@380
|
152 TextIO.output (out, "<frame src=\"");
|
adamc@380
|
153 TextIO.output (out, base);
|
adamc@380
|
154 TextIO.output (out, ".desc.html\">\n");
|
adamc@380
|
155 TextIO.output (out, "</frameset>\n");
|
adamc@380
|
156 TextIO.closeOut out)
|
adamc@380
|
157 val () = TextIO.closeOut out
|
adamc@380
|
158
|
adamc@380
|
159 val out = OS.Path.joinBaseExt {base = base,
|
adamc@380
|
160 ext = SOME "desc"}
|
adamc@380
|
161 val out = OS.Path.joinBaseExt {base = out,
|
adamc@380
|
162 ext = SOME "html"}
|
adamc@380
|
163 val out = TextIO.openOut (OS.Path.joinDirFile {dir = outDir,
|
adamc@380
|
164 file = out})
|
adamc@380
|
165 in
|
adamc@380
|
166 case parse (OS.Path.base urp_file) of
|
adamc@380
|
167 NONE => raise Fail ("Can't parse " ^ urp_file)
|
adamc@380
|
168 | SOME urpData =>
|
adamc@380
|
169 (TextIO.output (out, "<html><head>\n<title>");
|
adamc@380
|
170 TextIO.output (out, name);
|
adamc@380
|
171 TextIO.output (out, "</title>\n</head><body>\n\n<h1>");
|
adamc@380
|
172 TextIO.output (out, name);
|
adamc@380
|
173 TextIO.output (out, "</h1>\n\n<center>[ <a target=\"showcase\" href=\"");
|
adamc@382
|
174 TextIO.output (out, prefix);
|
adamc@382
|
175 TextIO.output (out, "/");
|
adamc@382
|
176 TextIO.output (out, name);
|
adamc@382
|
177 TextIO.output (out, "/main\">Application</a>");
|
adamc@382
|
178 TextIO.output (out, " | <a target=\"showcase\" href=\"");
|
adamc@380
|
179 TextIO.output (out, urp);
|
adamc@380
|
180 TextIO.output (out, ".html\"><tt>");
|
adamc@380
|
181 TextIO.output (out, urp);
|
adamc@380
|
182 TextIO.output (out, "</tt></a>");
|
adamc@380
|
183 app (fn file =>
|
adamc@380
|
184 let
|
adamc@380
|
185 fun ifEx s =
|
adamc@380
|
186 let
|
adamc@380
|
187 val src = OS.Path.joinBaseExt {base = file,
|
adamc@380
|
188 ext = SOME s}
|
adamc@380
|
189 val src' = OS.Path.file src
|
adamc@380
|
190 in
|
adamc@380
|
191 if OS.FileSys.access (src, []) then
|
adamc@380
|
192 (TextIO.output (out, " | <a target=\"showcase\" href=\"");
|
adamc@380
|
193 TextIO.output (out, src');
|
adamc@380
|
194 TextIO.output (out, ".html\"><tt>");
|
adamc@380
|
195 TextIO.output (out, src');
|
adamc@380
|
196 TextIO.output (out, "</tt></a>"))
|
adamc@380
|
197 else
|
adamc@380
|
198 ()
|
adamc@380
|
199 end
|
adamc@380
|
200 in
|
adamc@380
|
201 ifEx "urs";
|
adamc@380
|
202 ifEx "ur"
|
adamc@380
|
203 end) (#sources urpData);
|
adamc@380
|
204 TextIO.output (out, " ]</center>\n\n");
|
adamc@380
|
205
|
adamc@380
|
206 (urpData, out))
|
adamc@380
|
207 end
|
adamc@380
|
208
|
adamc@380
|
209 fun endUrp out =
|
adamc@380
|
210 (TextIO.output (out, "\n</body></html>\n");
|
adamc@380
|
211 TextIO.closeOut out)
|
adamc@380
|
212
|
adamc@380
|
213 fun readUrp (combined, out) =
|
adamc@380
|
214 let
|
adamc@380
|
215 fun finished () = endUrp out
|
adamc@380
|
216
|
adamc@380
|
217 fun readUrp' () =
|
adamc@380
|
218 case TextIO.inputLine inf of
|
adamc@384
|
219 NONE => (finished ();
|
adamc@384
|
220 combined)
|
adamc@380
|
221 | SOME line =>
|
adamc@380
|
222 if String.isSuffix ".urp\n" line then
|
adamc@380
|
223 let
|
adamc@380
|
224 val urp = String.substring (line, 0, size line - 1)
|
adamc@380
|
225 val (urpData, out) = startUrp urp
|
adamc@380
|
226 in
|
adamc@380
|
227 finished ();
|
adamc@380
|
228
|
adamc@380
|
229 readUrp (combiner (combined, urpData),
|
adamc@380
|
230 out)
|
adamc@380
|
231 end
|
adamc@380
|
232 else
|
adamc@380
|
233 (TextIO.output (out, line);
|
adamc@380
|
234 readUrp' ())
|
adamc@380
|
235 in
|
adamc@380
|
236 readUrp' ()
|
adamc@380
|
237 end
|
adamc@380
|
238
|
adamc@380
|
239 val indexFile = OS.Path.joinDirFile {dir = outDir,
|
adamc@380
|
240 file = "intro.html"}
|
adamc@380
|
241
|
adamc@380
|
242 val out = TextIO.openOut indexFile
|
adamc@380
|
243 val () = TextIO.output (out, "<html><head>\n<title>Ur/Web Demo</title>\n</head><body>\n\n")
|
adamc@380
|
244
|
adamc@380
|
245 fun readIndex () =
|
adamc@380
|
246 let
|
adamc@380
|
247 fun finished () = (TextIO.output (out, "\n</body></html>\n");
|
adamc@380
|
248 TextIO.closeOut out)
|
adamc@380
|
249 in
|
adamc@380
|
250 case TextIO.inputLine inf of
|
adamc@384
|
251 NONE => (finished ();
|
adamc@384
|
252 NONE)
|
adamc@380
|
253 | SOME line =>
|
adamc@380
|
254 if String.isSuffix ".urp\n" line then
|
adamc@380
|
255 let
|
adamc@380
|
256 val urp = String.substring (line, 0, size line - 1)
|
adamc@380
|
257 val (urpData, out) = startUrp urp
|
adamc@380
|
258 in
|
adamc@380
|
259 finished ();
|
adamc@380
|
260
|
adamc@384
|
261 SOME (readUrp (urpData,
|
adamc@384
|
262 out))
|
adamc@380
|
263 end
|
adamc@380
|
264 else
|
adamc@380
|
265 (TextIO.output (out, line);
|
adamc@380
|
266 readIndex ())
|
adamc@380
|
267 end
|
adamc@381
|
268
|
adamc@381
|
269 fun prettyPrint () =
|
adamc@381
|
270 let
|
adamc@381
|
271 val dir = Posix.FileSys.opendir dirname
|
adamc@381
|
272
|
adamc@381
|
273 fun loop () =
|
adamc@381
|
274 case Posix.FileSys.readdir dir of
|
adamc@381
|
275 NONE => Posix.FileSys.closedir dir
|
adamc@381
|
276 | SOME file =>
|
adamc@381
|
277 let
|
adamc@381
|
278 fun doit f =
|
adamc@381
|
279 f (OS.Path.joinDirFile {dir = dirname,
|
adamc@381
|
280 file = file},
|
adamc@382
|
281 OS.Path.mkAbsolute
|
adamc@382
|
282 {relativeTo = OS.FileSys.getDir (),
|
adamc@382
|
283 path = OS.Path.joinDirFile {dir = outDir,
|
adamc@382
|
284 file = OS.Path.joinBaseExt {base = file,
|
adamc@382
|
285 ext = SOME "html"}}})
|
adamc@382
|
286
|
adamc@382
|
287 fun highlight () =
|
adamc@382
|
288 doit (fn (src, html) =>
|
adamc@382
|
289 let
|
adamc@410
|
290 val dirty =
|
adamc@410
|
291 let
|
adamc@410
|
292 val srcSt = Posix.FileSys.stat src
|
adamc@410
|
293 val htmlSt = Posix.FileSys.stat html
|
adamc@410
|
294 in
|
adamc@410
|
295 Time.> (Posix.FileSys.ST.mtime srcSt,
|
adamc@410
|
296 Posix.FileSys.ST.mtime htmlSt)
|
adamc@410
|
297 end handle OS.SysErr _ => true
|
adamc@410
|
298
|
adamc@382
|
299 val cmd = "emacs --eval \"(progn "
|
adamc@382
|
300 ^ "(global-font-lock-mode t) "
|
adamc@382
|
301 ^ "(add-to-list 'load-path \\\""
|
adamc@382
|
302 ^ Config.sitelisp
|
adamc@382
|
303 ^ "/\\\") "
|
adamc@382
|
304 ^ "(load \\\"urweb-mode-startup\\\") "
|
adamc@382
|
305 ^ "(urweb-mode) "
|
adamc@382
|
306 ^ "(find-file \\\""
|
adamc@382
|
307 ^ src
|
adamc@382
|
308 ^ "\\\") "
|
adamc@382
|
309 ^ "(switch-to-buffer (htmlize-buffer)) "
|
adamc@382
|
310 ^ "(write-file \\\""
|
adamc@382
|
311 ^ html
|
adamc@382
|
312 ^ "\\\") "
|
adamc@382
|
313 ^ "(kill-emacs))\""
|
adamc@382
|
314 in
|
adamc@410
|
315 if dirty then
|
adamc@410
|
316 (print (">>> " ^ cmd ^ "\n");
|
adamc@410
|
317 ignore (OS.Process.system cmd))
|
adamc@410
|
318 else
|
adamc@410
|
319 ()
|
adamc@382
|
320 end)
|
adamc@381
|
321 in
|
adamc@384
|
322 if OS.Path.base file = "demo" then
|
adamc@384
|
323 ()
|
adamc@384
|
324 else case OS.Path.ext file of
|
adamc@384
|
325 SOME "urp" =>
|
adamc@384
|
326 doit (fn (src, html) =>
|
adamc@384
|
327 let
|
adamc@384
|
328 val inf = TextIO.openIn src
|
adamc@384
|
329 val out = TextIO.openOut html
|
adamc@381
|
330
|
adamc@384
|
331 fun loop () =
|
adamc@384
|
332 case TextIO.inputLine inf of
|
adamc@384
|
333 NONE => ()
|
adamc@384
|
334 | SOME line => (TextIO.output (out, line);
|
adamc@384
|
335 loop ())
|
adamc@384
|
336 in
|
adamc@384
|
337 TextIO.output (out, "<html><body>\n\n<pre>");
|
adamc@384
|
338 loop ();
|
adamc@384
|
339 TextIO.output (out, "</pre>\n\n</body></html>");
|
adamc@381
|
340
|
adamc@384
|
341 TextIO.closeIn inf;
|
adamc@384
|
342 TextIO.closeOut out
|
adamc@384
|
343 end)
|
adamc@384
|
344 | SOME "urs" => highlight ()
|
adamc@384
|
345 | SOME "ur" => highlight ()
|
adamc@384
|
346 | _ => ();
|
adamc@381
|
347 loop ()
|
adamc@381
|
348 end
|
adamc@381
|
349 in
|
adamc@381
|
350 loop ()
|
adamc@381
|
351 end
|
adamc@380
|
352 in
|
adamc@384
|
353 case readIndex () of
|
adamc@384
|
354 NONE => raise Fail "No demo applications!"
|
adamc@384
|
355 | SOME combined =>
|
adamc@384
|
356 let
|
adamc@384
|
357 val () = (TextIO.output (urOut, "</body></xml>\n");
|
adamc@384
|
358 TextIO.closeOut urOut)
|
adamc@384
|
359
|
adamc@384
|
360 val fname = OS.Path.joinDirFile {dir = dirname,
|
adamc@384
|
361 file = "demo.urp"}
|
adamc@384
|
362 val outf = TextIO.openOut fname
|
adamc@384
|
363 in
|
adamc@384
|
364 Option.app (fn db => (TextIO.output (outf, "database ");
|
adamc@384
|
365 TextIO.output (outf, db);
|
adamc@384
|
366 TextIO.output (outf, "\n")))
|
adamc@384
|
367 (#database combined);
|
adamc@384
|
368 TextIO.output (outf, "sql demo.sql\n");
|
adamc@385
|
369 TextIO.output (outf, "prefix ");
|
adamc@385
|
370 TextIO.output (outf, prefix);
|
adamc@385
|
371 TextIO.output (outf, "\n");
|
adamc@384
|
372 TextIO.output (outf, "\n");
|
adamc@384
|
373
|
adamc@384
|
374 app (fn s =>
|
adamc@384
|
375 let
|
adamc@384
|
376 val s = OS.Path.mkAbsolute {relativeTo = OS.FileSys.getDir (),
|
adamc@384
|
377 path = s}
|
adamc@384
|
378 in
|
adamc@384
|
379 TextIO.output (outf, s);
|
adamc@384
|
380 TextIO.output (outf, "\n")
|
adamc@384
|
381 end)
|
adamc@384
|
382 (#sources combined);
|
adamc@384
|
383 TextIO.output (outf, "\n");
|
adamc@384
|
384 TextIO.output (outf, "demo\n");
|
adamc@384
|
385
|
adamc@384
|
386 TextIO.closeOut outf;
|
adamc@384
|
387
|
adamc@384
|
388 Compiler.compile (OS.Path.base fname)
|
adamc@384
|
389 end;
|
adamc@380
|
390
|
adamc@381
|
391 TextIO.output (demosOut, "\n</body></html>\n");
|
adamc@381
|
392 TextIO.closeOut demosOut;
|
adamc@381
|
393
|
adamc@381
|
394 prettyPrint ()
|
adamc@380
|
395 end
|
adamc@380
|
396
|
adamc@380
|
397 end
|