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