annotate src/tutorial.sml @ 1496:3010472edf44

Tutorial section headings
author Adam Chlipala <adam@chlipala.net>
date Fri, 15 Jul 2011 17:31:57 -0400
parents af0d4d11c5d7
children 0b639858200b
rev   line source
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@1495 51 fun proseLoop source =
adam@1495 52 let
adam@1495 53 val (befor, after) = Substring.splitl (fn ch => ch <> #"&") source
adam@1495 54 in
adam@1495 55 if Substring.isEmpty after then
adam@1495 56 TextIO.outputSubstr (outf, source)
adam@1495 57 else if Substring.size after >= 8 andalso Substring.string (Substring.slice (after, 1, SOME 7)) = "amp;lt;" then
adam@1495 58 (TextIO.outputSubstr (outf, befor);
adam@1495 59 TextIO.output (outf, "<");
adam@1495 60 proseLoop (Substring.slice (after, 8, NONE)))
adam@1495 61 else if Substring.size after >= 4 andalso Substring.string (Substring.slice (after, 1, SOME 3)) = "gt;" then
adam@1495 62 (TextIO.outputSubstr (outf, befor);
adam@1495 63 TextIO.output (outf, ">");
adam@1495 64 proseLoop (Substring.slice (after, 4, NONE)))
adam@1495 65 else if Substring.size after >= 5 andalso Substring.string (Substring.slice (after, 1, SOME 4)) = "amp;" then
adam@1495 66 (TextIO.outputSubstr (outf, befor);
adam@1495 67 TextIO.output (outf, "&");
adam@1495 68 proseLoop (Substring.slice (after, 5, NONE)))
adam@1495 69 else
adam@1495 70 raise Fail "Unsupported HTML escape"
adam@1495 71 end
adam@1495 72
adam@1494 73 fun loop source =
adam@1494 74 let
adam@1494 75 val (befor, after) = Substring.position "<span class=\"comment-delimiter\">(* </span><span class=\"comment\">" source
adam@1494 76 in
adam@1494 77 if Substring.isEmpty after then
adam@1496 78 let
adam@1496 79 val (befor, after) = Substring.position "<span class=\"comment-delimiter\">(** </span><span class=\"comment\">" source
adam@1496 80 in
adam@1496 81 if Substring.isEmpty after then
adam@1496 82 TextIO.outputSubstr (outf, source)
adam@1496 83 else
adam@1496 84 let
adam@1496 85 val (befor', after) = Substring.position " </span><span class=\"comment-delimiter\">*)</span>"
adam@1496 86 (Substring.slice (after, 65, NONE))
adam@1496 87 in
adam@1496 88 if Substring.isEmpty after then
adam@1496 89 TextIO.outputSubstr (outf, source)
adam@1496 90 else
adam@1496 91 (TextIO.outputSubstr (outf, befor);
adam@1496 92 TextIO.output (outf, "<h2>");
adam@1496 93 proseLoop befor';
adam@1496 94 TextIO.output (outf, "</h2>");
adam@1496 95 loop (Substring.slice (after, 49, NONE)))
adam@1496 96 end
adam@1496 97 end
adam@1494 98 else
adam@1494 99 let
adam@1494 100 val (befor', after) = Substring.position " </span><span class=\"comment-delimiter\">*)</span>"
adam@1494 101 (Substring.slice (after, 64, NONE))
adam@1494 102 in
adam@1494 103 if Substring.isEmpty after then
adam@1494 104 TextIO.outputSubstr (outf, source)
adam@1494 105 else
adam@1494 106 (TextIO.outputSubstr (outf, befor);
adam@1494 107 TextIO.output (outf, "<div class=\"prose\">");
adam@1495 108 proseLoop befor';
adam@1494 109 TextIO.output (outf, "</div>");
adam@1494 110 loop (Substring.slice (after, 49, NONE)))
adam@1494 111 end
adam@1494 112 end
adam@1494 113 in
adam@1494 114 if Substring.isEmpty after then
adam@1494 115 raise Fail ("Missing <title> for " ^ title)
adam@1494 116 else
adam@1494 117 (TextIO.outputSubstr (outf, befor);
adam@1494 118 TextIO.output (outf, "<style type=\"text/css\">\n");
adam@1494 119 TextIO.output (outf, "<!--\n");
adam@1494 120 TextIO.output (outf, "\tdiv.prose {\n");
adam@1494 121 TextIO.output (outf, "\t\tfont-family: Arial;\n");
adam@1494 122 TextIO.output (outf, "\t\tbackground-color: #CCFFCC;\n");
adam@1494 123 TextIO.output (outf, "\t\tborder-style: solid;\n");
adam@1494 124 TextIO.output (outf, "\t\tpadding: 5px;\n");
adam@1494 125 TextIO.output (outf, "\t\tfont-size: larger;\n");
adam@1494 126 TextIO.output (outf, "\t}\n");
adam@1496 127 TextIO.output (outf, "\th2 {\n");
adam@1496 128 TextIO.output (outf, "\t\tfont-family: Arial;\n");
adam@1496 129 TextIO.output (outf, "\t\tfont-size: 20pt;\n");
adam@1496 130 TextIO.output (outf, "\t\tbackground-color: #99FF99;\n");
adam@1496 131 TextIO.output (outf, "\t\tpadding: 5px;\n");
adam@1496 132 TextIO.output (outf, "\t}\n");
adam@1494 133 TextIO.output (outf, "-->\n");
adam@1494 134 TextIO.output (outf, "</style>\n");
adam@1494 135 TextIO.output (outf, "<title>");
adam@1494 136 TextIO.output (outf, title);
adam@1494 137 let
adam@1494 138 val (befor, after) = Substring.position "</title>" after
adam@1494 139 in
adam@1494 140 if Substring.isEmpty after then
adam@1494 141 raise Fail ("Missing </title> for " ^ title)
adam@1494 142 else
adam@1494 143 let
adam@1494 144 val (befor, after) = Substring.position "<body>" after
adam@1494 145 in
adam@1494 146 if Substring.isEmpty after then
adam@1494 147 raise Fail ("Missing <body> for " ^ title)
adam@1494 148 else
adam@1494 149 (TextIO.outputSubstr (outf, befor);
adam@1494 150 TextIO.output (outf, "<body><h1>");
adam@1494 151 TextIO.output (outf, title);
adam@1494 152 TextIO.output (outf, "</h1>");
adam@1494 153 loop (Substring.slice (after, 6, NONE)))
adam@1494 154 end
adam@1494 155 end;
adam@1494 156 TextIO.closeOut outf)
adam@1494 157 end
adam@1493 158
adam@1493 159 fun doUr fname =
adam@1493 160 let
adam@1494 161 val inf = TextIO.openIn fname
adam@1494 162
adam@1494 163 val title = case TextIO.inputLine inf of
adam@1494 164 NONE => raise Fail ("No title comment at start of " ^ fname)
adam@1494 165 | SOME title => title
adam@1494 166
adam@1494 167 val title = String.substring (title, 3, size title - 7)
adam@1494 168
adam@1493 169 val eval = TextIO.openOut "/tmp/eval.ur"
adam@1493 170 val gen = TextIO.openOut "/tmp/gen.ur"
adam@1493 171
adam@1493 172 fun untilEnd source =
adam@1493 173 let
adam@1493 174 val (befor, after) = Substring.position "(* end *)" source
adam@1493 175 in
adam@1493 176 if Substring.isEmpty after then
adam@1493 177 (source, Substring.full "")
adam@1493 178 else
adam@1493 179 (befor, Substring.slice (after, 9, NONE))
adam@1493 180 end
adam@1493 181
adam@1493 182 fun doDirectives (count, source) =
adam@1493 183 let
adam@1493 184 val safe = String.translate (fn #"<" => "&lt;"
adam@1493 185 | #"&" => "&amp;"
adam@1493 186 | #"{" => "&#123;"
adam@1493 187 | #"(" => "&#40;"
adam@1493 188 | #"\n" => "&#40;*NL*)\n"
adam@1493 189 | ch => str ch) o Substring.string
adam@1493 190
adam@1493 191 val (befor, after) = Substring.position "(* begin " source
adam@1493 192
adam@1493 193 fun default () = (TextIO.outputSubstr (eval, source);
adam@1493 194 TextIO.output (gen, safe source))
adam@1493 195 in
adam@1493 196 if Substring.isEmpty after then
adam@1493 197 default ()
adam@1493 198 else
adam@1493 199 let
adam@1493 200 val (command, after) = Substring.splitl (not o Char.isSpace) (Substring.slice (after, 9, NONE))
adam@1493 201 in
adam@1493 202 if Substring.isEmpty after then
adam@1493 203 default ()
adam@1493 204 else
adam@1493 205 let
adam@1493 206 val (_, rest) = Substring.position "*)" after
adam@1493 207 in
adam@1493 208 if Substring.isEmpty rest then
adam@1493 209 default ()
adam@1493 210 else
adam@1493 211 let
adam@1493 212 val (arg, source) = untilEnd (Substring.slice (rest, 3, NONE))
adam@1493 213 val () = (TextIO.outputSubstr (eval, befor);
adam@1493 214 TextIO.output (gen, safe befor))
adam@1493 215 val (count, skip) =
adam@1493 216 case Substring.string command of
adam@1493 217 "hide" => (TextIO.outputSubstr (eval, arg);
adam@1493 218 (count, true))
adam@1493 219 | "eval" => (TextIO.output (eval, "val _eval");
adam@1493 220 TextIO.output (eval, Int.toString count);
adam@1493 221 TextIO.output (eval, " = ");
adam@1493 222 TextIO.outputSubstr (eval, arg);
adam@1493 223 TextIO.output (eval, "\n\n");
adam@1493 224
adam@1493 225 TextIO.output (gen, safe arg);
adam@1493 226 TextIO.output (gen, "== {[_eval");
adam@1493 227 TextIO.output (gen, Int.toString count);
adam@1493 228 TextIO.output (gen, "]}");
adam@1493 229
adam@1493 230 (count + 1, false))
adam@1493 231 | s => raise Fail ("Unknown tutorial directive: " ^ s)
adam@1493 232 in
adam@1493 233 doDirectives (count, if skip then
adam@1493 234 #2 (Substring.splitl Char.isSpace source)
adam@1493 235 else
adam@1493 236 source)
adam@1493 237 end
adam@1493 238 end
adam@1493 239 end
adam@1493 240 end
adam@1493 241 in
adam@1494 242 doDirectives (0, readAll inf);
adam@1493 243 TextIO.closeOut gen;
adam@1493 244
adam@1493 245 TextIO.output (eval, "\n\nfun main () : transaction page =\nreturn <xml><body>");
adam@1494 246 TextIO.outputSubstr (eval, readAllFile "/tmp/gen.ur");
adam@1493 247 TextIO.output (eval, "</body></xml>");
adam@1493 248 TextIO.closeOut eval;
adam@1493 249
adam@1493 250 if Compiler.compile "/tmp/eval" then
adam@1493 251 let
adam@1493 252 val proc = Unix.execute ("/bin/sh", ["-c", "/tmp/eval.exe /main"])
adam@1493 253 val inf = Unix.textInstreamOf proc
adam@1494 254 val s = readAll inf
adam@1493 255 val _ = Unix.reap proc
adam@1493 256
adam@1493 257 val (befor, after) = Substring.position "<sc>" s
adam@1493 258 in
adam@1493 259 if Substring.isEmpty after then
adam@1493 260 print ("Bad output for " ^ fname ^ "! [1]\n")
adam@1493 261 else
adam@1493 262 let
adam@1493 263 val after = Substring.slice (after, 4, NONE)
adam@1493 264 val (befor, after) = Substring.position "</body>" after
adam@1493 265 in
adam@1493 266 if Substring.isEmpty after then
adam@1493 267 print ("Bad output for " ^ fname ^ "! [2]\n")
adam@1493 268 else
adam@1493 269 let
adam@1493 270 val outf = TextIO.openOut "/tmp/final.ur"
adam@1493 271
adam@1493 272 fun eatNls source =
adam@1493 273 let
adam@1493 274 val (befor, after) = Substring.position "(*NL*)" source
adam@1493 275 in
adam@1493 276 if Substring.isEmpty after then
adam@1493 277 TextIO.outputSubstr (outf, source)
adam@1493 278 else
adam@1493 279 (TextIO.outputSubstr (outf, befor);
adam@1493 280 eatNls (Substring.slice (after, 6, NONE)))
adam@1493 281 end
adam@1493 282
adam@1493 283 val cmd = "emacs --eval \"(progn "
adam@1493 284 ^ "(global-font-lock-mode t) "
adam@1493 285 ^ "(add-to-list 'load-path \\\""
adam@1493 286 ^ Config.sitelisp
adam@1493 287 ^ "/\\\") "
adam@1493 288 ^ "(load \\\"urweb-mode-startup\\\") "
adam@1493 289 ^ "(urweb-mode) "
adam@1493 290 ^ "(find-file \\\"/tmp/final.ur\\\") "
adam@1493 291 ^ "(switch-to-buffer (htmlize-buffer)) "
adam@1494 292 ^ "(write-file \\\"/tmp/final.html\\\") "
adam@1493 293 ^ "(kill-emacs))\""
adam@1493 294 in
adam@1493 295 eatNls befor;
adam@1493 296 TextIO.closeOut outf;
adam@1494 297 ignore (OS.Process.system cmd);
adam@1494 298 fixupFile (fname, title)
adam@1493 299 end
adam@1493 300 end
adam@1493 301 end
adam@1493 302 else
adam@1493 303 ()
adam@1493 304 end
adam@1493 305
adam@1493 306 fun make dirname =
adam@1493 307 let
adam@1493 308 val dir = OS.FileSys.openDir dirname
adam@1493 309
adam@1493 310 fun doDir () =
adam@1493 311 case OS.FileSys.readDir dir of
adam@1493 312 NONE => OS.FileSys.closeDir dir
adam@1493 313 | SOME fname =>
adam@1493 314 (if OS.Path.ext fname = SOME "ur" then
adam@1493 315 doUr (OS.Path.joinDirFile {dir = dirname, file = fname})
adam@1493 316 else
adam@1493 317 ();
adam@1493 318 doDir ())
adam@1493 319 in
adam@1493 320 Settings.setProtocol "static";
adam@1493 321 doDir ()
adam@1493 322 end
adam@1493 323
adam@1493 324 end