src/Pure/Thy/html.ML
author wenzelm
Tue, 09 Mar 1999 12:12:45 +0100
changeset 6324 3b7111b360b1
child 6338 bf0d22535e47
permissions -rw-r--r--
HTML markup elements.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6324
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/Thy/HTML.ML
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
     3
    Author:     Markus Wenzel, TU Muenchen
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
     4
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
     5
HTML markup elements.
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
     6
*)
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
     7
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
     8
signature HTML =
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
     9
sig
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    10
  val output: string -> string
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    11
  val output_width: string -> string * real
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    12
  type text (* = string *)
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    13
  val plain: string -> text
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    14
  val name: string -> text
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    15
  val keyword: string -> text
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    16
  val file_name: string -> text
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    17
  val file_path: Path.T -> text
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    18
  val para: text -> text
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    19
  val href_name: string -> text -> text
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    20
  val href_path: Path.T -> text -> text
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    21
  val preform: text -> text
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    22
  val verbatim: string -> text
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    23
  val begin_document: string -> text
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    24
  val end_document: text
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    25
  val insert_here: text
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    26
  val source: (string, 'a) Source.source -> text
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    27
  val begin_theory: Path.T * string -> string -> string list -> ((Path.T * Path.T) * bool) list
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    28
    -> text -> text
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    29
  val end_theory: text
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    30
  val ml_file: Path.T -> string -> text
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    31
  val conclude_theory: text
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    32
  val theorem: string -> thm -> text
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    33
  val section: string -> text
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    34
  val setup: (theory -> theory) list
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    35
end;
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    36
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    37
structure HTML: HTML =
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    38
struct
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    39
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    40
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    41
(** HTML print modes **)
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    42
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    43
(* mode *)
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    44
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    45
val htmlN = "HTML";
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    46
fun html_mode f x = setmp print_mode [htmlN] f x;
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    47
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    48
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    49
(* symbol output *)
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    50
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    51
val escape = fn "<" => "&lt;" | ">" => "&gt;" | "&" => "&amp;" | c => c;
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    52
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    53
val output_chars = implode o map escape;
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    54
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    55
fun output s =
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    56
  if not (exists_string (equal "<" orf equal ">" orf equal "&") s) then s
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    57
  else implode (map escape (explode s));        (*sic!*)
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    58
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    59
fun output_width s = (output s, real (size s));
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    60
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    61
val _ = Symbol.add_mode htmlN output_width;
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    62
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    63
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    64
(* token translations *)
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    65
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    66
fun tagit bg en = apfst (enclose bg en) o output_width;
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    67
fun color col = tagit ("<font color=" ^ quote col ^ ">") "</font>";
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    68
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    69
val html_trans =
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    70
 [("class", color "red"),
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    71
  ("tfree", color "purple"),
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    72
  ("tvar",  color "purple"),
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    73
  ("free",  color "blue"),
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    74
  ("bound", color "green"),
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    75
  ("var",   color "blue"),
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    76
  ("xnum",  color "yellow"),
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    77
  ("xstr",  color "brown")];
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    78
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    79
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    80
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    81
(** HTML markup **)
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    82
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    83
type text = string;
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    84
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    85
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    86
(* atoms *)
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    87
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    88
val plain = output;
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    89
fun name s = "<i>" ^ output s ^ "</i>";
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    90
fun keyword s = "<b>" ^ output s ^ "</b>";
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    91
fun file_name s = "<tt>" ^ output s ^ "</tt>";
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    92
val file_path = file_name o Path.pack;
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    93
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    94
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    95
(* misc *)
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    96
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    97
fun para txt = "\n<p>\n" ^ txt ^ "\n</p>\n";
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    98
fun href_name s txt = "<a href=" ^ quote s ^ ">" ^ txt ^ "</a>";
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
    99
fun href_path path txt = href_name (Path.pack path) txt;
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   100
fun preform txt = "<pre>" ^ txt ^ "</pre>";
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   101
val verbatim = preform o output;
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   102
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   103
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   104
(* document *)
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   105
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   106
fun begin_document title =
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   107
  let val txt = plain title in
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   108
    "<html>\n\
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   109
    \<head>\n\
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   110
    \<title>" ^ txt ^ "</title>\n\
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   111
    \</head>\n\
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   112
    \\n\
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   113
    \<body>\n\
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   114
    \<h1>" ^ txt ^ "</h1>\n"
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   115
  end;
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   116
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   117
val end_document =
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   118
  "</body>\n\
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   119
  \</html>\n";
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   120
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   121
val insert_here = "<!--insert--here-->";
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   122
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   123
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   124
(* theory *)
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   125
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   126
fun source src =
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   127
  "\n<hr>\n<pre>" ^ output_chars (Source.exhaust src) ^ "</pre>\n<hr>\n";
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   128
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   129
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   130
local
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   131
  fun file ((p, p'), loadit) =
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   132
    href_path p' ((if not loadit then enclose "(" ")" else I) (file_path p));
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   133
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   134
  fun suffix_last _ [] = []
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   135
    | suffix_last s lst = let val (xs, x) = split_last lst in xs @ [x ^ s] end;
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   136
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   137
  val plus_names = space_implode " + " o map name;
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   138
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   139
  fun theory (back_path, back_name) A =
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   140
    begin_document ("Theory " ^ A) ^ "\n" ^
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   141
    href_path back_path "Back" ^ " to the index of " ^ plain back_name ^ "\n<p>\n" ^
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   142
    keyword "theory" ^ " " ^ name A ^ " = ";
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   143
in
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   144
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   145
fun begin_theory back A Bs [] body = theory back A ^ plus_names (suffix_last ":" Bs) ^ "\n" ^ body
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   146
  | begin_theory back A Bs Ps body =
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   147
      theory back A ^ plus_names Bs ^
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   148
      "<br>" ^ keyword "files" ^ " " ^ space_implode " + " (map file Ps) ^ ":" ^ "\n" ^ body;
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   149
end;
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   150
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   151
val end_theory = "";
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   152
val conclude_theory = end_document;
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   153
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   154
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   155
(* ML file *)
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   156
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   157
fun ml_file path str =
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   158
  begin_document ("File " ^ Path.pack path) ^
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   159
  "\n<hr>\n" ^ verbatim str ^ "\n<hr>\n" ^ end_document;
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   160
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   161
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   162
(* theorem *)
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   163
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   164
(* FIXME improve freeze_thaw (?) *)
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   165
val string_of_thm =
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   166
  Pretty.setmp_margin 80 Pretty.string_of o
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   167
    Display.pretty_thm_no_quote o #1 o Drule.freeze_thaw;
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   168
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   169
fun thm th = preform (prefix_lines "  " (html_mode string_of_thm th));
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   170
fun theorem a th = para (keyword "theorem" ^ " " ^ name (a ^ ":") ^ "\n" ^ thm th);
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   171
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   172
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   173
(* section *)
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   174
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   175
fun section heading = "\n\n<h2>" ^ plain heading ^ "</h2>\n";
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   176
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   177
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   178
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   179
(** theory setup **)
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   180
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   181
val setup =
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   182
 [Theory.add_mode_tokentrfuns htmlN html_trans];
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   183
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   184
3b7111b360b1 HTML markup elements.
wenzelm
parents:
diff changeset
   185
end;