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