src/Pure/Thy/present.ML
author wenzelm
Sat Nov 16 20:20:09 2013 +0100 (2013-11-16)
changeset 54455 1d977436c1bf
parent 53171 a5e54d4d9081
child 54456 f4b1440d9880
permissions -rw-r--r--
removed remains of HTML presentation of auxiliary files -- inactive since Isabelle2013;
     1 (*  Title:      Pure/Thy/present.ML
     2     Author:     Markus Wenzel and Stefan Berghofer, TU Muenchen
     3 
     4 Theory presentation: HTML, graph files, (PDF)LaTeX documents.
     5 *)
     6 
     7 signature PRESENT =
     8 sig
     9   val session_name: theory -> string
    10   val no_document: ('a -> 'b) -> 'a -> 'b  (*not thread-safe!*)
    11   val read_variant: string -> string * string
    12   val init: bool -> bool -> Path.T -> string -> bool -> string -> (string * string) list ->
    13     string * string -> bool * string -> bool -> theory list -> unit  (*not thread-safe!*)
    14   val finish: unit -> unit  (*not thread-safe!*)
    15   val init_theory: string -> unit
    16   val theory_source: string -> (unit -> HTML.text) -> unit
    17   val theory_output: string -> string -> unit
    18   val begin_theory: int -> theory -> theory
    19   val display_drafts: Path.T list -> int
    20 end;
    21 
    22 structure Present: PRESENT =
    23 struct
    24 
    25 
    26 (** paths **)
    27 
    28 val tex_ext = Path.ext "tex";
    29 val tex_path = tex_ext o Path.basic;
    30 val html_ext = Path.ext "html";
    31 val html_path = html_ext o Path.basic;
    32 val index_path = Path.basic "index.html";
    33 val readme_html_path = Path.basic "README.html";
    34 val documentN = "document";
    35 val document_path = Path.basic documentN;
    36 val doc_indexN = "session";
    37 val graph_path = Path.basic "session.graph";
    38 val graph_pdf_path = Path.basic "session_graph.pdf";
    39 val graph_eps_path = Path.basic "session_graph.eps";
    40 
    41 fun show_path path = Path.implode (Path.expand (Path.append (File.pwd ()) path));
    42 
    43 
    44 
    45 (** additional theory data **)
    46 
    47 structure Browser_Info = Theory_Data
    48 (
    49   type T = {chapter: string, name: string};
    50   val empty = {chapter = "Unsorted", name = "Unknown"}: T;
    51   fun extend _ = empty;
    52   fun merge _ = empty;
    53 );
    54 
    55 val _ = Theory.setup
    56   (Browser_Info.put {chapter = Context.PureN, name = Context.PureN});
    57 
    58 val session_name = #name o Browser_Info.get;
    59 val session_chapter_name = (fn {chapter, name} => [chapter, name]) o Browser_Info.get;
    60 
    61 
    62 
    63 (** graphs **)
    64 
    65 fun ID_of sess s = space_implode "/" (sess @ [s]);
    66 fun ID_of_thy thy = ID_of (session_chapter_name thy) (Context.theory_name thy);
    67 
    68 fun theory_link (curr_chapter, curr_session) thy =
    69   let
    70     val {chapter, name = session} = Browser_Info.get thy;
    71     val link = html_path (Context.theory_name thy);
    72   in
    73     if curr_session = session then SOME link
    74     else if curr_chapter = chapter then
    75       SOME (Path.appends [Path.parent, Path.basic session, link])
    76     else if chapter = Context.PureN then NONE
    77     else SOME (Path.appends [Path.parent, Path.parent, Path.basic chapter, Path.basic session, link])
    78   end;
    79 
    80 (*retrieve graph data from initial collection of theories*)
    81 fun init_graph (curr_chapter, curr_session) = rev o map (fn thy =>
    82   let
    83     val {chapter, name = session_name} = Browser_Info.get thy;
    84     val thy_name = Context.theory_name thy;
    85     val path =
    86       (case theory_link (curr_chapter, curr_session) thy of
    87         NONE => ""
    88       | SOME p => Path.implode p);
    89     val entry =
    90      {name = thy_name,
    91       ID = ID_of [chapter, session_name] thy_name,
    92       dir = session_name,
    93       path = path,
    94       unfold = false,
    95       parents = map ID_of_thy (Theory.parents_of thy),
    96       content = []};
    97   in (0, entry) end);
    98 
    99 fun ins_graph_entry (i, entry as {ID, ...}) (gr: (int * Graph_Display.node) list) =
   100   (i, entry) :: filter_out (fn (_, entry') => #ID entry' = ID) gr;
   101 
   102 
   103 
   104 (** global browser info state **)
   105 
   106 (* type theory_info *)
   107 
   108 type theory_info = {tex_source: Buffer.T, html_source: Buffer.T, html: Buffer.T};
   109 
   110 fun make_theory_info (tex_source, html_source, html) =
   111   {tex_source = tex_source, html_source = html_source, html = html}: theory_info;
   112 
   113 val empty_theory_info = make_theory_info (Buffer.empty, Buffer.empty, Buffer.empty);
   114 
   115 fun map_theory_info f {tex_source, html_source, html} =
   116   make_theory_info (f (tex_source, html_source, html));
   117 
   118 
   119 (* type browser_info *)
   120 
   121 type browser_info =
   122  {theories: theory_info Symtab.table,
   123   tex_index: (int * string) list,
   124   html_index: (int * string) list,
   125   graph: (int * Graph_Display.node) list};
   126 
   127 fun make_browser_info (theories, tex_index, html_index, graph) : browser_info =
   128   {theories = theories, tex_index = tex_index, html_index = html_index, graph = graph};
   129 
   130 val empty_browser_info = make_browser_info (Symtab.empty, [], [], []);
   131 
   132 fun init_browser_info session thys =
   133   make_browser_info (Symtab.empty, [], [], init_graph session thys);
   134 
   135 fun map_browser_info f {theories, tex_index, html_index, graph} =
   136   make_browser_info (f (theories, tex_index, html_index, graph));
   137 
   138 
   139 (* state *)
   140 
   141 val browser_info = Unsynchronized.ref empty_browser_info;
   142 fun change_browser_info f =
   143   CRITICAL (fn () => Unsynchronized.change browser_info (map_browser_info f));
   144 
   145 val suppress_tex_source = Unsynchronized.ref false;
   146 fun no_document f x = Unsynchronized.setmp suppress_tex_source true f x;
   147 
   148 fun init_theory_info name info =
   149   change_browser_info (fn (theories, tex_index, html_index, graph) =>
   150     (Symtab.update (name, info) theories, tex_index, html_index, graph));
   151 
   152 fun change_theory_info name f =
   153   change_browser_info (fn (theories, tex_index, html_index, graph) =>
   154     (case Symtab.lookup theories name of
   155       NONE => error ("Browser info: cannot access theory document " ^ quote name)
   156     | SOME info =>
   157         (Symtab.update (name, map_theory_info f info) theories, tex_index, html_index, graph)));
   158 
   159 
   160 fun add_tex_index txt =
   161   change_browser_info (fn (theories, tex_index, html_index, graph) =>
   162     (theories, txt :: tex_index, html_index, graph));
   163 
   164 fun add_html_index txt =
   165   change_browser_info (fn (theories, tex_index, html_index, graph) =>
   166     (theories, tex_index, txt :: html_index, graph));
   167 
   168 fun add_graph_entry entry =
   169   change_browser_info (fn (theories, tex_index, html_index, graph) =>
   170     (theories, tex_index, html_index, ins_graph_entry entry graph));
   171 
   172 fun add_tex_source name txt =
   173   if ! suppress_tex_source then ()
   174   else change_theory_info name (fn (tex_source, html_source, html) =>
   175     (Buffer.add txt tex_source, html_source, html));
   176 
   177 fun add_html_source name txt = change_theory_info name (fn (tex_source, html_source, html) =>
   178   (tex_source, Buffer.add txt html_source, html));
   179 
   180 
   181 
   182 (** global session state **)
   183 
   184 (* session_info *)
   185 
   186 type session_info =
   187   {name: string, chapter: string, info_path: Path.T,
   188     info: bool, doc_format: string, doc_graph: bool, doc_output: Path.T option,
   189     documents: (string * string) list, doc_dump: (bool * string), verbose: bool,
   190     readme: Path.T option};
   191 
   192 fun make_session_info
   193   (name, chapter, info_path, info, doc_format, doc_graph, doc_output,
   194     documents, doc_dump, verbose, readme) =
   195   {name = name, chapter = chapter, info_path = info_path, info = info,
   196     doc_format = doc_format, doc_graph = doc_graph, doc_output = doc_output,
   197     documents = documents, doc_dump = doc_dump, verbose = verbose,
   198     readme = readme}: session_info;
   199 
   200 
   201 (* state *)
   202 
   203 val session_info = Unsynchronized.ref (NONE: session_info option);
   204 
   205 fun with_session_info x f = (case ! session_info of NONE => x | SOME info => f info);
   206 
   207 
   208 
   209 (** document preparation **)
   210 
   211 (* document variants *)
   212 
   213 fun read_variant str =
   214   (case space_explode "=" str of
   215     [name] => (name, "")
   216   | [name, tags] => (name, tags)
   217   | _ => error ("Malformed document variant specification: " ^ quote str));
   218 
   219 
   220 (* init session *)
   221 
   222 fun init build info info_path doc doc_graph document_output doc_variants
   223     (chapter, name) (doc_dump as (_, dump_prefix)) verbose thys =
   224   if not build andalso not info andalso doc = "" andalso dump_prefix = "" then
   225     (browser_info := empty_browser_info; session_info := NONE)
   226   else
   227     let
   228       val doc_output =
   229         if document_output = "" then NONE else SOME (Path.explode document_output);
   230 
   231       val documents =
   232         if doc = "" then []
   233         else if not (can File.check_dir document_path) then
   234           (if verbose then Output.physical_stderr "Warning: missing document directory\n"
   235            else (); [])
   236         else doc_variants;
   237 
   238       val readme = if File.exists readme_html_path then SOME readme_html_path else NONE;
   239 
   240       val docs =
   241         (case readme of NONE => [] | SOME p => [(Url.File p, "README")]) @
   242           map (fn (name, _) => (Url.File (Path.ext doc (Path.basic name)), name)) documents;
   243     in
   244       session_info :=
   245         SOME (make_session_info (name, chapter, info_path, info, doc,
   246           doc_graph, doc_output, documents, doc_dump, verbose, readme));
   247       browser_info := init_browser_info (chapter, name) thys;
   248       add_html_index (0, HTML.begin_session_index name docs (Url.explode "medium.html"))
   249     end;
   250 
   251 
   252 (* isabelle tool wrappers *)
   253 
   254 fun isabelle_document {verbose, purge} format name tags dir =
   255   let
   256     val s = "\"$ISABELLE_TOOL\" document" ^ (if purge then " -c" else "") ^ " -o '" ^ format ^ "' \
   257       \-n '" ^ name ^ "' -t '" ^ tags ^ "' " ^ File.shell_path dir ^ " 2>&1";
   258     val doc_path = Path.appends [dir, Path.parent, Path.basic name |> Path.ext format];
   259     val _ = if verbose then writeln s else ();
   260     val (out, rc) = Isabelle_System.bash_output s;
   261     val _ =
   262       if not (File.exists doc_path) orelse rc <> 0 then
   263         cat_error out ("Failed to build document " ^ quote (show_path doc_path))
   264       else if verbose then writeln out
   265       else ();
   266   in doc_path end;
   267 
   268 fun isabelle_browser graph = Isabelle_System.with_tmp_dir "browser" (fn dir =>
   269   let
   270     val pdf_path = Path.append dir graph_pdf_path;
   271     val eps_path = Path.append dir graph_eps_path;
   272     val graph_path = Path.append dir graph_path;
   273     val _ = Graph_Display.write_graph_browser graph_path graph;
   274     val args = "-o " ^ File.shell_path pdf_path ^ " " ^ File.shell_path graph_path;
   275   in
   276     if Isabelle_System.isabelle_tool "browser" args = 0 andalso
   277       File.exists pdf_path andalso File.exists eps_path
   278     then (File.read pdf_path, File.read eps_path)
   279     else error "Failed to prepare dependency graph"
   280   end);
   281 
   282 
   283 (* finish session -- output all generated text *)
   284 
   285 fun sorted_index index = map snd (sort (int_ord o pairself fst) (rev index));
   286 fun index_buffer index = Buffer.add (implode (sorted_index index)) Buffer.empty;
   287 
   288 fun write_tex src name path =
   289   File.write_buffer (Path.append path (tex_path name)) src;
   290 
   291 fun write_tex_index tex_index path =
   292   write_tex (index_buffer tex_index |> Buffer.add Latex.tex_trailer) doc_indexN path;
   293 
   294 
   295 fun finish () =
   296   with_session_info () (fn {name, chapter, info, info_path, doc_format, doc_graph, doc_output,
   297     documents, doc_dump = (dump_copy, dump_prefix), verbose, readme, ...} =>
   298   let
   299     val {theories, tex_index, html_index, graph} = ! browser_info;
   300     val thys = Symtab.dest theories;
   301 
   302     val chapter_prefix = Path.append info_path (Path.basic chapter);
   303     val session_prefix = Path.append chapter_prefix (Path.basic name);
   304 
   305     fun finish_html (a, {html, ...}: theory_info) =
   306       File.write_buffer (Path.append session_prefix (html_path a))
   307         (Buffer.add HTML.end_document html);
   308 
   309     val sorted_graph = sorted_index graph;
   310     val opt_graphs =
   311       if doc_graph andalso (not (null documents) orelse dump_prefix <> "") then
   312         SOME (isabelle_browser sorted_graph)
   313       else NONE;
   314 
   315     val _ =
   316       if info then
   317        (Isabelle_System.mkdirs session_prefix;
   318         File.write_buffer (Path.append session_prefix index_path)
   319           (index_buffer html_index |> Buffer.add HTML.end_document);
   320         (case readme of NONE => () | SOME path => File.copy path session_prefix);
   321         Graph_Display.write_graph_browser (Path.append session_prefix graph_path) sorted_graph;
   322         Isabelle_System.isabelle_tool "browser" "-b";
   323         File.copy (Path.explode "~~/lib/browser/GraphBrowser.jar") session_prefix;
   324         List.app (fn (a, txt) => File.write (Path.append session_prefix (Path.basic a)) txt)
   325           (HTML.applet_pages name (Url.File index_path, name));
   326         File.copy (Path.explode "~~/etc/isabelle.css") session_prefix;
   327         List.app finish_html thys;
   328         if verbose
   329         then Output.physical_stderr ("Browser info at " ^ show_path session_prefix ^ "\n")
   330         else ())
   331       else ();
   332 
   333     fun prepare_sources doc_copy doc_dir =
   334      (Isabelle_System.mkdirs doc_dir;
   335       if doc_copy then Isabelle_System.copy_dir document_path doc_dir else ();
   336       Isabelle_System.isabelle_tool "latex"
   337         ("-o sty " ^ File.shell_path (Path.append doc_dir (Path.basic "root.tex")));
   338       (case opt_graphs of NONE => () | SOME (pdf, eps) =>
   339         (File.write (Path.append doc_dir graph_pdf_path) pdf;
   340           File.write (Path.append doc_dir graph_eps_path) eps));
   341       write_tex_index tex_index doc_dir;
   342       List.app (fn (a, {tex_source, ...}) => write_tex tex_source a doc_dir) thys);
   343 
   344     val _ =
   345       if dump_prefix = "" then ()
   346       else
   347         let
   348           val path = Path.explode dump_prefix;
   349           val _ = prepare_sources dump_copy path;
   350         in
   351           if verbose then
   352             Output.physical_stderr ("Document sources at " ^ show_path path ^ "\n")
   353           else ()
   354         end;
   355 
   356     fun document_job doc_prefix backdrop (name, tags) =
   357       let
   358         val _ =
   359           File.eq (document_path, doc_prefix) andalso
   360             error ("Overlap of document input and output directory " ^ Path.print doc_prefix);
   361         val dir = Path.append doc_prefix (Path.basic name);
   362         val copy = not (File.eq (document_path, dir));
   363         val _ = prepare_sources copy dir;
   364         fun inform doc =
   365           if verbose orelse not backdrop then
   366             Output.physical_stderr ("Document at " ^ show_path doc ^ "\n")
   367           else ();
   368       in
   369         fn () =>
   370           (isabelle_document {verbose = true, purge = backdrop} doc_format name tags dir, inform)
   371       end;
   372 
   373     val jobs =
   374       (if info orelse is_none doc_output then
   375         map (document_job session_prefix true) documents
   376        else []) @
   377       (case doc_output of
   378         NONE => []
   379       | SOME path => map (document_job path false) documents);
   380 
   381     val _ = jobs |> Par_List.map (fn job => job ()) |> List.app (op |>);
   382   in
   383     browser_info := empty_browser_info;
   384     session_info := NONE
   385   end);
   386 
   387 
   388 (* theory elements *)
   389 
   390 fun init_theory name = with_session_info () (fn _ => init_theory_info name empty_theory_info);
   391 
   392 fun theory_source name mk_text =
   393   with_session_info () (fn _ => add_html_source name (HTML.theory_source (mk_text ())));
   394 
   395 fun theory_output name s =
   396   with_session_info () (fn _ => add_tex_source name (Latex.isabelle_file name s));
   397 
   398 fun begin_theory update_time thy = with_session_info thy (fn {name = session_name, chapter, ...} =>
   399   let
   400     val name = Context.theory_name thy;
   401     val parents = Theory.parents_of thy;
   402     val parent_specs = parents |> map (fn parent =>
   403       (Option.map Url.File (theory_link (chapter, session_name) parent),
   404         (Context.theory_name parent)));
   405 
   406     fun prep_html_source (tex_source, html_source, html) =
   407       let val txt = HTML.begin_theory name parent_specs (Buffer.content html_source)
   408       in (tex_source, Buffer.empty, Buffer.add txt html) end;
   409 
   410     val entry =
   411      {name = name,
   412       ID = ID_of [chapter, session_name] name,
   413       dir = session_name,
   414       unfold = true,
   415       path = Path.implode (html_path name),
   416       parents = map ID_of_thy parents,
   417       content = []};
   418   in
   419     change_theory_info name prep_html_source;
   420     add_graph_entry (update_time, entry);
   421     add_html_index (update_time, HTML.theory_entry (Url.File (html_path name), name));
   422     add_tex_index (update_time, Latex.theory_entry name);
   423     Browser_Info.put {chapter = chapter, name = session_name} thy
   424   end);
   425 
   426 
   427 
   428 (** draft document output **)
   429 
   430 fun display_drafts src_paths = Isabelle_System.with_tmp_dir "drafts" (fn dir =>
   431   let
   432     fun prep_draft path i =
   433       let
   434         val base = Path.base path;
   435         val name =
   436           (case Path.implode (#1 (Path.split_ext base)) of
   437             "" => "DUMMY"
   438           | s => s)  ^ serial_string ();
   439       in
   440         if File.exists path then
   441           (((name, base, File.read path), (i, Latex.theory_entry name)), i + 1)
   442         else error ("Bad file: " ^ Path.print path)
   443       end;
   444     val (srcs, tex_index) = split_list (fst (fold_map prep_draft src_paths 0));
   445 
   446     val doc_path = Path.append dir document_path;
   447     val _ = Isabelle_System.mkdirs doc_path;
   448     val root_path = Path.append doc_path (Path.basic "root.tex");
   449     val _ = File.copy (Path.explode "~~/lib/texinputs/draft.tex") root_path;
   450     val _ = Isabelle_System.isabelle_tool "latex" ("-o sty " ^ File.shell_path root_path);
   451     val _ = Isabelle_System.isabelle_tool "latex" ("-o syms " ^ File.shell_path root_path);
   452 
   453     fun known name =
   454       let val ss = split_lines (File.read (Path.append doc_path (Path.basic name)))
   455       in member (op =) ss end;
   456     val known_syms = known "syms.lst";
   457     val known_ctrls = known "ctrls.lst";
   458 
   459     val _ = srcs |> List.app (fn (name, base, txt) =>
   460       Symbol.explode txt
   461       |> Latex.symbol_source (known_syms, known_ctrls) (Path.implode base)
   462       |> File.write (Path.append doc_path (tex_path name)));
   463     val _ = write_tex_index tex_index doc_path;
   464 
   465     val result =
   466       isabelle_document {verbose = false, purge = true} "pdf" documentN "" doc_path;
   467     val detachable_result = Isabelle_System.create_tmp_path documentN "pdf";
   468     val _ = File.copy result detachable_result;
   469   in
   470     Isabelle_System.isabelle_tool "display" ("-c " ^ File.shell_path detachable_result ^ " &")
   471   end);
   472 
   473 end;
   474