annotate src/tutorial.sml @ 1495:af0d4d11c5d7

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