src/Pure/Thy/html.ML
author wenzelm
Tue Sep 29 11:49:22 2009 +0200 (2009-09-29)
changeset 32738 15bb09ca0378
parent 29606 fedb8be05f24
child 32796 2e4485b9a39f
permissions -rw-r--r--
explicit indication of Unsynchronized.ref;
     1 (*  Title:      Pure/Thy/html.ML
     2     Author:     Markus Wenzel and Stefan Berghofer, TU Muenchen
     3 
     4 HTML presentation elements.
     5 *)
     6 
     7 signature HTML =
     8 sig
     9   val html_mode: ('a -> 'b) -> 'a -> 'b
    10   type text = string
    11   val plain: string -> text
    12   val name: string -> text
    13   val keyword: string -> text
    14   val command: string -> text
    15   val file_name: string -> text
    16   val file_path: Url.T -> text
    17   val href_name: string -> text -> text
    18   val href_path: Url.T -> text -> text
    19   val href_opt_path: Url.T option -> text -> text
    20   val para: text -> text
    21   val preform: text -> text
    22   val verbatim: string -> text
    23   val with_charset: string -> ('a -> 'b) -> 'a -> 'b
    24   val begin_document: string -> text
    25   val end_document: text
    26   val begin_index: Url.T * string -> Url.T * string -> (Url.T * string) list -> Url.T -> text
    27   val applet_pages: string -> Url.T * string -> (string * string) list
    28   val theory_entry: Url.T * string -> text
    29   val session_entries: (Url.T * string) list -> text
    30   val theory_source: text -> text
    31   val begin_theory: Url.T * string -> string -> (Url.T option * string) list ->
    32     (Url.T * Url.T * bool) list -> text -> text
    33   val ml_file: Url.T -> string -> text
    34 end;
    35 
    36 structure HTML: HTML =
    37 struct
    38 
    39 
    40 (** HTML print modes **)
    41 
    42 (* mode *)
    43 
    44 val htmlN = "HTML";
    45 fun html_mode f x = PrintMode.with_modes [htmlN] f x;
    46 
    47 
    48 (* symbol output *)
    49 
    50 local
    51   val html_syms = Symtab.make
    52    [("\\<spacespace>", (2, "&nbsp;&nbsp;")),
    53     ("\\<exclamdown>", (1, "&iexcl;")),
    54     ("\\<cent>", (1, "&cent;")),
    55     ("\\<pounds>", (1, "&pound;")),
    56     ("\\<currency>", (1, "&curren;")),
    57     ("\\<yen>", (1, "&yen;")),
    58     ("\\<bar>", (1, "&brvbar;")),
    59     ("\\<section>", (1, "&sect;")),
    60     ("\\<dieresis>", (1, "&uml;")),
    61     ("\\<copyright>", (1, "&copy;")),
    62     ("\\<ordfeminine>", (1, "&ordf;")),
    63     ("\\<guillemotleft>", (1, "&laquo;")),
    64     ("\\<not>", (1, "&not;")),
    65     ("\\<hyphen>", (1, "&shy;")),
    66     ("\\<registered>", (1, "&reg;")),
    67     ("\\<inverse>", (1, "&macr;")),
    68     ("\\<degree>", (1, "&deg;")),
    69     ("\\<plusminus>", (1, "&plusmn;")),
    70     ("\\<twosuperior>", (1, "&sup2;")),
    71     ("\\<threesuperior>", (1, "&sup3;")),
    72     ("\\<acute>", (1, "&acute;")),
    73     ("\\<paragraph>", (1, "&para;")),
    74     ("\\<cdot>", (1, "&middot;")),
    75     ("\\<cedilla>", (1, "&cedil;")),
    76     ("\\<onesuperior>", (1, "&sup1;")),
    77     ("\\<ordmasculine>", (1, "&ordm;")),
    78     ("\\<guillemotright>", (1, "&raquo;")),
    79     ("\\<onequarter>", (1, "&frac14;")),
    80     ("\\<onehalf>", (1, "&frac12;")),
    81     ("\\<threequarters>", (1, "&frac34;")),
    82     ("\\<questiondown>", (1, "&iquest;")),
    83     ("\\<times>", (1, "&times;")),
    84     ("\\<div>", (1, "&divide;")),
    85     ("\\<circ>", (1, "o")),
    86     ("\\<Alpha>", (1, "&Alpha;")),
    87     ("\\<Beta>", (1, "&Beta;")),
    88     ("\\<Gamma>", (1, "&Gamma;")),
    89     ("\\<Delta>", (1, "&Delta;")),
    90     ("\\<Epsilon>", (1, "&Epsilon;")),
    91     ("\\<Zeta>", (1, "&Zeta;")),
    92     ("\\<Eta>", (1, "&Eta;")),
    93     ("\\<Theta>", (1, "&Theta;")),
    94     ("\\<Iota>", (1, "&Iota;")),
    95     ("\\<Kappa>", (1, "&Kappa;")),
    96     ("\\<Lambda>", (1, "&Lambda;")),
    97     ("\\<Mu>", (1, "&Mu;")),
    98     ("\\<Nu>", (1, "&Nu;")),
    99     ("\\<Xi>", (1, "&Xi;")),
   100     ("\\<Omicron>", (1, "&Omicron;")),
   101     ("\\<Pi>", (1, "&Pi;")),
   102     ("\\<Rho>", (1, "&Rho;")),
   103     ("\\<Sigma>", (1, "&Sigma;")),
   104     ("\\<Tau>", (1, "&Tau;")),
   105     ("\\<Upsilon>", (1, "&Upsilon;")),
   106     ("\\<Phi>", (1, "&Phi;")),
   107     ("\\<Chi>", (1, "&Chi;")),
   108     ("\\<Psi>", (1, "&Psi;")),
   109     ("\\<Omega>", (1, "&Omega;")),
   110     ("\\<alpha>", (1, "&alpha;")),
   111     ("\\<beta>", (1, "&beta;")),
   112     ("\\<gamma>", (1, "&gamma;")),
   113     ("\\<delta>", (1, "&delta;")),
   114     ("\\<epsilon>", (1, "&epsilon;")),
   115     ("\\<zeta>", (1, "&zeta;")),
   116     ("\\<eta>", (1, "&eta;")),
   117     ("\\<theta>", (1, "&thetasym;")),
   118     ("\\<iota>", (1, "&iota;")),
   119     ("\\<kappa>", (1, "&kappa;")),
   120     ("\\<lambda>", (1, "&lambda;")),
   121     ("\\<mu>", (1, "&mu;")),
   122     ("\\<nu>", (1, "&nu;")),
   123     ("\\<xi>", (1, "&xi;")),
   124     ("\\<omicron>", (1, "&omicron;")),
   125     ("\\<pi>", (1, "&pi;")),
   126     ("\\<rho>", (1, "&rho;")),
   127     ("\\<sigma>", (1, "&sigma;")),
   128     ("\\<tau>", (1, "&tau;")),
   129     ("\\<upsilon>", (1, "&upsilon;")),
   130     ("\\<phi>", (1, "&phi;")),
   131     ("\\<chi>", (1, "&chi;")),
   132     ("\\<psi>", (1, "&psi;")),
   133     ("\\<omega>", (1, "&omega;")),
   134     ("\\<bullet>", (1, "&bull;")),
   135     ("\\<dots>", (1, "&hellip;")),
   136     ("\\<Re>", (1, "&real;")),
   137     ("\\<Im>", (1, "&image;")),
   138     ("\\<wp>", (1, "&weierp;")),
   139     ("\\<forall>", (1, "&forall;")),
   140     ("\\<partial>", (1, "&part;")),
   141     ("\\<exists>", (1, "&exist;")),
   142     ("\\<emptyset>", (1, "&empty;")),
   143     ("\\<nabla>", (1, "&nabla;")),
   144     ("\\<in>", (1, "&isin;")),
   145     ("\\<notin>", (1, "&notin;")),
   146     ("\\<Prod>", (1, "&prod;")),
   147     ("\\<Sum>", (1, "&sum;")),
   148     ("\\<star>", (1, "&lowast;")),
   149     ("\\<propto>", (1, "&prop;")),
   150     ("\\<infinity>", (1, "&infin;")),
   151     ("\\<angle>", (1, "&ang;")),
   152     ("\\<and>", (1, "&and;")),
   153     ("\\<or>", (1, "&or;")),
   154     ("\\<inter>", (1, "&cap;")),
   155     ("\\<union>", (1, "&cup;")),
   156     ("\\<sim>", (1, "&sim;")),
   157     ("\\<cong>", (1, "&cong;")),
   158     ("\\<approx>", (1, "&asymp;")),
   159     ("\\<noteq>", (1, "&ne;")),
   160     ("\\<equiv>", (1, "&equiv;")),
   161     ("\\<le>", (1, "&le;")),
   162     ("\\<ge>", (1, "&ge;")),
   163     ("\\<subset>", (1, "&sub;")),
   164     ("\\<supset>", (1, "&sup;")),
   165     ("\\<subseteq>", (1, "&sube;")),
   166     ("\\<supseteq>", (1, "&supe;")),
   167     ("\\<oplus>", (1, "&oplus;")),
   168     ("\\<otimes>", (1, "&otimes;")),
   169     ("\\<bottom>", (1, "&perp;")),
   170     ("\\<lceil>", (1, "&lceil;")),
   171     ("\\<rceil>", (1, "&rceil;")),
   172     ("\\<lfloor>", (1, "&lfloor;")),
   173     ("\\<rfloor>", (1, "&rfloor;")),
   174     ("\\<langle>", (1, "&lang;")),
   175     ("\\<rangle>", (1, "&rang;")),
   176     ("\\<lozenge>", (1, "&loz;")),
   177     ("\\<spadesuit>", (1, "&spades;")),
   178     ("\\<clubsuit>", (1, "&clubs;")),
   179     ("\\<heartsuit>", (1, "&hearts;")),
   180     ("\\<diamondsuit>", (1, "&diams;")),
   181     ("\\<lbrakk>", (2, "[|")),
   182     ("\\<rbrakk>", (2, "|]")),
   183     ("\\<Longrightarrow>", (3, "==&gt;")),
   184     ("\\<Rightarrow>", (2, "=&gt;")),
   185     ("\\<And>", (2, "!!")),
   186     ("\\<Colon>", (2, "::")),
   187     ("\\<lparr>", (2, "(|")),
   188     ("\\<rparr>", (2, "|)),")),
   189     ("\\<longleftrightarrow>", (3, "&lt;-&gt;")),
   190     ("\\<longrightarrow>", (3, "--&gt;")),
   191     ("\\<rightarrow>", (2, "-&gt;")),
   192     ("\\<^bsub>", (0, "<sub>")),
   193     ("\\<^esub>", (0, "</sub>")),
   194     ("\\<^bsup>", (0, "<sup>")),
   195     ("\\<^esup>", (0, "</sup>"))];
   196 
   197   fun output_sym s =
   198     if Symbol.is_raw s then (1, Symbol.decode_raw s)
   199     else
   200       (case Symtab.lookup html_syms s of SOME x => x
   201       | NONE => (size s, XML.text s));
   202 
   203   fun output_sub s = apsnd (enclose "<sub>" "</sub>") (output_sym s);
   204   fun output_sup s = apsnd (enclose "<sup>" "</sup>") (output_sym s);
   205   fun output_loc s = apsnd (enclose "<span class=\"loc\">" "</span>") (output_sym s);
   206 
   207   fun output_syms ("\\<^sub>" :: s :: ss) = output_sub s :: output_syms ss
   208     | output_syms ("\\<^isub>" :: s :: ss) = output_sub s :: output_syms ss
   209     | output_syms ("\\<^sup>" :: s :: ss) = output_sup s :: output_syms ss
   210     | output_syms ("\\<^isup>" :: s :: ss) = output_sup s :: output_syms ss
   211     | output_syms ("\\<^loc>" :: s :: ss) = output_loc s :: output_syms ss
   212     | output_syms (s :: ss) = output_sym s :: output_syms ss
   213     | output_syms [] = [];
   214 in
   215 
   216 fun output_width str =
   217   if not (exists_string (fn s => s = "\\" orelse s = "<" orelse s = ">" orelse s = "&") str)
   218   then Output.default_output str
   219   else
   220     let val (syms, width) = fold_map (fn (w, s) => fn width => (s, w + width))
   221       (output_syms (Symbol.explode str)) 0
   222     in (implode syms, width) end;
   223 
   224 val output = #1 o output_width;
   225 val output_symbols = map #2 o output_syms;
   226 
   227 val _ = Output.add_mode htmlN output_width Symbol.encode_raw;
   228 
   229 end;
   230 
   231 
   232 (* common markup *)
   233 
   234 fun span s = ("<span class=" ^ quote (XML.text s) ^ ">", "</span>");
   235 
   236 val _ = Markup.add_mode htmlN (fn (name, _) => span name);
   237 
   238 
   239 
   240 (** HTML markup **)
   241 
   242 type text = string;
   243 
   244 
   245 (* atoms *)
   246 
   247 val plain = output;
   248 val name = enclose "<span class=\"name\">" "</span>" o output;
   249 val keyword = enclose "<span class=\"keyword\">" "</span>" o output;
   250 val command = enclose "<span class=\"command\">" "</span>" o output;
   251 val file_name = enclose "<span class=\"filename\">" "</span>" o output;
   252 val file_path = file_name o Url.implode;
   253 
   254 
   255 (* misc *)
   256 
   257 fun href_name s txt = "<a href=" ^ quote s ^ ">" ^ txt ^ "</a>";
   258 fun href_path path txt = href_name (Url.implode path) txt;
   259 
   260 fun href_opt_path NONE txt = txt
   261   | href_opt_path (SOME p) txt = href_path p txt;
   262 
   263 fun para txt = "\n<p>" ^ txt ^ "</p>\n";
   264 fun preform txt = "<pre>" ^ txt ^ "</pre>";
   265 val verbatim = preform o output;
   266 
   267 
   268 (* document *)
   269 
   270 val charset = Unsynchronized.ref "ISO-8859-1";
   271 fun with_charset s = setmp_noncritical charset s;   (* FIXME *)
   272 
   273 fun begin_document title =
   274   let val cs = ! charset in
   275     "<?xml version=\"1.0\" encoding=\"" ^ cs ^ "\" ?>\n\
   276     \<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \
   277     \\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n\
   278     \<html xmlns=\"http://www.w3.org/1999/xhtml\">\n\
   279     \<head>\n\
   280     \<meta http-equiv=\"Content-Type\" content=\"text/html; charset=" ^ cs ^ "\"/>\n\
   281     \<title>" ^ plain (title ^ " (" ^ Distribution.version ^ ")") ^ "</title>\n\
   282     \<link media=\"all\" rel=\"stylesheet\" type=\"text/css\" href=\"isabelle.css\"/>\n\
   283     \</head>\n\
   284     \\n\
   285     \<body>\n\
   286     \<div class=\"head\">\
   287     \<h1>" ^ plain title ^ "</h1>\n"
   288   end;
   289 
   290 val end_document = "\n</div>\n</body>\n</html>\n";
   291 
   292 fun gen_link how (path, name) = para (href_path path how ^ " to index of " ^ plain name);
   293 val up_link = gen_link "Up";
   294 val back_link = gen_link "Back";
   295 
   296 
   297 (* session index *)
   298 
   299 fun begin_index up (index_path, session) docs graph =
   300   begin_document ("Index of " ^ session) ^ up_link up ^
   301   para ("View " ^ href_path graph "theory dependencies" ^
   302     implode (map (fn (p, name) => "<br/>\nView " ^ href_path p name) docs)) ^
   303   "\n</div>\n<hr/>\n<div class=\"theories\">\n<h2>Theories</h2>\n<ul>\n";
   304 
   305 fun choice chs s = space_implode " " (map (fn (s', lnk) =>
   306   enclose "[" "]" (if s = s' then keyword s' else href_name lnk s')) chs);
   307 
   308 fun applet_pages session back =
   309   let
   310     val sizes =
   311      [("small", "small.html", ("500", "400")),
   312       ("medium", "medium.html", ("650", "520")),
   313       ("large", "large.html", ("800", "640"))];
   314 
   315     fun applet_page (size, name, (width, height)) =
   316       let
   317         val browser_size = "Set browser size: " ^
   318           choice (map (fn (y, z, _) => (y, z)) sizes) size;
   319       in
   320         (name, begin_document ("Theory dependencies of " ^ session) ^
   321           back_link back ^
   322           para browser_size ^
   323           "\n</div>\n<hr/>\n<div class=\"graphbrowser\">\n\
   324           \<applet code=\"GraphBrowser/GraphBrowser.class\" \
   325           \archive=\"GraphBrowser.jar\" \
   326           \width=" ^ quote width ^ " height=" ^ quote height ^ ">\n\
   327           \<param name=\"graphfile\" value=\"" ^ "session.graph" ^ "\"/>\n\
   328           \</applet>\n<hr/>" ^ end_document)
   329       end;
   330   in map applet_page sizes end;
   331 
   332 
   333 fun entry (p, s) = "<li>" ^ href_path p (plain s) ^ "</li>\n";
   334 
   335 val theory_entry = entry;
   336 
   337 fun session_entries [] = "</ul>"
   338   | session_entries es =
   339       "</ul>\n</div>\n<hr/>\n<div class=\"sessions\">\n\
   340       \<h2>Sessions</h2>\n<ul>\n" ^ implode (map entry es) ^ "</ul>";
   341 
   342 
   343 (* theory *)
   344 
   345 val theory_source = enclose
   346   "\n</div>\n<hr/>\n<div class=\"source\">\n<pre>"
   347   "</pre>\n<hr/>\n";
   348 
   349 
   350 local
   351   fun file (href, path, loadit) =
   352     href_path href (if loadit then file_path path else enclose "(" ")" (file_path path));
   353 
   354   fun theory up A =
   355     begin_document ("Theory " ^ A) ^ "\n" ^ up_link up ^
   356     command "theory" ^ " " ^ name A;
   357 
   358   fun imports Bs =
   359     keyword "imports" ^ " " ^ space_implode " " (map (uncurry href_opt_path o apsnd name) Bs);
   360 
   361   fun uses Ps = keyword "uses" ^ " " ^ space_implode " " (map file Ps) ^ "<br/>\n";
   362 in
   363 
   364 fun begin_theory up A Bs Ps body =
   365   theory up A ^ "<br/>\n" ^
   366   imports Bs ^ "<br/>\n" ^
   367   (if null Ps then "" else uses Ps) ^
   368   body;
   369 
   370 end;
   371 
   372 
   373 (* ML file *)
   374 
   375 fun ml_file path str =
   376   begin_document ("File " ^ Url.implode path) ^
   377   "\n</div>\n<hr/><div class=\"mlsource\">\n" ^
   378   verbatim str ^
   379   "\n</div>\n<hr/>\n<div class=\"mlfooter\">" ^
   380   end_document;
   381 
   382 end;