src/Pure/Thy/present.ML
author wenzelm
Tue Feb 11 17:44:29 2014 +0100 (2014-02-11)
changeset 55431 e0f20a44ff9d
parent 54683 cf48ddc266e5
child 56530 5c178501cf78
permissions -rw-r--r--
common Command.Chunk for command source and auxiliary files (static Symbol.Index without actual String content);
more informative type Blob, to allow markup reports;
     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     string * string -> bool * 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,
   172     info: bool, doc_format: string, doc_graph: bool, doc_output: Path.T option,
   173     documents: (string * string) list, doc_dump: (bool * string), verbose: bool,
   174     readme: Path.T option};
   175 
   176 fun make_session_info
   177   (name, chapter, info_path, info, doc_format, doc_graph, doc_output,
   178     documents, doc_dump, 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     documents = documents, doc_dump = doc_dump, 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
   207     (chapter, name) (doc_dump as (_, dump_prefix)) verbose thys =
   208   if not build andalso not info andalso doc = "" andalso dump_prefix = "" 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 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, documents, doc_dump, 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 
   279 fun finish () =
   280   with_session_info () (fn {name, chapter, info, info_path, doc_format, doc_graph, doc_output,
   281     documents, doc_dump = (dump_copy, dump_prefix), verbose, readme, ...} =>
   282   let
   283     val {theories, tex_index, html_index, graph} = ! browser_info;
   284     val thys = Symtab.dest theories;
   285 
   286     val chapter_prefix = Path.append info_path (Path.basic chapter);
   287     val session_prefix = Path.append chapter_prefix (Path.basic name);
   288 
   289     fun finish_html (a, {html_source, ...}: theory_info) =
   290       File.write (Path.append session_prefix (html_path a)) html_source;
   291 
   292     val sorted_graph = sorted_index graph;
   293     val opt_graphs =
   294       if doc_graph andalso (not (null documents) orelse dump_prefix <> "") then
   295         SOME (isabelle_browser sorted_graph)
   296       else NONE;
   297 
   298     val _ =
   299       if info then
   300        (Isabelle_System.mkdirs session_prefix;
   301         File.write_buffer (Path.append session_prefix index_path)
   302           (index_buffer html_index |> Buffer.add HTML.end_document);
   303         (case readme of NONE => () | SOME path => File.copy path session_prefix);
   304         Graph_Display.write_graph_browser (Path.append session_prefix graph_path) sorted_graph;
   305         Isabelle_System.isabelle_tool "browser" "-b";
   306         File.copy (Path.explode "~~/lib/browser/GraphBrowser.jar") session_prefix;
   307         List.app (fn (a, txt) => File.write (Path.append session_prefix (Path.basic a)) txt)
   308           (HTML.applet_pages name (Url.File index_path, name));
   309         File.copy (Path.explode "~~/etc/isabelle.css") session_prefix;
   310         List.app finish_html thys;
   311         if verbose
   312         then Output.physical_stderr ("Browser info at " ^ show_path session_prefix ^ "\n")
   313         else ())
   314       else ();
   315 
   316     fun prepare_sources doc_copy doc_dir =
   317      (Isabelle_System.mkdirs doc_dir;
   318       if doc_copy then Isabelle_System.copy_dir document_path doc_dir else ();
   319       Isabelle_System.isabelle_tool "latex"
   320         ("-o sty " ^ File.shell_path (Path.append doc_dir (Path.basic "root.tex")));
   321       (case opt_graphs of NONE => () | SOME (pdf, eps) =>
   322         (File.write (Path.append doc_dir graph_pdf_path) pdf;
   323           File.write (Path.append doc_dir graph_eps_path) eps));
   324       write_tex_index tex_index doc_dir;
   325       List.app (fn (a, {tex_source, ...}) =>
   326         write_tex (Buffer.add tex_source Buffer.empty) a doc_dir) thys);
   327 
   328     val _ =
   329       if dump_prefix = "" then ()
   330       else
   331         let
   332           val path = Path.explode dump_prefix;
   333           val _ = prepare_sources dump_copy path;
   334         in
   335           if verbose then
   336             Output.physical_stderr ("Document sources at " ^ show_path path ^ "\n")
   337           else ()
   338         end;
   339 
   340     fun document_job doc_prefix backdrop (name, tags) =
   341       let
   342         val _ =
   343           File.eq (document_path, doc_prefix) andalso
   344             error ("Overlap of document input and output directory " ^ Path.print doc_prefix);
   345         val dir = Path.append doc_prefix (Path.basic name);
   346         val copy = not (File.eq (document_path, dir));
   347         val _ = prepare_sources copy dir;
   348         fun inform doc =
   349           if verbose orelse not backdrop then
   350             Output.physical_stderr ("Document at " ^ show_path doc ^ "\n")
   351           else ();
   352       in
   353         fn () =>
   354           (isabelle_document {verbose = true, purge = backdrop} doc_format name tags dir, inform)
   355       end;
   356 
   357     val jobs =
   358       (if info orelse is_none doc_output then
   359         map (document_job session_prefix true) documents
   360        else []) @
   361       (case doc_output of
   362         NONE => []
   363       | SOME path => map (document_job path false) documents);
   364 
   365     val _ = jobs |> Par_List.map (fn job => job ()) |> List.app (op |>);
   366   in
   367     browser_info := empty_browser_info;
   368     session_info := NONE
   369   end);
   370 
   371 
   372 (* theory elements *)
   373 
   374 fun theory_output name s =
   375   with_session_info () (fn _ =>
   376     change_theory_info name (fn (_, html_source) => (Latex.isabelle_file name s, html_source)));
   377 
   378 fun begin_theory update_time mk_text thy =
   379   with_session_info thy (fn {name = session_name, chapter, ...} =>
   380     let
   381       val name = Context.theory_name thy;
   382       val parents = Theory.parents_of thy;
   383 
   384       val parent_specs = parents |> map (fn parent =>
   385         (Option.map Url.File (theory_link (chapter, session_name) parent),
   386           (Context.theory_name parent)));
   387       val html_source = HTML.theory name parent_specs (mk_text ());
   388 
   389       val graph_entry =
   390        {name = name,
   391         ID = ID_of [chapter, session_name] name,
   392         dir = session_name,
   393         unfold = true,
   394         path = Path.implode (html_path name),
   395         parents = map ID_of_thy parents,
   396         content = []};
   397     in
   398       init_theory_info name (make_theory_info ("", html_source));
   399       add_graph_entry (update_time, graph_entry);
   400       add_html_index (update_time, HTML.theory_entry (Url.File (html_path name), name));
   401       add_tex_index (update_time, Latex.theory_entry name);
   402       Browser_Info.put {chapter = chapter, name = session_name} thy
   403     end);
   404 
   405 
   406 
   407 (** draft document output **)
   408 
   409 fun display_drafts src_paths = Isabelle_System.with_tmp_dir "drafts" (fn dir =>
   410   let
   411     fun prep_draft path i =
   412       let
   413         val base = Path.base path;
   414         val name =
   415           (case Path.implode (#1 (Path.split_ext base)) of
   416             "" => "DUMMY"
   417           | s => s)  ^ serial_string ();
   418       in
   419         if File.exists path then
   420           (((name, base, File.read path), (i, Latex.theory_entry name)), i + 1)
   421         else error ("Bad file: " ^ Path.print path)
   422       end;
   423     val (srcs, tex_index) = split_list (fst (fold_map prep_draft src_paths 0));
   424 
   425     val doc_path = Path.append dir document_path;
   426     val _ = Isabelle_System.mkdirs doc_path;
   427     val root_path = Path.append doc_path (Path.basic "root.tex");
   428     val _ = File.copy (Path.explode "~~/lib/texinputs/draft.tex") root_path;
   429     val _ = Isabelle_System.isabelle_tool "latex" ("-o sty " ^ File.shell_path root_path);
   430     val _ = Isabelle_System.isabelle_tool "latex" ("-o syms " ^ File.shell_path root_path);
   431 
   432     fun known name =
   433       let val ss = split_lines (File.read (Path.append doc_path (Path.basic name)))
   434       in member (op =) ss end;
   435     val known_syms = known "syms.lst";
   436     val known_ctrls = known "ctrls.lst";
   437 
   438     val _ = srcs |> List.app (fn (name, base, txt) =>
   439       Symbol.explode txt
   440       |> Latex.symbol_source (known_syms, known_ctrls) (Path.implode base)
   441       |> File.write (Path.append doc_path (tex_path name)));
   442     val _ = write_tex_index tex_index doc_path;
   443 
   444     val result =
   445       isabelle_document {verbose = false, purge = true} "pdf" documentN "" doc_path;
   446 
   447     val target_dir = Path.explode "$ISABELLE_HOME_USER/tmp";
   448     val target = Path.explode "$ISABELLE_HOME_USER/tmp/drafts.pdf"
   449     val _ = Isabelle_System.mkdirs target_dir;
   450     val _ = File.copy result target;
   451   in
   452     Isabelle_System.isabelle_tool "display" (File.shell_path target ^ " &")
   453   end);
   454 
   455 end;
   456