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