6324
|
1 |
(* Title: Pure/Thy/HTML.ML
|
|
2 |
ID: $Id$
|
|
3 |
Author: Markus Wenzel, TU Muenchen
|
|
4 |
|
6338
|
5 |
HTML presentation primitives.
|
6324
|
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 href_name: string -> text -> text
|
|
19 |
val href_path: Path.T -> text -> text
|
6376
|
20 |
val href_opt_name: string option -> text -> text
|
|
21 |
val href_opt_path: Path.T option -> text -> text
|
|
22 |
val para: text -> text
|
6324
|
23 |
val preform: text -> text
|
|
24 |
val verbatim: string -> text
|
|
25 |
val begin_document: string -> text
|
|
26 |
val end_document: text
|
6338
|
27 |
val begin_index: Path.T * string -> Path.T * string -> Path.T option -> text
|
|
28 |
val end_index: text
|
|
29 |
val theory_entry: Path.T * string -> text
|
|
30 |
val session_entries: (Path.T * string) list -> text
|
6324
|
31 |
val source: (string, 'a) Source.source -> text
|
6361
|
32 |
val begin_theory: Path.T * string -> string -> (Path.T option * string) list ->
|
6347
|
33 |
(Path.T option * Path.T * bool) list -> text -> text
|
6324
|
34 |
val end_theory: text
|
|
35 |
val ml_file: Path.T -> string -> text
|
|
36 |
val theorem: string -> thm -> text
|
|
37 |
val section: string -> text
|
|
38 |
val setup: (theory -> theory) list
|
|
39 |
end;
|
|
40 |
|
|
41 |
structure HTML: HTML =
|
|
42 |
struct
|
|
43 |
|
|
44 |
|
|
45 |
(** HTML print modes **)
|
|
46 |
|
|
47 |
(* mode *)
|
|
48 |
|
|
49 |
val htmlN = "HTML";
|
|
50 |
fun html_mode f x = setmp print_mode [htmlN] f x;
|
|
51 |
|
|
52 |
|
|
53 |
(* symbol output *)
|
|
54 |
|
6338
|
55 |
local
|
|
56 |
val escape = fn "<" => "<" | ">" => ">" | "&" => "&" | c => c;
|
6324
|
57 |
|
6338
|
58 |
val output_sym =
|
|
59 |
fn "\\<not>" => (1.0, "¬")
|
|
60 |
| "\\<hyphen>" => (1.0, "­")
|
|
61 |
| "\\<cdot>" => (1.0, "·")
|
|
62 |
| "\\<times>" => (1.0, "×")
|
|
63 |
| "\\<copyright>" => (1.0, "©")
|
|
64 |
| "\\<mu>" => (1.0, "µ")
|
|
65 |
| s => (real (size s), implode (map escape (explode s)));
|
6324
|
66 |
|
6338
|
67 |
fun add_sym (width, (w: real, s)) = (width + w, s);
|
|
68 |
in
|
6324
|
69 |
|
6338
|
70 |
fun output_width str =
|
|
71 |
if not (exists_string (equal "<" orf equal ">" orf equal "&") str) then (str, real (size str))
|
|
72 |
else
|
|
73 |
let val (width, syms) = foldl_map add_sym (0.0, map output_sym (Symbol.explode str))
|
|
74 |
in (implode syms, width) end;
|
|
75 |
|
|
76 |
val output = #1 o output_width;
|
|
77 |
val output_symbols = map (#2 o output_sym);
|
|
78 |
|
|
79 |
end;
|
|
80 |
|
6324
|
81 |
|
|
82 |
val _ = Symbol.add_mode htmlN output_width;
|
|
83 |
|
|
84 |
|
|
85 |
(* token translations *)
|
|
86 |
|
6347
|
87 |
fun color col =
|
|
88 |
apfst (enclose ("<font color=" ^ quote col ^ ">") "</font>") o output_width;
|
6324
|
89 |
|
|
90 |
val html_trans =
|
|
91 |
[("class", color "red"),
|
|
92 |
("tfree", color "purple"),
|
|
93 |
("tvar", color "purple"),
|
|
94 |
("free", color "blue"),
|
|
95 |
("bound", color "green"),
|
|
96 |
("var", color "blue"),
|
|
97 |
("xnum", color "yellow"),
|
|
98 |
("xstr", color "brown")];
|
|
99 |
|
|
100 |
|
|
101 |
|
|
102 |
(** HTML markup **)
|
|
103 |
|
|
104 |
type text = string;
|
|
105 |
|
|
106 |
|
|
107 |
(* atoms *)
|
|
108 |
|
|
109 |
val plain = output;
|
|
110 |
fun name s = "<i>" ^ output s ^ "</i>";
|
|
111 |
fun keyword s = "<b>" ^ output s ^ "</b>";
|
|
112 |
fun file_name s = "<tt>" ^ output s ^ "</tt>";
|
|
113 |
val file_path = file_name o Path.pack;
|
|
114 |
|
|
115 |
|
|
116 |
(* misc *)
|
|
117 |
|
|
118 |
fun href_name s txt = "<a href=" ^ quote s ^ ">" ^ txt ^ "</a>";
|
|
119 |
fun href_path path txt = href_name (Path.pack path) txt;
|
6376
|
120 |
|
|
121 |
fun href_opt_name None txt = txt
|
|
122 |
| href_opt_name (Some s) txt = href_name s txt;
|
|
123 |
|
|
124 |
fun href_opt_path None txt = txt
|
|
125 |
| href_opt_path (Some p) txt = href_path p txt;
|
|
126 |
|
|
127 |
fun para txt = "\n<p>\n" ^ txt ^ "\n</p>\n";
|
6324
|
128 |
fun preform txt = "<pre>" ^ txt ^ "</pre>";
|
|
129 |
val verbatim = preform o output;
|
|
130 |
|
|
131 |
|
|
132 |
(* document *)
|
|
133 |
|
|
134 |
fun begin_document title =
|
|
135 |
let val txt = plain title in
|
|
136 |
"<html>\n\
|
|
137 |
\<head>\n\
|
|
138 |
\<title>" ^ txt ^ "</title>\n\
|
|
139 |
\</head>\n\
|
|
140 |
\\n\
|
|
141 |
\<body>\n\
|
|
142 |
\<h1>" ^ txt ^ "</h1>\n"
|
|
143 |
end;
|
|
144 |
|
|
145 |
val end_document =
|
|
146 |
"</body>\n\
|
|
147 |
\</html>\n";
|
|
148 |
|
6376
|
149 |
fun up_link (up_path, up_name) =
|
|
150 |
para (href_path up_path "Up" ^ " to index of " ^ plain up_name);
|
6338
|
151 |
|
|
152 |
|
|
153 |
(* session index *)
|
|
154 |
|
6376
|
155 |
fun begin_index up (index_path, session) opt_readme =
|
|
156 |
begin_document ("Index of " ^ session) ^ up_link up ^
|
6347
|
157 |
(case opt_readme of None => "" | Some p => href_path p "README" ^ " file") ^
|
6338
|
158 |
"\n<hr>\n\n<h2>Theories</h2>\n";
|
|
159 |
|
|
160 |
val end_index = end_document;
|
|
161 |
|
|
162 |
fun entry (p, s) = href_path p (plain s) ^ "<br>\n";
|
|
163 |
val theory_entry = entry;
|
|
164 |
|
|
165 |
fun session_entries [] = ""
|
|
166 |
| session_entries es = "\n<hr>\n\n<h2>Sessions</h2>\n" ^ cat_lines (map entry es);
|
6324
|
167 |
|
|
168 |
|
|
169 |
(* theory *)
|
|
170 |
|
6338
|
171 |
fun output_source src = implode (output_symbols (Source.exhaust (Symbol.source false src)));
|
|
172 |
fun source src = "\n<hr>\n<pre>" ^ output_source src ^ "</pre>\n<hr>\n";
|
6324
|
173 |
|
|
174 |
|
|
175 |
local
|
6376
|
176 |
fun file (opt_ref, path, loadit) = href_opt_path opt_ref
|
|
177 |
((if not loadit then enclose "(" ")" else I) (file_path path));
|
6324
|
178 |
|
6376
|
179 |
val files = space_implode " + " o map file;
|
|
180 |
val parents = space_implode " + " o map (uncurry href_opt_path o apsnd name);
|
6361
|
181 |
|
6376
|
182 |
fun theory up A =
|
|
183 |
begin_document ("Theory " ^ A) ^ "\n" ^ up_link up ^
|
6324
|
184 |
keyword "theory" ^ " " ^ name A ^ " = ";
|
|
185 |
in
|
|
186 |
|
6376
|
187 |
fun begin_theory up A Bs [] body = theory up A ^ parents Bs ^ ":\n" ^ body
|
|
188 |
| begin_theory up A Bs Ps body = theory up A ^ parents Bs ^ "<br>" ^ keyword "files" ^
|
|
189 |
" " ^ files Ps ^ ":" ^ "\n" ^ body;
|
6324
|
190 |
end;
|
|
191 |
|
6338
|
192 |
val end_theory = end_document;
|
6324
|
193 |
|
|
194 |
|
|
195 |
(* ML file *)
|
|
196 |
|
|
197 |
fun ml_file path str =
|
|
198 |
begin_document ("File " ^ Path.pack path) ^
|
|
199 |
"\n<hr>\n" ^ verbatim str ^ "\n<hr>\n" ^ end_document;
|
|
200 |
|
|
201 |
|
|
202 |
(* theorem *)
|
|
203 |
|
6376
|
204 |
val string_of_thm = (* FIXME improve freeze_thaw (?) *)
|
6324
|
205 |
Pretty.setmp_margin 80 Pretty.string_of o
|
|
206 |
Display.pretty_thm_no_quote o #1 o Drule.freeze_thaw;
|
|
207 |
|
|
208 |
fun thm th = preform (prefix_lines " " (html_mode string_of_thm th));
|
|
209 |
fun theorem a th = para (keyword "theorem" ^ " " ^ name (a ^ ":") ^ "\n" ^ thm th);
|
|
210 |
|
|
211 |
|
|
212 |
(* section *)
|
|
213 |
|
|
214 |
fun section heading = "\n\n<h2>" ^ plain heading ^ "</h2>\n";
|
|
215 |
|
|
216 |
|
|
217 |
|
|
218 |
(** theory setup **)
|
|
219 |
|
|
220 |
val setup =
|
|
221 |
[Theory.add_mode_tokentrfuns htmlN html_trans];
|
|
222 |
|
|
223 |
|
|
224 |
end;
|