src/Pure/Thy/present.ML
changeset 15531 08c8dad8e399
parent 15159 2ef19a680646
child 15570 8d8c70b41bab
equal deleted inserted replaced
15530:6f43714517ee 15531:08c8dad8e399
   175     (Symtab.update ((name, info), theories), files, tex_index, html_index, graph));
   175     (Symtab.update ((name, info), theories), files, tex_index, html_index, graph));
   176 
   176 
   177 fun change_theory_info name f =
   177 fun change_theory_info name f =
   178   change_browser_info (fn (info as (theories, files, tex_index, html_index, graph)) =>
   178   change_browser_info (fn (info as (theories, files, tex_index, html_index, graph)) =>
   179     (case Symtab.lookup (theories, name) of
   179     (case Symtab.lookup (theories, name) of
   180       None => (warning ("Browser info: cannot access theory document " ^ quote name); info)
   180       NONE => (warning ("Browser info: cannot access theory document " ^ quote name); info)
   181     | Some info => (Symtab.update ((name, map_theory_info f info), theories), files,
   181     | SOME info => (Symtab.update ((name, map_theory_info f info), theories), files,
   182         tex_index, html_index, graph)));
   182         tex_index, html_index, graph)));
   183 
   183 
   184 
   184 
   185 fun add_file file =
   185 fun add_file file =
   186   change_browser_info (fn (theories, files, tex_index, html_index, graph) =>
   186   change_browser_info (fn (theories, files, tex_index, html_index, graph) =>
   229     readme = readme}: session_info;
   229     readme = readme}: session_info;
   230 
   230 
   231 
   231 
   232 (* state *)
   232 (* state *)
   233 
   233 
   234 val session_info = ref (None: session_info option);
   234 val session_info = ref (NONE: session_info option);
   235 
   235 
   236 fun with_session x f = (case ! session_info of None => x | Some info => f info);
   236 fun with_session x f = (case ! session_info of NONE => x | SOME info => f info);
   237 fun with_context f = f (PureThy.get_name (Context.the_context ()));
   237 fun with_context f = f (PureThy.get_name (Context.the_context ()));
   238 
   238 
   239 
   239 
   240 
   240 
   241 (** document preparation **)
   241 (** document preparation **)
   258     session_entries (get_entries dir) ^ HTML.end_index
   258     session_entries (get_entries dir) ^ HTML.end_index
   259   |> File.write (Path.append dir index_path);
   259   |> File.write (Path.append dir index_path);
   260 
   260 
   261 fun update_index dir name =
   261 fun update_index dir name =
   262   (case try get_entries dir of
   262   (case try get_entries dir of
   263     None => warning ("Browser info: cannot access session index of " ^ quote (Path.pack dir))
   263     NONE => warning ("Browser info: cannot access session index of " ^ quote (Path.pack dir))
   264   | Some es => (put_entries ((es \ name) @ [name]) dir; create_index dir));
   264   | SOME es => (put_entries ((es \ name) @ [name]) dir; create_index dir));
   265 
   265 
   266 
   266 
   267 (* init session *)
   267 (* init session *)
   268 
   268 
   269 fun copy_files path1 path2 =
   269 fun copy_files path1 path2 =
   278 
   278 
   279 fun name_of_session elems = space_implode "/" ("Isabelle" :: elems);
   279 fun name_of_session elems = space_implode "/" ("Isabelle" :: elems);
   280 
   280 
   281 fun init build info doc doc_graph path name doc_prefix2 (remote_path, first_time) verbose =
   281 fun init build info doc doc_graph path name doc_prefix2 (remote_path, first_time) verbose =
   282   if not build andalso not info andalso doc = "" andalso is_none doc_prefix2 then
   282   if not build andalso not info andalso doc = "" andalso is_none doc_prefix2 then
   283     (browser_info := empty_browser_info; session_info := None)
   283     (browser_info := empty_browser_info; session_info := NONE)
   284   else
   284   else
   285     let
   285     let
   286       val parent_name = name_of_session (Library.take (length path - 1, path));
   286       val parent_name = name_of_session (Library.take (length path - 1, path));
   287       val session_name = name_of_session path;
   287       val session_name = name_of_session path;
   288       val sess_prefix = Path.make path;
   288       val sess_prefix = Path.make path;
   289       val html_prefix = Path.append (Path.expand output_path) sess_prefix;
   289       val html_prefix = Path.append (Path.expand output_path) sess_prefix;
   290 
   290 
   291       val (doc_prefix1, document_path) =
   291       val (doc_prefix1, document_path) =
   292         if doc = "" then (None, None)
   292         if doc = "" then (NONE, NONE)
   293         else if not (File.exists doc_path) then (conditional verbose (fn () =>
   293         else if not (File.exists doc_path) then (conditional verbose (fn () =>
   294           std_error "Warning: missing document directory\n"); (None, None))
   294           std_error "Warning: missing document directory\n"); (NONE, NONE))
   295         else (Some (Path.append html_prefix doc_path), Some (Path.ext doc doc_path));
   295         else (SOME (Path.append html_prefix doc_path), SOME (Path.ext doc doc_path));
   296 
   296 
   297       val parent_index_path = Path.append Path.parent index_path;
   297       val parent_index_path = Path.append Path.parent index_path;
   298       val index_up_lnk = if first_time then
   298       val index_up_lnk = if first_time then
   299           Url.append (the remote_path) (Url.File (Path.append sess_prefix parent_index_path))
   299           Url.append (the remote_path) (Url.File (Path.append sess_prefix parent_index_path))
   300         else Url.File parent_index_path;
   300         else Url.File parent_index_path;
   301       val readme =
   301       val readme =
   302         if File.exists readme_html_path then Some readme_html_path
   302         if File.exists readme_html_path then SOME readme_html_path
   303         else if File.exists readme_path then Some readme_path
   303         else if File.exists readme_path then SOME readme_path
   304         else None;
   304         else NONE;
   305 
   305 
   306       val index_text = HTML.begin_index (index_up_lnk, parent_name)
   306       val index_text = HTML.begin_index (index_up_lnk, parent_name)
   307         (Url.File index_path, session_name) (apsome Url.File readme)
   307         (Url.File index_path, session_name) (apsome Url.File readme)
   308           (apsome Url.File document_path) (Url.unpack "medium.html");
   308           (apsome Url.File document_path) (Url.unpack "medium.html");
   309     in
   309     in
   310       session_info := Some (make_session_info (name, parent_name, session_name, path, html_prefix,
   310       session_info := SOME (make_session_info (name, parent_name, session_name, path, html_prefix,
   311       info, doc, doc_graph, doc_prefix1, doc_prefix2, remote_path, verbose, readme));
   311       info, doc, doc_graph, doc_prefix1, doc_prefix2, remote_path, verbose, readme));
   312       browser_info := init_browser_info remote_path path;
   312       browser_info := init_browser_info remote_path path;
   313       add_html_index index_text
   313       add_html_index index_text
   314     end;
   314     end;
   315 
   315 
   362     fun finish_html (a, {html, ...}: theory_info) =
   362     fun finish_html (a, {html, ...}: theory_info) =
   363       Buffer.write (Path.append html_prefix (html_path a)) (Buffer.add HTML.end_theory html);
   363       Buffer.write (Path.append html_prefix (html_path a)) (Buffer.add HTML.end_theory html);
   364 
   364 
   365     val opt_graphs =
   365     val opt_graphs =
   366       if doc_graph andalso (is_some doc_prefix1 orelse is_some doc_prefix2) then
   366       if doc_graph andalso (is_some doc_prefix1 orelse is_some doc_prefix2) then
   367         Some (isatool_browser graph)
   367         SOME (isatool_browser graph)
   368       else None;
   368       else NONE;
   369 
   369 
   370     fun prepare_sources path =
   370     fun prepare_sources path =
   371      (copy_all doc_path path;
   371      (copy_all doc_path path;
   372       copy_files (Path.unpack "~~/lib/texinputs/*.sty") path;
   372       copy_files (Path.unpack "~~/lib/texinputs/*.sty") path;
   373       (case opt_graphs of None => () | Some (pdf, eps) =>
   373       (case opt_graphs of NONE => () | SOME (pdf, eps) =>
   374         (File.write (Path.append path graph_pdf_path) pdf;
   374         (File.write (Path.append path graph_pdf_path) pdf;
   375           File.write (Path.append path graph_eps_path) eps));
   375           File.write (Path.append path graph_eps_path) eps));
   376       write_tex_index tex_index path;
   376       write_tex_index tex_index path;
   377       seq (finish_tex path) thys);
   377       seq (finish_tex path) thys);
   378   in
   378   in
   380      (File.mkdir (Path.append html_prefix session_path);
   380      (File.mkdir (Path.append html_prefix session_path);
   381       Buffer.write (Path.append html_prefix pre_index_path) html_index;
   381       Buffer.write (Path.append html_prefix pre_index_path) html_index;
   382       File.write (Path.append html_prefix session_entries_path) "";
   382       File.write (Path.append html_prefix session_entries_path) "";
   383       create_index html_prefix;
   383       create_index html_prefix;
   384       if length path > 1 then update_index parent_html_prefix name else ();
   384       if length path > 1 then update_index parent_html_prefix name else ();
   385       (case readme of None => () | Some path => File.copy path (Path.append html_prefix path));
   385       (case readme of NONE => () | SOME path => File.copy path (Path.append html_prefix path));
   386       write_graph graph (Path.append html_prefix graph_path);
   386       write_graph graph (Path.append html_prefix graph_path);
   387       copy_files (Path.unpack "~~/lib/browser/GraphBrowser.jar") html_prefix;
   387       copy_files (Path.unpack "~~/lib/browser/GraphBrowser.jar") html_prefix;
   388       seq (fn (a, txt) => File.write (Path.append html_prefix (Path.basic a)) txt)
   388       seq (fn (a, txt) => File.write (Path.append html_prefix (Path.basic a)) txt)
   389         (HTML.applet_pages name (Url.File index_path, name));
   389         (HTML.applet_pages name (Url.File index_path, name));
   390       copy_files (Path.unpack "~~/lib/html/isabelle.css") html_prefix;
   390       copy_files (Path.unpack "~~/lib/html/isabelle.css") html_prefix;
   391       seq finish_html thys;
   391       seq finish_html thys;
   392       seq (uncurry File.write) files;
   392       seq (uncurry File.write) files;
   393       conditional verbose (fn () =>
   393       conditional verbose (fn () =>
   394         std_error ("Browser info at " ^ show_path html_prefix ^ "\n"))));
   394         std_error ("Browser info at " ^ show_path html_prefix ^ "\n"))));
   395 
   395 
   396     (case doc_prefix2 of None => () | Some path =>
   396     (case doc_prefix2 of NONE => () | SOME path =>
   397      (prepare_sources path;
   397      (prepare_sources path;
   398       conditional verbose (fn () => std_error ("Document sources at " ^ show_path path ^ "\n"))));
   398       conditional verbose (fn () => std_error ("Document sources at " ^ show_path path ^ "\n"))));
   399 
   399 
   400     (case doc_prefix1 of None => () | Some path =>
   400     (case doc_prefix1 of NONE => () | SOME path =>
   401      (prepare_sources path;
   401      (prepare_sources path;
   402       isatool_document true doc_format path;
   402       isatool_document true doc_format path;
   403       conditional verbose (fn () =>
   403       conditional verbose (fn () =>
   404         std_error ("Document at " ^ show_path (Path.ext doc_format path) ^ "\n"))));
   404         std_error ("Document at " ^ show_path (Path.ext doc_format path) ^ "\n"))));
   405 
   405 
   406     browser_info := empty_browser_info;
   406     browser_info := empty_browser_info;
   407     session_info := None
   407     session_info := NONE
   408   end);
   408   end);
   409 
   409 
   410 
   410 
   411 (* theory elements *)
   411 (* theory elements *)
   412 
   412 
   423   with_session () (fn _ => add_tex_source name (Latex.isabelle_file name s));
   423   with_session () (fn _ => add_tex_source name (Latex.isabelle_file name s));
   424 
   424 
   425 
   425 
   426 fun parent_link remote_path curr_session name =
   426 fun parent_link remote_path curr_session name =
   427   let val {name = _, session, is_local} = get_info (ThyInfo.theory name)
   427   let val {name = _, session, is_local} = get_info (ThyInfo.theory name)
   428   in (if null session then None else
   428   in (if null session then NONE else
   429      Some (if is_some remote_path andalso not is_local then
   429      SOME (if is_some remote_path andalso not is_local then
   430        Url.append (the remote_path) (Url.File
   430        Url.append (the remote_path) (Url.File
   431          (Path.append (Path.make session) (html_path name)))
   431          (Path.append (Path.make session) (html_path name)))
   432      else Url.File (Path.append (mk_rel_path curr_session session)
   432      else Url.File (Path.append (mk_rel_path curr_session session)
   433        (html_path name))), name)
   433        (html_path name))), name)
   434   end;
   434   end;
   436 fun begin_theory optpath name raw_parents orig_files thy =
   436 fun begin_theory optpath name raw_parents orig_files thy =
   437     with_session thy (fn {name = sess_name, session, path, html_prefix, remote_path, ...} =>
   437     with_session thy (fn {name = sess_name, session, path, html_prefix, remote_path, ...} =>
   438   let
   438   let
   439     val parents = map (parent_link remote_path path) raw_parents;
   439     val parents = map (parent_link remote_path path) raw_parents;
   440     val ml_path = ThyLoad.ml_path name;
   440     val ml_path = ThyLoad.ml_path name;
   441     val files = map (apsnd Some) orig_files @
   441     val files = map (apsnd SOME) orig_files @
   442       (if is_some (ThyLoad.check_file optpath ml_path) then [(ml_path, None)] else []);
   442       (if is_some (ThyLoad.check_file optpath ml_path) then [(ml_path, NONE)] else []);
   443 
   443 
   444     fun prep_file (raw_path, loadit) =
   444     fun prep_file (raw_path, loadit) =
   445       (case ThyLoad.check_file optpath raw_path of
   445       (case ThyLoad.check_file optpath raw_path of
   446         Some (path, _) =>
   446         SOME (path, _) =>
   447           let
   447           let
   448             val base = Path.base path;
   448             val base = Path.base path;
   449             val base_html = html_ext base;
   449             val base_html = html_ext base;
   450           in
   450           in
   451             add_file (Path.append html_prefix base_html,
   451             add_file (Path.append html_prefix base_html,
   452               HTML.ml_file (Url.File base) (File.read path));
   452               HTML.ml_file (Url.File base) (File.read path));
   453             (Some (Url.File base_html), Url.File raw_path, loadit)
   453             (SOME (Url.File base_html), Url.File raw_path, loadit)
   454           end
   454           end
   455       | None => (warning ("Browser info: expected to find ML file" ^ quote (Path.pack raw_path));
   455       | NONE => (warning ("Browser info: expected to find ML file" ^ quote (Path.pack raw_path));
   456           (None, Url.File raw_path, loadit)));
   456           (NONE, Url.File raw_path, loadit)));
   457 
   457 
   458     val files_html = map prep_file files;
   458     val files_html = map prep_file files;
   459 
   459 
   460     fun prep_html_source (tex_source, html_source, html) =
   460     fun prep_html_source (tex_source, html_source, html) =
   461       let
   461       let