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 theory_output: string -> string -> unit
26 val begin_theory: Path.T option -> string -> string list ->
27 (Path.T * bool) list -> theory -> theory
28 val add_hook: (string -> (string * thm list) list -> unit) -> unit
29 val results: string -> (string * thm list) list -> unit
30 val theorem: string -> thm -> unit
31 val theorems: string -> thm list -> unit
32 val chapter: string -> unit
33 val subsection: string -> unit
34 val subsubsection: string -> unit
35 val drafts: string -> Path.T list -> Path.T
38 structure Present: PRESENT =
44 val output_path = Path.variable "ISABELLE_BROWSER_INFO";
46 val tex_ext = Path.ext "tex";
47 val tex_path = tex_ext o Path.basic;
48 val html_ext = Path.ext "html";
49 val html_path = html_ext o Path.basic;
50 val index_path = Path.basic "index.html";
51 val readme_html_path = Path.basic "README.html";
52 val readme_path = Path.basic "README";
53 val documentN = "document";
54 val document_path = Path.basic documentN;
55 val doc_indexN = "session";
56 val graph_path = Path.basic "session.graph";
57 val graph_pdf_path = Path.basic "session_graph.pdf";
58 val graph_eps_path = Path.basic "session_graph.eps";
60 val session_path = Path.basic ".session";
61 val session_entries_path = Path.unpack ".session/entries";
62 val pre_index_path = Path.unpack ".session/pre-index";
64 fun mk_rel_path [] ys = Path.make ys
65 | mk_rel_path xs [] = Path.appends (replicate (length xs) Path.parent)
66 | mk_rel_path (ps as x :: xs) (qs as y :: ys) = if x = y then mk_rel_path xs ys else
67 Path.appends (replicate (length ps) Path.parent @ [Path.make qs]);
69 fun show_path path = Path.pack (Path.append (File.pwd ()) path);
73 (** additional theory data **)
75 structure BrowserInfoData = TheoryDataFun
77 val name = "Pure/browser_info";
78 type T = {name: string, session: string list, is_local: bool};
79 val empty = {name = "", session = [], is_local = false}: T;
82 fun merge _ _ = empty;
86 val _ = Context.add_setup BrowserInfoData.init;
89 if Context.theory_name thy mem_string [Context.ProtoPureN, Context.PureN, Context.CPureN]
90 then {name = Context.PureN, session = [], is_local = false}
91 else BrowserInfoData.get thy;
98 {name: string, ID: string, dir: string, unfold: bool,
99 path: string, parents: string list};
101 fun write_graph gr path =
102 File.write path (cat_lines (map (fn {name, ID, dir, unfold, path, parents} =>
103 "\"" ^ name ^ "\" \"" ^ ID ^ "\" \"" ^ dir ^ (if unfold then "\" + \"" else "\" \"") ^
104 path ^ "\" > " ^ space_implode " " (map Library.quote parents) ^ " ;") gr));
106 fun ID_of sess s = space_implode "/" (sess @ [s]);
108 (*retrieve graph data from initial theory loader database*)
109 fun init_graph remote_path curr_sess = map (fn name =>
111 val {name = sess_name, session, is_local} = get_info (ThyInfo.theory name);
112 val path' = Path.append (Path.make session) (html_path name);
114 {name = name, ID = ID_of session name, dir = sess_name,
116 if null session then "" else
117 if is_some remote_path andalso not is_local then
118 Url.pack (Url.append (the remote_path) (Url.File
119 (Path.append (Path.make session) (html_path name))))
120 else Path.pack (Path.append (mk_rel_path curr_sess session) (html_path name)),
123 map (fn s => ID_of (#session (get_info (ThyInfo.theory s))) s) (ThyInfo.get_preds name)}
124 end) (ThyInfo.names ());
126 fun ins_graph_entry (entry as {ID, ...}) (gr: graph_node list) =
127 filter_out (fn entry' => #ID entry' = ID) gr @ [entry];
131 (** global browser info state **)
133 (* type theory_info *)
135 type theory_info = {tex_source: Buffer.T, html_source: Buffer.T, html: Buffer.T};
137 fun make_theory_info (tex_source, html_source, html) =
138 {tex_source = tex_source, html_source = html_source, html = html}: theory_info;
140 val empty_theory_info = make_theory_info (Buffer.empty, Buffer.empty, Buffer.empty);
142 fun map_theory_info f {tex_source, html_source, html} =
143 make_theory_info (f (tex_source, html_source, html));
146 (* type browser_info *)
148 type browser_info = {theories: theory_info Symtab.table, files: (Path.T * string) list,
149 tex_index: Buffer.T, html_index: Buffer.T, graph: graph_node list};
151 fun make_browser_info (theories, files, tex_index, html_index, graph) =
152 {theories = theories, files = files, tex_index = tex_index, html_index = html_index,
153 graph = graph}: browser_info;
155 val empty_browser_info = make_browser_info (Symtab.empty, [], Buffer.empty, Buffer.empty, []);
157 fun init_browser_info remote_path curr_sess = make_browser_info
158 (Symtab.empty, [], Buffer.empty, Buffer.empty, init_graph remote_path curr_sess);
160 fun map_browser_info f {theories, files, tex_index, html_index, graph} =
161 make_browser_info (f (theories, files, tex_index, html_index, graph));
166 val browser_info = ref empty_browser_info;
167 fun change_browser_info f = browser_info := map_browser_info f (! browser_info);
169 val suppress_tex_source = ref false;
170 fun no_document f x = Library.setmp suppress_tex_source true f x;
172 fun init_theory_info name info =
173 change_browser_info (fn (theories, files, tex_index, html_index, graph) =>
174 (Symtab.update (name, info) theories, files, tex_index, html_index, graph));
176 fun change_theory_info name f =
177 change_browser_info (fn (info as (theories, files, tex_index, html_index, graph)) =>
178 (case Symtab.lookup theories name of
179 NONE => (warning ("Browser info: cannot access theory document " ^ quote name); info)
180 | SOME info => (Symtab.update (name, map_theory_info f info) theories, files,
181 tex_index, html_index, graph)));
185 change_browser_info (fn (theories, files, tex_index, html_index, graph) =>
186 (theories, file :: files, tex_index, html_index, graph));
188 fun add_tex_index txt =
189 change_browser_info (fn (theories, files, tex_index, html_index, graph) =>
190 (theories, files, Buffer.add txt tex_index, html_index, graph));
192 fun add_html_index txt =
193 change_browser_info (fn (theories, files, tex_index, html_index, graph) =>
194 (theories, files, tex_index, Buffer.add txt html_index, graph));
196 fun add_graph_entry e =
197 change_browser_info (fn (theories, files, tex_index, html_index, graph) =>
198 (theories, files, tex_index, html_index, ins_graph_entry e graph));
200 fun add_tex_source name txt =
201 if ! suppress_tex_source then ()
202 else change_theory_info name (fn (tex_source, html_source, html) =>
203 (Buffer.add txt tex_source, html_source, html));
205 fun add_html_source name txt = change_theory_info name (fn (tex_source, html_source, html) =>
206 (tex_source, Buffer.add txt html_source, html));
208 fun add_html name txt = change_theory_info name (fn (tex_source, html_source, html) =>
209 (tex_source, html_source, Buffer.add txt html));
213 (** global session state **)
218 {name: string, parent: string, session: string, path: string list, html_prefix: Path.T,
219 info: bool, doc_format: string, doc_graph: bool, documents: (string * string) list,
220 doc_prefix1: (Path.T * Path.T) option, doc_prefix2: (bool * Path.T) option,
221 remote_path: Url.T option, verbose: bool, readme: Path.T option};
223 fun make_session_info
224 (name, parent, session, path, html_prefix, info, doc_format, doc_graph, documents,
225 doc_prefix1, doc_prefix2, remote_path, verbose, readme) =
226 {name = name, parent = parent, session = session, path = path, html_prefix = html_prefix,
227 info = info, doc_format = doc_format, doc_graph = doc_graph, documents = documents,
228 doc_prefix1 = doc_prefix1, doc_prefix2 = doc_prefix2, remote_path = remote_path,
229 verbose = verbose, readme = readme}: session_info;
234 val session_info = ref (NONE: session_info option);
236 fun with_session x f = (case ! session_info of NONE => x | SOME info => f info);
237 fun with_context f = f (Context.theory_name (Context.the_context ()));
241 (** document preparation **)
245 val session_entries =
246 HTML.session_entries o
247 map (fn name => (Url.File (Path.append (Path.basic name) index_path), name));
249 fun get_entries dir =
250 split_lines (File.read (Path.append dir session_entries_path));
252 fun put_entries entries dir =
253 File.write (Path.append dir session_entries_path) (cat_lines entries);
256 fun create_index dir =
257 File.read (Path.append dir pre_index_path) ^
258 session_entries (get_entries dir) ^ HTML.end_index
259 |> File.write (Path.append dir index_path);
261 fun update_index dir name =
262 (case try get_entries dir of
263 NONE => warning ("Browser info: cannot access session index of " ^ quote (Path.pack dir))
264 | SOME es => (put_entries ((es \ name) @ [name]) dir; create_index dir));
267 (* document versions *)
269 fun read_version str =
270 (case space_explode "=" str of
272 | [name, tags] => (name, tags)
273 | _ => error ("Malformed document version specification: " ^ quote str));
275 fun read_versions strs =
276 rev (distinct (eq_fst (op =)) (rev ((documentN, "") :: map read_version strs)))
277 |> filter_out (equal "-" o #2);
282 fun name_of_session elems = space_implode "/" ("Isabelle" :: elems);
284 fun init build info doc doc_graph doc_versions path name doc_prefix2
285 (remote_path, first_time) verbose =
286 if not build andalso not info andalso doc = "" andalso is_none doc_prefix2 then
287 (browser_info := empty_browser_info; session_info := NONE)
290 val parent_name = name_of_session (Library.take (length path - 1, path));
291 val session_name = name_of_session path;
292 val sess_prefix = Path.make path;
293 val html_prefix = Path.append (Path.expand output_path) sess_prefix;
295 val (doc_prefix1, documents) =
296 if doc = "" then (NONE, [])
297 else if not (File.exists document_path) then (conditional verbose (fn () =>
298 std_error "Warning: missing document directory\n"); (NONE, []))
299 else (SOME (Path.append html_prefix document_path, html_prefix),
300 read_versions doc_versions);
302 val parent_index_path = Path.append Path.parent index_path;
303 val index_up_lnk = if first_time then
304 Url.append (the remote_path) (Url.File (Path.append sess_prefix parent_index_path))
305 else Url.File parent_index_path;
307 if File.exists readme_html_path then SOME readme_html_path
308 else if File.exists readme_path then SOME readme_path
312 (case readme of NONE => [] | SOME p => [(Url.File p, "README")]) @
313 map (fn (name, _) => (Url.File (Path.ext doc (Path.basic name)), name)) documents;
314 val index_text = HTML.begin_index (index_up_lnk, parent_name)
315 (Url.File index_path, session_name) docs (Url.unpack "medium.html");
317 session_info := SOME (make_session_info (name, parent_name, session_name, path, html_prefix,
318 info, doc, doc_graph, documents, doc_prefix1, doc_prefix2, remote_path, verbose, readme));
319 browser_info := init_browser_info remote_path path;
320 add_html_index index_text
324 (* isatool wrappers *)
326 fun isatool_document verbose format name tags path result_path =
328 val s = "\"$ISATOOL\" document -c -o '" ^ format ^ "' \
329 \-n '" ^ name ^ "' -t '" ^ tags ^ "' " ^ File.shell_path path ^
330 " 2>&1" ^ (if verbose then "" else " >/dev/null");
331 val doc_path = Path.append result_path (Path.ext format (Path.basic name));
333 conditional verbose (fn () => writeln s);
335 conditional (not (File.exists doc_path)) (fn () =>
336 error ("No document: " ^ quote (show_path doc_path)));
340 fun isatool_browser graph =
342 val pdf_path = File.tmp_path graph_pdf_path;
343 val eps_path = File.tmp_path graph_eps_path;
344 val gr_path = File.tmp_path graph_path;
345 val s = "browser -o " ^ File.shell_path pdf_path ^ " " ^ File.shell_path gr_path;
347 write_graph graph gr_path;
348 if File.isatool s <> 0 orelse not (File.exists eps_path) orelse not (File.exists pdf_path)
349 then error "Failed to prepare dependency graph"
351 let val pdf = File.read pdf_path and eps = File.read eps_path
352 in File.rm pdf_path; File.rm eps_path; File.rm gr_path; (pdf, eps) end
356 (* finish session -- output all generated text *)
358 fun write_tex src name path =
359 Buffer.write (Path.append path (tex_path name)) src;
361 fun write_tex_index tex_index path =
362 write_tex (Buffer.add Latex.tex_trailer tex_index) doc_indexN path;
365 fun finish () = with_session () (fn {name, info, html_prefix, doc_format, doc_graph,
366 documents, doc_prefix1, doc_prefix2, path, verbose, readme, ...} =>
368 val {theories, files, tex_index, html_index, graph} = ! browser_info;
369 val thys = Symtab.dest theories;
370 val parent_html_prefix = Path.append html_prefix Path.parent;
372 fun finish_tex path (a, {tex_source, ...}: theory_info) = write_tex tex_source a path;
373 fun finish_html (a, {html, ...}: theory_info) =
374 Buffer.write (Path.append html_prefix (html_path a)) (Buffer.add HTML.end_theory html);
377 if doc_graph andalso (is_some doc_prefix1 orelse is_some doc_prefix2) then
378 SOME (isatool_browser graph)
381 fun prepare_sources cp path =
383 if cp then File.copy_dir document_path path else ();
384 File.isatool ("latex -o sty " ^ File.shell_path (Path.append path (Path.basic "root.tex")));
385 (case opt_graphs of NONE => () | SOME (pdf, eps) =>
386 (File.write (Path.append path graph_pdf_path) pdf;
387 File.write (Path.append path graph_eps_path) eps));
388 write_tex_index tex_index path;
389 List.app (finish_tex path) thys);
391 conditional info (fn () =>
392 (File.mkdir (Path.append html_prefix session_path);
393 Buffer.write (Path.append html_prefix pre_index_path) html_index;
394 File.write (Path.append html_prefix session_entries_path) "";
395 create_index html_prefix;
396 if length path > 1 then update_index parent_html_prefix name else ();
397 (case readme of NONE => () | SOME path => File.copy path html_prefix);
398 write_graph graph (Path.append html_prefix graph_path);
399 File.copy (Path.unpack "~~/lib/browser/GraphBrowser.jar") html_prefix;
400 List.app (fn (a, txt) => File.write (Path.append html_prefix (Path.basic a)) txt)
401 (HTML.applet_pages name (Url.File index_path, name));
402 File.copy (Path.unpack "~~/lib/html/isabelle.css") html_prefix;
403 List.app finish_html thys;
404 List.app (uncurry File.write) files;
405 conditional verbose (fn () =>
406 std_error ("Browser info at " ^ show_path html_prefix ^ "\n"))));
408 (case doc_prefix2 of NONE => () | SOME (cp, path) =>
409 (prepare_sources cp path;
410 conditional verbose (fn () => std_error ("Document sources at " ^ show_path path ^ "\n"))));
412 (case doc_prefix1 of NONE => () | SOME (path, result_path) =>
413 documents |> List.app (fn (name, tags) =>
415 val _ = prepare_sources true path;
416 val doc_path = isatool_document true doc_format name tags path result_path;
418 conditional verbose (fn () => std_error ("Document at " ^ show_path doc_path ^ "\n"))
421 browser_info := empty_browser_info;
426 (* theory elements *)
428 fun init_theory name = with_session () (fn _ => init_theory_info name empty_theory_info);
430 fun verbatim_source name mk_text =
431 with_session () (fn _ => add_html_source name (HTML.verbatim_source (mk_text ())));
433 fun theory_output name s =
434 with_session () (fn _ => add_tex_source name (Latex.isabelle_file name s));
437 fun parent_link remote_path curr_session name =
438 let val {name = _, session, is_local} = get_info (ThyInfo.theory name)
439 in (if null session then NONE else
440 SOME (if is_some remote_path andalso not is_local then
441 Url.append (the remote_path) (Url.File
442 (Path.append (Path.make session) (html_path name)))
443 else Url.File (Path.append (mk_rel_path curr_session session)
444 (html_path name))), name)
447 fun begin_theory optpath name raw_parents orig_files thy =
448 with_session thy (fn {name = sess_name, session, path, html_prefix, remote_path, ...} =>
450 val parents = map (parent_link remote_path path) raw_parents;
451 val ml_path = ThyLoad.ml_path name;
452 val files = map (apsnd SOME) orig_files @
453 (if is_some (ThyLoad.check_file optpath ml_path) then [(ml_path, NONE)] else []);
455 fun prep_file (raw_path, loadit) =
456 (case ThyLoad.check_file optpath raw_path of
459 val base = Path.base path;
460 val base_html = html_ext base;
462 add_file (Path.append html_prefix base_html,
463 HTML.ml_file (Url.File base) (File.read path));
464 (SOME (Url.File base_html), Url.File raw_path, loadit)
466 | NONE => (warning ("Browser info: expected to find ML file" ^ quote (Path.pack raw_path));
467 (NONE, Url.File raw_path, loadit)));
469 val files_html = map prep_file files;
471 fun prep_html_source (tex_source, html_source, html) =
473 val txt = HTML.begin_theory (Url.File index_path, session)
474 name parents files_html (Buffer.content html_source)
475 in (tex_source, Buffer.empty, Buffer.add txt html) end;
478 {name = name, ID = ID_of path name, dir = sess_name, unfold = true,
479 path = Path.pack (html_path name),
480 parents = map (fn s => ID_of (#session (get_info (ThyInfo.theory s))) s) raw_parents};
483 change_theory_info name prep_html_source;
484 add_graph_entry entry;
485 add_html_index (HTML.theory_entry (Url.File (html_path name), name));
486 add_tex_index (Latex.theory_entry name);
487 BrowserInfoData.put {name = sess_name, session = path, is_local = is_some remote_path} thy
491 val hooks = ref ([]: (string -> (string * thm list) list -> unit) list);
492 fun add_hook f = hooks := (f :: ! hooks);
495 (List.app (fn f => (try (fn () => f k xs) (); ())) (! hooks);
496 with_session () (fn _ => with_context add_html (HTML.results k xs)));
498 fun theorem a th = results "theorem" [(a, [th])];
499 fun theorems a ths = results "theorems" [(a, ths)];
500 fun chapter s = with_session () (fn _ => with_context add_html (HTML.chapter s));
501 fun section s = with_session () (fn _ => with_context add_html (HTML.section s));
502 fun subsection s = with_session () (fn _ => with_context add_html (HTML.subsection s));
503 fun subsubsection s = with_session () (fn _ => with_context add_html (HTML.subsubsection s));
507 (** draft document output **)
509 fun drafts doc_format src_paths =
511 fun prep_draft (tex_index, path) =
513 val base = Path.base path;
515 (case Path.pack (#1 (Path.split_ext base)) of "" => gensym "DUMMY" | s => s);
517 if File.exists path then
518 (Buffer.add (Latex.theory_entry name) tex_index, (name, base, File.read path))
519 else error ("Bad file: " ^ quote (Path.pack path))
521 val (tex_index, srcs) = foldl_map prep_draft (Buffer.empty, src_paths);
523 val doc_path = File.tmp_path document_path;
524 val result_path = Path.append doc_path Path.parent;
525 val _ = File.mkdir doc_path;
526 val root_path = Path.append doc_path (Path.basic "root.tex");
527 val _ = File.copy (Path.unpack "~~/lib/texinputs/draft.tex") root_path;
528 val _ = File.isatool ("latex -o sty " ^ File.shell_path root_path);
529 val _ = File.isatool ("latex -o syms " ^ File.shell_path root_path);
532 let val ss = split_lines (File.read (Path.append doc_path (Path.basic name)))
533 in fn s => s mem_string ss end;
534 val known_syms = known "syms.lst";
535 val known_ctrls = known "ctrls.lst";
537 val _ = srcs |> List.app (fn (name, base, txt) =>
539 |> Latex.symbol_source (known_syms, known_ctrls) (Path.pack base)
540 |> File.write (Path.append doc_path (tex_path name)));
541 val _ = write_tex_index tex_index doc_path;
542 in isatool_document false doc_format documentN "" doc_path result_path end;
547 structure BasicPresent: BASIC_PRESENT = Present;