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