src/Pure/Thy/present.ML
author wenzelm
Wed Jan 05 11:38:48 2000 +0100 (2000-01-05)
changeset 8088 6ae943d080de
parent 7853 a4acf1b4d5a8
child 8192 45a7027136e3
permissions -rw-r--r--
chapter;
wenzelm@6203
     1
(*  Title:      Pure/Thy/present.ML
wenzelm@6203
     2
    ID:         $Id$
wenzelm@7727
     3
    Author:     Stefan Berghofer and Markus Wenzel, TU Muenchen
wenzelm@6203
     4
wenzelm@7727
     5
Theory presentation (HTML, graph files, simple LaTeX documents).
wenzelm@6203
     6
*)
wenzelm@6203
     7
wenzelm@6203
     8
signature BASIC_PRESENT =
wenzelm@6203
     9
sig
wenzelm@7727
    10
  val section: string -> unit
wenzelm@6203
    11
end;
wenzelm@6203
    12
wenzelm@6203
    13
signature PRESENT =
wenzelm@6203
    14
sig
wenzelm@7727
    15
  include BASIC_PRESENT
wenzelm@7727
    16
  val init: bool -> string -> string list -> string -> Url.T option * bool -> unit
wenzelm@7727
    17
  val finish: unit -> unit
wenzelm@7727
    18
  val init_theory: string -> unit
wenzelm@7727
    19
  val verbatim_source: string -> (unit -> string list) -> unit
wenzelm@7757
    20
  type token
wenzelm@7757
    21
  val basic_token: OuterLex.token -> token
wenzelm@7757
    22
  val markup_token: string * string -> token
wenzelm@7789
    23
  val verbatim_token: string -> token
wenzelm@7757
    24
  val token_source: string -> (unit -> token list) -> unit
wenzelm@7727
    25
  val begin_theory: string -> string list -> (Path.T * bool) list -> theory -> theory
wenzelm@7727
    26
  val result: string -> string -> thm -> unit
wenzelm@7727
    27
  val results: string -> string -> thm list -> unit
wenzelm@7727
    28
  val theorem: string -> thm -> unit
wenzelm@7727
    29
  val theorems: string -> thm list -> unit
wenzelm@8088
    30
  val chapter: string -> unit
wenzelm@7727
    31
  val subsection: string -> unit
wenzelm@7727
    32
  val subsubsection: string -> unit
wenzelm@7727
    33
  val setup: (theory -> theory) list
berghofe@7763
    34
  val get_info: theory -> {session: string list, is_local: bool}
wenzelm@6203
    35
end;
wenzelm@6203
    36
wenzelm@7685
    37
structure Present: PRESENT =
wenzelm@7685
    38
struct
wenzelm@7685
    39
wenzelm@7727
    40
wenzelm@7727
    41
(** paths **)
wenzelm@7727
    42
wenzelm@7727
    43
val output_path = Path.variable "ISABELLE_BROWSER_INFO";
wenzelm@7727
    44
wenzelm@7727
    45
fun top_path sess_path offset =
wenzelm@7727
    46
  Path.append (Path.appends (replicate (offset + length sess_path) Path.parent));
wenzelm@7727
    47
wenzelm@7727
    48
val tex_ext = Path.ext "tex";
wenzelm@7727
    49
val tex_path = tex_ext o Path.basic;
wenzelm@7727
    50
val html_ext = Path.ext "html";
wenzelm@7727
    51
val html_path = html_ext o Path.basic;
wenzelm@7727
    52
val graph_path = Path.ext "graph" o Path.basic;
wenzelm@7727
    53
val index_path = Path.basic "index.html";
wenzelm@7727
    54
val doc_path = Path.basic "document";
wenzelm@7727
    55
val doc_index_path = tex_path "session";
wenzelm@7727
    56
wenzelm@7727
    57
val session_path = Path.basic ".session";
wenzelm@7727
    58
val session_entries_path = Path.unpack ".session/entries";
wenzelm@7727
    59
val pre_index_path = Path.unpack ".session/pre-index";
wenzelm@7727
    60
wenzelm@7727
    61
wenzelm@7727
    62
wenzelm@7727
    63
(** additional theory data **)
wenzelm@7727
    64
wenzelm@7727
    65
structure BrowserInfoArgs =
wenzelm@7727
    66
struct
wenzelm@7727
    67
  val name = "Pure/browser_info";
wenzelm@7727
    68
  type T = {session: string list, is_local: bool};
wenzelm@7727
    69
wenzelm@7727
    70
  val empty = {session = [], is_local = false};
wenzelm@7727
    71
  val copy = I;
berghofe@7763
    72
  fun prep_ext _ = empty;
wenzelm@7727
    73
  fun merge _ = empty;
wenzelm@7727
    74
  fun print _ _ = ();
wenzelm@7727
    75
end;
wenzelm@7727
    76
wenzelm@7727
    77
structure BrowserInfoData = TheoryDataFun(BrowserInfoArgs);
wenzelm@7727
    78
wenzelm@7727
    79
fun get_info thy =
wenzelm@7727
    80
  if Theory.eq_thy (thy, ProtoPure.thy) then BrowserInfoArgs.empty
wenzelm@7727
    81
  else BrowserInfoData.get thy;
wenzelm@7727
    82
wenzelm@7727
    83
wenzelm@7727
    84
wenzelm@7727
    85
(** graphs **)
wenzelm@7727
    86
wenzelm@7727
    87
type graph_node =
wenzelm@7727
    88
  {name: string, ID: string, dir: string, unfold: bool,
wenzelm@7727
    89
   path: string, parents: string list};
wenzelm@7727
    90
wenzelm@7727
    91
fun get_graph path = map (fn (a :: b :: c :: d :: e :: r) =>
wenzelm@7727
    92
  {name = unenclose a, ID = unenclose b,
wenzelm@7727
    93
   dir = unenclose c, unfold = (d = "+"),
wenzelm@7727
    94
   path = unenclose (if d = "+" then e else d),
wenzelm@7727
    95
   parents = map unenclose (fst (split_last (if d = "+" then tl r else r)))})
wenzelm@7727
    96
     (map (filter_out (equal "") o space_explode " ") (split_lines (File.read path)));
wenzelm@7727
    97
wenzelm@7727
    98
fun put_graph gr path =
wenzelm@7727
    99
    File.write path (cat_lines (map (fn {name, ID, dir, unfold, path, parents} =>
wenzelm@7727
   100
      "\"" ^ name ^ "\" \"" ^ ID ^ "\" \"" ^ dir ^ (if unfold then "\" + \"" else "\" \"") ^
wenzelm@7727
   101
      path ^ "\" > " ^ space_implode " " (map quote parents) ^ " ;") gr));
wenzelm@7727
   102
wenzelm@7727
   103
fun dir_of sess = space_implode "/" (case sess of
wenzelm@7727
   104
  [] => ["Pure"] | [x] => [x, "base"] | xs => xs);
wenzelm@7727
   105
wenzelm@7727
   106
fun ID_of sess s = dir_of sess ^ "/" ^ s;
wenzelm@7727
   107
wenzelm@7727
   108
wenzelm@7727
   109
(* retrieve graph data from theory loader database *)
wenzelm@7727
   110
wenzelm@7727
   111
fun make_graph remote_path unfold curr_sess = map (fn name =>
wenzelm@7727
   112
  let
wenzelm@7727
   113
    val {session, is_local} = get_info (ThyInfo.theory name);
wenzelm@7727
   114
    val path' = Path.append (Path.make session) (html_path name)
wenzelm@7727
   115
  in
wenzelm@7727
   116
    {name = name, ID = ID_of session name, dir = dir_of session,
wenzelm@7727
   117
     path = if null session then "" else
wenzelm@7727
   118
            if is_some remote_path andalso not is_local then
wenzelm@7727
   119
              Url.pack (Url.append (the remote_path) (Url.file path'))
wenzelm@7727
   120
            else Path.pack (top_path curr_sess 2 path'),
wenzelm@7727
   121
     unfold = unfold andalso (length session = 1),
wenzelm@7727
   122
     parents = map (fn s => ID_of (#session (get_info (ThyInfo.theory s))) s)
wenzelm@7727
   123
       (ThyInfo.get_preds name)}
wenzelm@7727
   124
  end) (ThyInfo.names ());
wenzelm@7727
   125
wenzelm@7727
   126
fun add_graph_entry' (entry as {ID, ...}) (gr : graph_node list) =
wenzelm@7727
   127
  filter_out (fn entry' => #ID entry' = ID) gr @ [entry];
wenzelm@7727
   128
wenzelm@7727
   129
wenzelm@7727
   130
wenzelm@7727
   131
(** global browser info state **)
wenzelm@7727
   132
wenzelm@7727
   133
(* type theory_info *)
wenzelm@7727
   134
wenzelm@7727
   135
type theory_info = {tex_source: Buffer.T, html_source: Buffer.T, html: Buffer.T};
wenzelm@7727
   136
wenzelm@7727
   137
fun make_theory_info (tex_source, html_source, html) =
wenzelm@7727
   138
  {tex_source = tex_source, html_source = html_source, html = html}: theory_info;
wenzelm@7727
   139
wenzelm@7727
   140
val empty_theory_info = make_theory_info (Buffer.empty, Buffer.empty, Buffer.empty);
wenzelm@7727
   141
wenzelm@7727
   142
fun map_theory_info f {tex_source, html_source, html} =
wenzelm@7727
   143
  make_theory_info (f (tex_source, html_source, html));
wenzelm@7727
   144
wenzelm@7727
   145
wenzelm@7727
   146
(* type browser_info *)
wenzelm@7727
   147
wenzelm@7727
   148
type browser_info = {theories: theory_info Symtab.table, tex_index: Buffer.T,
wenzelm@7727
   149
  html_index: Buffer.T, graph: graph_node list, all_graph: graph_node list};
wenzelm@7727
   150
wenzelm@7727
   151
fun make_browser_info (theories, tex_index, html_index, graph, all_graph) =
wenzelm@7727
   152
  {theories = theories, tex_index = tex_index, html_index = html_index,
wenzelm@7727
   153
    graph = graph, all_graph = all_graph}: browser_info;
wenzelm@7727
   154
wenzelm@7727
   155
val empty_browser_info = make_browser_info (Symtab.empty, Buffer.empty, Buffer.empty, [], []);
wenzelm@7727
   156
wenzelm@7727
   157
fun initial_browser_info remote_path graph_path curr_sess = make_browser_info
wenzelm@7727
   158
  (Symtab.empty, Buffer.empty, Buffer.empty, make_graph remote_path false curr_sess,
wenzelm@7727
   159
   if_none (try get_graph graph_path) (make_graph remote_path true [""]));
wenzelm@7727
   160
wenzelm@7727
   161
fun map_browser_info f {theories, tex_index, html_index, graph, all_graph} =
wenzelm@7727
   162
  make_browser_info (f (theories, tex_index, html_index, graph, all_graph));
wenzelm@7727
   163
wenzelm@7727
   164
wenzelm@7727
   165
(* state *)
wenzelm@7727
   166
wenzelm@7727
   167
val browser_info = ref empty_browser_info;
wenzelm@7727
   168
wenzelm@7727
   169
fun change_browser_info f = browser_info := map_browser_info f (! browser_info);
wenzelm@7727
   170
wenzelm@7727
   171
fun init_theory_info name info =
wenzelm@7727
   172
  change_browser_info (fn (theories, tex_index, html_index, graph, all_graph) =>
wenzelm@7727
   173
    (Symtab.update ((name, info), theories), tex_index, html_index, graph, all_graph));
wenzelm@7727
   174
wenzelm@7727
   175
fun change_theory_info name f =
wenzelm@7727
   176
  change_browser_info (fn (info as (theories, tex_index, html_index, graph, all_graph)) =>
wenzelm@7727
   177
    (case Symtab.lookup (theories, name) of
wenzelm@7727
   178
      None => (warning ("Browser info: cannot access theory document " ^ quote name); info)
wenzelm@7727
   179
    | Some info => (Symtab.update ((name, map_theory_info f info), theories),
wenzelm@7727
   180
        tex_index, html_index, graph, all_graph)));
wenzelm@7727
   181
wenzelm@7727
   182
wenzelm@7727
   183
fun add_tex_index txt =
wenzelm@7727
   184
  change_browser_info (fn (theories, tex_index, html_index, graph, all_graph) =>
wenzelm@7727
   185
    (theories, Buffer.add txt tex_index, html_index, graph, all_graph));
wenzelm@7727
   186
wenzelm@7727
   187
fun add_html_index txt =
wenzelm@7727
   188
  change_browser_info (fn (theories, tex_index, html_index, graph, all_graph) =>
wenzelm@7727
   189
    (theories, tex_index, Buffer.add txt html_index, graph, all_graph));
wenzelm@7727
   190
wenzelm@7727
   191
(*add entry to graph for current session*)
wenzelm@7727
   192
fun add_graph_entry e =
wenzelm@7727
   193
  change_browser_info (fn (theories, tex_index, html_index, graph, all_graph) =>
wenzelm@7727
   194
    (theories, tex_index, html_index, add_graph_entry' e graph, all_graph));
wenzelm@7727
   195
wenzelm@7727
   196
(*add entry to graph for all sessions*)
wenzelm@7727
   197
fun add_all_graph_entry e =
wenzelm@7727
   198
  change_browser_info (fn (theories, tex_index, html_index, graph, all_graph) =>
wenzelm@7727
   199
    (theories, tex_index, html_index, graph, add_graph_entry' e all_graph));
wenzelm@7727
   200
wenzelm@7727
   201
fun add_tex_source name txt = change_theory_info name (fn (tex_source, html_source, html) =>
wenzelm@7727
   202
  (Buffer.add txt tex_source, html_source, html));
wenzelm@7727
   203
wenzelm@7727
   204
fun add_html_source name txt = change_theory_info name (fn (tex_source, html_source, html) =>
wenzelm@7727
   205
  (tex_source, Buffer.add txt html_source, html));
wenzelm@7727
   206
wenzelm@7727
   207
fun add_html name txt = change_theory_info name (fn (tex_source, html_source, html) =>
wenzelm@7727
   208
  (tex_source, html_source, Buffer.add txt html));
wenzelm@7727
   209
wenzelm@7727
   210
wenzelm@7727
   211
wenzelm@7727
   212
(** global session state **)
wenzelm@7727
   213
wenzelm@7727
   214
(* session_info *)
wenzelm@7727
   215
wenzelm@7727
   216
type session_info =
wenzelm@7727
   217
  {name: string, parent: string, session: string, path: string list, html_prefix: Path.T,
wenzelm@7802
   218
    doc_format: string, doc_prefix: Path.T option, graph_path: Path.T, all_graph_path: Path.T,
wenzelm@7727
   219
    remote_path: Url.T option};
wenzelm@7727
   220
wenzelm@7802
   221
fun make_session_info (name, parent, session, path, html_prefix, doc_format, doc_prefix,
wenzelm@7727
   222
    graph_path, all_graph_path, remote_path) =
wenzelm@7802
   223
  {name = name, parent = parent, session = session, path = path, html_prefix = html_prefix,
wenzelm@7802
   224
    doc_format = doc_format, doc_prefix = doc_prefix, graph_path = graph_path,
wenzelm@7727
   225
    all_graph_path = all_graph_path, remote_path = remote_path}: session_info;
wenzelm@7685
   226
wenzelm@7685
   227
wenzelm@7727
   228
(* state *)
wenzelm@7727
   229
wenzelm@7727
   230
val session_info = ref (None: session_info option);
wenzelm@7727
   231
wenzelm@7727
   232
fun with_session x f = (case ! session_info of None => x | Some info => f info);
wenzelm@7727
   233
fun with_context f = f (PureThy.get_name (Context.the_context ()));
wenzelm@7727
   234
wenzelm@7727
   235
wenzelm@7727
   236
wenzelm@7727
   237
(** HTML output **)
wenzelm@7727
   238
wenzelm@7727
   239
(* maintain index *)
wenzelm@7727
   240
wenzelm@7727
   241
val session_entries =
wenzelm@7727
   242
  HTML.session_entries o
wenzelm@7727
   243
    map (fn name => (Url.file (Path.append (Path.basic name) index_path), name));
wenzelm@7727
   244
wenzelm@7727
   245
fun get_entries dir =
wenzelm@7727
   246
  split_lines (File.read (Path.append dir session_entries_path));
wenzelm@7727
   247
wenzelm@7727
   248
fun put_entries entries dir =
wenzelm@7727
   249
  File.write (Path.append dir session_entries_path) (cat_lines entries);
wenzelm@7727
   250
wenzelm@7727
   251
wenzelm@7727
   252
fun create_index dir =
wenzelm@7727
   253
  File.read (Path.append dir pre_index_path) ^
wenzelm@7727
   254
    session_entries (get_entries dir) ^ HTML.end_index
wenzelm@7727
   255
  |> File.write (Path.append dir index_path);
wenzelm@7727
   256
wenzelm@7727
   257
fun update_index dir name =
wenzelm@7727
   258
  (case try get_entries dir of
wenzelm@7727
   259
    None => warning ("Browser info: cannot access session index of " ^ quote (Path.pack dir))
wenzelm@7727
   260
  | Some es => (put_entries ((es \ name) @ [name]) dir; create_index dir));
wenzelm@7727
   261
wenzelm@7727
   262
wenzelm@7727
   263
(* init session *)
wenzelm@7727
   264
wenzelm@7727
   265
fun name_of_session elems = space_implode "/" ("Isabelle" :: elems);
wenzelm@7727
   266
wenzelm@7727
   267
fun could_copy inpath outpath =
wenzelm@7727
   268
  if File.exists inpath then (File.copy inpath outpath; true)
wenzelm@7727
   269
  else false;
wenzelm@7727
   270
wenzelm@7727
   271
fun init false _ _ _ _ = (browser_info := empty_browser_info; session_info := None)
wenzelm@7727
   272
  | init true doc path name (remote_path, first_time) =
wenzelm@7727
   273
      let
wenzelm@7727
   274
        val parent_name = name_of_session (take (length path - 1, path));
wenzelm@7727
   275
        val session_name = name_of_session path;
wenzelm@7727
   276
        val sess_prefix = Path.make path;
wenzelm@7727
   277
wenzelm@7727
   278
        val out_path = Path.expand output_path;
wenzelm@7727
   279
        val html_prefix = Path.append out_path sess_prefix;
wenzelm@7727
   280
wenzelm@7727
   281
        val (doc_prefix, document_path) =
wenzelm@7727
   282
          if doc = "" then (None, None)
wenzelm@7727
   283
          else (Some (Path.append html_prefix doc_path), Some (Path.ext doc doc_path));
wenzelm@7727
   284
wenzelm@7727
   285
        val graph_prefix = Path.appends [out_path, Path.unpack "graph/data", sess_prefix];
wenzelm@7727
   286
        val graph_path = Path.append graph_prefix (Path.basic "session.graph");
wenzelm@7727
   287
        val graph_lnk = Url.file (top_path path 0 (Path.appends
wenzelm@7727
   288
          [Path.unpack "graph/data", sess_prefix, Path.basic "medium.html"]));
wenzelm@7727
   289
        val graph_up_lnk =
wenzelm@7727
   290
          (Url.file (top_path path 2 (Path.append sess_prefix index_path)), session_name);
wenzelm@7727
   291
        val codebase = Url.file (top_path path 1 Path.current);
wenzelm@7727
   292
        val all_graph_path = Path.appends [out_path, Path.unpack "graph/data",
wenzelm@7727
   293
          Path.basic (hd path), Path.basic "all_sessions.graph"];
wenzelm@7727
   294
wenzelm@7727
   295
        val _ = (File.mkdir html_prefix; File.mkdir graph_prefix);
wenzelm@7727
   296
wenzelm@7727
   297
        fun prep_readme readme =
wenzelm@7727
   298
          let val readme_path = Path.basic readme in
wenzelm@7727
   299
            if could_copy readme_path (Path.append html_prefix readme_path) then Some readme_path
wenzelm@7727
   300
            else None
wenzelm@7727
   301
          end;
wenzelm@7727
   302
        val opt_readme =
wenzelm@7727
   303
          (case prep_readme "README.html" of None => prep_readme "README" | some => some);
wenzelm@7727
   304
wenzelm@7727
   305
        val parent_index_path = Path.append Path.parent index_path;
wenzelm@7727
   306
        val index_up_lnk = if first_time then
wenzelm@7727
   307
            Url.append (the remote_path) (Url.file (Path.append sess_prefix parent_index_path))
wenzelm@7727
   308
          else Url.file parent_index_path;
wenzelm@7727
   309
wenzelm@7727
   310
        val index_text = HTML.begin_index (index_up_lnk, parent_name)
wenzelm@7727
   311
          (Url.file index_path, session_name) (apsome Url.file opt_readme)
wenzelm@7727
   312
            (apsome Url.file document_path) graph_lnk;
wenzelm@7727
   313
      in
wenzelm@7727
   314
        File.mkdir (Path.append html_prefix session_path);
wenzelm@7727
   315
        File.write (Path.append html_prefix session_entries_path) "";
wenzelm@7727
   316
        if is_some doc_prefix then File.copy_all doc_path html_prefix else ();
wenzelm@7727
   317
        seq (fn (name, txt) => File.write (Path.append graph_prefix (Path.basic name)) txt)
wenzelm@7727
   318
          (HTML.applet_pages session_name codebase graph_up_lnk (length path = 1));
wenzelm@7727
   319
        session_info := Some (make_session_info (name, parent_name, session_name, path,
wenzelm@7802
   320
          html_prefix, doc, doc_prefix, graph_path, all_graph_path, remote_path));
wenzelm@7727
   321
        browser_info := initial_browser_info remote_path all_graph_path path;
wenzelm@7727
   322
        add_html_index index_text
wenzelm@7727
   323
      end;
wenzelm@7727
   324
wenzelm@7727
   325
wenzelm@7727
   326
(* finish session *)
wenzelm@7685
   327
wenzelm@7802
   328
fun isatool_document doc_format doc_prefix =
wenzelm@7802
   329
  let val cmd = "$ISATOOL document -o '" ^ doc_format ^ "' '" ^ File.sysify_path doc_prefix ^ "'"
wenzelm@7853
   330
  in writeln cmd; if system cmd <> 0 then error "Failed to build document" else () end;
wenzelm@7802
   331
wenzelm@7727
   332
fun finish () = with_session ()
wenzelm@7802
   333
    (fn {name, html_prefix, doc_format, doc_prefix, graph_path, all_graph_path, path, ...} =>
wenzelm@7727
   334
  let
wenzelm@7727
   335
    val {theories, tex_index, html_index, graph, all_graph} = ! browser_info;
wenzelm@7727
   336
wenzelm@7727
   337
    fun finish_node (a, {tex_source, html_source = _, html}) =
wenzelm@7802
   338
     (doc_prefix |> apsome (fn p => Buffer.write (Path.append p (tex_path a)) tex_source);
wenzelm@7727
   339
      Buffer.write (Path.append html_prefix (html_path a)) (Buffer.add HTML.end_theory html));
wenzelm@7727
   340
  in
wenzelm@7727
   341
    seq finish_node (Symtab.dest theories);
wenzelm@7727
   342
    Buffer.write (Path.append html_prefix pre_index_path) html_index;
wenzelm@7802
   343
    doc_prefix |> apsome (fn p =>
wenzelm@7802
   344
     (Buffer.write (Path.append p doc_index_path) tex_index; isatool_document doc_format p));
wenzelm@7727
   345
    put_graph graph graph_path;
wenzelm@7727
   346
    put_graph all_graph all_graph_path;
wenzelm@7727
   347
    create_index html_prefix;
wenzelm@7727
   348
    update_index (Path.append html_prefix Path.parent) name;
wenzelm@7727
   349
    browser_info := empty_browser_info;
wenzelm@7727
   350
    session_info := None
wenzelm@7727
   351
  end);
wenzelm@7727
   352
wenzelm@7727
   353
wenzelm@7727
   354
(* theory elements *)
wenzelm@7727
   355
wenzelm@7727
   356
fun init_theory name = with_session () (fn _ => init_theory_info name empty_theory_info);
wenzelm@7727
   357
wenzelm@7727
   358
fun verbatim_source name mk_text =
wenzelm@7727
   359
  with_session () (fn _ => add_html_source name (HTML.verbatim_source (mk_text ())));
wenzelm@7727
   360
wenzelm@7757
   361
wenzelm@7757
   362
type token = Latex.token;
wenzelm@7757
   363
val basic_token = Latex.Basic;
wenzelm@7757
   364
val markup_token = Latex.Markup;
wenzelm@7789
   365
val verbatim_token = Latex.Verbatim;
wenzelm@7757
   366
wenzelm@7727
   367
fun token_source name mk_toks =
wenzelm@7727
   368
  with_session () (fn _ => add_tex_source name (Latex.token_source (mk_toks ())));
wenzelm@7727
   369
wenzelm@7727
   370
wenzelm@7727
   371
fun parent_link remote_path curr_session name =
wenzelm@7727
   372
  let
wenzelm@7727
   373
    val {session, is_local} = get_info (ThyInfo.theory name);
wenzelm@7727
   374
    val path = Path.append (Path.make session) (html_path name)
wenzelm@7727
   375
  in (if null session then None else
wenzelm@7727
   376
     Some (if is_some remote_path andalso not is_local then
wenzelm@7727
   377
       Url.append (the remote_path) (Url.file path)
wenzelm@7727
   378
     else Url.file (top_path curr_session 0 path)), name)
wenzelm@7727
   379
  end;
wenzelm@7727
   380
wenzelm@7727
   381
fun begin_theory name raw_parents orig_files thy =
wenzelm@7727
   382
  with_session thy (fn {session, path, html_prefix, remote_path, ...} =>
wenzelm@7727
   383
  let
wenzelm@7727
   384
    val parents = map (parent_link remote_path path) raw_parents;
wenzelm@7727
   385
    val ml_path = ThyLoad.ml_path name;
wenzelm@7727
   386
    val files = map (apsnd Some) orig_files @
wenzelm@7727
   387
      (if is_some (ThyLoad.check_file ml_path) then [(ml_path, None)] else []);
wenzelm@7727
   388
wenzelm@7727
   389
    fun prep_file (raw_path, loadit) =
wenzelm@7727
   390
      (case ThyLoad.check_file raw_path of
wenzelm@7727
   391
        Some (path, _) =>
wenzelm@7727
   392
          let
wenzelm@7727
   393
            val base = Path.base path;
wenzelm@7727
   394
            val base_html = html_ext base;
wenzelm@7727
   395
          in
wenzelm@7727
   396
            File.write (Path.append html_prefix base_html)
wenzelm@7727
   397
              (HTML.ml_file (Url.file base) (File.read path));
wenzelm@7727
   398
            (Some (Url.file base_html), Url.file raw_path, loadit)
wenzelm@7727
   399
          end
wenzelm@7727
   400
      | None => (warning ("Browser info: expected to find ML file" ^ quote (Path.pack raw_path));
wenzelm@7727
   401
          (None, Url.file raw_path, loadit)));
wenzelm@7727
   402
wenzelm@7727
   403
    val files_html = map prep_file files;
wenzelm@7727
   404
wenzelm@7727
   405
    fun prep_html_source (tex_source, html_source, html) =
wenzelm@7727
   406
      let
wenzelm@7727
   407
        val txt = HTML.begin_theory (Url.file index_path, session)
wenzelm@7727
   408
          name parents files_html (Buffer.content html_source)
wenzelm@7727
   409
      in (tex_source, Buffer.empty, Buffer.add txt html) end;
wenzelm@7727
   410
wenzelm@7727
   411
    fun make_entry unfold all =
wenzelm@7727
   412
      {name = name, ID = ID_of path name, dir = dir_of path, unfold = unfold,
wenzelm@7727
   413
       path = Path.pack (top_path (if all then [""] else path) 2
wenzelm@7727
   414
         (Path.append (Path.make path) (html_path name))),
wenzelm@7727
   415
       parents = map (fn s => ID_of (#session (get_info (ThyInfo.theory s))) s) raw_parents};
wenzelm@7727
   416
wenzelm@7727
   417
  in
wenzelm@7727
   418
    change_theory_info name prep_html_source;
wenzelm@7727
   419
    add_all_graph_entry (make_entry (length path = 1) true);
wenzelm@7727
   420
    add_graph_entry (make_entry true false);
wenzelm@7727
   421
    add_html_index (HTML.theory_entry (Url.file (html_path name), name));
wenzelm@7727
   422
    add_tex_index (Latex.theory_entry name);
wenzelm@7727
   423
    BrowserInfoData.put {session = path, is_local = is_some remote_path} thy
wenzelm@7727
   424
  end);
wenzelm@7727
   425
wenzelm@7727
   426
wenzelm@7727
   427
fun result k a thm = with_session () (fn _ => with_context add_html (HTML.result k a thm));
wenzelm@7727
   428
fun results k a thms = with_session () (fn _ => with_context add_html (HTML.results k a thms));
wenzelm@7727
   429
fun theorem a thm = with_session () (fn _ => with_context add_html (HTML.theorem a thm));
wenzelm@7727
   430
fun theorems a thms = with_session () (fn _ => with_context add_html (HTML.theorems a thms));
wenzelm@8088
   431
fun chapter s = with_session () (fn _ => with_context add_html (HTML.chapter s));
wenzelm@7727
   432
fun section s = with_session () (fn _ => with_context add_html (HTML.section s));
wenzelm@7727
   433
fun subsection s = with_session () (fn _ => with_context add_html (HTML.subsection s));
wenzelm@7727
   434
fun subsubsection s = with_session () (fn _ => with_context add_html (HTML.subsubsection s));
wenzelm@7727
   435
wenzelm@7727
   436
wenzelm@7727
   437
wenzelm@7727
   438
(** theory setup **)
wenzelm@7727
   439
wenzelm@7727
   440
val setup = [BrowserInfoData.init];
wenzelm@7685
   441
wenzelm@7685
   442
wenzelm@7685
   443
end;
wenzelm@7685
   444
wenzelm@6203
   445
wenzelm@6203
   446
structure BasicPresent: BASIC_PRESENT = Present;
wenzelm@6203
   447
open BasicPresent;