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 display_graph: {name: string, ID: string, dir: string, unfold: bool,
21 path: string, parents: string list} list -> unit
22 val init: bool -> bool -> string -> bool -> string list -> string list ->
23 string -> (bool * Path.T) option -> Url.T option * bool -> bool -> unit
24 val finish: unit -> unit
25 val init_theory: string -> unit
26 val verbatim_source: string -> (unit -> Symbol.symbol list) -> unit
27 val theory_output: string -> string -> unit
28 val begin_theory: Path.T option -> string -> string list ->
29 (Path.T * bool) list -> theory -> theory
30 val add_hook: (string -> (string * thm list) list -> unit) -> unit
31 val results: string -> (string * thm list) list -> unit
32 val theorem: string -> thm -> unit
33 val theorems: string -> thm list -> unit
34 val chapter: string -> unit
35 val subsection: string -> unit
36 val subsubsection: string -> unit
37 val drafts: string -> Path.T list -> Path.T
40 structure Present: PRESENT =
46 val output_path = Path.variable "ISABELLE_BROWSER_INFO";
48 val tex_ext = Path.ext "tex";
49 val tex_path = tex_ext o Path.basic;
50 val html_ext = Path.ext "html";
51 val html_path = html_ext o Path.basic;
52 val index_path = Path.basic "index.html";
53 val readme_html_path = Path.basic "README.html";
54 val readme_path = Path.basic "README";
55 val documentN = "document";
56 val document_path = Path.basic documentN;
57 val doc_indexN = "session";
58 val graph_path = Path.basic "session.graph";
59 val graph_pdf_path = Path.basic "session_graph.pdf";
60 val graph_eps_path = Path.basic "session_graph.eps";
62 val session_path = Path.basic ".session";
63 val session_entries_path = Path.unpack ".session/entries";
64 val pre_index_path = Path.unpack ".session/pre-index";
66 fun mk_rel_path [] ys = Path.make ys
67 | mk_rel_path xs [] = Path.appends (replicate (length xs) Path.parent)
68 | mk_rel_path (ps as x :: xs) (qs as y :: ys) = if x = y then mk_rel_path xs ys else
69 Path.appends (replicate (length ps) Path.parent @ [Path.make qs]);
71 fun show_path path = Path.pack (Path.append (File.pwd ()) path);
75 (** additional theory data **)
77 structure BrowserInfoData = TheoryDataFun
79 val name = "Pure/browser_info";
80 type T = {name: string, session: string list, is_local: bool};
81 val empty = {name = "", session = [], is_local = false}: T;
84 fun merge _ _ = empty;
88 val _ = Context.add_setup BrowserInfoData.init;
91 if Context.theory_name thy mem_string [Context.ProtoPureN, Context.PureN, Context.CPureN]
92 then {name = Context.PureN, session = [], is_local = false}
93 else BrowserInfoData.get thy;
100 {name: string, ID: string, dir: string, unfold: bool,
101 path: string, parents: string list};
103 fun write_graph gr path =
104 File.write path (cat_lines (map (fn {name, ID, dir, unfold, path, parents} =>
105 "\"" ^ name ^ "\" \"" ^ ID ^ "\" \"" ^ dir ^ (if unfold then "\" + \"" else "\" \"") ^
106 path ^ "\" > " ^ space_implode " " (map Library.quote parents) ^ " ;") gr));
108 fun display_graph gr =
110 val _ = writeln "Generating graph ...";
111 val path = File.tmp_path (Path.unpack "tmp.graph");
112 val _ = write_graph gr path;
113 val _ = File.isatool ("browser -c " ^ File.shell_path path ^ " &");
117 fun ID_of sess s = space_implode "/" (sess @ [s]);
119 (*retrieve graph data from initial theory loader database*)
120 fun init_graph remote_path curr_sess = map (fn name =>
122 val {name = sess_name, session, is_local} = get_info (ThyInfo.theory name);
123 val path' = Path.append (Path.make session) (html_path name);
125 {name = name, ID = ID_of session name, dir = sess_name,
127 if null session then "" else
128 if is_some remote_path andalso not is_local then
129 Url.pack (Url.append (the remote_path) (Url.File
130 (Path.append (Path.make session) (html_path name))))
131 else Path.pack (Path.append (mk_rel_path curr_sess session) (html_path name)),
134 map (fn s => ID_of (#session (get_info (ThyInfo.theory s))) s) (ThyInfo.get_preds name)}
135 end) (ThyInfo.names ());
137 fun ins_graph_entry (entry as {ID, ...}) (gr: graph_node list) =
138 filter_out (fn entry' => #ID entry' = ID) gr @ [entry];
142 (** global browser info state **)
144 (* type theory_info *)
146 type theory_info = {tex_source: Buffer.T, html_source: Buffer.T, html: Buffer.T};
148 fun make_theory_info (tex_source, html_source, html) =
149 {tex_source = tex_source, html_source = html_source, html = html}: theory_info;
151 val empty_theory_info = make_theory_info (Buffer.empty, Buffer.empty, Buffer.empty);
153 fun map_theory_info f {tex_source, html_source, html} =
154 make_theory_info (f (tex_source, html_source, html));
157 (* type browser_info *)
159 type browser_info = {theories: theory_info Symtab.table, files: (Path.T * string) list,
160 tex_index: Buffer.T, html_index: Buffer.T, graph: graph_node list};
162 fun make_browser_info (theories, files, tex_index, html_index, graph) =
163 {theories = theories, files = files, tex_index = tex_index, html_index = html_index,
164 graph = graph}: browser_info;
166 val empty_browser_info = make_browser_info (Symtab.empty, [], Buffer.empty, Buffer.empty, []);
168 fun init_browser_info remote_path curr_sess = make_browser_info
169 (Symtab.empty, [], Buffer.empty, Buffer.empty, init_graph remote_path curr_sess);
171 fun map_browser_info f {theories, files, tex_index, html_index, graph} =
172 make_browser_info (f (theories, files, tex_index, html_index, graph));
177 val browser_info = ref empty_browser_info;
178 fun change_browser_info f = browser_info := map_browser_info f (! browser_info);
180 val suppress_tex_source = ref false;
181 fun no_document f x = Library.setmp suppress_tex_source true f x;
183 fun init_theory_info name info =
184 change_browser_info (fn (theories, files, tex_index, html_index, graph) =>
185 (Symtab.update (name, info) theories, files, tex_index, html_index, graph));
187 fun change_theory_info name f =
188 change_browser_info (fn (info as (theories, files, tex_index, html_index, graph)) =>
189 (case Symtab.lookup theories name of
190 NONE => (warning ("Browser info: cannot access theory document " ^ quote name); info)
191 | SOME info => (Symtab.update (name, map_theory_info f info) theories, files,
192 tex_index, html_index, graph)));
196 change_browser_info (fn (theories, files, tex_index, html_index, graph) =>
197 (theories, file :: files, tex_index, html_index, graph));
199 fun add_tex_index txt =
200 change_browser_info (fn (theories, files, tex_index, html_index, graph) =>
201 (theories, files, Buffer.add txt tex_index, html_index, graph));
203 fun add_html_index txt =
204 change_browser_info (fn (theories, files, tex_index, html_index, graph) =>
205 (theories, files, tex_index, Buffer.add txt html_index, graph));
207 fun add_graph_entry e =
208 change_browser_info (fn (theories, files, tex_index, html_index, graph) =>
209 (theories, files, tex_index, html_index, ins_graph_entry e graph));
211 fun add_tex_source name txt =
212 if ! suppress_tex_source then ()
213 else change_theory_info name (fn (tex_source, html_source, html) =>
214 (Buffer.add txt tex_source, html_source, html));
216 fun add_html_source name txt = change_theory_info name (fn (tex_source, html_source, html) =>
217 (tex_source, Buffer.add txt html_source, html));
219 fun add_html name txt = change_theory_info name (fn (tex_source, html_source, html) =>
220 (tex_source, html_source, Buffer.add txt html));
224 (** global session state **)
229 {name: string, parent: string, session: string, path: string list, html_prefix: Path.T,
230 info: bool, doc_format: string, doc_graph: bool, documents: (string * string) list,
231 doc_prefix1: (Path.T * Path.T) option, doc_prefix2: (bool * Path.T) option,
232 remote_path: Url.T option, verbose: bool, readme: Path.T option};
234 fun make_session_info
235 (name, parent, session, path, html_prefix, info, doc_format, doc_graph, documents,
236 doc_prefix1, doc_prefix2, remote_path, verbose, readme) =
237 {name = name, parent = parent, session = session, path = path, html_prefix = html_prefix,
238 info = info, doc_format = doc_format, doc_graph = doc_graph, documents = documents,
239 doc_prefix1 = doc_prefix1, doc_prefix2 = doc_prefix2, remote_path = remote_path,
240 verbose = verbose, readme = readme}: session_info;
245 val session_info = ref (NONE: session_info option);
247 fun with_session x f = (case ! session_info of NONE => x | SOME info => f info);
248 fun with_context f = f (Context.theory_name (Context.the_context ()));
252 (** document preparation **)
256 val session_entries =
257 HTML.session_entries o
258 map (fn name => (Url.File (Path.append (Path.basic name) index_path), name));
260 fun get_entries dir =
261 split_lines (File.read (Path.append dir session_entries_path));
263 fun put_entries entries dir =
264 File.write (Path.append dir session_entries_path) (cat_lines entries);
267 fun create_index dir =
268 File.read (Path.append dir pre_index_path) ^
269 session_entries (get_entries dir) ^ HTML.end_index
270 |> File.write (Path.append dir index_path);
272 fun update_index dir name =
273 (case try get_entries dir of
274 NONE => warning ("Browser info: cannot access session index of " ^ quote (Path.pack dir))
275 | SOME es => (put_entries ((remove (op =) name es) @ [name]) dir; create_index dir));
278 (* document versions *)
280 fun read_version str =
281 (case space_explode "=" str of
283 | [name, tags] => (name, tags)
284 | _ => error ("Malformed document version specification: " ^ quote str));
286 fun read_versions strs =
287 rev (distinct (eq_fst (op =)) (rev ((documentN, "") :: map read_version strs)))
288 |> filter_out (equal "-" o #2);
293 fun name_of_session elems = space_implode "/" ("Isabelle" :: elems);
295 fun init build info doc doc_graph doc_versions path name doc_prefix2
296 (remote_path, first_time) verbose =
297 if not build andalso not info andalso doc = "" andalso is_none doc_prefix2 then
298 (browser_info := empty_browser_info; session_info := NONE)
301 val parent_name = name_of_session (Library.take (length path - 1, path));
302 val session_name = name_of_session path;
303 val sess_prefix = Path.make path;
304 val html_prefix = Path.append (Path.expand output_path) sess_prefix;
306 val (doc_prefix1, documents) =
307 if doc = "" then (NONE, [])
308 else if not (File.exists document_path) then (conditional verbose (fn () =>
309 std_error "Warning: missing document directory\n"); (NONE, []))
310 else (SOME (Path.append html_prefix document_path, html_prefix),
311 read_versions doc_versions);
313 val parent_index_path = Path.append Path.parent index_path;
314 val index_up_lnk = if first_time then
315 Url.append (the remote_path) (Url.File (Path.append sess_prefix parent_index_path))
316 else Url.File parent_index_path;
318 if File.exists readme_html_path then SOME readme_html_path
319 else if File.exists readme_path then SOME readme_path
323 (case readme of NONE => [] | SOME p => [(Url.File p, "README")]) @
324 map (fn (name, _) => (Url.File (Path.ext doc (Path.basic name)), name)) documents;
325 val index_text = HTML.begin_index (index_up_lnk, parent_name)
326 (Url.File index_path, session_name) docs (Url.unpack "medium.html");
328 session_info := SOME (make_session_info (name, parent_name, session_name, path, html_prefix,
329 info, doc, doc_graph, documents, doc_prefix1, doc_prefix2, remote_path, verbose, readme));
330 browser_info := init_browser_info remote_path path;
331 add_html_index index_text
335 (* isatool wrappers *)
337 fun isatool_document verbose format name tags path result_path =
339 val s = "\"$ISATOOL\" document -c -o '" ^ format ^ "' \
340 \-n '" ^ name ^ "' -t '" ^ tags ^ "' " ^ File.shell_path path ^
341 " 2>&1" ^ (if verbose then "" else " >/dev/null");
342 val doc_path = Path.append result_path (Path.ext format (Path.basic name));
344 conditional verbose (fn () => writeln s);
346 conditional (not (File.exists doc_path)) (fn () =>
347 error ("No document: " ^ quote (show_path doc_path)));
351 fun isatool_browser graph =
353 val pdf_path = File.tmp_path graph_pdf_path;
354 val eps_path = File.tmp_path graph_eps_path;
355 val gr_path = File.tmp_path graph_path;
356 val s = "browser -o " ^ File.shell_path pdf_path ^ " " ^ File.shell_path gr_path;
358 write_graph graph gr_path;
359 if File.isatool s <> 0 orelse not (File.exists eps_path) orelse not (File.exists pdf_path)
360 then error "Failed to prepare dependency graph"
362 let val pdf = File.read pdf_path and eps = File.read eps_path
363 in File.rm pdf_path; File.rm eps_path; File.rm gr_path; (pdf, eps) end
367 (* finish session -- output all generated text *)
369 fun write_tex src name path =
370 Buffer.write (Path.append path (tex_path name)) src;
372 fun write_tex_index tex_index path =
373 write_tex (Buffer.add Latex.tex_trailer tex_index) doc_indexN path;
376 fun finish () = with_session () (fn {name, info, html_prefix, doc_format, doc_graph,
377 documents, doc_prefix1, doc_prefix2, path, verbose, readme, ...} =>
379 val {theories, files, tex_index, html_index, graph} = ! browser_info;
380 val thys = Symtab.dest theories;
381 val parent_html_prefix = Path.append html_prefix Path.parent;
383 fun finish_tex path (a, {tex_source, ...}: theory_info) = write_tex tex_source a path;
384 fun finish_html (a, {html, ...}: theory_info) =
385 Buffer.write (Path.append html_prefix (html_path a)) (Buffer.add HTML.end_theory html);
388 if doc_graph andalso (is_some doc_prefix1 orelse is_some doc_prefix2) then
389 SOME (isatool_browser graph)
392 fun prepare_sources cp path =
394 if cp then File.copy_dir document_path path else ();
395 File.isatool ("latex -o sty " ^ File.shell_path (Path.append path (Path.basic "root.tex")));
396 (case opt_graphs of NONE => () | SOME (pdf, eps) =>
397 (File.write (Path.append path graph_pdf_path) pdf;
398 File.write (Path.append path graph_eps_path) eps));
399 write_tex_index tex_index path;
400 List.app (finish_tex path) thys);
402 conditional info (fn () =>
403 (File.mkdir (Path.append html_prefix session_path);
404 Buffer.write (Path.append html_prefix pre_index_path) html_index;
405 File.write (Path.append html_prefix session_entries_path) "";
406 create_index html_prefix;
407 if length path > 1 then update_index parent_html_prefix name else ();
408 (case readme of NONE => () | SOME path => File.copy path html_prefix);
409 write_graph graph (Path.append html_prefix graph_path);
410 File.copy (Path.unpack "~~/lib/browser/GraphBrowser.jar") html_prefix;
411 List.app (fn (a, txt) => File.write (Path.append html_prefix (Path.basic a)) txt)
412 (HTML.applet_pages name (Url.File index_path, name));
413 File.copy (Path.unpack "~~/lib/html/isabelle.css") html_prefix;
414 List.app finish_html thys;
415 List.app (uncurry File.write) files;
416 conditional verbose (fn () =>
417 std_error ("Browser info at " ^ show_path html_prefix ^ "\n"))));
419 (case doc_prefix2 of NONE => () | SOME (cp, path) =>
420 (prepare_sources cp path;
421 conditional verbose (fn () => std_error ("Document sources at " ^ show_path path ^ "\n"))));
423 (case doc_prefix1 of NONE => () | SOME (path, result_path) =>
424 documents |> List.app (fn (name, tags) =>
426 val _ = prepare_sources true path;
427 val doc_path = isatool_document true doc_format name tags path result_path;
429 conditional verbose (fn () => std_error ("Document at " ^ show_path doc_path ^ "\n"))
432 browser_info := empty_browser_info;
437 (* theory elements *)
439 fun init_theory name = with_session () (fn _ => init_theory_info name empty_theory_info);
441 fun verbatim_source name mk_text =
442 with_session () (fn _ => add_html_source name (HTML.verbatim_source (mk_text ())));
444 fun theory_output name s =
445 with_session () (fn _ => add_tex_source name (Latex.isabelle_file name s));
448 fun parent_link remote_path curr_session name =
449 let val {name = _, session, is_local} = get_info (ThyInfo.theory name)
450 in (if null session then NONE else
451 SOME (if is_some remote_path andalso not is_local then
452 Url.append (the remote_path) (Url.File
453 (Path.append (Path.make session) (html_path name)))
454 else Url.File (Path.append (mk_rel_path curr_session session)
455 (html_path name))), name)
458 fun begin_theory optpath name raw_parents orig_files thy =
459 with_session thy (fn {name = sess_name, session, path, html_prefix, remote_path, ...} =>
461 val parents = map (parent_link remote_path path) raw_parents;
462 val ml_path = ThyLoad.ml_path name;
463 val files = map (apsnd SOME) orig_files @
464 (if is_some (ThyLoad.check_file optpath ml_path) then [(ml_path, NONE)] else []);
466 fun prep_file (raw_path, loadit) =
467 (case ThyLoad.check_file optpath raw_path of
470 val base = Path.base path;
471 val base_html = html_ext base;
473 add_file (Path.append html_prefix base_html,
474 HTML.ml_file (Url.File base) (File.read path));
475 (SOME (Url.File base_html), Url.File raw_path, loadit)
477 | NONE => (warning ("Browser info: expected to find ML file" ^ quote (Path.pack raw_path));
478 (NONE, Url.File raw_path, loadit)));
480 val files_html = map prep_file files;
482 fun prep_html_source (tex_source, html_source, html) =
484 val txt = HTML.begin_theory (Url.File index_path, session)
485 name parents files_html (Buffer.content html_source)
486 in (tex_source, Buffer.empty, Buffer.add txt html) end;
489 {name = name, ID = ID_of path name, dir = sess_name, unfold = true,
490 path = Path.pack (html_path name),
491 parents = map (fn s => ID_of (#session (get_info (ThyInfo.theory s))) s) raw_parents};
494 change_theory_info name prep_html_source;
495 add_graph_entry entry;
496 add_html_index (HTML.theory_entry (Url.File (html_path name), name));
497 add_tex_index (Latex.theory_entry name);
498 BrowserInfoData.put {name = sess_name, session = path, is_local = is_some remote_path} thy
502 val hooks = ref ([]: (string -> (string * thm list) list -> unit) list);
503 fun add_hook f = hooks := (f :: ! hooks);
506 (List.app (fn f => (try (fn () => f k xs) (); ())) (! hooks);
507 with_session () (fn _ => with_context add_html (HTML.results k xs)));
509 fun theorem a th = results "theorem" [(a, [th])];
510 fun theorems a ths = results "theorems" [(a, ths)];
511 fun chapter s = with_session () (fn _ => with_context add_html (HTML.chapter s));
512 fun section s = with_session () (fn _ => with_context add_html (HTML.section s));
513 fun subsection s = with_session () (fn _ => with_context add_html (HTML.subsection s));
514 fun subsubsection s = with_session () (fn _ => with_context add_html (HTML.subsubsection s));
518 (** draft document output **)
520 fun drafts doc_format src_paths =
522 fun prep_draft (tex_index, path) =
524 val base = Path.base path;
526 (case Path.pack (#1 (Path.split_ext base)) of "" => gensym "DUMMY" | s => s);
528 if File.exists path then
529 (Buffer.add (Latex.theory_entry name) tex_index, (name, base, File.read path))
530 else error ("Bad file: " ^ quote (Path.pack path))
532 val (tex_index, srcs) = foldl_map prep_draft (Buffer.empty, src_paths);
534 val doc_path = File.tmp_path document_path;
535 val result_path = Path.append doc_path Path.parent;
536 val _ = File.mkdir doc_path;
537 val root_path = Path.append doc_path (Path.basic "root.tex");
538 val _ = File.copy (Path.unpack "~~/lib/texinputs/draft.tex") root_path;
539 val _ = File.isatool ("latex -o sty " ^ File.shell_path root_path);
540 val _ = File.isatool ("latex -o syms " ^ File.shell_path root_path);
543 let val ss = split_lines (File.read (Path.append doc_path (Path.basic name)))
544 in fn s => s mem_string ss end;
545 val known_syms = known "syms.lst";
546 val known_ctrls = known "ctrls.lst";
548 val _ = srcs |> List.app (fn (name, base, txt) =>
550 |> Latex.symbol_source (known_syms, known_ctrls) (Path.pack base)
551 |> File.write (Path.append doc_path (tex_path name)));
552 val _ = write_tex_index tex_index doc_path;
553 in isatool_document false doc_format documentN "" doc_path result_path end;
558 structure BasicPresent: BASIC_PRESENT = Present;