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