src/Pure/Thy/present.ML
author wenzelm
Mon, 23 Jun 2008 16:01:03 +0200
changeset 27327 efd626efcb04
parent 26957 e3f04fdd994d
child 27329 91c0c894e1b4
permissions -rw-r--r--
info: default name is "", not "Pure";
wenzelm@6203
     1
(*  Title:      Pure/Thy/present.ML
wenzelm@6203
     2
    ID:         $Id$
wenzelm@9416
     3
    Author:     Markus Wenzel and Stefan Berghofer, TU Muenchen
wenzelm@6203
     4
wenzelm@9416
     5
Theory presentation: HTML, graph files, (PDF)LaTeX documents.
wenzelm@6203
     6
*)
wenzelm@6203
     7
wenzelm@6203
     8
signature BASIC_PRESENT =
wenzelm@6203
     9
sig
wenzelm@11057
    10
  val no_document: ('a -> 'b) -> 'a -> 'b
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@24561
    17
  val session_name: theory -> string
wenzelm@9452
    18
  val write_graph: {name: string, ID: string, dir: string, unfold: bool,
wenzelm@9452
    19
   path: string, parents: string list} list -> Path.T -> unit
wenzelm@20577
    20
  val display_graph: {name: string, ID: string, dir: string, unfold: bool,
wenzelm@20577
    21
   path: string, parents: string list} list -> unit
wenzelm@17082
    22
  val init: bool -> bool -> string -> bool -> string list -> string list ->
wenzelm@23899
    23
    string -> (bool * Path.T) option -> Url.T option * bool -> bool -> theory list -> unit
wenzelm@7727
    24
  val finish: unit -> unit
wenzelm@7727
    25
  val init_theory: string -> unit
wenzelm@8192
    26
  val verbatim_source: string -> (unit -> Symbol.symbol list) -> unit
wenzelm@9136
    27
  val theory_output: string -> string -> unit
wenzelm@24150
    28
  val begin_theory: int -> Path.T -> (Path.T * bool) list -> theory -> theory
wenzelm@13532
    29
  val add_hook: (string -> (string * thm list) list -> unit) -> unit
wenzelm@13532
    30
  val results: string -> (string * thm list) 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@14922
    36
  val drafts: string -> Path.T list -> Path.T
wenzelm@6203
    37
end;
wenzelm@6203
    38
wenzelm@7685
    39
structure Present: PRESENT =
wenzelm@7685
    40
struct
wenzelm@7685
    41
wenzelm@7685
    42
wenzelm@7727
    43
(** paths **)
wenzelm@7685
    44
wenzelm@7727
    45
val output_path = Path.variable "ISABELLE_BROWSER_INFO";
wenzelm@7685
    46
wenzelm@7727
    47
val tex_ext = Path.ext "tex";
wenzelm@7727
    48
val tex_path = tex_ext o Path.basic;
wenzelm@7727
    49
val html_ext = Path.ext "html";
wenzelm@7727
    50
val html_path = html_ext o Path.basic;
wenzelm@7727
    51
val index_path = Path.basic "index.html";
wenzelm@11856
    52
val readme_html_path = Path.basic "README.html";
wenzelm@11856
    53
val readme_path = Path.basic "README";
wenzelm@17082
    54
val documentN = "document";
wenzelm@17082
    55
val document_path = Path.basic documentN;
wenzelm@8196
    56
val doc_indexN = "session";
wenzelm@11856
    57
val graph_path = Path.basic "session.graph";
wenzelm@11856
    58
val graph_pdf_path = Path.basic "session_graph.pdf";
wenzelm@11856
    59
val graph_eps_path = Path.basic "session_graph.eps";
wenzelm@7727
    60
wenzelm@7727
    61
val session_path = Path.basic ".session";
wenzelm@21858
    62
val session_entries_path = Path.explode ".session/entries";
wenzelm@21858
    63
val pre_index_path = Path.explode ".session/pre-index";
wenzelm@7727
    64
berghofe@9044
    65
fun mk_rel_path [] ys = Path.make ys
berghofe@9044
    66
  | mk_rel_path xs [] = Path.appends (replicate (length xs) Path.parent)
wenzelm@9416
    67
  | mk_rel_path (ps as x :: xs) (qs as y :: ys) = if x = y then mk_rel_path xs ys else
berghofe@9044
    68
      Path.appends (replicate (length ps) Path.parent @ [Path.make qs]);
wenzelm@7727
    69
wenzelm@21858
    70
fun show_path path = Path.implode (Path.append (File.pwd ()) path);
wenzelm@11856
    71
wenzelm@7727
    72
wenzelm@14922
    73
wenzelm@7727
    74
(** additional theory data **)
wenzelm@7727
    75
wenzelm@16426
    76
structure BrowserInfoData = TheoryDataFun
wenzelm@22846
    77
(
wenzelm@9416
    78
  type T = {name: string, session: string list, is_local: bool};
wenzelm@26957
    79
  val empty = {name = Context.PureN, session = [], is_local = false}: T;
wenzelm@16426
    80
  val copy = I;
wenzelm@16503
    81
  fun extend _ = empty;
wenzelm@16426
    82
  fun merge _ _ = empty;
wenzelm@22846
    83
);
wenzelm@7727
    84
wenzelm@27327
    85
val put_info = BrowserInfoData.put;
wenzelm@26957
    86
val get_info = BrowserInfoData.get;
wenzelm@24561
    87
val session_name = #name o get_info;
wenzelm@24561
    88
wenzelm@27327
    89
val _ = Context.>> (Context.map_theory (put_info {name = "", session = [], is_local = false}));
wenzelm@27327
    90
wenzelm@7727
    91
wenzelm@7727
    92
wenzelm@7727
    93
(** graphs **)
wenzelm@7727
    94
wenzelm@7727
    95
type graph_node =
wenzelm@7727
    96
  {name: string, ID: string, dir: string, unfold: bool,
wenzelm@7727
    97
   path: string, parents: string list};
wenzelm@7727
    98
wenzelm@9416
    99
fun write_graph gr path =
wenzelm@9416
   100
  File.write path (cat_lines (map (fn {name, ID, dir, unfold, path, parents} =>
wenzelm@9416
   101
    "\"" ^ name ^ "\" \"" ^ ID ^ "\" \"" ^ dir ^ (if unfold then "\" + \"" else "\" \"") ^
berghofe@14598
   102
    path ^ "\" > " ^ space_implode " " (map Library.quote parents) ^ " ;") gr));
wenzelm@7727
   103
wenzelm@20577
   104
fun display_graph gr =
wenzelm@20577
   105
  let
wenzelm@24561
   106
    val _ = writeln "Displaying graph ...";
wenzelm@21858
   107
    val path = File.tmp_path (Path.explode "tmp.graph");
wenzelm@20577
   108
    val _ = write_graph gr path;
wenzelm@20577
   109
    val _ = File.isatool ("browser -c " ^ File.shell_path path ^ " &");
wenzelm@20577
   110
  in () end;
wenzelm@20577
   111
wenzelm@20577
   112
wenzelm@9416
   113
fun ID_of sess s = space_implode "/" (sess @ [s]);
wenzelm@23899
   114
fun ID_of_thy thy = ID_of (#session (get_info thy)) (Context.theory_name thy);
wenzelm@7727
   115
wenzelm@23899
   116
wenzelm@23899
   117
(*retrieve graph data from initial collection of theories*)
wenzelm@24150
   118
fun init_graph remote_path curr_sess = rev o map (fn thy =>
wenzelm@7727
   119
  let
wenzelm@23899
   120
    val name = Context.theory_name thy;
wenzelm@23899
   121
    val {name = sess_name, session, is_local} = get_info thy;
wenzelm@9416
   122
    val path' = Path.append (Path.make session) (html_path name);
wenzelm@24150
   123
    val entry =
wenzelm@24150
   124
     {name = name, ID = ID_of session name, dir = sess_name,
wenzelm@24150
   125
      path =
wenzelm@24150
   126
        if null session then "" else
wenzelm@24150
   127
        if is_some remote_path andalso not is_local then
wenzelm@24150
   128
          Url.implode (Url.append (the remote_path) (Url.File
wenzelm@24150
   129
            (Path.append (Path.make session) (html_path name))))
wenzelm@24150
   130
        else Path.implode (Path.append (mk_rel_path curr_sess session) (html_path name)),
wenzelm@24150
   131
      unfold = false,
wenzelm@24150
   132
      parents = map ID_of_thy (Theory.parents_of thy)};
wenzelm@24150
   133
  in (0, entry) end);
wenzelm@7727
   134
wenzelm@24150
   135
fun ins_graph_entry (i, entry as {ID, ...}) (gr: (int * graph_node) list) =
wenzelm@24150
   136
  (i, entry) :: filter_out (fn (_, entry') => #ID entry' = ID) gr;
wenzelm@7727
   137
wenzelm@7727
   138
wenzelm@7727
   139
wenzelm@7727
   140
(** global browser info state **)
wenzelm@7727
   141
wenzelm@7727
   142
(* type theory_info *)
wenzelm@7727
   143
wenzelm@7727
   144
type theory_info = {tex_source: Buffer.T, html_source: Buffer.T, html: Buffer.T};
wenzelm@7727
   145
wenzelm@7727
   146
fun make_theory_info (tex_source, html_source, html) =
wenzelm@7727
   147
  {tex_source = tex_source, html_source = html_source, html = html}: theory_info;
wenzelm@7727
   148
wenzelm@7727
   149
val empty_theory_info = make_theory_info (Buffer.empty, Buffer.empty, Buffer.empty);
wenzelm@7727
   150
wenzelm@7727
   151
fun map_theory_info f {tex_source, html_source, html} =
wenzelm@7727
   152
  make_theory_info (f (tex_source, html_source, html));
wenzelm@7727
   153
wenzelm@7727
   154
wenzelm@7727
   155
(* type browser_info *)
wenzelm@7727
   156
wenzelm@11856
   157
type browser_info = {theories: theory_info Symtab.table, files: (Path.T * string) list,
wenzelm@24150
   158
  tex_index: (int * string) list, html_index: (int * string) list, graph: (int * graph_node) list};
wenzelm@7727
   159
wenzelm@11856
   160
fun make_browser_info (theories, files, tex_index, html_index, graph) =
wenzelm@11856
   161
  {theories = theories, files = files, tex_index = tex_index, html_index = html_index,
wenzelm@9416
   162
    graph = graph}: browser_info;
wenzelm@7727
   163
wenzelm@24150
   164
val empty_browser_info = make_browser_info (Symtab.empty, [], [], [], []);
wenzelm@7727
   165
wenzelm@23899
   166
fun init_browser_info remote_path curr_sess thys = make_browser_info
wenzelm@24150
   167
  (Symtab.empty, [], [], [], init_graph remote_path curr_sess thys);
wenzelm@7727
   168
wenzelm@11856
   169
fun map_browser_info f {theories, files, tex_index, html_index, graph} =
wenzelm@11856
   170
  make_browser_info (f (theories, files, tex_index, html_index, graph));
wenzelm@7727
   171
wenzelm@7727
   172
wenzelm@7727
   173
(* state *)
wenzelm@7727
   174
wenzelm@7727
   175
val browser_info = ref empty_browser_info;
wenzelm@23944
   176
fun change_browser_info f = CRITICAL (fn () => change browser_info (map_browser_info f));
wenzelm@7727
   177
wenzelm@11057
   178
val suppress_tex_source = ref false;
wenzelm@24102
   179
fun no_document f x = setmp_noncritical suppress_tex_source true f x;
wenzelm@7727
   180
wenzelm@7727
   181
fun init_theory_info name info =
wenzelm@11856
   182
  change_browser_info (fn (theories, files, tex_index, html_index, graph) =>
wenzelm@17412
   183
    (Symtab.update (name, info) theories, files, tex_index, html_index, graph));
wenzelm@7727
   184
wenzelm@7727
   185
fun change_theory_info name f =
wenzelm@11856
   186
  change_browser_info (fn (info as (theories, files, tex_index, html_index, graph)) =>
wenzelm@17412
   187
    (case Symtab.lookup theories name of
skalberg@15531
   188
      NONE => (warning ("Browser info: cannot access theory document " ^ quote name); info)
wenzelm@17412
   189
    | SOME info => (Symtab.update (name, map_theory_info f info) theories, files,
wenzelm@9416
   190
        tex_index, html_index, graph)));
wenzelm@7727
   191
wenzelm@7727
   192
wenzelm@11856
   193
fun add_file file =
wenzelm@11856
   194
  change_browser_info (fn (theories, files, tex_index, html_index, graph) =>
wenzelm@11856
   195
    (theories, file :: files, tex_index, html_index, graph));
wenzelm@11856
   196
wenzelm@7727
   197
fun add_tex_index txt =
wenzelm@11856
   198
  change_browser_info (fn (theories, files, tex_index, html_index, graph) =>
wenzelm@24150
   199
    (theories, files, txt :: tex_index, html_index, graph));
wenzelm@7727
   200
wenzelm@7727
   201
fun add_html_index txt =
wenzelm@11856
   202
  change_browser_info (fn (theories, files, tex_index, html_index, graph) =>
wenzelm@24150
   203
    (theories, files, tex_index, txt :: html_index, graph));
wenzelm@7727
   204
wenzelm@24150
   205
fun add_graph_entry entry =
wenzelm@11856
   206
  change_browser_info (fn (theories, files, tex_index, html_index, graph) =>
wenzelm@24150
   207
    (theories, files, tex_index, html_index, ins_graph_entry entry graph));
wenzelm@7727
   208
wenzelm@11057
   209
fun add_tex_source name txt =
wenzelm@11057
   210
  if ! suppress_tex_source then ()
wenzelm@11057
   211
  else change_theory_info name (fn (tex_source, html_source, html) =>
wenzelm@11057
   212
    (Buffer.add txt tex_source, html_source, html));
wenzelm@7727
   213
wenzelm@7727
   214
fun add_html_source name txt = change_theory_info name (fn (tex_source, html_source, html) =>
wenzelm@7727
   215
  (tex_source, Buffer.add txt html_source, html));
wenzelm@7727
   216
wenzelm@7727
   217
fun add_html name txt = change_theory_info name (fn (tex_source, html_source, html) =>
wenzelm@7727
   218
  (tex_source, html_source, Buffer.add txt html));
wenzelm@7727
   219
wenzelm@7727
   220
wenzelm@7727
   221
wenzelm@7727
   222
(** global session state **)
wenzelm@7727
   223
wenzelm@7727
   224
(* session_info *)
wenzelm@7727
   225
wenzelm@7727
   226
type session_info =
wenzelm@7727
   227
  {name: string, parent: string, session: string, path: string list, html_prefix: Path.T,
wenzelm@17082
   228
    info: bool, doc_format: string, doc_graph: bool, documents: (string * string) list,
wenzelm@17210
   229
    doc_prefix1: (Path.T * Path.T) option, doc_prefix2: (bool * Path.T) option,
wenzelm@17210
   230
    remote_path: Url.T option, verbose: bool, readme: Path.T option};
wenzelm@7727
   231
wenzelm@9416
   232
fun make_session_info
wenzelm@17082
   233
  (name, parent, session, path, html_prefix, info, doc_format, doc_graph, documents,
wenzelm@17082
   234
    doc_prefix1, doc_prefix2, remote_path, verbose, readme) =
wenzelm@7802
   235
  {name = name, parent = parent, session = session, path = path, html_prefix = html_prefix,
wenzelm@17082
   236
    info = info, doc_format = doc_format, doc_graph = doc_graph, documents = documents,
wenzelm@17082
   237
    doc_prefix1 = doc_prefix1, doc_prefix2 = doc_prefix2, remote_path = remote_path,
wenzelm@17082
   238
    verbose = verbose, readme = readme}: session_info;
wenzelm@7727
   239
wenzelm@7727
   240
wenzelm@7727
   241
(* state *)
wenzelm@7727
   242
skalberg@15531
   243
val session_info = ref (NONE: session_info option);
wenzelm@7727
   244
skalberg@15531
   245
fun with_session x f = (case ! session_info of NONE => x | SOME info => f info);
wenzelm@26433
   246
fun with_context f = f (Context.theory_name (ML_Context.the_global_context ()));
wenzelm@7727
   247
wenzelm@7727
   248
wenzelm@7727
   249
wenzelm@14922
   250
(** document preparation **)
wenzelm@7727
   251
wenzelm@7727
   252
(* maintain index *)
wenzelm@7727
   253
wenzelm@7727
   254
val session_entries =
wenzelm@7727
   255
  HTML.session_entries o
wenzelm@14898
   256
    map (fn name => (Url.File (Path.append (Path.basic name) index_path), name));
wenzelm@7727
   257
wenzelm@7727
   258
fun get_entries dir =
wenzelm@7727
   259
  split_lines (File.read (Path.append dir session_entries_path));
wenzelm@7727
   260
wenzelm@7727
   261
fun put_entries entries dir =
wenzelm@7727
   262
  File.write (Path.append dir session_entries_path) (cat_lines entries);
wenzelm@7727
   263
wenzelm@7727
   264
wenzelm@7727
   265
fun create_index dir =
wenzelm@7727
   266
  File.read (Path.append dir pre_index_path) ^
wenzelm@7727
   267
    session_entries (get_entries dir) ^ HTML.end_index
wenzelm@7727
   268
  |> File.write (Path.append dir index_path);
wenzelm@7727
   269
wenzelm@24150
   270
fun update_index dir name = CRITICAL (fn () =>
wenzelm@7727
   271
  (case try get_entries dir of
wenzelm@21858
   272
    NONE => warning ("Browser info: cannot access session index of " ^ quote (Path.implode dir))
wenzelm@24150
   273
  | SOME es => (put_entries ((remove (op =) name es) @ [name]) dir; create_index dir)));
wenzelm@7727
   274
wenzelm@7727
   275
wenzelm@17082
   276
(* document versions *)
wenzelm@17082
   277
wenzelm@17082
   278
fun read_version str =
wenzelm@17082
   279
  (case space_explode "=" str of
wenzelm@17082
   280
    [name] => (name, "")
wenzelm@17082
   281
  | [name, tags] => (name, tags)
wenzelm@17082
   282
  | _ => error ("Malformed document version specification: " ^ quote str));
wenzelm@17082
   283
wenzelm@17082
   284
fun read_versions strs =
wenzelm@19046
   285
  rev (distinct (eq_fst (op =)) (rev ((documentN, "") :: map read_version strs)))
wenzelm@17082
   286
  |> filter_out (equal "-" o #2);
wenzelm@17082
   287
wenzelm@17082
   288
wenzelm@7727
   289
(* init session *)
wenzelm@7727
   290
wenzelm@7727
   291
fun name_of_session elems = space_implode "/" ("Isabelle" :: elems);
wenzelm@7727
   292
wenzelm@17082
   293
fun init build info doc doc_graph doc_versions path name doc_prefix2
wenzelm@23944
   294
    (remote_path, first_time) verbose thys = CRITICAL (fn () =>
wenzelm@11911
   295
  if not build andalso not info andalso doc = "" andalso is_none doc_prefix2 then
skalberg@15531
   296
    (browser_info := empty_browser_info; session_info := NONE)
wenzelm@11856
   297
  else
wenzelm@11856
   298
    let
wenzelm@11856
   299
      val parent_name = name_of_session (Library.take (length path - 1, path));
wenzelm@11856
   300
      val session_name = name_of_session path;
wenzelm@11856
   301
      val sess_prefix = Path.make path;
wenzelm@11856
   302
      val html_prefix = Path.append (Path.expand output_path) sess_prefix;
wenzelm@7727
   303
wenzelm@17082
   304
      val (doc_prefix1, documents) =
wenzelm@17082
   305
        if doc = "" then (NONE, [])
wenzelm@21962
   306
        else if not (File.exists document_path) then
wenzelm@22580
   307
          (if verbose then Output.std_error "Warning: missing document directory\n" else ();
wenzelm@22580
   308
            (NONE, []))
wenzelm@17082
   309
        else (SOME (Path.append html_prefix document_path, html_prefix),
wenzelm@17082
   310
          read_versions doc_versions);
wenzelm@7727
   311
wenzelm@11856
   312
      val parent_index_path = Path.append Path.parent index_path;
wenzelm@11856
   313
      val index_up_lnk = if first_time then
wenzelm@16263
   314
          Url.append (the remote_path) (Url.File (Path.append sess_prefix parent_index_path))
wenzelm@14898
   315
        else Url.File parent_index_path;
wenzelm@11856
   316
      val readme =
skalberg@15531
   317
        if File.exists readme_html_path then SOME readme_html_path
skalberg@15531
   318
        else if File.exists readme_path then SOME readme_path
skalberg@15531
   319
        else NONE;
wenzelm@7727
   320
wenzelm@17082
   321
      val docs =
wenzelm@17082
   322
        (case readme of NONE => [] | SOME p => [(Url.File p, "README")]) @
wenzelm@17082
   323
        map (fn (name, _) => (Url.File (Path.ext doc (Path.basic name)), name)) documents;
wenzelm@11856
   324
      val index_text = HTML.begin_index (index_up_lnk, parent_name)
wenzelm@21858
   325
        (Url.File index_path, session_name) docs (Url.explode "medium.html");
wenzelm@11856
   326
    in
skalberg@15531
   327
      session_info := SOME (make_session_info (name, parent_name, session_name, path, html_prefix,
wenzelm@17082
   328
      info, doc, doc_graph, documents, doc_prefix1, doc_prefix2, remote_path, verbose, readme));
wenzelm@23899
   329
      browser_info := init_browser_info remote_path path thys;
wenzelm@24150
   330
      add_html_index (0, index_text)
wenzelm@23944
   331
    end);
wenzelm@7727
   332
wenzelm@7727
   333
wenzelm@17082
   334
(* isatool wrappers *)
wenzelm@17082
   335
wenzelm@17082
   336
fun isatool_document verbose format name tags path result_path =
wenzelm@17082
   337
  let
wenzelm@17082
   338
    val s = "\"$ISATOOL\" document -c -o '" ^ format ^ "' \
wenzelm@17082
   339
      \-n '" ^ name ^ "' -t '" ^ tags ^ "' " ^ File.shell_path path ^
wenzelm@17082
   340
      " 2>&1" ^ (if verbose then "" else " >/dev/null");
wenzelm@17082
   341
    val doc_path = Path.append result_path (Path.ext format (Path.basic name));
wenzelm@17082
   342
  in
wenzelm@21962
   343
    if verbose then writeln s else ();
wenzelm@17082
   344
    system s;
wenzelm@21962
   345
    File.exists doc_path orelse
wenzelm@21962
   346
      error ("No document: " ^ quote (show_path doc_path));
wenzelm@17082
   347
    doc_path
wenzelm@17082
   348
  end;
wenzelm@17082
   349
wenzelm@17082
   350
fun isatool_browser graph =
wenzelm@17082
   351
  let
wenzelm@17082
   352
    val pdf_path = File.tmp_path graph_pdf_path;
wenzelm@17082
   353
    val eps_path = File.tmp_path graph_eps_path;
wenzelm@17082
   354
    val gr_path = File.tmp_path graph_path;
wenzelm@17082
   355
    val s = "browser -o " ^ File.shell_path pdf_path ^ " " ^ File.shell_path gr_path;
wenzelm@17082
   356
  in
wenzelm@17082
   357
    write_graph graph gr_path;
wenzelm@17082
   358
    if File.isatool s <> 0 orelse not (File.exists eps_path) orelse not (File.exists pdf_path)
wenzelm@17082
   359
    then error "Failed to prepare dependency graph"
wenzelm@17082
   360
    else
wenzelm@17082
   361
      let val pdf = File.read pdf_path and eps = File.read eps_path
wenzelm@17082
   362
      in File.rm pdf_path; File.rm eps_path; File.rm gr_path; (pdf, eps) end
wenzelm@17082
   363
  end;
wenzelm@17082
   364
wenzelm@17082
   365
wenzelm@11856
   366
(* finish session -- output all generated text *)
wenzelm@7727
   367
wenzelm@24150
   368
fun sorted_index index = map snd (sort (int_ord o pairself fst) (rev index));
wenzelm@24150
   369
fun index_buffer index = Buffer.add (implode (sorted_index index)) Buffer.empty;
wenzelm@24150
   370
wenzelm@14922
   371
fun write_tex src name path =
wenzelm@14922
   372
  Buffer.write (Path.append path (tex_path name)) src;
wenzelm@14922
   373
wenzelm@14922
   374
fun write_tex_index tex_index path =
wenzelm@24150
   375
  write_tex (index_buffer tex_index |> Buffer.add Latex.tex_trailer) doc_indexN path;
wenzelm@14922
   376
wenzelm@7727
   377
wenzelm@23944
   378
fun finish () = CRITICAL (fn () =>
wenzelm@23944
   379
    with_session () (fn {name, info, html_prefix, doc_format, doc_graph,
wenzelm@23944
   380
      documents, doc_prefix1, doc_prefix2, path, verbose, readme, ...} =>
wenzelm@7727
   381
  let
wenzelm@11856
   382
    val {theories, files, tex_index, html_index, graph} = ! browser_info;
wenzelm@11856
   383
    val thys = Symtab.dest theories;
wenzelm@9416
   384
    val parent_html_prefix = Path.append html_prefix Path.parent;
wenzelm@7727
   385
wenzelm@11856
   386
    fun finish_tex path (a, {tex_source, ...}: theory_info) = write_tex tex_source a path;
wenzelm@11856
   387
    fun finish_html (a, {html, ...}: theory_info) =
wenzelm@11856
   388
      Buffer.write (Path.append html_prefix (html_path a)) (Buffer.add HTML.end_theory html);
wenzelm@11856
   389
wenzelm@24150
   390
    val sorted_graph = sorted_index graph;
wenzelm@11856
   391
    val opt_graphs =
wenzelm@16263
   392
      if doc_graph andalso (is_some doc_prefix1 orelse is_some doc_prefix2) then
wenzelm@24150
   393
        SOME (isatool_browser sorted_graph)
skalberg@15531
   394
      else NONE;
wenzelm@11856
   395
wenzelm@17210
   396
    fun prepare_sources cp path =
wenzelm@16263
   397
     (File.mkdir path;
wenzelm@17210
   398
      if cp then File.copy_dir document_path path else ();
wenzelm@17177
   399
      File.isatool ("latex -o sty " ^ File.shell_path (Path.append path (Path.basic "root.tex")));
skalberg@15531
   400
      (case opt_graphs of NONE => () | SOME (pdf, eps) =>
wenzelm@11856
   401
        (File.write (Path.append path graph_pdf_path) pdf;
wenzelm@11856
   402
          File.write (Path.append path graph_eps_path) eps));
wenzelm@14922
   403
      write_tex_index tex_index path;
skalberg@15570
   404
      List.app (finish_tex path) thys);
wenzelm@7727
   405
  in
wenzelm@21962
   406
    if info then
wenzelm@11856
   407
     (File.mkdir (Path.append html_prefix session_path);
wenzelm@24150
   408
      Buffer.write (Path.append html_prefix pre_index_path) (index_buffer html_index);
wenzelm@11856
   409
      File.write (Path.append html_prefix session_entries_path) "";
wenzelm@11856
   410
      create_index html_prefix;
wenzelm@11856
   411
      if length path > 1 then update_index parent_html_prefix name else ();
wenzelm@16263
   412
      (case readme of NONE => () | SOME path => File.copy path html_prefix);
wenzelm@24150
   413
      write_graph sorted_graph (Path.append html_prefix graph_path);
wenzelm@21858
   414
      File.copy (Path.explode "~~/lib/browser/GraphBrowser.jar") html_prefix;
skalberg@15570
   415
      List.app (fn (a, txt) => File.write (Path.append html_prefix (Path.basic a)) txt)
wenzelm@14898
   416
        (HTML.applet_pages name (Url.File index_path, name));
wenzelm@21858
   417
      File.copy (Path.explode "~~/lib/html/isabelle.css") html_prefix;
skalberg@15570
   418
      List.app finish_html thys;
skalberg@15570
   419
      List.app (uncurry File.write) files;
wenzelm@22580
   420
      if verbose then Output.std_error ("Browser info at " ^ show_path html_prefix ^ "\n") else ())
wenzelm@21962
   421
    else ();
wenzelm@11856
   422
wenzelm@17210
   423
    (case doc_prefix2 of NONE => () | SOME (cp, path) =>
wenzelm@17210
   424
     (prepare_sources cp path;
wenzelm@22580
   425
      if verbose then Output.std_error ("Document sources at " ^ show_path path ^ "\n") else ()));
wenzelm@12895
   426
wenzelm@17082
   427
    (case doc_prefix1 of NONE => () | SOME (path, result_path) =>
wenzelm@17082
   428
      documents |> List.app (fn (name, tags) =>
wenzelm@17082
   429
       let
wenzelm@17210
   430
         val _ = prepare_sources true path;
wenzelm@17082
   431
         val doc_path = isatool_document true doc_format name tags path result_path;
wenzelm@17082
   432
       in
wenzelm@22580
   433
         if verbose then Output.std_error ("Document at " ^ show_path doc_path ^ "\n") else ()
wenzelm@17082
   434
       end));
wenzelm@11856
   435
wenzelm@7727
   436
    browser_info := empty_browser_info;
skalberg@15531
   437
    session_info := NONE
wenzelm@23944
   438
  end));
wenzelm@7727
   439
wenzelm@7727
   440
wenzelm@7727
   441
(* theory elements *)
wenzelm@7727
   442
wenzelm@7727
   443
fun init_theory name = with_session () (fn _ => init_theory_info name empty_theory_info);
wenzelm@7727
   444
wenzelm@7727
   445
fun verbatim_source name mk_text =
wenzelm@7727
   446
  with_session () (fn _ => add_html_source name (HTML.verbatim_source (mk_text ())));
wenzelm@7727
   447
wenzelm@9136
   448
fun theory_output name s =
wenzelm@9917
   449
  with_session () (fn _ => add_tex_source name (Latex.isabelle_file name s));
wenzelm@7727
   450
wenzelm@7727
   451
wenzelm@23899
   452
fun parent_link remote_path curr_session thy =
wenzelm@23899
   453
  let
wenzelm@23899
   454
    val {name = _, session, is_local} = get_info thy;
wenzelm@23899
   455
    val name = Context.theory_name thy;
wenzelm@23899
   456
    val link =
wenzelm@23899
   457
      if null session then NONE
wenzelm@23899
   458
      else SOME
wenzelm@23899
   459
       (if is_some remote_path andalso not is_local then
wenzelm@23899
   460
         Url.append (the remote_path) (Url.File (Path.append (Path.make session) (html_path name)))
wenzelm@23899
   461
        else Url.File (Path.append (mk_rel_path curr_session session) (html_path name)));
wenzelm@23899
   462
  in (link, name) end;
wenzelm@7727
   463
wenzelm@26323
   464
fun begin_theory update_time dir files thy =
wenzelm@9416
   465
    with_session thy (fn {name = sess_name, session, path, html_prefix, remote_path, ...} =>
wenzelm@7727
   466
  let
wenzelm@23899
   467
    val name = Context.theory_name thy;
wenzelm@23899
   468
    val parents = Theory.parents_of thy;
wenzelm@23899
   469
    val parent_specs = map (parent_link remote_path path) parents;
wenzelm@7727
   470
wenzelm@7727
   471
    fun prep_file (raw_path, loadit) =
wenzelm@23884
   472
      (case ThyLoad.check_ml dir raw_path of
skalberg@15531
   473
        SOME (path, _) =>
wenzelm@7727
   474
          let
wenzelm@7727
   475
            val base = Path.base path;
wenzelm@7727
   476
            val base_html = html_ext base;
wenzelm@26323
   477
            val _ = add_file (Path.append html_prefix base_html,
wenzelm@14898
   478
              HTML.ml_file (Url.File base) (File.read path));
wenzelm@26323
   479
          in (SOME (Url.File base_html), Url.File raw_path, loadit) end
wenzelm@23884
   480
      | NONE =>
wenzelm@23884
   481
          (warning ("Browser info: expected to find ML file" ^ quote (Path.implode raw_path));
wenzelm@23884
   482
            (NONE, Url.File raw_path, loadit)));
wenzelm@7727
   483
wenzelm@7727
   484
    fun prep_html_source (tex_source, html_source, html) =
wenzelm@7727
   485
      let
wenzelm@14898
   486
        val txt = HTML.begin_theory (Url.File index_path, session)
wenzelm@26323
   487
          name parent_specs (map prep_file files) (Buffer.content html_source)
wenzelm@7727
   488
      in (tex_source, Buffer.empty, Buffer.add txt html) end;
wenzelm@7727
   489
wenzelm@9416
   490
    val entry =
wenzelm@9416
   491
     {name = name, ID = ID_of path name, dir = sess_name, unfold = true,
wenzelm@21858
   492
      path = Path.implode (html_path name),
wenzelm@23899
   493
      parents = map ID_of_thy parents};
wenzelm@7727
   494
  in
wenzelm@7727
   495
    change_theory_info name prep_html_source;
wenzelm@24150
   496
    add_graph_entry (update_time, entry);
wenzelm@24150
   497
    add_html_index (update_time, HTML.theory_entry (Url.File (html_path name), name));
wenzelm@24150
   498
    add_tex_index (update_time, Latex.theory_entry name);
wenzelm@27327
   499
    put_info {name = sess_name, session = path, is_local = is_some remote_path} thy
wenzelm@7727
   500
  end);
wenzelm@7727
   501
wenzelm@7727
   502
wenzelm@13532
   503
val hooks = ref ([]: (string -> (string * thm list) list -> unit) list);
wenzelm@23944
   504
fun add_hook f = CRITICAL (fn () => change hooks (cons f));
wenzelm@13532
   505
wenzelm@13532
   506
fun results k xs =
skalberg@15570
   507
 (List.app (fn f => (try (fn () => f k xs) (); ())) (! hooks);
wenzelm@22123
   508
  with_session () (fn _ => with_context add_html
wenzelm@22123
   509
    (HTML.results (ML_Context.the_local_context ()) k xs)));
wenzelm@13532
   510
wenzelm@22123
   511
fun theorem a th = results Thm.theoremK [(a, [th])];
wenzelm@22123
   512
fun theorems a ths = results Thm.theoremK [(a, ths)];
wenzelm@8088
   513
fun chapter s = with_session () (fn _ => with_context add_html (HTML.chapter s));
wenzelm@7727
   514
fun section s = with_session () (fn _ => with_context add_html (HTML.section s));
wenzelm@7727
   515
fun subsection s = with_session () (fn _ => with_context add_html (HTML.subsection s));
wenzelm@7727
   516
fun subsubsection s = with_session () (fn _ => with_context add_html (HTML.subsubsection s));
wenzelm@7727
   517
wenzelm@7727
   518
wenzelm@7727
   519
wenzelm@14922
   520
(** draft document output **)
wenzelm@14922
   521
wenzelm@14922
   522
fun drafts doc_format src_paths =
wenzelm@14922
   523
  let
wenzelm@24150
   524
    fun prep_draft path i =
wenzelm@14935
   525
      let
wenzelm@14935
   526
        val base = Path.base path;
wenzelm@14972
   527
        val name =
wenzelm@24829
   528
          (case Path.implode (#1 (Path.split_ext base)) of
wenzelm@24829
   529
            "" => "DUMMY" ^ serial_string ()
wenzelm@24829
   530
          | s => s);
wenzelm@14935
   531
      in
wenzelm@14935
   532
        if File.exists path then
wenzelm@24150
   533
          (((name, base, File.read path), (i, Latex.theory_entry name)), i + 1)
wenzelm@21858
   534
        else error ("Bad file: " ^ quote (Path.implode path))
wenzelm@14935
   535
      end;
wenzelm@24150
   536
    val (srcs, tex_index) = split_list (fst (fold_map prep_draft src_paths 0));
wenzelm@14935
   537
wenzelm@17082
   538
    val doc_path = File.tmp_path document_path;
wenzelm@17082
   539
    val result_path = Path.append doc_path Path.parent;
wenzelm@14922
   540
    val _ = File.mkdir doc_path;
wenzelm@14922
   541
    val root_path = Path.append doc_path (Path.basic "root.tex");
wenzelm@21858
   542
    val _ = File.copy (Path.explode "~~/lib/texinputs/draft.tex") root_path;
wenzelm@16263
   543
    val _ = File.isatool ("latex -o sty " ^ File.shell_path root_path);
wenzelm@16263
   544
    val _ = File.isatool ("latex -o syms " ^ File.shell_path root_path);
wenzelm@14922
   545
wenzelm@14922
   546
    fun known name =
wenzelm@14922
   547
      let val ss = split_lines (File.read (Path.append doc_path (Path.basic name)))
wenzelm@20664
   548
      in member (op =) ss end;
wenzelm@14922
   549
    val known_syms = known "syms.lst";
wenzelm@14922
   550
    val known_ctrls = known "ctrls.lst";
wenzelm@14922
   551
skalberg@15570
   552
    val _ = srcs |> List.app (fn (name, base, txt) =>
wenzelm@14935
   553
      Symbol.explode txt
wenzelm@21858
   554
      |> Latex.symbol_source (known_syms, known_ctrls) (Path.implode base)
wenzelm@14935
   555
      |> File.write (Path.append doc_path (tex_path name)));
wenzelm@14922
   556
    val _ = write_tex_index tex_index doc_path;
wenzelm@17082
   557
  in isatool_document false doc_format documentN "" doc_path result_path end;
wenzelm@14922
   558
wenzelm@14922
   559
wenzelm@7685
   560
end;
wenzelm@7685
   561
wenzelm@6203
   562
structure BasicPresent: BASIC_PRESENT = Present;
wenzelm@6203
   563
open BasicPresent;