src/Pure/Thy/present.ML
author wenzelm
Mon Feb 23 14:50:30 2015 +0100 (2015-02-23)
changeset 59564 fdc03c8daacc
parent 59448 149d2bc5ddb6
child 60082 d3573eb7728f
permissions -rw-r--r--
Goal.prove_multi is superseded by the fully general Goal.prove_common;
     1 (*  Title:      Pure/Thy/present.ML
     2     Author:     Markus Wenzel and Stefan Berghofer, TU Muenchen
     3 
     4 Theory presentation: HTML and 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 -> string -> (string * string) list ->
    13     (Path.T * Path.T) list -> Path.T -> string * string -> bool -> unit
    14   val finish: unit -> unit
    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 session_graph_path = Path.basic "session_graph.pdf";
    36 
    37 fun show_path path = Path.implode (Path.expand (Path.append (File.pwd ()) path));
    38 
    39 
    40 
    41 (** additional theory data **)
    42 
    43 structure Browser_Info = Theory_Data
    44 (
    45   type T = {chapter: string, name: string};
    46   val empty = {chapter = "Unsorted", name = "Unknown"}: T;
    47   fun extend _ = empty;
    48   fun merge _ = empty;
    49 );
    50 
    51 val _ = Theory.setup
    52   (Browser_Info.put {chapter = Context.PureN, name = Context.PureN});
    53 
    54 val session_name = #name o Browser_Info.get;
    55 
    56 
    57 
    58 (** global browser info state **)
    59 
    60 (* type theory_info *)
    61 
    62 type theory_info = {tex_source: string, html_source: string};
    63 
    64 fun make_theory_info (tex_source, html_source) =
    65   {tex_source = tex_source, html_source = html_source}: theory_info;
    66 
    67 fun map_theory_info f {tex_source, html_source} =
    68   make_theory_info (f (tex_source, html_source));
    69 
    70 
    71 (* type browser_info *)
    72 
    73 type browser_info =
    74  {theories: theory_info Symtab.table,
    75   tex_index: (int * string) list,
    76   html_index: (int * string) list};
    77 
    78 fun make_browser_info (theories, tex_index, html_index) : browser_info =
    79   {theories = theories, tex_index = tex_index, html_index = html_index};
    80 
    81 val empty_browser_info = make_browser_info (Symtab.empty, [], []);
    82 
    83 fun map_browser_info f {theories, tex_index, html_index} =
    84   make_browser_info (f (theories, tex_index, html_index));
    85 
    86 
    87 (* state *)
    88 
    89 val browser_info = Synchronized.var "browser_info" empty_browser_info;
    90 fun change_browser_info f = Synchronized.change browser_info (map_browser_info f);
    91 
    92 fun init_theory_info name info =
    93   change_browser_info (fn (theories, tex_index, html_index) =>
    94     (Symtab.update (name, info) theories, tex_index, html_index));
    95 
    96 fun change_theory_info name f =
    97   change_browser_info (fn (theories, tex_index, html_index) =>
    98     (case Symtab.lookup theories name of
    99       NONE => error ("Browser info: cannot access theory document " ^ quote name)
   100     | SOME info => (Symtab.update (name, map_theory_info f info) theories, tex_index, html_index)));
   101 
   102 
   103 fun add_tex_index txt =
   104   change_browser_info (fn (theories, tex_index, html_index) =>
   105     (theories, txt :: tex_index, html_index));
   106 
   107 fun add_html_index txt =
   108   change_browser_info (fn (theories, tex_index, html_index) =>
   109     (theories, tex_index, txt :: html_index));
   110 
   111 
   112 
   113 (** global session state **)
   114 
   115 (* session_info *)
   116 
   117 type session_info =
   118   {name: string, chapter: string, info_path: Path.T, info: bool, doc_format: string,
   119     doc_output: Path.T option, doc_files: (Path.T * Path.T) list, graph_file: Path.T,
   120     documents: (string * string) list, verbose: bool, readme: Path.T option};
   121 
   122 fun make_session_info
   123   (name, chapter, info_path, info, doc_format, doc_output, doc_files,
   124     graph_file, documents, verbose, readme) =
   125   {name = name, chapter = chapter, info_path = info_path, info = info, doc_format = doc_format,
   126     doc_output = doc_output, doc_files = doc_files, graph_file = graph_file,
   127     documents = documents, verbose = verbose, readme = readme}: session_info;
   128 
   129 
   130 (* state *)
   131 
   132 val session_info = Unsynchronized.ref (NONE: session_info option);
   133 
   134 fun with_session_info x f = (case ! session_info of NONE => x | SOME info => f info);
   135 
   136 
   137 
   138 (** document preparation **)
   139 
   140 (* options *)
   141 
   142 fun document_enabled s = s <> "" andalso s <> "false";
   143 
   144 fun document_variants str =
   145   let
   146     fun read_variant s =
   147       (case space_explode "=" s of
   148         [name] => (name, "")
   149       | [name, tags] => (name, tags)
   150       | _ => error ("Malformed document variant specification: " ^ quote s));
   151     val variants = map read_variant (space_explode ":" str);
   152     val _ =
   153       (case duplicates (op =) (map #1 variants) of
   154         [] => ()
   155       | dups => error ("Duplicate document variants: " ^ commas_quote dups));
   156   in variants end;
   157 
   158 
   159 (* init session *)
   160 
   161 fun init build info info_path doc document_output doc_variants doc_files graph_file
   162     (chapter, name) verbose =
   163   if not build andalso not info andalso doc = "" then
   164     (Synchronized.change browser_info (K empty_browser_info); session_info := NONE)
   165   else
   166     let
   167       val doc_output =
   168         if document_output = "" then NONE else SOME (Path.explode document_output);
   169 
   170       val documents =
   171         if doc = "" then []
   172         else if null doc_files andalso not (can File.check_dir document_path) then
   173           (if verbose then Output.physical_stderr "Warning: missing document directory\n"
   174            else (); [])
   175         else doc_variants;
   176 
   177       val readme = if File.exists readme_html_path then SOME readme_html_path else NONE;
   178 
   179       val docs =
   180         (case readme of NONE => [] | SOME p => [(Url.File p, "README")]) @
   181           map (fn (name, _) => (Url.File (Path.ext doc (Path.basic name)), name)) documents;
   182     in
   183       session_info :=
   184         SOME (make_session_info (name, chapter, info_path, info, doc,
   185           doc_output, doc_files, graph_file, documents, verbose, readme));
   186       Synchronized.change browser_info (K empty_browser_info);
   187       add_html_index (0, HTML.begin_session_index name (Url.File session_graph_path) docs)
   188     end;
   189 
   190 
   191 (* isabelle tool wrappers *)
   192 
   193 fun isabelle_document {verbose, purge} format name tags dir =
   194   let
   195     val s = "\"$ISABELLE_TOOL\" document" ^ (if purge then " -c" else "") ^ " -o '" ^ format ^ "' \
   196       \-n '" ^ name ^ "' -t '" ^ tags ^ "' " ^ File.shell_path dir ^ " 2>&1";
   197     val doc_path = Path.appends [dir, Path.parent, Path.basic name |> Path.ext format];
   198     val _ = if verbose then writeln s else ();
   199     val (out, rc) = Isabelle_System.bash_output s;
   200     val _ =
   201       if not (File.exists doc_path) orelse rc <> 0 then
   202         cat_error out ("Failed to build document " ^ quote (show_path doc_path))
   203       else if verbose then writeln out
   204       else ();
   205   in doc_path end;
   206 
   207 
   208 (* finish session -- output all generated text *)
   209 
   210 fun sorted_index index = map snd (sort (int_ord o apply2 fst) (rev index));
   211 fun index_buffer index = Buffer.add (implode (sorted_index index)) Buffer.empty;
   212 
   213 fun write_tex src name path =
   214   File.write_buffer (Path.append path (tex_path name)) src;
   215 
   216 fun write_tex_index tex_index path =
   217   write_tex (index_buffer tex_index |> Buffer.add Latex.tex_trailer) doc_indexN path;
   218 
   219 fun finish () =
   220   with_session_info () (fn {name, chapter, info, info_path, doc_format,
   221     doc_output, doc_files, graph_file, documents, verbose, readme, ...} =>
   222   let
   223     val {theories, tex_index, html_index} = Synchronized.value browser_info;
   224     val thys = Symtab.dest theories;
   225 
   226     val chapter_prefix = Path.append info_path (Path.basic chapter);
   227     val session_prefix = Path.append chapter_prefix (Path.basic name);
   228 
   229     fun finish_html (a, {html_source, ...}: theory_info) =
   230       File.write (Path.append session_prefix (html_path a)) html_source;
   231 
   232     val _ =
   233       if info then
   234        (Isabelle_System.mkdirs session_prefix;
   235         File.write_buffer (Path.append session_prefix index_path)
   236           (index_buffer html_index |> Buffer.add HTML.end_document);
   237         Isabelle_System.copy_file graph_file (Path.append session_prefix session_graph_path);
   238         (case readme of NONE => () | SOME path => Isabelle_System.copy_file path session_prefix);
   239         Isabelle_System.copy_file (Path.explode "~~/etc/isabelle.css") session_prefix;
   240         List.app finish_html thys;
   241         if verbose
   242         then Output.physical_stderr ("Browser info at " ^ show_path session_prefix ^ "\n")
   243         else ())
   244       else ();
   245 
   246     fun document_job doc_prefix backdrop (doc_name, tags) =
   247       let
   248         val doc_dir = Path.append doc_prefix (Path.basic doc_name);
   249         val _ = Isabelle_System.mkdirs doc_dir;
   250         val _ =
   251           Isabelle_System.isabelle_tool "latex"
   252             ("-o sty " ^ File.shell_path (Path.append doc_dir (Path.basic "root.tex")));
   253         val _ = List.app (fn file => Isabelle_System.copy_file_base file doc_dir) doc_files;
   254         val _ = Isabelle_System.copy_file graph_file (Path.append doc_dir session_graph_path);
   255         val _ = write_tex_index tex_index doc_dir;
   256         val _ =
   257           List.app (fn (a, {tex_source, ...}) =>
   258             write_tex (Buffer.add tex_source Buffer.empty) a doc_dir) thys;
   259       in
   260         fn () =>
   261           (isabelle_document {verbose = true, purge = backdrop} doc_format doc_name tags doc_dir,
   262             fn doc =>
   263               if verbose orelse not backdrop then
   264                 Output.physical_stderr ("Document at " ^ show_path doc ^ "\n")
   265               else ())
   266       end;
   267 
   268     val jobs =
   269       (if info orelse is_none doc_output then
   270         map (document_job session_prefix true) documents
   271        else []) @
   272       (case doc_output of
   273         NONE => []
   274       | SOME path => map (document_job path false) documents);
   275 
   276     val _ = jobs |> Par_List.map (fn job => job ()) |> List.app (op |>);
   277   in
   278     Synchronized.change browser_info (K empty_browser_info);
   279     session_info := NONE
   280   end);
   281 
   282 
   283 (* theory elements *)
   284 
   285 fun theory_output name s =
   286   with_session_info () (fn _ =>
   287     change_theory_info name (fn (_, html_source) => (Latex.isabelle_theory name s, html_source)));
   288 
   289 fun theory_link (curr_chapter, curr_session) thy =
   290   let
   291     val {chapter, name = session} = Browser_Info.get thy;
   292     val link = html_path (Context.theory_name thy);
   293   in
   294     if curr_session = session then SOME link
   295     else if curr_chapter = chapter then
   296       SOME (Path.appends [Path.parent, Path.basic session, link])
   297     else if chapter = Context.PureN then NONE
   298     else SOME (Path.appends [Path.parent, Path.parent, Path.basic chapter, Path.basic session, link])
   299   end;
   300 
   301 fun begin_theory update_time mk_text thy =
   302   with_session_info thy (fn {name = session_name, chapter, ...} =>
   303     let
   304       val name = Context.theory_name thy;
   305       val parents = Theory.parents_of thy;
   306 
   307       val parent_specs = parents |> map (fn parent =>
   308         (Option.map Url.File (theory_link (chapter, session_name) parent),
   309           (Context.theory_name parent)));
   310       val html_source = HTML.theory name parent_specs (mk_text ());
   311     in
   312       init_theory_info name (make_theory_info ("", html_source));
   313       add_html_index (update_time, HTML.theory_entry (Url.File (html_path name), name));
   314       add_tex_index (update_time, Latex.theory_entry name);
   315       Browser_Info.put {chapter = chapter, name = session_name} thy
   316     end);
   317 
   318 
   319 
   320 (** draft document output **)
   321 
   322 fun display_drafts src_paths = Isabelle_System.with_tmp_dir "drafts" (fn dir =>
   323   let
   324     fun prep_draft path i =
   325       let
   326         val base = Path.base path;
   327         val name =
   328           (case Path.implode (#1 (Path.split_ext base)) of
   329             "" => "DUMMY"
   330           | s => s)  ^ serial_string ();
   331       in
   332         if File.exists path then
   333           (((name, base, File.read path), (i, Latex.theory_entry name)), i + 1)
   334         else error ("Bad file: " ^ Path.print path)
   335       end;
   336     val (srcs, tex_index) = split_list (fst (fold_map prep_draft src_paths 0));
   337 
   338     val doc_path = Path.append dir document_path;
   339     val _ = Isabelle_System.mkdirs doc_path;
   340     val root_path = Path.append doc_path (Path.basic "root.tex");
   341     val _ = Isabelle_System.copy_file (Path.explode "~~/lib/texinputs/draft.tex") root_path;
   342     val _ = Isabelle_System.isabelle_tool "latex" ("-o sty " ^ File.shell_path root_path);
   343     val _ = Isabelle_System.isabelle_tool "latex" ("-o syms " ^ File.shell_path root_path);
   344 
   345     fun known name =
   346       let val ss = split_lines (File.read (Path.append doc_path (Path.basic name)))
   347       in member (op =) ss end;
   348     val known_syms = known "syms.lst";
   349     val known_ctrls = known "ctrls.lst";
   350 
   351     val _ = srcs |> List.app (fn (name, base, txt) =>
   352       Symbol.explode txt
   353       |> Latex.symbol_source (known_syms, known_ctrls) (Path.implode base)
   354       |> File.write (Path.append doc_path (tex_path name)));
   355     val _ = write_tex_index tex_index doc_path;
   356 
   357     val result =
   358       isabelle_document {verbose = false, purge = true} "pdf" documentN "" doc_path;
   359 
   360     val target_dir = Path.explode "$ISABELLE_HOME_USER/tmp";
   361     val target = Path.explode "$ISABELLE_HOME_USER/tmp/drafts.pdf"
   362     val _ = Isabelle_System.mkdirs target_dir;
   363     val _ = Isabelle_System.copy_file result target;
   364   in
   365     Isabelle_System.isabelle_tool "display" (File.shell_path target ^ " &")
   366   end);
   367 
   368 end;
   369