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