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