src/Pure/Thy/present.ML
author wenzelm
Tue Sep 25 15:40:41 2012 +0200 (2012-09-25)
changeset 49561 26fc70e983c2
parent 48935 4c92a2f310b6
child 49565 ea4308b7ef0f
permissions -rw-r--r--
separate module Graph_Display;
tuned signature;
wenzelm@6203
     1
(*  Title:      Pure/Thy/present.ML
wenzelm@9416
     2
    Author:     Markus Wenzel and Stefan Berghofer, TU Muenchen
wenzelm@6203
     3
wenzelm@9416
     4
Theory presentation: HTML, graph files, (PDF)LaTeX documents.
wenzelm@6203
     5
*)
wenzelm@6203
     6
wenzelm@6203
     7
signature BASIC_PRESENT =
wenzelm@6203
     8
sig
wenzelm@42008
     9
  val no_document: ('a -> 'b) -> 'a -> 'b  (*not thread-safe!*)
wenzelm@6203
    10
end;
wenzelm@6203
    11
wenzelm@6203
    12
signature PRESENT =
wenzelm@6203
    13
sig
wenzelm@7727
    14
  include BASIC_PRESENT
wenzelm@24561
    15
  val session_name: theory -> string
wenzelm@48543
    16
  datatype dump_mode = Dump_all | Dump_tex | Dump_tex_sty
wenzelm@48543
    17
  val dump_mode: string -> dump_mode
wenzelm@48804
    18
  val read_variant: string -> string * string
wenzelm@48805
    19
  val init: bool -> bool -> string -> string -> bool -> string -> (string * string) list ->
wenzelm@48805
    20
    string list -> string -> string * dump_mode -> Url.T option * bool -> bool ->
wenzelm@42008
    21
    theory list -> unit  (*not thread-safe!*)
wenzelm@42008
    22
  val finish: unit -> unit  (*not thread-safe!*)
wenzelm@7727
    23
  val init_theory: string -> unit
wenzelm@27862
    24
  val theory_source: string -> (unit -> HTML.text) -> unit
wenzelm@9136
    25
  val theory_output: string -> string -> unit
wenzelm@24150
    26
  val begin_theory: int -> Path.T -> (Path.T * bool) list -> theory -> theory
wenzelm@14922
    27
  val drafts: string -> Path.T list -> Path.T
wenzelm@6203
    28
end;
wenzelm@6203
    29
wenzelm@7685
    30
structure Present: PRESENT =
wenzelm@7685
    31
struct
wenzelm@7685
    32
wenzelm@7727
    33
wenzelm@7727
    34
(** paths **)
wenzelm@7727
    35
wenzelm@7727
    36
val tex_ext = Path.ext "tex";
wenzelm@7727
    37
val tex_path = tex_ext o Path.basic;
wenzelm@7727
    38
val html_ext = Path.ext "html";
wenzelm@7727
    39
val html_path = html_ext o Path.basic;
wenzelm@7727
    40
val index_path = Path.basic "index.html";
wenzelm@11856
    41
val readme_html_path = Path.basic "README.html";
wenzelm@11856
    42
val readme_path = Path.basic "README";
wenzelm@17082
    43
val documentN = "document";
wenzelm@17082
    44
val document_path = Path.basic documentN;
wenzelm@8196
    45
val doc_indexN = "session";
wenzelm@11856
    46
val graph_path = Path.basic "session.graph";
wenzelm@11856
    47
val graph_pdf_path = Path.basic "session_graph.pdf";
wenzelm@11856
    48
val graph_eps_path = Path.basic "session_graph.eps";
wenzelm@7727
    49
wenzelm@7727
    50
val session_path = Path.basic ".session";
wenzelm@21858
    51
val session_entries_path = Path.explode ".session/entries";
wenzelm@21858
    52
val pre_index_path = Path.explode ".session/pre-index";
wenzelm@7727
    53
berghofe@9044
    54
fun mk_rel_path [] ys = Path.make ys
berghofe@9044
    55
  | mk_rel_path xs [] = Path.appends (replicate (length xs) Path.parent)
wenzelm@9416
    56
  | mk_rel_path (ps as x :: xs) (qs as y :: ys) = if x = y then mk_rel_path xs ys else
berghofe@9044
    57
      Path.appends (replicate (length ps) Path.parent @ [Path.make qs]);
wenzelm@7727
    58
wenzelm@21858
    59
fun show_path path = Path.implode (Path.append (File.pwd ()) path);
wenzelm@11856
    60
wenzelm@7727
    61
wenzelm@14922
    62
wenzelm@7727
    63
(** additional theory data **)
wenzelm@7727
    64
wenzelm@42008
    65
structure Browser_Info = Theory_Data
wenzelm@22846
    66
(
wenzelm@9416
    67
  type T = {name: string, session: string list, is_local: bool};
wenzelm@27329
    68
  val empty = {name = "", session = [], is_local = false}: T;
wenzelm@16503
    69
  fun extend _ = empty;
wenzelm@33522
    70
  fun merge _ = empty;
wenzelm@22846
    71
);
wenzelm@7727
    72
wenzelm@42008
    73
val put_info = Browser_Info.put;
wenzelm@42008
    74
val get_info = Browser_Info.get;
wenzelm@24561
    75
val session_name = #name o get_info;
wenzelm@24561
    76
wenzelm@7727
    77
wenzelm@7727
    78
wenzelm@7727
    79
(** graphs **)
wenzelm@7727
    80
wenzelm@9416
    81
fun ID_of sess s = space_implode "/" (sess @ [s]);
wenzelm@23899
    82
fun ID_of_thy thy = ID_of (#session (get_info thy)) (Context.theory_name thy);
wenzelm@7727
    83
wenzelm@23899
    84
wenzelm@23899
    85
(*retrieve graph data from initial collection of theories*)
wenzelm@24150
    86
fun init_graph remote_path curr_sess = rev o map (fn thy =>
wenzelm@7727
    87
  let
wenzelm@23899
    88
    val name = Context.theory_name thy;
wenzelm@23899
    89
    val {name = sess_name, session, is_local} = get_info thy;
wenzelm@24150
    90
    val entry =
wenzelm@24150
    91
     {name = name, ID = ID_of session name, dir = sess_name,
wenzelm@24150
    92
      path =
wenzelm@24150
    93
        if null session then "" else
wenzelm@24150
    94
        if is_some remote_path andalso not is_local then
wenzelm@24150
    95
          Url.implode (Url.append (the remote_path) (Url.File
wenzelm@24150
    96
            (Path.append (Path.make session) (html_path name))))
wenzelm@24150
    97
        else Path.implode (Path.append (mk_rel_path curr_sess session) (html_path name)),
wenzelm@24150
    98
      unfold = false,
wenzelm@49561
    99
      parents = map ID_of_thy (Theory.parents_of thy),
wenzelm@49561
   100
      content = []};
wenzelm@24150
   101
  in (0, entry) end);
wenzelm@7727
   102
wenzelm@49561
   103
fun ins_graph_entry (i, entry as {ID, ...}) (gr: (int * Graph_Display.node) list) =
wenzelm@24150
   104
  (i, entry) :: filter_out (fn (_, entry') => #ID entry' = ID) gr;
wenzelm@7727
   105
wenzelm@7727
   106
wenzelm@7727
   107
wenzelm@7727
   108
(** global browser info state **)
wenzelm@7727
   109
wenzelm@7727
   110
(* type theory_info *)
wenzelm@7727
   111
wenzelm@7727
   112
type theory_info = {tex_source: Buffer.T, html_source: Buffer.T, html: Buffer.T};
wenzelm@7727
   113
wenzelm@7727
   114
fun make_theory_info (tex_source, html_source, html) =
wenzelm@7727
   115
  {tex_source = tex_source, html_source = html_source, html = html}: theory_info;
wenzelm@7727
   116
wenzelm@7727
   117
val empty_theory_info = make_theory_info (Buffer.empty, Buffer.empty, Buffer.empty);
wenzelm@7727
   118
wenzelm@7727
   119
fun map_theory_info f {tex_source, html_source, html} =
wenzelm@7727
   120
  make_theory_info (f (tex_source, html_source, html));
wenzelm@7727
   121
wenzelm@7727
   122
wenzelm@7727
   123
(* type browser_info *)
wenzelm@7727
   124
wenzelm@11856
   125
type browser_info = {theories: theory_info Symtab.table, files: (Path.T * string) list,
wenzelm@49561
   126
  tex_index: (int * string) list, html_index: (int * string) list,
wenzelm@49561
   127
  graph: (int * Graph_Display.node) list};
wenzelm@7727
   128
wenzelm@11856
   129
fun make_browser_info (theories, files, tex_index, html_index, graph) =
wenzelm@11856
   130
  {theories = theories, files = files, tex_index = tex_index, html_index = html_index,
wenzelm@9416
   131
    graph = graph}: browser_info;
wenzelm@7727
   132
wenzelm@24150
   133
val empty_browser_info = make_browser_info (Symtab.empty, [], [], [], []);
wenzelm@7727
   134
wenzelm@23899
   135
fun init_browser_info remote_path curr_sess thys = make_browser_info
wenzelm@24150
   136
  (Symtab.empty, [], [], [], init_graph remote_path curr_sess thys);
wenzelm@7727
   137
wenzelm@11856
   138
fun map_browser_info f {theories, files, tex_index, html_index, graph} =
wenzelm@11856
   139
  make_browser_info (f (theories, files, tex_index, html_index, graph));
wenzelm@7727
   140
wenzelm@7727
   141
wenzelm@7727
   142
(* state *)
wenzelm@7727
   143
wenzelm@32738
   144
val browser_info = Unsynchronized.ref empty_browser_info;
wenzelm@32738
   145
fun change_browser_info f =
wenzelm@32738
   146
  CRITICAL (fn () => Unsynchronized.change browser_info (map_browser_info f));
wenzelm@7727
   147
wenzelm@32738
   148
val suppress_tex_source = Unsynchronized.ref false;
wenzelm@42008
   149
fun no_document f x = Unsynchronized.setmp suppress_tex_source true f x;
wenzelm@7727
   150
wenzelm@7727
   151
fun init_theory_info name info =
wenzelm@11856
   152
  change_browser_info (fn (theories, files, tex_index, html_index, graph) =>
wenzelm@17412
   153
    (Symtab.update (name, info) theories, files, tex_index, html_index, graph));
wenzelm@7727
   154
wenzelm@7727
   155
fun change_theory_info name f =
wenzelm@42010
   156
  change_browser_info (fn (theories, files, tex_index, html_index, graph) =>
wenzelm@17412
   157
    (case Symtab.lookup theories name of
wenzelm@27491
   158
      NONE => error ("Browser info: cannot access theory document " ^ quote name)
wenzelm@17412
   159
    | SOME info => (Symtab.update (name, map_theory_info f info) theories, files,
wenzelm@9416
   160
        tex_index, html_index, graph)));
wenzelm@7727
   161
wenzelm@7727
   162
wenzelm@11856
   163
fun add_file file =
wenzelm@11856
   164
  change_browser_info (fn (theories, files, tex_index, html_index, graph) =>
wenzelm@11856
   165
    (theories, file :: files, tex_index, html_index, graph));
wenzelm@11856
   166
wenzelm@7727
   167
fun add_tex_index txt =
wenzelm@11856
   168
  change_browser_info (fn (theories, files, tex_index, html_index, graph) =>
wenzelm@24150
   169
    (theories, files, txt :: tex_index, html_index, graph));
wenzelm@7727
   170
wenzelm@7727
   171
fun add_html_index txt =
wenzelm@11856
   172
  change_browser_info (fn (theories, files, tex_index, html_index, graph) =>
wenzelm@24150
   173
    (theories, files, tex_index, txt :: html_index, graph));
wenzelm@7727
   174
wenzelm@24150
   175
fun add_graph_entry entry =
wenzelm@11856
   176
  change_browser_info (fn (theories, files, tex_index, html_index, graph) =>
wenzelm@24150
   177
    (theories, files, tex_index, html_index, ins_graph_entry entry graph));
wenzelm@7727
   178
wenzelm@11057
   179
fun add_tex_source name txt =
wenzelm@11057
   180
  if ! suppress_tex_source then ()
wenzelm@11057
   181
  else change_theory_info name (fn (tex_source, html_source, html) =>
wenzelm@11057
   182
    (Buffer.add txt tex_source, html_source, html));
wenzelm@7727
   183
wenzelm@7727
   184
fun add_html_source name txt = change_theory_info name (fn (tex_source, html_source, html) =>
wenzelm@7727
   185
  (tex_source, Buffer.add txt html_source, html));
wenzelm@7727
   186
wenzelm@7727
   187
wenzelm@7727
   188
wenzelm@7727
   189
(** global session state **)
wenzelm@7727
   190
wenzelm@7727
   191
(* session_info *)
wenzelm@7727
   192
wenzelm@48543
   193
datatype dump_mode = Dump_all | Dump_tex | Dump_tex_sty;
wenzelm@48543
   194
wenzelm@48543
   195
fun dump_mode "all" = Dump_all
wenzelm@48543
   196
  | dump_mode "tex" = Dump_tex
wenzelm@48543
   197
  | dump_mode "tex+sty" = Dump_tex_sty
wenzelm@48543
   198
  | dump_mode s =
wenzelm@48543
   199
      error ("Illegal document dump mode: " ^ quote s ^ " (expected \"all\", \"tex\", \"tex+sty\")");
wenzelm@48543
   200
wenzelm@7727
   201
type session_info =
wenzelm@7727
   202
  {name: string, parent: string, session: string, path: string list, html_prefix: Path.T,
wenzelm@48805
   203
    info: bool, doc_format: string, doc_graph: bool, doc_output: Path.T option,
wenzelm@48805
   204
    documents: (string * string) list, doc_dump: (string * dump_mode), remote_path: Url.T option,
wenzelm@48805
   205
    verbose: bool, readme: Path.T option};
wenzelm@7727
   206
wenzelm@9416
   207
fun make_session_info
wenzelm@48805
   208
  (name, parent, session, path, html_prefix, info, doc_format, doc_graph, doc_output,
wenzelm@48805
   209
    documents, doc_dump, remote_path, verbose, readme) =
wenzelm@7802
   210
  {name = name, parent = parent, session = session, path = path, html_prefix = html_prefix,
wenzelm@48805
   211
    info = info, doc_format = doc_format, doc_graph = doc_graph, doc_output = doc_output,
wenzelm@48805
   212
    documents = documents, doc_dump = doc_dump, remote_path = remote_path,
wenzelm@48805
   213
    verbose = verbose, readme = readme}: session_info;
wenzelm@7685
   214
wenzelm@7685
   215
wenzelm@7727
   216
(* state *)
wenzelm@7727
   217
wenzelm@32738
   218
val session_info = Unsynchronized.ref (NONE: session_info option);
wenzelm@7727
   219
wenzelm@42126
   220
fun session_default x f = (case ! session_info of NONE => x | SOME info => f info);
wenzelm@7727
   221
wenzelm@7727
   222
wenzelm@7727
   223
wenzelm@14922
   224
(** document preparation **)
wenzelm@7727
   225
wenzelm@7727
   226
(* maintain index *)
wenzelm@7727
   227
wenzelm@7727
   228
val session_entries =
wenzelm@7727
   229
  HTML.session_entries o
wenzelm@14898
   230
    map (fn name => (Url.File (Path.append (Path.basic name) index_path), name));
wenzelm@7727
   231
wenzelm@7727
   232
fun get_entries dir =
wenzelm@7727
   233
  split_lines (File.read (Path.append dir session_entries_path));
wenzelm@7727
   234
wenzelm@7727
   235
fun put_entries entries dir =
wenzelm@7727
   236
  File.write (Path.append dir session_entries_path) (cat_lines entries);
wenzelm@7727
   237
wenzelm@7727
   238
wenzelm@7727
   239
fun create_index dir =
wenzelm@7727
   240
  File.read (Path.append dir pre_index_path) ^
wenzelm@27862
   241
    session_entries (get_entries dir) ^ HTML.end_document
wenzelm@7727
   242
  |> File.write (Path.append dir index_path);
wenzelm@7727
   243
wenzelm@42008
   244
fun update_index dir name =
wenzelm@7727
   245
  (case try get_entries dir of
wenzelm@41944
   246
    NONE => warning ("Browser info: cannot access session index of " ^ Path.print dir)
wenzelm@42008
   247
  | SOME es => (put_entries ((remove (op =) name es) @ [name]) dir; create_index dir));
wenzelm@7727
   248
wenzelm@7727
   249
wenzelm@42004
   250
(* document variants *)
wenzelm@17082
   251
wenzelm@42004
   252
fun read_variant str =
wenzelm@17082
   253
  (case space_explode "=" str of
wenzelm@17082
   254
    [name] => (name, "")
wenzelm@17082
   255
  | [name, tags] => (name, tags)
wenzelm@42004
   256
  | _ => error ("Malformed document variant specification: " ^ quote str));
wenzelm@17082
   257
wenzelm@17082
   258
wenzelm@7727
   259
(* init session *)
wenzelm@7727
   260
wenzelm@12895
   261
fun name_of_session elems = space_implode "/" ("Isabelle" :: elems);
wenzelm@12895
   262
wenzelm@48805
   263
fun init build info info_path doc doc_graph document_output doc_variants path name
wenzelm@48516
   264
    (doc_dump as (dump_prefix, _)) (remote_path, first_time) verbose thys =
wenzelm@48516
   265
  if not build andalso not info andalso doc = "" andalso dump_prefix = "" then
skalberg@15531
   266
    (browser_info := empty_browser_info; session_info := NONE)
wenzelm@11856
   267
  else
wenzelm@11856
   268
    let
haftmann@33957
   269
      val parent_name = name_of_session (take (length path - 1) path);
wenzelm@11856
   270
      val session_name = name_of_session path;
wenzelm@11856
   271
      val sess_prefix = Path.make path;
wenzelm@48445
   272
      val html_prefix = Path.append (Path.expand (Path.explode info_path)) sess_prefix;
wenzelm@48805
   273
      val doc_output = if document_output = "" then NONE else SOME (Path.explode document_output);
wenzelm@7727
   274
wenzelm@42007
   275
      val documents =
wenzelm@42007
   276
        if doc = "" then []
wenzelm@42005
   277
        else if not (can File.check_dir document_path) then
wenzelm@44389
   278
          (if verbose then Output.physical_stderr "Warning: missing document directory\n"
wenzelm@44389
   279
           else (); [])
wenzelm@48804
   280
        else doc_variants;
wenzelm@7727
   281
wenzelm@11856
   282
      val parent_index_path = Path.append Path.parent index_path;
wenzelm@42007
   283
      val index_up_lnk =
wenzelm@42007
   284
        if first_time then
wenzelm@16263
   285
          Url.append (the remote_path) (Url.File (Path.append sess_prefix parent_index_path))
wenzelm@14898
   286
        else Url.File parent_index_path;
wenzelm@11856
   287
      val readme =
skalberg@15531
   288
        if File.exists readme_html_path then SOME readme_html_path
skalberg@15531
   289
        else if File.exists readme_path then SOME readme_path
skalberg@15531
   290
        else NONE;
wenzelm@7727
   291
wenzelm@17082
   292
      val docs =
wenzelm@17082
   293
        (case readme of NONE => [] | SOME p => [(Url.File p, "README")]) @
wenzelm@42007
   294
          map (fn (name, _) => (Url.File (Path.ext doc (Path.basic name)), name)) documents;
wenzelm@11856
   295
      val index_text = HTML.begin_index (index_up_lnk, parent_name)
wenzelm@21858
   296
        (Url.File index_path, session_name) docs (Url.explode "medium.html");
wenzelm@11856
   297
    in
wenzelm@42007
   298
      session_info :=
wenzelm@48805
   299
        SOME (make_session_info (name, parent_name, session_name, path, html_prefix, info, doc,
wenzelm@48805
   300
          doc_graph, doc_output, documents, doc_dump, remote_path, verbose, readme));
wenzelm@23899
   301
      browser_info := init_browser_info remote_path path thys;
wenzelm@24150
   302
      add_html_index (0, index_text)
wenzelm@42008
   303
    end;
wenzelm@7727
   304
wenzelm@7727
   305
wenzelm@28496
   306
(* isabelle tool wrappers *)
wenzelm@17082
   307
wenzelm@48805
   308
fun isabelle_document {verbose, purge} format name tags dir =
wenzelm@17082
   309
  let
wenzelm@48805
   310
    val s = "\"$ISABELLE_TOOL\" document" ^ (if purge then " -c" else "") ^ " -o '" ^ format ^ "' \
wenzelm@48805
   311
      \-n '" ^ name ^ "' -t '" ^ tags ^ "' " ^ File.shell_path dir ^ " 2>&1";
wenzelm@48805
   312
    val doc_path = Path.appends [dir, Path.parent, Path.basic name |> Path.ext format];
wenzelm@33682
   313
    val _ = if verbose then writeln s else ();
wenzelm@43850
   314
    val (out, rc) = Isabelle_System.bash_output s;
wenzelm@33682
   315
    val _ =
wenzelm@33682
   316
      if not (File.exists doc_path) orelse rc <> 0 then
wenzelm@33682
   317
        cat_error out ("Failed to build document " ^ quote (show_path doc_path))
wenzelm@33682
   318
      else if verbose then writeln out
wenzelm@33682
   319
      else ();
wenzelm@33682
   320
  in doc_path end;
wenzelm@17082
   321
wenzelm@42127
   322
fun isabelle_browser graph = Isabelle_System.with_tmp_dir "browser" (fn dir =>
wenzelm@17082
   323
  let
wenzelm@42127
   324
    val pdf_path = Path.append dir graph_pdf_path;
wenzelm@42127
   325
    val eps_path = Path.append dir graph_eps_path;
wenzelm@42127
   326
    val graph_path = Path.append dir graph_path;
wenzelm@49561
   327
    val _ = Graph_Display.write_graph graph_path graph;
wenzelm@42127
   328
    val args = "-o " ^ File.shell_path pdf_path ^ " " ^ File.shell_path graph_path;
wenzelm@17082
   329
  in
wenzelm@42127
   330
    if Isabelle_System.isabelle_tool "browser" args = 0 andalso
wenzelm@42127
   331
      File.exists pdf_path andalso File.exists eps_path
wenzelm@42127
   332
    then (File.read pdf_path, File.read eps_path)
wenzelm@42127
   333
    else error "Failed to prepare dependency graph"
wenzelm@42127
   334
  end);
wenzelm@17082
   335
wenzelm@17082
   336
wenzelm@11856
   337
(* finish session -- output all generated text *)
wenzelm@11856
   338
wenzelm@24150
   339
fun sorted_index index = map snd (sort (int_ord o pairself fst) (rev index));
wenzelm@24150
   340
fun index_buffer index = Buffer.add (implode (sorted_index index)) Buffer.empty;
wenzelm@24150
   341
wenzelm@14922
   342
fun write_tex src name path =
wenzelm@28027
   343
  File.write_buffer (Path.append path (tex_path name)) src;
wenzelm@14922
   344
wenzelm@14922
   345
fun write_tex_index tex_index path =
wenzelm@24150
   346
  write_tex (index_buffer tex_index |> Buffer.add Latex.tex_trailer) doc_indexN path;
wenzelm@14922
   347
wenzelm@7685
   348
wenzelm@42008
   349
fun finish () =
wenzelm@48805
   350
  session_default () (fn {name, info, html_prefix, doc_format, doc_graph, doc_output,
wenzelm@48805
   351
    documents, doc_dump = (dump_prefix, dump_mode), path, verbose, readme, ...} =>
wenzelm@7727
   352
  let
wenzelm@11856
   353
    val {theories, files, tex_index, html_index, graph} = ! browser_info;
wenzelm@11856
   354
    val thys = Symtab.dest theories;
wenzelm@9416
   355
    val parent_html_prefix = Path.append html_prefix Path.parent;
wenzelm@7727
   356
wenzelm@11856
   357
    fun finish_html (a, {html, ...}: theory_info) =
wenzelm@28027
   358
      File.write_buffer (Path.append html_prefix (html_path a)) (Buffer.add HTML.end_document html);
wenzelm@11856
   359
wenzelm@24150
   360
    val sorted_graph = sorted_index graph;
wenzelm@11856
   361
    val opt_graphs =
wenzelm@48516
   362
      if doc_graph andalso (not (null documents) orelse dump_prefix <> "") then
wenzelm@28496
   363
        SOME (isabelle_browser sorted_graph)
skalberg@15531
   364
      else NONE;
wenzelm@11856
   365
wenzelm@42007
   366
    val _ =
wenzelm@42007
   367
      if info then
wenzelm@42007
   368
       (Isabelle_System.mkdirs (Path.append html_prefix session_path);
wenzelm@42007
   369
        File.write_buffer (Path.append html_prefix pre_index_path) (index_buffer html_index);
wenzelm@42007
   370
        File.write (Path.append html_prefix session_entries_path) "";
wenzelm@42007
   371
        create_index html_prefix;
wenzelm@42007
   372
        if length path > 1 then update_index parent_html_prefix name else ();
wenzelm@42007
   373
        (case readme of NONE => () | SOME path => File.copy path html_prefix);
wenzelm@49561
   374
        Graph_Display.write_graph (Path.append html_prefix graph_path) sorted_graph;
wenzelm@42007
   375
        Isabelle_System.isabelle_tool "browser" "-b";
wenzelm@42007
   376
        File.copy (Path.explode "~~/lib/browser/GraphBrowser.jar") html_prefix;
wenzelm@42007
   377
        List.app (fn (a, txt) => File.write (Path.append html_prefix (Path.basic a)) txt)
wenzelm@42007
   378
          (HTML.applet_pages name (Url.File index_path, name));
wenzelm@43437
   379
        File.copy (Path.explode "~~/etc/isabelle.css") html_prefix;
wenzelm@42007
   380
        List.app finish_html thys;
wenzelm@42007
   381
        List.app (uncurry File.write) files;
wenzelm@44389
   382
        if verbose then Output.physical_stderr ("Browser info at " ^ show_path html_prefix ^ "\n")
wenzelm@42007
   383
        else ())
wenzelm@42007
   384
      else ();
wenzelm@42007
   385
wenzelm@48933
   386
    fun prepare_sources doc_dir doc_mode =
wenzelm@48933
   387
     (Isabelle_System.mkdirs doc_dir;
wenzelm@48933
   388
      (case doc_mode of
wenzelm@48933
   389
        Dump_all => Isabelle_System.copy_dir document_path doc_dir
wenzelm@48933
   390
      | Dump_tex_sty =>
wenzelm@48933
   391
          ignore (Isabelle_System.isabelle_tool "latex"
wenzelm@48933
   392
            ("-o sty " ^ File.shell_path (Path.append doc_dir (Path.basic "root.tex"))))
wenzelm@48933
   393
      | Dump_tex => ());
wenzelm@48933
   394
      (case opt_graphs of NONE => () | SOME (pdf, eps) =>
wenzelm@48933
   395
        (File.write (Path.append doc_dir graph_pdf_path) pdf;
wenzelm@48933
   396
          File.write (Path.append doc_dir graph_eps_path) eps));
wenzelm@48933
   397
      write_tex_index tex_index doc_dir;
wenzelm@48933
   398
      List.app (fn (a, {tex_source, ...}) => write_tex tex_source a doc_dir) thys);
wenzelm@48933
   399
wenzelm@42007
   400
    val _ =
wenzelm@48516
   401
      if dump_prefix = "" then ()
wenzelm@48516
   402
      else
wenzelm@48516
   403
        let
wenzelm@48516
   404
          val path = Path.explode dump_prefix;
wenzelm@48516
   405
          val _ = prepare_sources path dump_mode;
wenzelm@48516
   406
        in
wenzelm@48516
   407
          if verbose then
wenzelm@48516
   408
            Output.physical_stderr ("Document sources at " ^ show_path path ^ "\n")
wenzelm@48516
   409
          else ()
wenzelm@48516
   410
        end;
wenzelm@42007
   411
wenzelm@48935
   412
    fun document_job doc_prefix backdrop (name, tags) =
wenzelm@48935
   413
      let
wenzelm@48935
   414
        val _ =
wenzelm@48935
   415
          File.eq (document_path, doc_prefix) andalso
wenzelm@48935
   416
            error ("Overlap of document input and output directory " ^ Path.print doc_prefix);
wenzelm@48935
   417
        val dir = Path.append doc_prefix (Path.basic name);
wenzelm@48935
   418
        val mode = if File.eq (document_path, dir) then Dump_tex_sty else Dump_all;
wenzelm@48935
   419
        val _ = prepare_sources dir mode;
wenzelm@48935
   420
        fun inform doc =
wenzelm@48935
   421
          if verbose orelse not backdrop then
wenzelm@48935
   422
            Output.physical_stderr ("Document at " ^ show_path doc ^ "\n")
wenzelm@48935
   423
          else ();
wenzelm@48935
   424
      in
wenzelm@48935
   425
        fn () =>
wenzelm@48935
   426
          (isabelle_document {verbose = true, purge = backdrop} doc_format name tags dir, inform)
wenzelm@48935
   427
      end;
wenzelm@48935
   428
wenzelm@48935
   429
    val jobs =
wenzelm@48935
   430
      (if info orelse is_none doc_output then
wenzelm@48935
   431
        map (document_job html_prefix true) documents
wenzelm@48935
   432
       else []) @
wenzelm@48935
   433
      (case doc_output of
wenzelm@48935
   434
        NONE => []
wenzelm@48935
   435
      | SOME path => map (document_job path false) documents);
wenzelm@48935
   436
wenzelm@48935
   437
    val _ = jobs |> Par_List.map (fn job => job ()) |> List.app (op |>);
wenzelm@7727
   438
  in
wenzelm@7727
   439
    browser_info := empty_browser_info;
skalberg@15531
   440
    session_info := NONE
wenzelm@42008
   441
  end);
wenzelm@7727
   442
wenzelm@7727
   443
wenzelm@7727
   444
(* theory elements *)
wenzelm@7727
   445
wenzelm@42126
   446
fun init_theory name = session_default () (fn _ => init_theory_info name empty_theory_info);
wenzelm@7727
   447
wenzelm@27862
   448
fun theory_source name mk_text =
wenzelm@42126
   449
  session_default () (fn _ => add_html_source name (HTML.theory_source (mk_text ())));
wenzelm@7727
   450
wenzelm@9136
   451
fun theory_output name s =
wenzelm@42126
   452
  session_default () (fn _ => add_tex_source name (Latex.isabelle_file name s));
wenzelm@7727
   453
wenzelm@7727
   454
wenzelm@23899
   455
fun parent_link remote_path curr_session thy =
wenzelm@23899
   456
  let
wenzelm@23899
   457
    val {name = _, session, is_local} = get_info thy;
wenzelm@23899
   458
    val name = Context.theory_name thy;
wenzelm@23899
   459
    val link =
wenzelm@23899
   460
      if null session then NONE
wenzelm@23899
   461
      else SOME
wenzelm@23899
   462
       (if is_some remote_path andalso not is_local then
wenzelm@23899
   463
         Url.append (the remote_path) (Url.File (Path.append (Path.make session) (html_path name)))
wenzelm@23899
   464
        else Url.File (Path.append (mk_rel_path curr_session session) (html_path name)));
wenzelm@23899
   465
  in (link, name) end;
wenzelm@7727
   466
wenzelm@26323
   467
fun begin_theory update_time dir files thy =
wenzelm@42126
   468
    session_default thy (fn {name = sess_name, session, path, html_prefix, remote_path, ...} =>
wenzelm@7727
   469
  let
wenzelm@23899
   470
    val name = Context.theory_name thy;
wenzelm@23899
   471
    val parents = Theory.parents_of thy;
wenzelm@23899
   472
    val parent_specs = map (parent_link remote_path path) parents;
wenzelm@7727
   473
wenzelm@37939
   474
    val files_html = files |> map (fn (raw_path, loadit) =>
wenzelm@37939
   475
      let
wenzelm@43712
   476
        val path = File.check_file (File.full_path dir raw_path);
wenzelm@37939
   477
        val base = Path.base path;
wenzelm@37939
   478
        val base_html = html_ext base;
wenzelm@37939
   479
        val _ = add_file (Path.append html_prefix base_html,
wenzelm@37940
   480
          HTML.external_file (Url.File base) (File.read path));
wenzelm@37939
   481
      in (Url.File base_html, Url.File raw_path, loadit) end);
wenzelm@7727
   482
wenzelm@7727
   483
    fun prep_html_source (tex_source, html_source, html) =
wenzelm@7727
   484
      let
wenzelm@14898
   485
        val txt = HTML.begin_theory (Url.File index_path, session)
wenzelm@27491
   486
          name parent_specs files_html (Buffer.content html_source)
wenzelm@7727
   487
      in (tex_source, Buffer.empty, Buffer.add txt html) end;
wenzelm@7727
   488
wenzelm@9416
   489
    val entry =
wenzelm@9416
   490
     {name = name, ID = ID_of path name, dir = sess_name, unfold = true,
wenzelm@21858
   491
      path = Path.implode (html_path name),
wenzelm@49561
   492
      parents = map ID_of_thy parents,
wenzelm@49561
   493
      content = []};
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@7727
   503
wenzelm@42127
   504
(** draft document output **)
wenzelm@14922
   505
wenzelm@42127
   506
fun drafts doc_format src_paths = Isabelle_System.with_tmp_dir "drafts" (fn dir =>
wenzelm@14922
   507
  let
wenzelm@24150
   508
    fun prep_draft path i =
wenzelm@14935
   509
      let
wenzelm@14935
   510
        val base = Path.base path;
wenzelm@14972
   511
        val name =
wenzelm@24829
   512
          (case Path.implode (#1 (Path.split_ext base)) of
wenzelm@44986
   513
            "" => "DUMMY"
wenzelm@44986
   514
          | s => s)  ^ serial_string ();
wenzelm@14935
   515
      in
wenzelm@14935
   516
        if File.exists path then
wenzelm@24150
   517
          (((name, base, File.read path), (i, Latex.theory_entry name)), i + 1)
wenzelm@41944
   518
        else error ("Bad file: " ^ Path.print path)
wenzelm@14935
   519
      end;
wenzelm@24150
   520
    val (srcs, tex_index) = split_list (fst (fold_map prep_draft src_paths 0));
wenzelm@14935
   521
wenzelm@42127
   522
    val doc_path = Path.append dir document_path;
wenzelm@40743
   523
    val _ = Isabelle_System.mkdirs doc_path;
wenzelm@14922
   524
    val root_path = Path.append doc_path (Path.basic "root.tex");
wenzelm@21858
   525
    val _ = File.copy (Path.explode "~~/lib/texinputs/draft.tex") root_path;
wenzelm@40743
   526
    val _ = Isabelle_System.isabelle_tool "latex" ("-o sty " ^ File.shell_path root_path);
wenzelm@40743
   527
    val _ = Isabelle_System.isabelle_tool "latex" ("-o syms " ^ File.shell_path root_path);
wenzelm@14922
   528
wenzelm@14922
   529
    fun known name =
wenzelm@14922
   530
      let val ss = split_lines (File.read (Path.append doc_path (Path.basic name)))
wenzelm@20664
   531
      in member (op =) ss end;
wenzelm@14922
   532
    val known_syms = known "syms.lst";
wenzelm@14922
   533
    val known_ctrls = known "ctrls.lst";
wenzelm@14922
   534
skalberg@15570
   535
    val _ = srcs |> List.app (fn (name, base, txt) =>
wenzelm@14935
   536
      Symbol.explode txt
wenzelm@21858
   537
      |> Latex.symbol_source (known_syms, known_ctrls) (Path.implode base)
wenzelm@14935
   538
      |> File.write (Path.append doc_path (tex_path name)));
wenzelm@14922
   539
    val _ = write_tex_index tex_index doc_path;
wenzelm@14922
   540
wenzelm@48805
   541
    val result =
wenzelm@48805
   542
      isabelle_document {verbose = false, purge = true} doc_format documentN "" doc_path;
wenzelm@42127
   543
    val result' = Isabelle_System.create_tmp_path documentN doc_format;
wenzelm@42127
   544
    val _ = File.copy result result';
wenzelm@42127
   545
  in result' end);
wenzelm@14922
   546
wenzelm@7685
   547
end;
wenzelm@7685
   548
wenzelm@32738
   549
structure Basic_Present: BASIC_PRESENT = Present;
wenzelm@32738
   550
open Basic_Present;