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