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@380
|
30 fun make {prefix, dirname} =
|
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 demo_urp = OS.Path.joinDirFile {dir = dirname,
|
adamc@380
|
37 file = "demo.urp"}
|
adamc@380
|
38
|
adamc@380
|
39 val outDir = OS.Path.concat (dirname, "out")
|
adamc@380
|
40
|
adamc@380
|
41 val () = if OS.FileSys.access (outDir, []) then
|
adamc@380
|
42 ()
|
adamc@380
|
43 else
|
adamc@380
|
44 OS.FileSys.mkDir outDir
|
adamc@380
|
45
|
adamc@380
|
46 val fname = OS.Path.joinDirFile {dir = outDir,
|
adamc@380
|
47 file = "index.html"}
|
adamc@380
|
48
|
adamc@380
|
49 val out = TextIO.openOut fname
|
adamc@381
|
50 val () = (TextIO.output (out, "<frameset cols=\"10%,90%\">\n");
|
adamc@380
|
51 TextIO.output (out, "<frame src=\"demos.html\">\n");
|
adamc@380
|
52 TextIO.output (out, "<frame src=\"intro.html\" name=\"staging\">\n");
|
adamc@380
|
53 TextIO.output (out, "</frameset>\n");
|
adamc@380
|
54 TextIO.closeOut out)
|
adamc@380
|
55
|
adamc@380
|
56 val fname = OS.Path.joinDirFile {dir = outDir,
|
adamc@380
|
57 file = "demos.html"}
|
adamc@380
|
58
|
adamc@380
|
59 val demosOut = TextIO.openOut fname
|
adamc@381
|
60 val () = (TextIO.output (demosOut, "<html><body>\n\n");
|
adamc@380
|
61 TextIO.output (demosOut, "<li> <a target=\"staging\" href=\"intro.html\">Intro</a></li>\n\n"))
|
adamc@380
|
62
|
adamc@380
|
63 fun mergeWith f (o1, o2) =
|
adamc@380
|
64 case (o1, o2) of
|
adamc@380
|
65 (NONE, _) => o2
|
adamc@380
|
66 | (_, NONE) => o1
|
adamc@380
|
67 | (SOME v1, SOME v2) => SOME (f (v1, v2))
|
adamc@380
|
68
|
adamc@380
|
69 fun combiner (combined : Compiler.job, urp : Compiler.job) = {
|
adamc@380
|
70 database = mergeWith (fn (v1, v2) =>
|
adamc@380
|
71 if v1 = v2 then
|
adamc@380
|
72 v1
|
adamc@380
|
73 else
|
adamc@380
|
74 raise Fail "Different demos want to use different database strings")
|
adamc@380
|
75 (#database combined, #database urp),
|
adamc@380
|
76 sources = foldl (fn (file, files) =>
|
adamc@380
|
77 if List.exists (fn x => x = file) files then
|
adamc@380
|
78 files
|
adamc@380
|
79 else
|
adamc@380
|
80 files @ [file])
|
adamc@380
|
81 (#sources combined) (#sources urp),
|
adamc@380
|
82 exe = OS.Path.joinDirFile {dir = dirname,
|
adamc@380
|
83 file = "demo.exe"},
|
adamc@380
|
84 sql = SOME (OS.Path.joinDirFile {dir = dirname,
|
adamc@380
|
85 file = "demo.sql"}),
|
adamc@380
|
86 debug = false
|
adamc@380
|
87 }
|
adamc@380
|
88
|
adamc@380
|
89 val parse = Compiler.run (Compiler.transform Compiler.parseUrp "Demo parseUrp")
|
adamc@380
|
90
|
adamc@380
|
91 fun capitalize "" = ""
|
adamc@380
|
92 | capitalize s = str (Char.toUpper (String.sub (s, 0)))
|
adamc@380
|
93 ^ String.extract (s, 1, NONE)
|
adamc@380
|
94
|
adamc@380
|
95 fun startUrp urp =
|
adamc@380
|
96 let
|
adamc@380
|
97 val base = OS.Path.base urp
|
adamc@380
|
98 val name = capitalize base
|
adamc@380
|
99
|
adamc@380
|
100 val () = (TextIO.output (demosOut, "<li> <a target=\"staging\" href=\"");
|
adamc@380
|
101 TextIO.output (demosOut, base);
|
adamc@380
|
102 TextIO.output (demosOut, ".html\">");
|
adamc@380
|
103 TextIO.output (demosOut, name);
|
adamc@380
|
104 TextIO.output (demosOut, "</a></li>\n"))
|
adamc@380
|
105
|
adamc@380
|
106 val urp_file = OS.Path.joinDirFile {dir = dirname,
|
adamc@380
|
107 file = urp}
|
adamc@380
|
108
|
adamc@380
|
109 val out = OS.Path.joinBaseExt {base = base,
|
adamc@380
|
110 ext = SOME "html"}
|
adamc@380
|
111 val out = OS.Path.joinDirFile {dir = outDir,
|
adamc@380
|
112 file = out}
|
adamc@380
|
113 val out = TextIO.openOut out
|
adamc@380
|
114
|
adamc@383
|
115 val () = (TextIO.output (out, "<frameset rows=\"50%,*\">\n");
|
adamc@380
|
116 TextIO.output (out, "<frame src=\"");
|
adamc@380
|
117 TextIO.output (out, prefix);
|
adamc@380
|
118 TextIO.output (out, "/");
|
adamc@380
|
119 TextIO.output (out, name);
|
adamc@380
|
120 TextIO.output (out, "/main\" name=\"showcase\">\n");
|
adamc@380
|
121 TextIO.output (out, "<frame src=\"");
|
adamc@380
|
122 TextIO.output (out, base);
|
adamc@380
|
123 TextIO.output (out, ".desc.html\">\n");
|
adamc@380
|
124 TextIO.output (out, "</frameset>\n");
|
adamc@380
|
125 TextIO.closeOut out)
|
adamc@380
|
126 val () = TextIO.closeOut out
|
adamc@380
|
127
|
adamc@380
|
128 val out = OS.Path.joinBaseExt {base = base,
|
adamc@380
|
129 ext = SOME "desc"}
|
adamc@380
|
130 val out = OS.Path.joinBaseExt {base = out,
|
adamc@380
|
131 ext = SOME "html"}
|
adamc@380
|
132 val out = TextIO.openOut (OS.Path.joinDirFile {dir = outDir,
|
adamc@380
|
133 file = out})
|
adamc@380
|
134 in
|
adamc@380
|
135 case parse (OS.Path.base urp_file) of
|
adamc@380
|
136 NONE => raise Fail ("Can't parse " ^ urp_file)
|
adamc@380
|
137 | SOME urpData =>
|
adamc@380
|
138 (TextIO.output (out, "<html><head>\n<title>");
|
adamc@380
|
139 TextIO.output (out, name);
|
adamc@380
|
140 TextIO.output (out, "</title>\n</head><body>\n\n<h1>");
|
adamc@380
|
141 TextIO.output (out, name);
|
adamc@380
|
142 TextIO.output (out, "</h1>\n\n<center>[ <a target=\"showcase\" href=\"");
|
adamc@382
|
143 TextIO.output (out, prefix);
|
adamc@382
|
144 TextIO.output (out, "/");
|
adamc@382
|
145 TextIO.output (out, name);
|
adamc@382
|
146 TextIO.output (out, "/main\">Application</a>");
|
adamc@382
|
147 TextIO.output (out, " | <a target=\"showcase\" href=\"");
|
adamc@380
|
148 TextIO.output (out, urp);
|
adamc@380
|
149 TextIO.output (out, ".html\"><tt>");
|
adamc@380
|
150 TextIO.output (out, urp);
|
adamc@380
|
151 TextIO.output (out, "</tt></a>");
|
adamc@380
|
152 app (fn file =>
|
adamc@380
|
153 let
|
adamc@380
|
154 fun ifEx s =
|
adamc@380
|
155 let
|
adamc@380
|
156 val src = OS.Path.joinBaseExt {base = file,
|
adamc@380
|
157 ext = SOME s}
|
adamc@380
|
158 val src' = OS.Path.file src
|
adamc@380
|
159 in
|
adamc@380
|
160 if OS.FileSys.access (src, []) then
|
adamc@380
|
161 (TextIO.output (out, " | <a target=\"showcase\" href=\"");
|
adamc@380
|
162 TextIO.output (out, src');
|
adamc@380
|
163 TextIO.output (out, ".html\"><tt>");
|
adamc@380
|
164 TextIO.output (out, src');
|
adamc@380
|
165 TextIO.output (out, "</tt></a>"))
|
adamc@380
|
166 else
|
adamc@380
|
167 ()
|
adamc@380
|
168 end
|
adamc@380
|
169 in
|
adamc@380
|
170 ifEx "urs";
|
adamc@380
|
171 ifEx "ur"
|
adamc@380
|
172 end) (#sources urpData);
|
adamc@380
|
173 TextIO.output (out, " ]</center>\n\n");
|
adamc@380
|
174
|
adamc@380
|
175 (urpData, out))
|
adamc@380
|
176 end
|
adamc@380
|
177
|
adamc@380
|
178 fun endUrp out =
|
adamc@380
|
179 (TextIO.output (out, "\n</body></html>\n");
|
adamc@380
|
180 TextIO.closeOut out)
|
adamc@380
|
181
|
adamc@380
|
182 fun readUrp (combined, out) =
|
adamc@380
|
183 let
|
adamc@380
|
184 fun finished () = endUrp out
|
adamc@380
|
185
|
adamc@380
|
186 fun readUrp' () =
|
adamc@380
|
187 case TextIO.inputLine inf of
|
adamc@380
|
188 NONE => finished ()
|
adamc@380
|
189 | SOME line =>
|
adamc@380
|
190 if String.isSuffix ".urp\n" line then
|
adamc@380
|
191 let
|
adamc@380
|
192 val urp = String.substring (line, 0, size line - 1)
|
adamc@380
|
193 val (urpData, out) = startUrp urp
|
adamc@380
|
194 in
|
adamc@380
|
195 finished ();
|
adamc@380
|
196
|
adamc@380
|
197 readUrp (combiner (combined, urpData),
|
adamc@380
|
198 out)
|
adamc@380
|
199 end
|
adamc@380
|
200 else
|
adamc@380
|
201 (TextIO.output (out, line);
|
adamc@380
|
202 readUrp' ())
|
adamc@380
|
203 in
|
adamc@380
|
204 readUrp' ()
|
adamc@380
|
205 end
|
adamc@380
|
206
|
adamc@380
|
207 val indexFile = OS.Path.joinDirFile {dir = outDir,
|
adamc@380
|
208 file = "intro.html"}
|
adamc@380
|
209
|
adamc@380
|
210 val out = TextIO.openOut indexFile
|
adamc@380
|
211 val () = TextIO.output (out, "<html><head>\n<title>Ur/Web Demo</title>\n</head><body>\n\n")
|
adamc@380
|
212
|
adamc@380
|
213 fun readIndex () =
|
adamc@380
|
214 let
|
adamc@380
|
215 fun finished () = (TextIO.output (out, "\n</body></html>\n");
|
adamc@380
|
216 TextIO.closeOut out)
|
adamc@380
|
217 in
|
adamc@380
|
218 case TextIO.inputLine inf of
|
adamc@380
|
219 NONE => finished ()
|
adamc@380
|
220 | SOME line =>
|
adamc@380
|
221 if String.isSuffix ".urp\n" line then
|
adamc@380
|
222 let
|
adamc@380
|
223 val urp = String.substring (line, 0, size line - 1)
|
adamc@380
|
224 val (urpData, out) = startUrp urp
|
adamc@380
|
225 in
|
adamc@380
|
226 finished ();
|
adamc@380
|
227
|
adamc@380
|
228 readUrp (urpData,
|
adamc@380
|
229 out)
|
adamc@380
|
230 end
|
adamc@380
|
231 else
|
adamc@380
|
232 (TextIO.output (out, line);
|
adamc@380
|
233 readIndex ())
|
adamc@380
|
234 end
|
adamc@381
|
235
|
adamc@381
|
236 fun prettyPrint () =
|
adamc@381
|
237 let
|
adamc@381
|
238 val dir = Posix.FileSys.opendir dirname
|
adamc@381
|
239
|
adamc@381
|
240 fun loop () =
|
adamc@381
|
241 case Posix.FileSys.readdir dir of
|
adamc@381
|
242 NONE => Posix.FileSys.closedir dir
|
adamc@381
|
243 | SOME file =>
|
adamc@381
|
244 let
|
adamc@381
|
245 fun doit f =
|
adamc@381
|
246 f (OS.Path.joinDirFile {dir = dirname,
|
adamc@381
|
247 file = file},
|
adamc@382
|
248 OS.Path.mkAbsolute
|
adamc@382
|
249 {relativeTo = OS.FileSys.getDir (),
|
adamc@382
|
250 path = OS.Path.joinDirFile {dir = outDir,
|
adamc@382
|
251 file = OS.Path.joinBaseExt {base = file,
|
adamc@382
|
252 ext = SOME "html"}}})
|
adamc@382
|
253
|
adamc@382
|
254 fun highlight () =
|
adamc@382
|
255 doit (fn (src, html) =>
|
adamc@382
|
256 let
|
adamc@382
|
257 val cmd = "emacs --eval \"(progn "
|
adamc@382
|
258 ^ "(global-font-lock-mode t) "
|
adamc@382
|
259 ^ "(add-to-list 'load-path \\\""
|
adamc@382
|
260 ^ Config.sitelisp
|
adamc@382
|
261 ^ "/\\\") "
|
adamc@382
|
262 ^ "(load \\\"urweb-mode-startup\\\") "
|
adamc@382
|
263 ^ "(urweb-mode) "
|
adamc@382
|
264 ^ "(find-file \\\""
|
adamc@382
|
265 ^ src
|
adamc@382
|
266 ^ "\\\") "
|
adamc@382
|
267 ^ "(switch-to-buffer (htmlize-buffer)) "
|
adamc@382
|
268 ^ "(write-file \\\""
|
adamc@382
|
269 ^ html
|
adamc@382
|
270 ^ "\\\") "
|
adamc@382
|
271 ^ "(kill-emacs))\""
|
adamc@382
|
272 in
|
adamc@382
|
273 print (">>> " ^ cmd ^ "\n");
|
adamc@382
|
274 ignore (OS.Process.system cmd)
|
adamc@382
|
275 end)
|
adamc@381
|
276 in
|
adamc@381
|
277 case OS.Path.ext file of
|
adamc@381
|
278 SOME "urp" =>
|
adamc@381
|
279 doit (fn (src, html) =>
|
adamc@381
|
280 let
|
adamc@381
|
281 val inf = TextIO.openIn src
|
adamc@381
|
282 val out = TextIO.openOut html
|
adamc@381
|
283
|
adamc@381
|
284 fun loop () =
|
adamc@381
|
285 case TextIO.inputLine inf of
|
adamc@381
|
286 NONE => ()
|
adamc@381
|
287 | SOME line => (TextIO.output (out, line);
|
adamc@381
|
288 loop ())
|
adamc@381
|
289 in
|
adamc@383
|
290 TextIO.output (out, "<html><body>\n\n<pre>");
|
adamc@381
|
291 loop ();
|
adamc@381
|
292 TextIO.output (out, "</pre>\n\n</body></html>");
|
adamc@381
|
293
|
adamc@381
|
294 TextIO.closeIn inf;
|
adamc@381
|
295 TextIO.closeOut out
|
adamc@381
|
296 end)
|
adamc@382
|
297 | SOME "urs" => highlight ()
|
adamc@382
|
298 | SOME "ur" => highlight ()
|
adamc@381
|
299 | _ => ();
|
adamc@381
|
300 loop ()
|
adamc@381
|
301 end
|
adamc@381
|
302 in
|
adamc@381
|
303 loop ()
|
adamc@381
|
304 end
|
adamc@380
|
305 in
|
adamc@380
|
306 readIndex ();
|
adamc@380
|
307
|
adamc@381
|
308 TextIO.output (demosOut, "\n</body></html>\n");
|
adamc@381
|
309 TextIO.closeOut demosOut;
|
adamc@381
|
310
|
adamc@381
|
311 prettyPrint ()
|
adamc@380
|
312 end
|
adamc@380
|
313
|
adamc@380
|
314 end
|