1 (* Title: Pure/Thy/present.ML
3 Author: Markus Wenzel and Stefan Berghofer, TU Muenchen
5 Theory presentation: HTML, graph files, (PDF)LaTeX documents.
8 signature BASIC_PRESENT =
10 val no_document: ('a -> 'b) -> 'a -> 'b
11 val section: string -> unit
17 val get_info: theory -> {name: string, session: string list, is_local: bool}
18 val write_graph: {name: string, ID: string, dir: string, unfold: bool,
19 path: string, parents: string list} list -> Path.T -> unit
20 val init: bool -> bool -> string -> bool -> string list -> string list ->
21 string -> (bool * Path.T) option -> Url.T option * bool -> bool -> unit
22 val finish: unit -> unit
23 val init_theory: string -> unit
24 val verbatim_source: string -> (unit -> Symbol.symbol list) -> unit
25 val old_symbol_source: string -> (unit -> Symbol.symbol list) -> unit
26 val theory_output: string -> string -> unit
27 val begin_theory: Path.T option -> string -> string list ->
28 (Path.T * bool) list -> theory -> theory
29 val add_hook: (string -> (string * thm list) list -> unit) -> unit
30 val results: string -> (string * thm list) list -> unit
31 val theorem: string -> thm -> unit
32 val theorems: string -> thm list -> unit
33 val chapter: string -> unit
34 val subsection: string -> unit
35 val subsubsection: string -> unit
36 val drafts: string -> Path.T list -> Path.T
39 structure Present: PRESENT =
45 val output_path = Path.variable "ISABELLE_BROWSER_INFO";
47 val tex_ext = Path.ext "tex";
48 val tex_path = tex_ext o Path.basic;
49 val html_ext = Path.ext "html";
50 val html_path = html_ext o Path.basic;
51 val index_path = Path.basic "index.html";
52 val readme_html_path = Path.basic "README.html";
53 val readme_path = Path.basic "README";
54 val documentN = "document";
55 val document_path = Path.basic documentN;
56 val doc_indexN = "session";
57 val graph_path = Path.basic "session.graph";
58 val graph_pdf_path = Path.basic "session_graph.pdf";
59 val graph_eps_path = Path.basic "session_graph.eps";
61 val session_path = Path.basic ".session";
62 val session_entries_path = Path.unpack ".session/entries";
63 val pre_index_path = Path.unpack ".session/pre-index";
65 fun mk_rel_path [] ys = Path.make ys
66 | mk_rel_path xs [] = Path.appends (replicate (length xs) Path.parent)
67 | mk_rel_path (ps as x :: xs) (qs as y :: ys) = if x = y then mk_rel_path xs ys else
68 Path.appends (replicate (length ps) Path.parent @ [Path.make qs]);
70 fun show_path path = Path.pack (Path.append (File.pwd ()) path);
74 (** additional theory data **)
76 structure BrowserInfoData = TheoryDataFun
78 val name = "Pure/browser_info";
79 type T = {name: string, session: string list, is_local: bool};
80 val empty = {name = "", session = [], is_local = false}: T;
83 fun merge _ _ = empty;
87 val _ = Context.add_setup [BrowserInfoData.init];
90 if Context.theory_name thy mem_string [Context.ProtoPureN, Context.PureN, Context.CPureN]
91 then {name = Context.PureN, session = [], is_local = false}
92 else BrowserInfoData.get thy;
99 {name: string, ID: string, dir: string, unfold: bool,
100 path: string, parents: string list};
102 fun write_graph gr path =
103 File.write path (cat_lines (map (fn {name, ID, dir, unfold, path, parents} =>
104 "\"" ^ name ^ "\" \"" ^ ID ^ "\" \"" ^ dir ^ (if unfold then "\" + \"" else "\" \"") ^
105 path ^ "\" > " ^ space_implode " " (map Library.quote parents) ^ " ;") gr));
107 fun ID_of sess s = space_implode "/" (sess @ [s]);
109 (*retrieve graph data from initial theory loader database*)
110 fun init_graph remote_path curr_sess = map (fn name =>
112 val {name = sess_name, session, is_local} = get_info (ThyInfo.theory name);
113 val path' = Path.append (Path.make session) (html_path name);
115 {name = name, ID = ID_of session name, dir = sess_name,
117 if null session then "" else
118 if is_some remote_path andalso not is_local then
119 Url.pack (Url.append (the remote_path) (Url.File
120 (Path.append (Path.make session) (html_path name))))
121 else Path.pack (Path.append (mk_rel_path curr_sess session) (html_path name)),
124 map (fn s => ID_of (#session (get_info (ThyInfo.theory s))) s) (ThyInfo.get_preds name)}
125 end) (ThyInfo.names ());
127 fun ins_graph_entry (entry as {ID, ...}) (gr: graph_node list) =
128 filter_out (fn entry' => #ID entry' = ID) gr @ [entry];
132 (** global browser info state **)
134 (* type theory_info *)
136 type theory_info = {tex_source: Buffer.T, html_source: Buffer.T, html: Buffer.T};
138 fun make_theory_info (tex_source, html_source, html) =
139 {tex_source = tex_source, html_source = html_source, html = html}: theory_info;
141 val empty_theory_info = make_theory_info (Buffer.empty, Buffer.empty, Buffer.empty);
143 fun map_theory_info f {tex_source, html_source, html} =
144 make_theory_info (f (tex_source, html_source, html));
147 (* type browser_info *)
149 type browser_info = {theories: theory_info Symtab.table, files: (Path.T * string) list,
150 tex_index: Buffer.T, html_index: Buffer.T, graph: graph_node list};
152 fun make_browser_info (theories, files, tex_index, html_index, graph) =
153 {theories = theories, files = files, tex_index = tex_index, html_index = html_index,
154 graph = graph}: browser_info;
156 val empty_browser_info = make_browser_info (Symtab.empty, [], Buffer.empty, Buffer.empty, []);
158 fun init_browser_info remote_path curr_sess = make_browser_info
159 (Symtab.empty, [], Buffer.empty, Buffer.empty, init_graph remote_path curr_sess);
161 fun map_browser_info f {theories, files, tex_index, html_index, graph} =
162 make_browser_info (f (theories, files, tex_index, html_index, graph));
167 val browser_info = ref empty_browser_info;
168 fun change_browser_info f = browser_info := map_browser_info f (! browser_info);
170 val suppress_tex_source = ref false;
171 fun no_document f x = Library.setmp suppress_tex_source true f x;
173 fun init_theory_info name info =
174 change_browser_info (fn (theories, files, tex_index, html_index, graph) =>
175 (Symtab.update (name, info) theories, files, tex_index, html_index, graph));
177 fun change_theory_info name f =
178 change_browser_info (fn (info as (theories, files, tex_index, html_index, graph)) =>
179 (case Symtab.lookup theories name of
180 NONE => (warning ("Browser info: cannot access theory document " ^ quote name); info)
181 | SOME info => (Symtab.update (name, map_theory_info f info) theories, files,
182 tex_index, html_index, graph)));
186 change_browser_info (fn (theories, files, tex_index, html_index, graph) =>
187 (theories, file :: files, tex_index, html_index, graph));
189 fun add_tex_index txt =
190 change_browser_info (fn (theories, files, tex_index, html_index, graph) =>
191 (theories, files, Buffer.add txt tex_index, html_index, graph));
193 fun add_html_index txt =
194 change_browser_info (fn (theories, files, tex_index, html_index, graph) =>
195 (theories, files, tex_index, Buffer.add txt html_index, graph));
197 fun add_graph_entry e =
198 change_browser_info (fn (theories, files, tex_index, html_index, graph) =>
199 (theories, files, tex_index, html_index, ins_graph_entry e graph));
201 fun add_tex_source name txt =
202 if ! suppress_tex_source then ()
203 else change_theory_info name (fn (tex_source, html_source, html) =>
204 (Buffer.add txt tex_source, html_source, html));
206 fun add_html_source name txt = change_theory_info name (fn (tex_source, html_source, html) =>
207 (tex_source, Buffer.add txt html_source, html));
209 fun add_html name txt = change_theory_info name (fn (tex_source, html_source, html) =>
210 (tex_source, html_source, Buffer.add txt html));
214 (** global session state **)
219 {name: string, parent: string, session: string, path: string list, html_prefix: Path.T,
220 info: bool, doc_format: string, doc_graph: bool, documents: (string * string) list,
221 doc_prefix1: (Path.T * Path.T) option, doc_prefix2: (bool * Path.T) option,
222 remote_path: Url.T option, verbose: bool, readme: Path.T option};
224 fun make_session_info
225 (name, parent, session, path, html_prefix, info, doc_format, doc_graph, documents,
226 doc_prefix1, doc_prefix2, remote_path, verbose, readme) =
227 {name = name, parent = parent, session = session, path = path, html_prefix = html_prefix,
228 info = info, doc_format = doc_format, doc_graph = doc_graph, documents = documents,
229 doc_prefix1 = doc_prefix1, doc_prefix2 = doc_prefix2, remote_path = remote_path,
230 verbose = verbose, readme = readme}: session_info;
235 val session_info = ref (NONE: session_info option);
237 fun with_session x f = (case ! session_info of NONE => x | SOME info => f info);
238 fun with_context f = f (Context.theory_name (Context.the_context ()));
242 (** document preparation **)
246 val session_entries =
247 HTML.session_entries o
248 map (fn name => (Url.File (Path.append (Path.basic name) index_path), name));
250 fun get_entries dir =
251 split_lines (File.read (Path.append dir session_entries_path));
253 fun put_entries entries dir =
254 File.write (Path.append dir session_entries_path) (cat_lines entries);
257 fun create_index dir =
258 File.read (Path.append dir pre_index_path) ^
259 session_entries (get_entries dir) ^ HTML.end_index
260 |> File.write (Path.append dir index_path);
262 fun update_index dir name =
263 (case try get_entries dir of
264 NONE => warning ("Browser info: cannot access session index of " ^ quote (Path.pack dir))
265 | SOME es => (put_entries ((es \ name) @ [name]) dir; create_index dir));
268 (* document versions *)
270 fun read_version str =
271 (case space_explode "=" str of
273 | [name, tags] => (name, tags)
274 | _ => error ("Malformed document version specification: " ^ quote str));
276 fun read_versions strs =
277 rev (gen_distinct eq_fst (rev ((documentN, "") :: map read_version strs)))
278 |> filter_out (equal "-" o #2);
283 fun name_of_session elems = space_implode "/" ("Isabelle" :: elems);
285 fun init build info doc doc_graph doc_versions path name doc_prefix2
286 (remote_path, first_time) verbose =
287 if not build andalso not info andalso doc = "" andalso is_none doc_prefix2 then
288 (browser_info := empty_browser_info; session_info := NONE)
291 val parent_name = name_of_session (Library.take (length path - 1, path));
292 val session_name = name_of_session path;
293 val sess_prefix = Path.make path;
294 val html_prefix = Path.append (Path.expand output_path) sess_prefix;
296 val (doc_prefix1, documents) =
297 if doc = "" then (NONE, [])
298 else if not (File.exists document_path) then (conditional verbose (fn () =>
299 std_error "Warning: missing document directory\n"); (NONE, []))
300 else (SOME (Path.append html_prefix document_path, html_prefix),
301 read_versions doc_versions);
303 val parent_index_path = Path.append Path.parent index_path;
304 val index_up_lnk = if first_time then
305 Url.append (the remote_path) (Url.File (Path.append sess_prefix parent_index_path))
306 else Url.File parent_index_path;
308 if File.exists readme_html_path then SOME readme_html_path
309 else if File.exists readme_path then SOME readme_path
313 (case readme of NONE => [] | SOME p => [(Url.File p, "README")]) @
314 map (fn (name, _) => (Url.File (Path.ext doc (Path.basic name)), name)) documents;
315 val index_text = HTML.begin_index (index_up_lnk, parent_name)
316 (Url.File index_path, session_name) docs (Url.unpack "medium.html");
318 session_info := SOME (make_session_info (name, parent_name, session_name, path, html_prefix,
319 info, doc, doc_graph, documents, doc_prefix1, doc_prefix2, remote_path, verbose, readme));
320 browser_info := init_browser_info remote_path path;
321 add_html_index index_text
325 (* isatool wrappers *)
327 fun isatool_document verbose format name tags path result_path =
329 val s = "\"$ISATOOL\" document -c -o '" ^ format ^ "' \
330 \-n '" ^ name ^ "' -t '" ^ tags ^ "' " ^ File.shell_path path ^
331 " 2>&1" ^ (if verbose then "" else " >/dev/null");
332 val doc_path = Path.append result_path (Path.ext format (Path.basic name));
334 conditional verbose (fn () => writeln s);
336 conditional (not (File.exists doc_path)) (fn () =>
337 error ("No document: " ^ quote (show_path doc_path)));
341 fun isatool_browser graph =
343 val pdf_path = File.tmp_path graph_pdf_path;
344 val eps_path = File.tmp_path graph_eps_path;
345 val gr_path = File.tmp_path graph_path;
346 val s = "browser -o " ^ File.shell_path pdf_path ^ " " ^ File.shell_path gr_path;
348 write_graph graph gr_path;
349 if File.isatool s <> 0 orelse not (File.exists eps_path) orelse not (File.exists pdf_path)
350 then error "Failed to prepare dependency graph"
352 let val pdf = File.read pdf_path and eps = File.read eps_path
353 in File.rm pdf_path; File.rm eps_path; File.rm gr_path; (pdf, eps) end
357 (* finish session -- output all generated text *)
359 fun write_tex src name path =
360 Buffer.write (Path.append path (tex_path name)) src;
362 fun write_tex_index tex_index path =
363 write_tex (Buffer.add Latex.tex_trailer tex_index) doc_indexN path;
366 fun finish () = with_session () (fn {name, info, html_prefix, doc_format, doc_graph,
367 documents, doc_prefix1, doc_prefix2, path, verbose, readme, ...} =>
369 val {theories, files, tex_index, html_index, graph} = ! browser_info;
370 val thys = Symtab.dest theories;
371 val parent_html_prefix = Path.append html_prefix Path.parent;
373 fun finish_tex path (a, {tex_source, ...}: theory_info) = write_tex tex_source a path;
374 fun finish_html (a, {html, ...}: theory_info) =
375 Buffer.write (Path.append html_prefix (html_path a)) (Buffer.add HTML.end_theory html);
378 if doc_graph andalso (is_some doc_prefix1 orelse is_some doc_prefix2) then
379 SOME (isatool_browser graph)
382 fun prepare_sources cp path =
384 if cp then File.copy_dir document_path path else ();
385 File.isatool ("latex -o sty " ^ File.shell_path (Path.append path (Path.basic "root.tex")));
386 (case opt_graphs of NONE => () | SOME (pdf, eps) =>
387 (File.write (Path.append path graph_pdf_path) pdf;
388 File.write (Path.append path graph_eps_path) eps));
389 write_tex_index tex_index path;
390 List.app (finish_tex path) thys);
392 conditional info (fn () =>
393 (File.mkdir (Path.append html_prefix session_path);
394 Buffer.write (Path.append html_prefix pre_index_path) html_index;
395 File.write (Path.append html_prefix session_entries_path) "";
396 create_index html_prefix;
397 if length path > 1 then update_index parent_html_prefix name else ();
398 (case readme of NONE => () | SOME path => File.copy path html_prefix);
399 write_graph graph (Path.append html_prefix graph_path);
400 File.copy (Path.unpack "~~/lib/browser/GraphBrowser.jar") html_prefix;
401 List.app (fn (a, txt) => File.write (Path.append html_prefix (Path.basic a)) txt)
402 (HTML.applet_pages name (Url.File index_path, name));
403 File.copy (Path.unpack "~~/lib/html/isabelle.css") html_prefix;
404 List.app finish_html thys;
405 List.app (uncurry File.write) files;
406 conditional verbose (fn () =>
407 std_error ("Browser info at " ^ show_path html_prefix ^ "\n"))));
409 (case doc_prefix2 of NONE => () | SOME (cp, path) =>
410 (prepare_sources cp path;
411 conditional verbose (fn () => std_error ("Document sources at " ^ show_path path ^ "\n"))));
413 (case doc_prefix1 of NONE => () | SOME (path, result_path) =>
414 documents |> List.app (fn (name, tags) =>
416 val _ = prepare_sources true path;
417 val doc_path = isatool_document true doc_format name tags path result_path;
419 conditional verbose (fn () => std_error ("Document at " ^ show_path doc_path ^ "\n"))
422 browser_info := empty_browser_info;
427 (* theory elements *)
429 fun init_theory name = with_session () (fn _ => init_theory_info name empty_theory_info);
431 fun verbatim_source name mk_text =
432 with_session () (fn _ => add_html_source name (HTML.verbatim_source (mk_text ())));
434 fun old_symbol_source name mk_text =
435 with_session () (fn _ => add_tex_source name
436 (Latex.symbol_source (K true, K true) name (mk_text ())));
438 fun theory_output name s =
439 with_session () (fn _ => add_tex_source name (Latex.isabelle_file name s));
442 fun parent_link remote_path curr_session name =
443 let val {name = _, session, is_local} = get_info (ThyInfo.theory name)
444 in (if null session then NONE else
445 SOME (if is_some remote_path andalso not is_local then
446 Url.append (the remote_path) (Url.File
447 (Path.append (Path.make session) (html_path name)))
448 else Url.File (Path.append (mk_rel_path curr_session session)
449 (html_path name))), name)
452 fun begin_theory optpath name raw_parents orig_files thy =
453 with_session thy (fn {name = sess_name, session, path, html_prefix, remote_path, ...} =>
455 val parents = map (parent_link remote_path path) raw_parents;
456 val ml_path = ThyLoad.ml_path name;
457 val files = map (apsnd SOME) orig_files @
458 (if is_some (ThyLoad.check_file optpath ml_path) then [(ml_path, NONE)] else []);
460 fun prep_file (raw_path, loadit) =
461 (case ThyLoad.check_file optpath raw_path of
464 val base = Path.base path;
465 val base_html = html_ext base;
467 add_file (Path.append html_prefix base_html,
468 HTML.ml_file (Url.File base) (File.read path));
469 (SOME (Url.File base_html), Url.File raw_path, loadit)
471 | NONE => (warning ("Browser info: expected to find ML file" ^ quote (Path.pack raw_path));
472 (NONE, Url.File raw_path, loadit)));
474 val files_html = map prep_file files;
476 fun prep_html_source (tex_source, html_source, html) =
478 val txt = HTML.begin_theory (Url.File index_path, session)
479 name parents files_html (Buffer.content html_source)
480 in (tex_source, Buffer.empty, Buffer.add txt html) end;
483 {name = name, ID = ID_of path name, dir = sess_name, unfold = true,
484 path = Path.pack (html_path name),
485 parents = map (fn s => ID_of (#session (get_info (ThyInfo.theory s))) s) raw_parents};
488 change_theory_info name prep_html_source;
489 add_graph_entry entry;
490 add_html_index (HTML.theory_entry (Url.File (html_path name), name));
491 add_tex_index (Latex.theory_entry name);
492 BrowserInfoData.put {name = sess_name, session = path, is_local = is_some remote_path} thy
496 val hooks = ref ([]: (string -> (string * thm list) list -> unit) list);
497 fun add_hook f = hooks := (f :: ! hooks);
500 (List.app (fn f => (try (fn () => f k xs) (); ())) (! hooks);
501 with_session () (fn _ => with_context add_html (HTML.results k xs)));
503 fun theorem a th = results "theorem" [(a, [th])];
504 fun theorems a ths = results "theorems" [(a, ths)];
505 fun chapter s = with_session () (fn _ => with_context add_html (HTML.chapter s));
506 fun section s = with_session () (fn _ => with_context add_html (HTML.section s));
507 fun subsection s = with_session () (fn _ => with_context add_html (HTML.subsection s));
508 fun subsubsection s = with_session () (fn _ => with_context add_html (HTML.subsubsection s));
512 (** draft document output **)
514 fun drafts doc_format src_paths =
516 fun prep_draft (tex_index, path) =
518 val base = Path.base path;
520 (case Path.pack (#1 (Path.split_ext base)) of "" => gensym "DUMMY" | s => s);
522 if File.exists path then
523 (Buffer.add (Latex.theory_entry name) tex_index, (name, base, File.read path))
524 else error ("Bad file: " ^ quote (Path.pack path))
526 val (tex_index, srcs) = foldl_map prep_draft (Buffer.empty, src_paths);
528 val doc_path = File.tmp_path document_path;
529 val result_path = Path.append doc_path Path.parent;
530 val _ = File.mkdir doc_path;
531 val root_path = Path.append doc_path (Path.basic "root.tex");
532 val _ = File.copy (Path.unpack "~~/lib/texinputs/draft.tex") root_path;
533 val _ = File.isatool ("latex -o sty " ^ File.shell_path root_path);
534 val _ = File.isatool ("latex -o syms " ^ File.shell_path root_path);
537 let val ss = split_lines (File.read (Path.append doc_path (Path.basic name)))
538 in fn s => s mem_string ss end;
539 val known_syms = known "syms.lst";
540 val known_ctrls = known "ctrls.lst";
542 val _ = srcs |> List.app (fn (name, base, txt) =>
544 |> Latex.symbol_source (known_syms, known_ctrls) (Path.pack base)
545 |> File.write (Path.append doc_path (tex_path name)));
546 val _ = write_tex_index tex_index doc_path;
547 in isatool_document false doc_format documentN "" doc_path result_path end;
552 structure BasicPresent: BASIC_PRESENT = Present;