adam@1493
|
1 (* Copyright (c) 2011, Adam Chlipala
|
adam@1493
|
2 * All rights reserved.
|
adam@1493
|
3 *
|
adam@1493
|
4 * Redistribution and use in source and binary forms, with or without
|
adam@1493
|
5 * modification, are permitted provided that the following conditions are met:
|
adam@1493
|
6 *
|
adam@1493
|
7 * - Redistributions of source code must retain the above copyright notice,
|
adam@1493
|
8 * this list of conditions and the following disclaimer.
|
adam@1493
|
9 * - Redistributions in binary form must reproduce the above copyright notice,
|
adam@1493
|
10 * this list of conditions and the following disclaimer in the documentation
|
adam@1493
|
11 * and/or other materials provided with the distribution.
|
adam@1493
|
12 * - The names of contributors may not be used to endorse or promote products
|
adam@1493
|
13 * derived from this software without specific prior written permission.
|
adam@1493
|
14 *
|
adam@1493
|
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
adam@1493
|
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
adam@1493
|
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
adam@1493
|
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
adam@1493
|
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
adam@1493
|
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
adam@1493
|
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
adam@1493
|
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
adam@1493
|
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
adam@1493
|
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
adam@1493
|
25 * POSSIBILITY OF SUCH DAMAGE.
|
adam@1493
|
26 *)
|
adam@1493
|
27
|
adam@1493
|
28 structure Tutorial :> TUTORIAL = struct
|
adam@1493
|
29
|
adam@1494
|
30 fun readAll inf =
|
adam@1493
|
31 let
|
adam@1493
|
32 fun loop acc =
|
adam@1493
|
33 case TextIO.inputLine inf of
|
adam@1493
|
34 NONE => Substring.full (String.concat (rev acc))
|
adam@1493
|
35 | SOME line => loop (line :: acc)
|
adam@1493
|
36 in
|
adam@1493
|
37 loop []
|
adam@1493
|
38 before TextIO.closeIn inf
|
adam@1493
|
39 end
|
adam@1493
|
40
|
adam@1494
|
41 val readAllFile = readAll o TextIO.openIn
|
adam@1494
|
42
|
adam@1494
|
43 fun fixupFile (fname, title) =
|
adam@1494
|
44 let
|
adam@1494
|
45 val source = readAllFile "/tmp/final.html"
|
adam@1494
|
46 val outf = TextIO.openOut (OS.Path.mkAbsolute {relativeTo = OS.FileSys.getDir (),
|
adam@1494
|
47 path = OS.Path.joinBaseExt {base = OS.Path.base fname, ext = SOME "html"}})
|
adam@1494
|
48
|
adam@1494
|
49 val (befor, after) = Substring.position "<title>" source
|
adam@1494
|
50
|
adam@1494
|
51 fun loop source =
|
adam@1494
|
52 let
|
adam@1494
|
53 val (befor, after) = Substring.position "<span class=\"comment-delimiter\">(* </span><span class=\"comment\">" source
|
adam@1494
|
54 in
|
adam@1494
|
55 if Substring.isEmpty after then
|
adam@1494
|
56 TextIO.outputSubstr (outf, source)
|
adam@1494
|
57 else
|
adam@1494
|
58 let
|
adam@1494
|
59 val (befor', after) = Substring.position " </span><span class=\"comment-delimiter\">*)</span>"
|
adam@1494
|
60 (Substring.slice (after, 64, NONE))
|
adam@1494
|
61 in
|
adam@1494
|
62 if Substring.isEmpty after then
|
adam@1494
|
63 TextIO.outputSubstr (outf, source)
|
adam@1494
|
64 else
|
adam@1494
|
65 (TextIO.outputSubstr (outf, befor);
|
adam@1494
|
66 TextIO.output (outf, "<div class=\"prose\">");
|
adam@1494
|
67 TextIO.outputSubstr (outf, befor');
|
adam@1494
|
68 TextIO.output (outf, "</div>");
|
adam@1494
|
69 loop (Substring.slice (after, 49, NONE)))
|
adam@1494
|
70 end
|
adam@1494
|
71 end
|
adam@1494
|
72 in
|
adam@1494
|
73 if Substring.isEmpty after then
|
adam@1494
|
74 raise Fail ("Missing <title> for " ^ title)
|
adam@1494
|
75 else
|
adam@1494
|
76 (TextIO.outputSubstr (outf, befor);
|
adam@1494
|
77 TextIO.output (outf, "<style type=\"text/css\">\n");
|
adam@1494
|
78 TextIO.output (outf, "<!--\n");
|
adam@1494
|
79 TextIO.output (outf, "\tdiv.prose {\n");
|
adam@1494
|
80 TextIO.output (outf, "\t\tfont-family: Arial;\n");
|
adam@1494
|
81 TextIO.output (outf, "\t\tbackground-color: #CCFFCC;\n");
|
adam@1494
|
82 TextIO.output (outf, "\t\tborder-style: solid;\n");
|
adam@1494
|
83 TextIO.output (outf, "\t\tpadding: 5px;\n");
|
adam@1494
|
84 TextIO.output (outf, "\t\tfont-size: larger;\n");
|
adam@1494
|
85 TextIO.output (outf, "\t}\n");
|
adam@1494
|
86 TextIO.output (outf, "-->\n");
|
adam@1494
|
87 TextIO.output (outf, "</style>\n");
|
adam@1494
|
88 TextIO.output (outf, "<title>");
|
adam@1494
|
89 TextIO.output (outf, title);
|
adam@1494
|
90 let
|
adam@1494
|
91 val (befor, after) = Substring.position "</title>" after
|
adam@1494
|
92 in
|
adam@1494
|
93 if Substring.isEmpty after then
|
adam@1494
|
94 raise Fail ("Missing </title> for " ^ title)
|
adam@1494
|
95 else
|
adam@1494
|
96 let
|
adam@1494
|
97 val (befor, after) = Substring.position "<body>" after
|
adam@1494
|
98 in
|
adam@1494
|
99 if Substring.isEmpty after then
|
adam@1494
|
100 raise Fail ("Missing <body> for " ^ title)
|
adam@1494
|
101 else
|
adam@1494
|
102 (TextIO.outputSubstr (outf, befor);
|
adam@1494
|
103 TextIO.output (outf, "<body><h1>");
|
adam@1494
|
104 TextIO.output (outf, title);
|
adam@1494
|
105 TextIO.output (outf, "</h1>");
|
adam@1494
|
106 loop (Substring.slice (after, 6, NONE)))
|
adam@1494
|
107 end
|
adam@1494
|
108 end;
|
adam@1494
|
109 TextIO.closeOut outf)
|
adam@1494
|
110 end
|
adam@1493
|
111
|
adam@1493
|
112 fun doUr fname =
|
adam@1493
|
113 let
|
adam@1494
|
114 val inf = TextIO.openIn fname
|
adam@1494
|
115
|
adam@1494
|
116 val title = case TextIO.inputLine inf of
|
adam@1494
|
117 NONE => raise Fail ("No title comment at start of " ^ fname)
|
adam@1494
|
118 | SOME title => title
|
adam@1494
|
119
|
adam@1494
|
120 val title = String.substring (title, 3, size title - 7)
|
adam@1494
|
121
|
adam@1493
|
122 val eval = TextIO.openOut "/tmp/eval.ur"
|
adam@1493
|
123 val gen = TextIO.openOut "/tmp/gen.ur"
|
adam@1493
|
124
|
adam@1493
|
125 fun untilEnd source =
|
adam@1493
|
126 let
|
adam@1493
|
127 val (befor, after) = Substring.position "(* end *)" source
|
adam@1493
|
128 in
|
adam@1493
|
129 if Substring.isEmpty after then
|
adam@1493
|
130 (source, Substring.full "")
|
adam@1493
|
131 else
|
adam@1493
|
132 (befor, Substring.slice (after, 9, NONE))
|
adam@1493
|
133 end
|
adam@1493
|
134
|
adam@1493
|
135 fun doDirectives (count, source) =
|
adam@1493
|
136 let
|
adam@1493
|
137 val safe = String.translate (fn #"<" => "<"
|
adam@1493
|
138 | #"&" => "&"
|
adam@1493
|
139 | #"{" => "{"
|
adam@1493
|
140 | #"(" => "("
|
adam@1493
|
141 | #"\n" => "(*NL*)\n"
|
adam@1493
|
142 | ch => str ch) o Substring.string
|
adam@1493
|
143
|
adam@1493
|
144 val (befor, after) = Substring.position "(* begin " source
|
adam@1493
|
145
|
adam@1493
|
146 fun default () = (TextIO.outputSubstr (eval, source);
|
adam@1493
|
147 TextIO.output (gen, safe source))
|
adam@1493
|
148 in
|
adam@1493
|
149 if Substring.isEmpty after then
|
adam@1493
|
150 default ()
|
adam@1493
|
151 else
|
adam@1493
|
152 let
|
adam@1493
|
153 val (command, after) = Substring.splitl (not o Char.isSpace) (Substring.slice (after, 9, NONE))
|
adam@1493
|
154 in
|
adam@1493
|
155 if Substring.isEmpty after then
|
adam@1493
|
156 default ()
|
adam@1493
|
157 else
|
adam@1493
|
158 let
|
adam@1493
|
159 val (_, rest) = Substring.position "*)" after
|
adam@1493
|
160 in
|
adam@1493
|
161 if Substring.isEmpty rest then
|
adam@1493
|
162 default ()
|
adam@1493
|
163 else
|
adam@1493
|
164 let
|
adam@1493
|
165 val (arg, source) = untilEnd (Substring.slice (rest, 3, NONE))
|
adam@1493
|
166 val () = (TextIO.outputSubstr (eval, befor);
|
adam@1493
|
167 TextIO.output (gen, safe befor))
|
adam@1493
|
168 val (count, skip) =
|
adam@1493
|
169 case Substring.string command of
|
adam@1493
|
170 "hide" => (TextIO.outputSubstr (eval, arg);
|
adam@1493
|
171 (count, true))
|
adam@1493
|
172 | "eval" => (TextIO.output (eval, "val _eval");
|
adam@1493
|
173 TextIO.output (eval, Int.toString count);
|
adam@1493
|
174 TextIO.output (eval, " = ");
|
adam@1493
|
175 TextIO.outputSubstr (eval, arg);
|
adam@1493
|
176 TextIO.output (eval, "\n\n");
|
adam@1493
|
177
|
adam@1493
|
178 TextIO.output (gen, safe arg);
|
adam@1493
|
179 TextIO.output (gen, "== {[_eval");
|
adam@1493
|
180 TextIO.output (gen, Int.toString count);
|
adam@1493
|
181 TextIO.output (gen, "]}");
|
adam@1493
|
182
|
adam@1493
|
183 (count + 1, false))
|
adam@1493
|
184 | s => raise Fail ("Unknown tutorial directive: " ^ s)
|
adam@1493
|
185 in
|
adam@1493
|
186 doDirectives (count, if skip then
|
adam@1493
|
187 #2 (Substring.splitl Char.isSpace source)
|
adam@1493
|
188 else
|
adam@1493
|
189 source)
|
adam@1493
|
190 end
|
adam@1493
|
191 end
|
adam@1493
|
192 end
|
adam@1493
|
193 end
|
adam@1493
|
194 in
|
adam@1494
|
195 doDirectives (0, readAll inf);
|
adam@1493
|
196 TextIO.closeOut gen;
|
adam@1493
|
197
|
adam@1493
|
198 TextIO.output (eval, "\n\nfun main () : transaction page =\nreturn <xml><body>");
|
adam@1494
|
199 TextIO.outputSubstr (eval, readAllFile "/tmp/gen.ur");
|
adam@1493
|
200 TextIO.output (eval, "</body></xml>");
|
adam@1493
|
201 TextIO.closeOut eval;
|
adam@1493
|
202
|
adam@1493
|
203 if Compiler.compile "/tmp/eval" then
|
adam@1493
|
204 let
|
adam@1493
|
205 val proc = Unix.execute ("/bin/sh", ["-c", "/tmp/eval.exe /main"])
|
adam@1493
|
206 val inf = Unix.textInstreamOf proc
|
adam@1494
|
207 val s = readAll inf
|
adam@1493
|
208 val _ = Unix.reap proc
|
adam@1493
|
209
|
adam@1493
|
210 val (befor, after) = Substring.position "<sc>" s
|
adam@1493
|
211 in
|
adam@1493
|
212 if Substring.isEmpty after then
|
adam@1493
|
213 print ("Bad output for " ^ fname ^ "! [1]\n")
|
adam@1493
|
214 else
|
adam@1493
|
215 let
|
adam@1493
|
216 val after = Substring.slice (after, 4, NONE)
|
adam@1493
|
217 val (befor, after) = Substring.position "</body>" after
|
adam@1493
|
218 in
|
adam@1493
|
219 if Substring.isEmpty after then
|
adam@1493
|
220 print ("Bad output for " ^ fname ^ "! [2]\n")
|
adam@1493
|
221 else
|
adam@1493
|
222 let
|
adam@1493
|
223 val outf = TextIO.openOut "/tmp/final.ur"
|
adam@1493
|
224
|
adam@1493
|
225 fun eatNls source =
|
adam@1493
|
226 let
|
adam@1493
|
227 val (befor, after) = Substring.position "(*NL*)" source
|
adam@1493
|
228 in
|
adam@1493
|
229 if Substring.isEmpty after then
|
adam@1493
|
230 TextIO.outputSubstr (outf, source)
|
adam@1493
|
231 else
|
adam@1493
|
232 (TextIO.outputSubstr (outf, befor);
|
adam@1493
|
233 eatNls (Substring.slice (after, 6, NONE)))
|
adam@1493
|
234 end
|
adam@1493
|
235
|
adam@1493
|
236 val cmd = "emacs --eval \"(progn "
|
adam@1493
|
237 ^ "(global-font-lock-mode t) "
|
adam@1493
|
238 ^ "(add-to-list 'load-path \\\""
|
adam@1493
|
239 ^ Config.sitelisp
|
adam@1493
|
240 ^ "/\\\") "
|
adam@1493
|
241 ^ "(load \\\"urweb-mode-startup\\\") "
|
adam@1493
|
242 ^ "(urweb-mode) "
|
adam@1493
|
243 ^ "(find-file \\\"/tmp/final.ur\\\") "
|
adam@1493
|
244 ^ "(switch-to-buffer (htmlize-buffer)) "
|
adam@1494
|
245 ^ "(write-file \\\"/tmp/final.html\\\") "
|
adam@1493
|
246 ^ "(kill-emacs))\""
|
adam@1493
|
247 in
|
adam@1493
|
248 eatNls befor;
|
adam@1493
|
249 TextIO.closeOut outf;
|
adam@1494
|
250 ignore (OS.Process.system cmd);
|
adam@1494
|
251 fixupFile (fname, title)
|
adam@1493
|
252 end
|
adam@1493
|
253 end
|
adam@1493
|
254 end
|
adam@1493
|
255 else
|
adam@1493
|
256 ()
|
adam@1493
|
257 end
|
adam@1493
|
258
|
adam@1493
|
259 fun make dirname =
|
adam@1493
|
260 let
|
adam@1493
|
261 val dir = OS.FileSys.openDir dirname
|
adam@1493
|
262
|
adam@1493
|
263 fun doDir () =
|
adam@1493
|
264 case OS.FileSys.readDir dir of
|
adam@1493
|
265 NONE => OS.FileSys.closeDir dir
|
adam@1493
|
266 | SOME fname =>
|
adam@1493
|
267 (if OS.Path.ext fname = SOME "ur" then
|
adam@1493
|
268 doUr (OS.Path.joinDirFile {dir = dirname, file = fname})
|
adam@1493
|
269 else
|
adam@1493
|
270 ();
|
adam@1493
|
271 doDir ())
|
adam@1493
|
272 in
|
adam@1493
|
273 Settings.setProtocol "static";
|
adam@1493
|
274 doDir ()
|
adam@1493
|
275 end
|
adam@1493
|
276
|
adam@1493
|
277 end
|