src/Pure/Thy/html.ML
author wenzelm
Mon Feb 23 14:50:30 2015 +0100 (2015-02-23)
changeset 59564 fdc03c8daacc
parent 59448 149d2bc5ddb6
child 61374 b3c665940d62
permissions -rw-r--r--
Goal.prove_multi is superseded by the fully general Goal.prove_common;
     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 href_name: string -> text -> text
    16   val href_path: Url.T -> text -> text
    17   val href_opt_path: Url.T option -> text -> text
    18   val para: text -> text
    19   val preform: text -> text
    20   val verbatim: string -> text
    21   val begin_document: string -> text
    22   val end_document: text
    23   val begin_session_index: string -> Url.T -> (Url.T * string) list -> text
    24   val theory_entry: Url.T * string -> text
    25   val theory: string -> (Url.T option * string) list -> text -> text
    26 end;
    27 
    28 structure HTML: HTML =
    29 struct
    30 
    31 
    32 (** HTML print modes **)
    33 
    34 (* mode *)
    35 
    36 val htmlN = "HTML";
    37 fun html_mode f x = Print_Mode.with_modes [htmlN] f x;
    38 
    39 
    40 (* common markup *)
    41 
    42 fun span class = ("<span class=" ^ quote (XML.text class) ^ ">", "</span>");
    43 
    44 val _ = Markup.add_mode htmlN (span o fst);
    45 
    46 
    47 (* symbol output *)
    48 
    49 local
    50   val hidden = span Markup.hiddenN |-> enclose;
    51 
    52   (* FIXME proper unicode -- produced on Scala side *)
    53   val html_syms = Symtab.make
    54    [("", (0, "")),
    55     ("'", (1, "&#39;")),
    56     ("\\<exclamdown>", (1, "&iexcl;")),
    57     ("\\<cent>", (1, "&cent;")),
    58     ("\\<pounds>", (1, "&pound;")),
    59     ("\\<currency>", (1, "&curren;")),
    60     ("\\<yen>", (1, "&yen;")),
    61     ("\\<bar>", (1, "&brvbar;")),
    62     ("\\<section>", (1, "&sect;")),
    63     ("\\<dieresis>", (1, "&uml;")),
    64     ("\\<copyright>", (1, "&copy;")),
    65     ("\\<ordfeminine>", (1, "&ordf;")),
    66     ("\\<guillemotleft>", (1, "&laquo;")),
    67     ("\\<not>", (1, "&not;")),
    68     ("\\<hyphen>", (1, "&shy;")),
    69     ("\\<registered>", (1, "&reg;")),
    70     ("\\<inverse>", (1, "&macr;")),
    71     ("\\<degree>", (1, "&deg;")),
    72     ("\\<plusminus>", (1, "&plusmn;")),
    73     ("\\<acute>", (1, "&acute;")),
    74     ("\\<paragraph>", (1, "&para;")),
    75     ("\\<cdot>", (1, "&middot;")),
    76     ("\\<cedilla>", (1, "&cedil;")),
    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     ("\\<wp>", (1, "&weierp;")),
   137     ("\\<forall>", (1, "&forall;")),
   138     ("\\<partial>", (1, "&part;")),
   139     ("\\<exists>", (1, "&exist;")),
   140     ("\\<emptyset>", (1, "&empty;")),
   141     ("\\<nabla>", (1, "&nabla;")),
   142     ("\\<in>", (1, "&isin;")),
   143     ("\\<notin>", (1, "&notin;")),
   144     ("\\<Prod>", (1, "&prod;")),
   145     ("\\<Sum>", (1, "&sum;")),
   146     ("\\<star>", (1, "&lowast;")),
   147     ("\\<propto>", (1, "&prop;")),
   148     ("\\<infinity>", (1, "&infin;")),
   149     ("\\<angle>", (1, "&ang;")),
   150     ("\\<and>", (1, "&and;")),
   151     ("\\<or>", (1, "&or;")),
   152     ("\\<inter>", (1, "&cap;")),
   153     ("\\<union>", (1, "&cup;")),
   154     ("\\<sim>", (1, "&sim;")),
   155     ("\\<cong>", (1, "&cong;")),
   156     ("\\<approx>", (1, "&asymp;")),
   157     ("\\<noteq>", (1, "&ne;")),
   158     ("\\<equiv>", (1, "&equiv;")),
   159     ("\\<le>", (1, "&le;")),
   160     ("\\<ge>", (1, "&ge;")),
   161     ("\\<subset>", (1, "&sub;")),
   162     ("\\<supset>", (1, "&sup;")),
   163     ("\\<subseteq>", (1, "&sube;")),
   164     ("\\<supseteq>", (1, "&supe;")),
   165     ("\\<oplus>", (1, "&oplus;")),
   166     ("\\<otimes>", (1, "&otimes;")),
   167     ("\\<bottom>", (1, "&perp;")),
   168     ("\\<lceil>", (1, "&lceil;")),
   169     ("\\<rceil>", (1, "&rceil;")),
   170     ("\\<lfloor>", (1, "&lfloor;")),
   171     ("\\<rfloor>", (1, "&rfloor;")),
   172     ("\\<langle>", (1, "&lang;")),
   173     ("\\<rangle>", (1, "&rang;")),
   174     ("\\<lozenge>", (1, "&loz;")),
   175     ("\\<spadesuit>", (1, "&spades;")),
   176     ("\\<clubsuit>", (1, "&clubs;")),
   177     ("\\<heartsuit>", (1, "&hearts;")),
   178     ("\\<diamondsuit>", (1, "&diams;")),
   179     ("\\<lbrakk>", (2, "[|")),
   180     ("\\<rbrakk>", (2, "|]")),
   181     ("\\<Longrightarrow>", (3, "==&gt;")),
   182     ("\\<Rightarrow>", (2, "=&gt;")),
   183     ("\\<And>", (2, "!!")),
   184     ("\\<Colon>", (2, "::")),
   185     ("\\<lparr>", (2, "(|")),
   186     ("\\<rparr>", (2, "|)),")),
   187     ("\\<longleftrightarrow>", (3, "&lt;-&gt;")),
   188     ("\\<longrightarrow>", (3, "--&gt;")),
   189     ("\\<rightarrow>", (2, "-&gt;")),
   190     ("\\<open>", (1, "&#8249;")),
   191     ("\\<close>", (1, "&#8250;")),
   192     ("\\<newline>", (1, "&#9166;")),
   193     ("\\<^bsub>", (0, hidden "&#8664;" ^ "<sub>")),
   194     ("\\<^esub>", (0, hidden "&#8665;" ^ "</sub>")),
   195     ("\\<^bsup>", (0, hidden "&#8663;" ^ "<sup>")),
   196     ("\\<^esup>", (0, hidden "&#8662;" ^ "</sup>"))];
   197 
   198   fun output_sym s =
   199     if Symbol.is_raw s then (1, Symbol.decode_raw s)
   200     else
   201       (case Symtab.lookup html_syms s of
   202         SOME x => x
   203       | NONE => (size s, XML.text s));
   204 
   205   fun output_markup (bg, en) s1 s2 =
   206     let val (n, txt) = output_sym s2
   207     in (n, hidden s1 ^ enclose bg en txt) end;
   208 
   209   val output_sub = output_markup ("<sub>", "</sub>");
   210   val output_sup = output_markup ("<sup>", "</sup>");
   211   val output_bold = output_markup (span "bold");
   212 
   213   fun output_syms [] (result, width) = (implode (rev result), width)
   214     | output_syms (s1 :: rest) (result, width) =
   215         let
   216           val (s2, ss) = (case rest of [] => ("", []) | s2 :: ss => (s2, ss));
   217           val ((w, s), r) =
   218             if s1 = "\\<^sub>" then (output_sub "&#8681;" s2, ss)
   219             else if s1 = "\\<^sup>" then (output_sup "&#8679;" s2, ss)
   220             else if s1 = "\\<^bold>" then (output_bold "&#10073;" s2, ss)
   221             else (output_sym s1, rest);
   222         in output_syms r (s :: result, width + w) end;
   223 in
   224 
   225 fun output_width str = output_syms (Symbol.explode str) ([], 0);
   226 val output = #1 o output_width;
   227 
   228 val _ = Output.add_mode htmlN output_width Symbol.encode_raw;
   229 
   230 end;
   231 
   232 
   233 
   234 (** HTML markup **)
   235 
   236 type text = string;
   237 
   238 
   239 (* atoms *)
   240 
   241 val plain = output;
   242 val name = enclose "<span class=\"name\">" "</span>" o output;
   243 val keyword = enclose "<span class=\"keyword\">" "</span>" o output;
   244 val command = enclose "<span class=\"command\">" "</span>" o output;
   245 
   246 
   247 (* misc *)
   248 
   249 fun href_name s txt = "<a href=" ^ quote s ^ ">" ^ txt ^ "</a>";
   250 fun href_path path txt = href_name (Url.implode path) txt;
   251 
   252 fun href_opt_path NONE txt = txt
   253   | href_opt_path (SOME p) txt = href_path p txt;
   254 
   255 fun para txt = "\n<p>" ^ txt ^ "</p>\n";
   256 fun preform txt = "<pre>" ^ txt ^ "</pre>";
   257 val verbatim = preform o output;
   258 
   259 
   260 (* document *)
   261 
   262 fun begin_document title =
   263   "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n\
   264   \<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \
   265   \\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n\
   266   \<html xmlns=\"http://www.w3.org/1999/xhtml\">\n\
   267   \<head>\n\
   268   \<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>\n\
   269   \<title>" ^ plain (title ^ " (" ^ Distribution.version ^ ")") ^ "</title>\n\
   270   \<link media=\"all\" rel=\"stylesheet\" type=\"text/css\" href=\"isabelle.css\"/>\n\
   271   \</head>\n\
   272   \\n\
   273   \<body>\n\
   274   \<div class=\"head\">\
   275   \<h1>" ^ plain title ^ "</h1>\n";
   276 
   277 val end_document = "\n</div>\n</body>\n</html>\n";
   278 
   279 
   280 (* session index *)
   281 
   282 fun begin_session_index session graph docs =
   283   begin_document ("Session " ^ plain session) ^
   284   para ("View " ^ href_path graph "theory dependencies" ^
   285     implode (map (fn (p, name) => "<br/>\nView " ^ href_path p name) docs)) ^
   286   "\n</div>\n<div class=\"theories\">\n<h2>Theories</h2>\n<ul>\n";
   287 
   288 fun theory_entry (p, s) = "<li>" ^ href_path p (plain s) ^ "</li>\n";
   289 
   290 
   291 (* theory *)
   292 
   293 fun theory A Bs txt =
   294   begin_document ("Theory " ^ A) ^ "\n" ^
   295   command "theory" ^ " " ^ name A ^ "<br/>\n" ^
   296   keyword "imports" ^ " " ^ space_implode " " (map (uncurry href_opt_path o apsnd name) Bs) ^
   297   "<br/>\n" ^
   298   enclose "\n</div>\n<div class=\"source\">\n<pre>" "</pre>\n" txt ^
   299   end_document;
   300 
   301 end;