author | wenzelm |
Fri, 03 Feb 2023 20:23:37 +0100 | |
changeset 77185 | 9dc4d9ed886f |
parent 77184 | 861777e58b77 |
child 77195 | e312c7fa3bad |
permissions | -rw-r--r-- |
76606 | 1 |
/* Title: Pure/PIDE/document_editor.scala |
2 |
Author: Makarius |
|
3 |
||
76730 | 4 |
Central resources and configuration for interactive document preparation. |
76606 | 5 |
*/ |
6 |
||
7 |
package isabelle |
|
8 |
||
9 |
||
10 |
object Document_Editor { |
|
11 |
/* document output */ |
|
12 |
||
13 |
def document_name: String = "document" |
|
14 |
def document_output_dir(): Path = Path.explode("$ISABELLE_HOME_USER/document_output") |
|
77185 | 15 |
def document_output(name: String): Path = document_output_dir() + Path.basic(name) |
16 |
||
17 |
object Meta_Data { |
|
18 |
def read(name: String = document_name): Option[Meta_Data] = { |
|
19 |
val json_path = document_output(name).json |
|
20 |
if (json_path.is_file) { |
|
21 |
val json = JSON.parse(File.read(json_path)) |
|
22 |
for { |
|
23 |
selection <- JSON.list(json, "selection", JSON.Value.String.unapply) |
|
24 |
sources <- JSON.string(json, "sources") |
|
25 |
log <- JSON.string(json, "log") |
|
26 |
pdf <- JSON.string(json, "pdf") |
|
27 |
} yield { |
|
28 |
Meta_Data(name, |
|
29 |
selection, |
|
30 |
SHA1.fake_digest(sources), |
|
31 |
SHA1.fake_digest(log), |
|
32 |
SHA1.fake_digest(pdf)) |
|
33 |
} |
|
34 |
} |
|
35 |
else None |
|
36 |
} |
|
76606 | 37 |
|
77185 | 38 |
def write( |
39 |
selection: Set[Document.Node.Name], |
|
40 |
doc: Document_Build.Document_Output, |
|
41 |
name: String = document_name |
|
42 |
): Unit = { |
|
43 |
val json = |
|
44 |
JSON.Object( |
|
45 |
"selection" -> selection.toList.map(_.theory).sorted, |
|
46 |
"sources" -> doc.sources.toString, |
|
47 |
"log" -> SHA1.digest(doc.log).toString, |
|
48 |
"pdf" -> SHA1.digest(doc.pdf).toString) |
|
49 |
File.write(document_output(name).json, JSON.Format.pretty_print(json)) |
|
50 |
} |
|
51 |
} |
|
52 |
||
53 |
sealed case class Meta_Data( |
|
54 |
name: String, |
|
55 |
selection: List[String], |
|
56 |
sources: SHA1.Digest, |
|
57 |
log: SHA1.Digest, |
|
58 |
pdf: SHA1.Digest |
|
59 |
) { |
|
60 |
def check_files(): Boolean = { |
|
61 |
val path = document_output(name) |
|
62 |
path.log.is_file && |
|
63 |
path.pdf.is_file && |
|
64 |
log == SHA1.digest(File.read(path.log)) && |
|
65 |
pdf == SHA1.digest(path.pdf) |
|
66 |
} |
|
67 |
} |
|
68 |
||
69 |
def write_document( |
|
70 |
selection: Set[Document.Node.Name], |
|
71 |
doc: Document_Build.Document_Output, |
|
72 |
name: String = document_name |
|
73 |
): Unit = { |
|
74 |
val output = document_output(name) |
|
77184 | 75 |
File.write(output.log, doc.log) |
76 |
Bytes.write(output.pdf, doc.pdf) |
|
77185 | 77 |
Meta_Data.write(selection, doc, name = name) |
77184 | 78 |
} |
79 |
||
77185 | 80 |
def view_document(name: String = document_name): Unit = { |
81 |
val path = document_output(name).pdf |
|
76606 | 82 |
if (path.is_file) Isabelle_System.pdf_viewer(path) |
83 |
} |
|
84 |
||
85 |
||
76730 | 86 |
/* configuration state */ |
76609
cc9ddf373bd2
maintain global state of document editor views, notably for is_active operation;
wenzelm
parents:
76606
diff
changeset
|
87 |
|
77144
42c3970e1ac1
clarified Document_Editor.Session: more explicit types, more robust operations;
wenzelm
parents:
76994
diff
changeset
|
88 |
sealed case class Session( |
42c3970e1ac1
clarified Document_Editor.Session: more explicit types, more robust operations;
wenzelm
parents:
76994
diff
changeset
|
89 |
background: Option[Sessions.Background], |
42c3970e1ac1
clarified Document_Editor.Session: more explicit types, more robust operations;
wenzelm
parents:
76994
diff
changeset
|
90 |
selection: Set[Document.Node.Name], |
42c3970e1ac1
clarified Document_Editor.Session: more explicit types, more robust operations;
wenzelm
parents:
76994
diff
changeset
|
91 |
snapshot: Option[Document.Snapshot] |
42c3970e1ac1
clarified Document_Editor.Session: more explicit types, more robust operations;
wenzelm
parents:
76994
diff
changeset
|
92 |
) { |
42c3970e1ac1
clarified Document_Editor.Session: more explicit types, more robust operations;
wenzelm
parents:
76994
diff
changeset
|
93 |
def is_vacuous: Boolean = background.isEmpty |
42c3970e1ac1
clarified Document_Editor.Session: more explicit types, more robust operations;
wenzelm
parents:
76994
diff
changeset
|
94 |
def is_pending: Boolean = snapshot.isEmpty |
77149
3991a35cd740
automatically build document when selected theories are finished;
wenzelm
parents:
77147
diff
changeset
|
95 |
def is_ready: Boolean = background.isDefined && snapshot.isDefined |
77144
42c3970e1ac1
clarified Document_Editor.Session: more explicit types, more robust operations;
wenzelm
parents:
76994
diff
changeset
|
96 |
|
42c3970e1ac1
clarified Document_Editor.Session: more explicit types, more robust operations;
wenzelm
parents:
76994
diff
changeset
|
97 |
def get_background: Sessions.Background = background.get |
42c3970e1ac1
clarified Document_Editor.Session: more explicit types, more robust operations;
wenzelm
parents:
76994
diff
changeset
|
98 |
def get_variant: Document_Build.Document_Variant = get_background.info.documents.head |
42c3970e1ac1
clarified Document_Editor.Session: more explicit types, more robust operations;
wenzelm
parents:
76994
diff
changeset
|
99 |
def get_snapshot: Document.Snapshot = snapshot.get |
42c3970e1ac1
clarified Document_Editor.Session: more explicit types, more robust operations;
wenzelm
parents:
76994
diff
changeset
|
100 |
} |
42c3970e1ac1
clarified Document_Editor.Session: more explicit types, more robust operations;
wenzelm
parents:
76994
diff
changeset
|
101 |
|
76609
cc9ddf373bd2
maintain global state of document editor views, notably for is_active operation;
wenzelm
parents:
76606
diff
changeset
|
102 |
sealed case class State( |
76678 | 103 |
session_background: Option[Sessions.Background] = None, |
76715
bf5ff407f32f
clarified state of document model vs. document editor selection (again, see also a9d52d02bd83);
wenzelm
parents:
76678
diff
changeset
|
104 |
selection: Set[Document.Node.Name] = Set.empty, |
76609
cc9ddf373bd2
maintain global state of document editor views, notably for is_active operation;
wenzelm
parents:
76606
diff
changeset
|
105 |
views: Set[AnyRef] = Set.empty, |
cc9ddf373bd2
maintain global state of document editor views, notably for is_active operation;
wenzelm
parents:
76606
diff
changeset
|
106 |
) { |
76678 | 107 |
def is_active: Boolean = session_background.isDefined && views.nonEmpty |
76609
cc9ddf373bd2
maintain global state of document editor views, notably for is_active operation;
wenzelm
parents:
76606
diff
changeset
|
108 |
|
76715
bf5ff407f32f
clarified state of document model vs. document editor selection (again, see also a9d52d02bd83);
wenzelm
parents:
76678
diff
changeset
|
109 |
def all_document_theories: List[Document.Node.Name] = |
bf5ff407f32f
clarified state of document model vs. document editor selection (again, see also a9d52d02bd83);
wenzelm
parents:
76678
diff
changeset
|
110 |
session_background match { |
bf5ff407f32f
clarified state of document model vs. document editor selection (again, see also a9d52d02bd83);
wenzelm
parents:
76678
diff
changeset
|
111 |
case Some(background) => background.base.all_document_theories |
bf5ff407f32f
clarified state of document model vs. document editor selection (again, see also a9d52d02bd83);
wenzelm
parents:
76678
diff
changeset
|
112 |
case None => Nil |
bf5ff407f32f
clarified state of document model vs. document editor selection (again, see also a9d52d02bd83);
wenzelm
parents:
76678
diff
changeset
|
113 |
} |
bf5ff407f32f
clarified state of document model vs. document editor selection (again, see also a9d52d02bd83);
wenzelm
parents:
76678
diff
changeset
|
114 |
|
76716
a7602257a825
clarified state document nodes for Theories_Status / Document_Dockable;
wenzelm
parents:
76715
diff
changeset
|
115 |
def active_document_theories: List[Document.Node.Name] = |
a7602257a825
clarified state document nodes for Theories_Status / Document_Dockable;
wenzelm
parents:
76715
diff
changeset
|
116 |
if (is_active) all_document_theories else Nil |
a7602257a825
clarified state document nodes for Theories_Status / Document_Dockable;
wenzelm
parents:
76715
diff
changeset
|
117 |
|
76715
bf5ff407f32f
clarified state of document model vs. document editor selection (again, see also a9d52d02bd83);
wenzelm
parents:
76678
diff
changeset
|
118 |
def select( |
bf5ff407f32f
clarified state of document model vs. document editor selection (again, see also a9d52d02bd83);
wenzelm
parents:
76678
diff
changeset
|
119 |
names: Iterable[Document.Node.Name], |
bf5ff407f32f
clarified state of document model vs. document editor selection (again, see also a9d52d02bd83);
wenzelm
parents:
76678
diff
changeset
|
120 |
set: Boolean = false, |
bf5ff407f32f
clarified state of document model vs. document editor selection (again, see also a9d52d02bd83);
wenzelm
parents:
76678
diff
changeset
|
121 |
toggle: Boolean = false |
bf5ff407f32f
clarified state of document model vs. document editor selection (again, see also a9d52d02bd83);
wenzelm
parents:
76678
diff
changeset
|
122 |
): State = { |
bf5ff407f32f
clarified state of document model vs. document editor selection (again, see also a9d52d02bd83);
wenzelm
parents:
76678
diff
changeset
|
123 |
copy(selection = |
bf5ff407f32f
clarified state of document model vs. document editor selection (again, see also a9d52d02bd83);
wenzelm
parents:
76678
diff
changeset
|
124 |
names.foldLeft(selection) { |
bf5ff407f32f
clarified state of document model vs. document editor selection (again, see also a9d52d02bd83);
wenzelm
parents:
76678
diff
changeset
|
125 |
case (sel, name) => |
bf5ff407f32f
clarified state of document model vs. document editor selection (again, see also a9d52d02bd83);
wenzelm
parents:
76678
diff
changeset
|
126 |
val b = if (toggle) !selection(name) else set |
bf5ff407f32f
clarified state of document model vs. document editor selection (again, see also a9d52d02bd83);
wenzelm
parents:
76678
diff
changeset
|
127 |
if (b) sel + name else sel - name |
bf5ff407f32f
clarified state of document model vs. document editor selection (again, see also a9d52d02bd83);
wenzelm
parents:
76678
diff
changeset
|
128 |
}) |
bf5ff407f32f
clarified state of document model vs. document editor selection (again, see also a9d52d02bd83);
wenzelm
parents:
76678
diff
changeset
|
129 |
} |
bf5ff407f32f
clarified state of document model vs. document editor selection (again, see also a9d52d02bd83);
wenzelm
parents:
76678
diff
changeset
|
130 |
|
76609
cc9ddf373bd2
maintain global state of document editor views, notably for is_active operation;
wenzelm
parents:
76606
diff
changeset
|
131 |
def register_view(id: AnyRef): State = copy(views = views + id) |
cc9ddf373bd2
maintain global state of document editor views, notably for is_active operation;
wenzelm
parents:
76606
diff
changeset
|
132 |
def unregister_view(id: AnyRef): State = copy(views = views - id) |
77144
42c3970e1ac1
clarified Document_Editor.Session: more explicit types, more robust operations;
wenzelm
parents:
76994
diff
changeset
|
133 |
|
77161
913c781ff6ba
support document preparation from already loaded theories;
wenzelm
parents:
77149
diff
changeset
|
134 |
def session(pide_session: isabelle.Session): Session = { |
77144
42c3970e1ac1
clarified Document_Editor.Session: more explicit types, more robust operations;
wenzelm
parents:
76994
diff
changeset
|
135 |
val background = session_background.filter(_.info.documents.nonEmpty) |
42c3970e1ac1
clarified Document_Editor.Session: more explicit types, more robust operations;
wenzelm
parents:
76994
diff
changeset
|
136 |
val snapshot = |
42c3970e1ac1
clarified Document_Editor.Session: more explicit types, more robust operations;
wenzelm
parents:
76994
diff
changeset
|
137 |
if (background.isEmpty) None |
42c3970e1ac1
clarified Document_Editor.Session: more explicit types, more robust operations;
wenzelm
parents:
76994
diff
changeset
|
138 |
else { |
77161
913c781ff6ba
support document preparation from already loaded theories;
wenzelm
parents:
77149
diff
changeset
|
139 |
val snapshot = pide_session.snapshot() |
913c781ff6ba
support document preparation from already loaded theories;
wenzelm
parents:
77149
diff
changeset
|
140 |
def document_ready(name: Document.Node.Name): Boolean = |
913c781ff6ba
support document preparation from already loaded theories;
wenzelm
parents:
77149
diff
changeset
|
141 |
pide_session.resources.session_base.loaded_theory(name) || |
913c781ff6ba
support document preparation from already loaded theories;
wenzelm
parents:
77149
diff
changeset
|
142 |
snapshot.node_consolidated(name) |
913c781ff6ba
support document preparation from already loaded theories;
wenzelm
parents:
77149
diff
changeset
|
143 |
if (snapshot.is_outdated || !selection.forall(document_ready)) None |
77147 | 144 |
else Some(snapshot) |
77144
42c3970e1ac1
clarified Document_Editor.Session: more explicit types, more robust operations;
wenzelm
parents:
76994
diff
changeset
|
145 |
} |
42c3970e1ac1
clarified Document_Editor.Session: more explicit types, more robust operations;
wenzelm
parents:
76994
diff
changeset
|
146 |
Session(background, selection, snapshot) |
42c3970e1ac1
clarified Document_Editor.Session: more explicit types, more robust operations;
wenzelm
parents:
76994
diff
changeset
|
147 |
} |
76609
cc9ddf373bd2
maintain global state of document editor views, notably for is_active operation;
wenzelm
parents:
76606
diff
changeset
|
148 |
} |
76606 | 149 |
} |