src/Pure/Thy/html.ML
author wenzelm
Wed Sep 22 20:57:51 1999 +0200 (1999-09-22 ago)
changeset 7572 6e6dafacbc28
parent 7408 1ec1567c1307
child 7634 c326808da921
permissions -rw-r--r--
present results;
     1 (*  Title:      Pure/Thy/HTML.ML
     2     ID:         $Id$
     3     Author:     Markus Wenzel, TU Muenchen
     4 
     5 HTML presentation primitives.
     6 *)
     7 
     8 signature HTML =
     9 sig
    10   val output: string -> string
    11   val output_width: string -> string * real
    12   type text (* = string *)
    13   val plain: string -> text
    14   val name: string -> text
    15   val keyword: string -> text
    16   val file_name: string -> text
    17   val file_path: Url.T -> text
    18   val href_name: string -> text -> text
    19   val href_path: Url.T -> text -> text
    20   val href_opt_name: string option -> text -> text
    21   val href_opt_path: Url.T option -> text -> text
    22   val para: text -> text
    23   val preform: text -> text
    24   val verbatim: string -> text
    25   val begin_index: Url.T * string -> Url.T * string -> Url.T option -> Url.T -> text
    26   val end_index: text
    27   val applet_pages: string -> Url.T -> Url.T * string -> bool -> (string * string) list
    28   val theory_entry: Url.T * string -> text
    29   val session_entries: (Url.T * string) list -> text
    30   val source: (string, 'a) Source.source -> text
    31   val begin_theory: Url.T * string -> string -> (Url.T option * string) list ->
    32     (Url.T option * Url.T * bool option) list -> text -> text
    33   val end_theory: text
    34   val ml_file: Url.T -> string -> text
    35   val result: string -> string -> thm -> text
    36   val results: string -> string -> thm list -> text
    37   val theorem: string -> thm -> text
    38   val theorems: string -> thm list -> text
    39   val section: string -> text
    40   val setup: (theory -> theory) list
    41 end;
    42 
    43 structure HTML: HTML =
    44 struct
    45 
    46 
    47 (** HTML print modes **)
    48 
    49 (* mode *)
    50 
    51 val htmlN = "HTML";
    52 fun html_mode f x = setmp print_mode [htmlN] f x;
    53 
    54 
    55 (* symbol output *)
    56 
    57 local
    58   val escape = fn "<" => "&lt;" | ">" => "&gt;" | "&" => "&amp;" | c => c;
    59 
    60   val output_sym =
    61     fn "\\<not>" => (1.0, "&not;")
    62      | "\\<hyphen>" => (1.0, "&shy;")
    63      | "\\<cdot>" => (1.0, "&middot;")
    64      | "\\<times>" => (1.0, "&times;")
    65      | "\\<copyright>" => (1.0, "&copy;")
    66      | "\\<mu>" => (1.0, "&micro;")
    67      | s => (real (size s), implode (map escape (explode s)));
    68 
    69   fun add_sym (width, (w: real, s)) = (width + w, s);
    70 in
    71 
    72 fun output_width str =
    73   if not (exists_string (equal "<" orf equal ">" orf equal "&") str) then (str, real (size str))
    74   else
    75     let val (width, syms) = foldl_map add_sym (0.0, map output_sym (Symbol.explode str))
    76     in (implode syms, width) end;
    77 
    78 val output = #1 o output_width;
    79 val output_symbols = map (#2 o output_sym);
    80 
    81 end;
    82 
    83 
    84 val _ = Symbol.add_mode htmlN output_width;
    85 
    86 
    87 (* token translations *)
    88 
    89 fun color col =
    90   apfst (enclose ("<font color=" ^ quote col ^ ">") "</font>") o output_width;
    91 
    92 val html_trans =
    93  [("class", color "red"),
    94   ("tfree", color "purple"),
    95   ("tvar",  color "purple"),
    96   ("free",  color "blue"),
    97   ("bound", color "green"),
    98   ("var",   color "blue"),
    99   ("xnum",  color "yellow"),
   100   ("xstr",  color "brown")];
   101 
   102 
   103 
   104 (** HTML markup **)
   105 
   106 type text = string;
   107 
   108 
   109 (* atoms *)
   110 
   111 val plain = output;
   112 fun name s = "<i>" ^ output s ^ "</i>";
   113 fun keyword s = "<b>" ^ output s ^ "</b>";
   114 fun file_name s = "<tt>" ^ output s ^ "</tt>";
   115 val file_path = file_name o Url.pack;
   116 
   117 
   118 (* misc *)
   119 
   120 fun href_name s txt = "<a href=" ^ quote s ^ ">" ^ txt ^ "</a>";
   121 fun href_path path txt = href_name (Url.pack path) txt;
   122 
   123 fun href_opt_name None txt = txt
   124   | href_opt_name (Some s) txt = href_name s txt;
   125 
   126 fun href_opt_path None txt = txt
   127   | href_opt_path (Some p) txt = href_path p txt;
   128 
   129 fun para txt = "\n<p>\n" ^ txt ^ "\n</p>\n";
   130 fun preform txt = "<pre>" ^ txt ^ "</pre>";
   131 val verbatim = preform o output;
   132 
   133 
   134 (* document *)
   135 
   136 fun begin_document title =
   137   "<html>\n\
   138   \<head>\n\
   139   \<title>" ^ plain (title ^ " (" ^ version ^ ")") ^ "</title>\n\
   140   \</head>\n\
   141   \\n\
   142   \<body>\n\
   143   \<h1>" ^ plain title ^ "</h1>\n"
   144 
   145 val end_document = "\n</body>\n</html>\n";
   146 
   147 fun gen_link how (path, name) = para (href_path path how ^ " to index of " ^ plain name);
   148 val up_link = gen_link "Up";
   149 val back_link = gen_link "Back";
   150 
   151 
   152 (* session index *)
   153 
   154 fun begin_index up (index_path, session) opt_readme graph =
   155   begin_document ("Index of " ^ session) ^ up_link up ^
   156   para ("View " ^ href_path graph "theory dependencies" ^
   157     (case opt_readme of None => "" | Some p => "<br>\nView " ^ href_path p "README")) ^
   158   "\n<hr>\n\n<h2>Theories</h2>\n<ul>\n";
   159 
   160 val end_index = end_document;
   161 
   162 fun choice chs s = space_implode " " (map (fn (s', lnk) =>
   163   enclose "[" "]" (if s = s' then keyword s' else href_name lnk s')) chs);
   164 
   165 fun applet_pages session codebase back alt_graph =
   166   let
   167     val choices = [("none", "small",  "small.html"),
   168                    ("none", "medium", "medium.html"),
   169                    ("none", "large",  "large.html"),
   170                    ("all",  "small",  "small_all.html"),
   171                    ("all",  "medium", "medium_all.html"),
   172                    ("all",  "large",  "large_all.html")];
   173 
   174     val sizes = [("small",  ("500", "400")),
   175                  ("medium", ("650", "520")),
   176                  ("large",  ("800", "640"))];
   177 
   178     fun applet_page (gtype, size, name) =
   179       let
   180         val (width, height) = the (assoc (sizes, size));
   181         val subsessions =
   182           if alt_graph then
   183             "View subsessions: " ^ choice (map (fn (x, _, z) => (x, z))
   184               (filter (fn (_, y, _) => y = size) choices)) gtype ^ "<br>\n"
   185           else "";
   186         val browser_size = "Set browser size: " ^
   187           choice (map (fn (_, y, z) => (y, z)) (filter (fn (x, _, _) => x = gtype) choices)) size;
   188       in
   189         (name, begin_document ("Theory dependencies of " ^ session) ^
   190           back_link back ^
   191           para (subsessions ^ browser_size) ^
   192           "\n<hr>\n<applet code=\"GraphBrowser/GraphBrowser.class\" \
   193           \codebase=\"" ^ Url.pack codebase ^ "\" width=" ^ width ^ " height=" ^ height ^ ">\n\
   194           \<param name=\"graphfile\" value=\"" ^
   195           (if gtype = "all" then "all_sessions.graph" else "session.graph") ^ "\">\n\
   196           \</applet>\n<hr>" ^ end_document)
   197       end;
   198   in map applet_page (take (if alt_graph then 6 else 3, choices)) end;
   199 
   200 
   201 fun entry (p, s) = "<li>" ^ href_path p (plain s) ^ "\n";
   202 
   203 val theory_entry = entry;
   204 
   205 fun session_entries [] = "</ul>"
   206   | session_entries es =
   207       "</ul>\n<hr>\n\n<h2>Sessions</h2>\n<ul>\n" ^ implode (map entry es) ^ "</ul>";
   208 
   209 
   210 (* theory *)
   211 
   212 fun output_source src = implode (output_symbols (Source.exhaust (Symbol.source false src)));
   213 fun source src = "\n<hr>\n<pre>" ^ output_source src ^ "</pre>\n<hr>\n";
   214 
   215 
   216 local
   217   fun encl None = enclose "[" "]"
   218     | encl (Some false) = enclose "(" ")"
   219     | encl (Some true) = I;
   220 
   221   fun file (opt_ref, path, loadit) = href_opt_path opt_ref (encl loadit (file_path path));
   222   val files = space_implode " + " o map file;
   223   val parents = space_implode " + " o map (uncurry href_opt_path o apsnd name);
   224 
   225   fun theory up A =
   226     begin_document ("Theory " ^ A) ^ "\n" ^ up_link up ^
   227     keyword "theory" ^ " " ^ name A ^ " = ";
   228 in
   229 
   230 fun begin_theory up A Bs [] body = theory up A ^ parents Bs ^ ":\n" ^ body
   231   | begin_theory up A Bs Ps body = theory up A ^ parents Bs ^ "<br>" ^ keyword "files" ^
   232       " " ^ files Ps ^ ":" ^ "\n" ^ body;
   233 end;
   234 
   235 val end_theory = end_document;
   236 
   237 
   238 (* ML file *)
   239 
   240 fun ml_file path str =
   241   begin_document ("File " ^ Url.pack path) ^
   242   "\n<hr>\n" ^ verbatim str ^ "\n<hr>\n" ^ end_document;
   243 
   244 
   245 (* theorems *)
   246 
   247 local
   248 
   249 val string_of_thm =
   250   Pretty.setmp_margin 80 Pretty.string_of o
   251     Display.pretty_thm_no_quote o #1 o Drule.freeze_thaw;
   252 
   253 fun thm th = preform (prefix_lines "  " (html_mode string_of_thm th));
   254 
   255 fun thm_name "" = ""
   256   | thm_name a = " " ^ name (a ^ ":");
   257 
   258 in
   259 
   260 fun result k a th = para (keyword k ^ thm_name a ^ "\n" ^ thm th);
   261 fun results k a ths = para (cat_lines ((keyword k ^ thm_name a) :: map thm ths));
   262 
   263 val theorem = result "theorem";
   264 val theorems = results "theorems";
   265 
   266 end;
   267 
   268 
   269 (* section *)
   270 
   271 fun section heading = "\n\n<h2>" ^ plain heading ^ "</h2>\n";
   272 
   273 
   274 
   275 (** theory setup **)
   276 
   277 val setup =
   278  [Theory.add_mode_tokentrfuns htmlN html_trans];
   279 
   280 
   281 end;