| author | wenzelm |
| Sun, 04 Dec 2022 14:15:12 +0100 | |
| changeset 76552 | 13fde66c7cf6 |
| parent 76454 | f2d17e69e520 |
| child 76628 | 46017d6b9bfa |
| permissions | -rw-r--r-- |
| 73718 | 1 |
/* Title: Pure/Thy/document_build.scala |
2 |
Author: Makarius |
|
3 |
||
4 |
Build theory document (PDF) from session database. |
|
5 |
*/ |
|
6 |
||
7 |
package isabelle |
|
8 |
||
9 |
||
| 75393 | 10 |
object Document_Build {
|
| 73718 | 11 |
/* document variants */ |
12 |
||
| 75393 | 13 |
abstract class Document_Name {
|
| 73718 | 14 |
def name: String |
15 |
def path: Path = Path.basic(name) |
|
16 |
||
17 |
override def toString: String = name |
|
18 |
} |
|
19 |
||
| 75393 | 20 |
object Document_Variant {
|
| 73718 | 21 |
def parse(opt: String): Document_Variant = |
22 |
Library.space_explode('=', opt) match {
|
|
| 74839 | 23 |
case List(name) => Document_Variant(name, Latex.Tags.empty) |
24 |
case List(name, tags) => Document_Variant(name, Latex.Tags(tags)) |
|
| 73718 | 25 |
case _ => error("Malformed document variant: " + quote(opt))
|
26 |
} |
|
27 |
} |
|
28 |
||
| 75393 | 29 |
sealed case class Document_Variant(name: String, tags: Latex.Tags) extends Document_Name {
|
| 74839 | 30 |
def print: String = if (tags.toString.isEmpty) name else name + "=" + tags.toString |
| 73718 | 31 |
} |
32 |
||
33 |
sealed case class Document_Input(name: String, sources: SHA1.Digest) |
|
| 75822 | 34 |
extends Document_Name { override def toString: String = name }
|
| 73718 | 35 |
|
36 |
sealed case class Document_Output(name: String, sources: SHA1.Digest, log_xz: Bytes, pdf: Bytes) |
|
| 75393 | 37 |
extends Document_Name {
|
| 75822 | 38 |
override def toString: String = name |
39 |
||
| 73718 | 40 |
def log: String = log_xz.uncompress().text |
41 |
def log_lines: List[String] = split_lines(log) |
|
42 |
||
43 |
def write(db: SQL.Database, session_name: String): Unit = |
|
44 |
write_document(db, session_name, this) |
|
| 73719 | 45 |
|
| 75393 | 46 |
def write(dir: Path): Path = {
|
| 73719 | 47 |
val path = dir + Path.basic(name).pdf |
48 |
Isabelle_System.make_directory(path.expand.dir) |
|
49 |
Bytes.write(path, pdf) |
|
50 |
path |
|
51 |
} |
|
| 73718 | 52 |
} |
53 |
||
54 |
||
55 |
/* SQL data model */ |
|
56 |
||
| 75393 | 57 |
object Data {
|
| 73718 | 58 |
val session_name = SQL.Column.string("session_name").make_primary_key
|
59 |
val name = SQL.Column.string("name").make_primary_key
|
|
60 |
val sources = SQL.Column.string("sources")
|
|
61 |
val log_xz = SQL.Column.bytes("log_xz")
|
|
62 |
val pdf = SQL.Column.bytes("pdf")
|
|
63 |
||
64 |
val table = SQL.Table("isabelle_documents", List(session_name, name, sources, log_xz, pdf))
|
|
65 |
||
66 |
def where_equal(session_name: String, name: String = ""): SQL.Source = |
|
67 |
"WHERE " + Data.session_name.equal(session_name) + |
|
68 |
(if (name == "") "" else " AND " + Data.name.equal(name)) |
|
69 |
} |
|
70 |
||
| 75393 | 71 |
def read_documents(db: SQL.Database, session_name: String): List[Document_Input] = {
|
| 73718 | 72 |
val select = Data.table.select(List(Data.name, Data.sources), Data.where_equal(session_name)) |
73 |
db.using_statement(select)(stmt => |
|
| 75394 | 74 |
stmt.execute_query().iterator({ res =>
|
| 73718 | 75 |
val name = res.string(Data.name) |
76 |
val sources = res.string(Data.sources) |
|
| 75309 | 77 |
Document_Input(name, SHA1.fake_digest(sources)) |
| 73718 | 78 |
}).toList) |
79 |
} |
|
80 |
||
| 75393 | 81 |
def read_document( |
82 |
db: SQL.Database, |
|
83 |
session_name: String, |
|
84 |
name: String |
|
85 |
): Option[Document_Output] = {
|
|
| 73718 | 86 |
val select = Data.table.select(sql = Data.where_equal(session_name, name)) |
| 75394 | 87 |
db.using_statement(select)({ stmt =>
|
| 73718 | 88 |
val res = stmt.execute_query() |
89 |
if (res.next()) {
|
|
90 |
val name = res.string(Data.name) |
|
91 |
val sources = res.string(Data.sources) |
|
92 |
val log_xz = res.bytes(Data.log_xz) |
|
93 |
val pdf = res.bytes(Data.pdf) |
|
| 75309 | 94 |
Some(Document_Output(name, SHA1.fake_digest(sources), log_xz, pdf)) |
| 73718 | 95 |
} |
96 |
else None |
|
97 |
}) |
|
98 |
} |
|
99 |
||
| 75393 | 100 |
def write_document(db: SQL.Database, session_name: String, doc: Document_Output): Unit = {
|
| 75394 | 101 |
db.using_statement(Data.table.insert()){ stmt =>
|
| 73718 | 102 |
stmt.string(1) = session_name |
103 |
stmt.string(2) = doc.name |
|
104 |
stmt.string(3) = doc.sources.toString |
|
105 |
stmt.bytes(4) = doc.log_xz |
|
106 |
stmt.bytes(5) = doc.pdf |
|
107 |
stmt.execute() |
|
| 75394 | 108 |
} |
| 73718 | 109 |
} |
110 |
||
111 |
||
| 73719 | 112 |
/* context */ |
113 |
||
|
74840
4faf0ec33cbf
option document_comment_latex supports e.g. Dagstuhl LIPIcs;
wenzelm
parents:
74839
diff
changeset
|
114 |
val texinputs: Path = Path.explode("~~/lib/texinputs")
|
|
4faf0ec33cbf
option document_comment_latex supports e.g. Dagstuhl LIPIcs;
wenzelm
parents:
74839
diff
changeset
|
115 |
|
| 73724 | 116 |
val isabelle_styles: List[Path] = |
|
74840
4faf0ec33cbf
option document_comment_latex supports e.g. Dagstuhl LIPIcs;
wenzelm
parents:
74839
diff
changeset
|
117 |
List("isabelle.sty", "isabellesym.sty", "pdfsetup.sty", "railsetup.sty").
|
|
4faf0ec33cbf
option document_comment_latex supports e.g. Dagstuhl LIPIcs;
wenzelm
parents:
74839
diff
changeset
|
118 |
map(name => texinputs + Path.basic(name)) |
| 73724 | 119 |
|
| 73719 | 120 |
def context( |
|
75782
dba571dd0ba9
clarified signature: prefer Export.Session_Context over Sessions.Database_Context;
wenzelm
parents:
75748
diff
changeset
|
121 |
session_context: Export.Session_Context, |
|
75821
affd69bad2d4
clarified signature: support different document_session, e.g. within running PIDE session;
wenzelm
parents:
75782
diff
changeset
|
122 |
document_session: Option[Sessions.Base] = None, |
| 75393 | 123 |
progress: Progress = new Progress |
|
75821
affd69bad2d4
clarified signature: support different document_session, e.g. within running PIDE session;
wenzelm
parents:
75782
diff
changeset
|
124 |
): Context = new Context(session_context, document_session, progress) |
| 73719 | 125 |
|
126 |
final class Context private[Document_Build]( |
|
| 76370 | 127 |
val session_context: Export.Session_Context, |
|
75821
affd69bad2d4
clarified signature: support different document_session, e.g. within running PIDE session;
wenzelm
parents:
75782
diff
changeset
|
128 |
document_session: Option[Sessions.Base], |
| 75393 | 129 |
val progress: Progress = new Progress |
130 |
) {
|
|
| 75826 | 131 |
context => |
132 |
||
133 |
||
| 73719 | 134 |
/* session info */ |
135 |
||
|
75821
affd69bad2d4
clarified signature: support different document_session, e.g. within running PIDE session;
wenzelm
parents:
75782
diff
changeset
|
136 |
private val base = document_session getOrElse session_context.session_base |
|
affd69bad2d4
clarified signature: support different document_session, e.g. within running PIDE session;
wenzelm
parents:
75782
diff
changeset
|
137 |
private val info = session_context.sessions_structure(base.session_name) |
|
75782
dba571dd0ba9
clarified signature: prefer Export.Session_Context over Sessions.Database_Context;
wenzelm
parents:
75748
diff
changeset
|
138 |
|
|
75821
affd69bad2d4
clarified signature: support different document_session, e.g. within running PIDE session;
wenzelm
parents:
75782
diff
changeset
|
139 |
def session: String = info.name |
|
affd69bad2d4
clarified signature: support different document_session, e.g. within running PIDE session;
wenzelm
parents:
75782
diff
changeset
|
140 |
def options: Options = info.options |
|
affd69bad2d4
clarified signature: support different document_session, e.g. within running PIDE session;
wenzelm
parents:
75782
diff
changeset
|
141 |
|
|
affd69bad2d4
clarified signature: support different document_session, e.g. within running PIDE session;
wenzelm
parents:
75782
diff
changeset
|
142 |
override def toString: String = session |
|
75782
dba571dd0ba9
clarified signature: prefer Export.Session_Context over Sessions.Database_Context;
wenzelm
parents:
75748
diff
changeset
|
143 |
|
| 75825 | 144 |
val classpath: List[File.Content] = session_context.classpath() |
|
75782
dba571dd0ba9
clarified signature: prefer Export.Session_Context over Sessions.Database_Context;
wenzelm
parents:
75748
diff
changeset
|
145 |
|
| 73743 | 146 |
def document_bibliography: Boolean = options.bool("document_bibliography")
|
147 |
||
| 73723 | 148 |
def document_logo: Option[String] = |
149 |
options.string("document_logo") match {
|
|
150 |
case "" => None |
|
151 |
case "_" => Some("")
|
|
152 |
case name => Some(name) |
|
153 |
} |
|
154 |
||
|
73721
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
155 |
def document_build: String = options.string("document_build")
|
|
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
156 |
|
| 75393 | 157 |
def get_engine(): Engine = {
|
|
73721
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
158 |
val name = document_build |
| 75702 | 159 |
Classpath(jar_contents = classpath).make_services(classOf[Engine]) |
| 75697 | 160 |
.find(_.name == name).getOrElse(error("Bad document_build engine " + quote(name)))
|
|
73721
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
161 |
} |
|
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
162 |
|
| 73719 | 163 |
|
164 |
/* document content */ |
|
165 |
||
166 |
def documents: List[Document_Variant] = info.documents |
|
167 |
||
| 75748 | 168 |
def proper_session_theories: List[Document.Node.Name] = base.proper_session_theories |
169 |
||
170 |
def document_theories: List[Document.Node.Name] = |
|
171 |
proper_session_theories ::: base.document_theories |
|
| 73719 | 172 |
|
| 74811 | 173 |
lazy val document_latex: List[File.Content_XML] = |
| 73719 | 174 |
for (name <- document_theories) |
175 |
yield {
|
|
176 |
val path = Path.basic(tex_name(name)) |
|
|
75782
dba571dd0ba9
clarified signature: prefer Export.Session_Context over Sessions.Database_Context;
wenzelm
parents:
75748
diff
changeset
|
177 |
val entry = session_context(name.theory, Export.DOCUMENT_LATEX, permissive = true) |
|
dba571dd0ba9
clarified signature: prefer Export.Session_Context over Sessions.Database_Context;
wenzelm
parents:
75748
diff
changeset
|
178 |
val content = YXML.parse_body(entry.text) |
| 75824 | 179 |
File.content(path, content) |
| 73719 | 180 |
} |
181 |
||
| 75393 | 182 |
lazy val session_graph: File.Content = {
|
| 75941 | 183 |
val path = Browser_Info.session_graph_path |
| 73778 | 184 |
val content = graphview.Graph_File.make_pdf(options, base.session_graph_display) |
| 75824 | 185 |
File.content(path, content) |
| 73719 | 186 |
} |
187 |
||
| 75393 | 188 |
lazy val session_tex: File.Content = {
|
| 73719 | 189 |
val path = Path.basic("session.tex")
|
| 73778 | 190 |
val content = |
| 73719 | 191 |
Library.terminate_lines( |
| 75748 | 192 |
base.proper_session_theories.map(name => "\\input{" + tex_name(name) + "}"))
|
| 75824 | 193 |
File.content(path, content) |
| 73719 | 194 |
} |
195 |
||
| 75393 | 196 |
lazy val isabelle_logo: Option[File.Content] = {
|
| 73723 | 197 |
document_logo.map(logo_name => |
| 75394 | 198 |
Isabelle_System.with_tmp_file("logo", ext = "pdf") { tmp_path =>
|
| 73723 | 199 |
Logo.create_logo(logo_name, output_file = tmp_path, quiet = true) |
200 |
val path = Path.basic("isabelle_logo.pdf")
|
|
| 73778 | 201 |
val content = Bytes.read(tmp_path) |
| 75824 | 202 |
File.content(path, content) |
| 75394 | 203 |
}) |
| 73723 | 204 |
} |
205 |
||
| 73719 | 206 |
|
| 75826 | 207 |
/* build document */ |
208 |
||
209 |
def build_document(doc: Document_Variant, verbose: Boolean = false): Document_Output = {
|
|
210 |
Isabelle_System.with_tmp_dir("document") { tmp_dir =>
|
|
211 |
val engine = get_engine() |
|
212 |
val directory = engine.prepare_directory(context, tmp_dir, doc) |
|
213 |
engine.build_document(context, directory, verbose) |
|
214 |
} |
|
215 |
} |
|
216 |
||
217 |
||
| 73719 | 218 |
/* document directory */ |
219 |
||
|
76451
87cd8506e000
document_build engine for "lipics", with options and document_files;
wenzelm
parents:
76450
diff
changeset
|
220 |
def make_directory(dir: Path, doc: Document_Variant): Path = |
|
87cd8506e000
document_build engine for "lipics", with options and document_files;
wenzelm
parents:
76450
diff
changeset
|
221 |
Isabelle_System.make_directory(dir + Path.basic(doc.name)) |
|
87cd8506e000
document_build engine for "lipics", with options and document_files;
wenzelm
parents:
76450
diff
changeset
|
222 |
|
| 75393 | 223 |
def prepare_directory( |
224 |
dir: Path, |
|
225 |
doc: Document_Variant, |
|
226 |
latex_output: Latex.Output |
|
227 |
): Directory = {
|
|
|
76451
87cd8506e000
document_build engine for "lipics", with options and document_files;
wenzelm
parents:
76450
diff
changeset
|
228 |
val doc_dir = make_directory(dir, doc) |
| 73719 | 229 |
|
230 |
||
| 74747 | 231 |
/* actual sources: with SHA1 digest */ |
| 73719 | 232 |
|
| 73724 | 233 |
isabelle_styles.foreach(Isabelle_System.copy_file(_, doc_dir)) |
|
74840
4faf0ec33cbf
option document_comment_latex supports e.g. Dagstuhl LIPIcs;
wenzelm
parents:
74839
diff
changeset
|
234 |
|
|
76450
107d8203fbd7
clarified signature: allow to change options in instances of Document_Build.Engine;
wenzelm
parents:
76370
diff
changeset
|
235 |
val comment_latex = latex_output.options.bool("document_comment_latex")
|
|
74840
4faf0ec33cbf
option document_comment_latex supports e.g. Dagstuhl LIPIcs;
wenzelm
parents:
74839
diff
changeset
|
236 |
if (!comment_latex) {
|
|
4faf0ec33cbf
option document_comment_latex supports e.g. Dagstuhl LIPIcs;
wenzelm
parents:
74839
diff
changeset
|
237 |
Isabelle_System.copy_file(texinputs + Path.basic("comment.sty"), doc_dir)
|
|
4faf0ec33cbf
option document_comment_latex supports e.g. Dagstuhl LIPIcs;
wenzelm
parents:
74839
diff
changeset
|
238 |
} |
|
4faf0ec33cbf
option document_comment_latex supports e.g. Dagstuhl LIPIcs;
wenzelm
parents:
74839
diff
changeset
|
239 |
doc.tags.sty(comment_latex).write(doc_dir) |
| 73724 | 240 |
|
| 73719 | 241 |
for ((base_dir, src) <- info.document_files) {
|
242 |
Isabelle_System.copy_file_base(info.dir + base_dir, src, doc_dir) |
|
243 |
} |
|
244 |
||
245 |
session_tex.write(doc_dir) |
|
|
74777
2fd0c33fe440
clarified signature: Latex.Output as parameter to Document_Build.Engine;
wenzelm
parents:
74747
diff
changeset
|
246 |
|
|
2fd0c33fe440
clarified signature: Latex.Output as parameter to Document_Build.Engine;
wenzelm
parents:
74747
diff
changeset
|
247 |
for (content <- document_latex) {
|
|
2fd0c33fe440
clarified signature: Latex.Output as parameter to Document_Build.Engine;
wenzelm
parents:
74747
diff
changeset
|
248 |
content.output(latex_output(_, file_pos = content.path.implode_symbolic)) |
|
2fd0c33fe440
clarified signature: Latex.Output as parameter to Document_Build.Engine;
wenzelm
parents:
74747
diff
changeset
|
249 |
.write(doc_dir) |
|
2fd0c33fe440
clarified signature: Latex.Output as parameter to Document_Build.Engine;
wenzelm
parents:
74747
diff
changeset
|
250 |
} |
| 73719 | 251 |
|
252 |
val root_name1 = "root_" + doc.name |
|
253 |
val root_name = if ((doc_dir + Path.explode(root_name1).tex).is_file) root_name1 else "root" |
|
254 |
||
| 73723 | 255 |
val digests1 = List(doc.print, document_logo.toString, document_build).map(SHA1.digest) |
256 |
val digests2 = File.find_files(doc_dir.file, follow_links = true).map(SHA1.digest) |
|
257 |
val sources = SHA1.digest_set(digests1 ::: digests2) |
|
| 73719 | 258 |
|
259 |
||
| 74747 | 260 |
/* derived material: without SHA1 digest */ |
| 73723 | 261 |
|
262 |
isabelle_logo.foreach(_.write(doc_dir)) |
|
| 73719 | 263 |
|
264 |
session_graph.write(doc_dir) |
|
265 |
||
266 |
Directory(doc_dir, doc, root_name, sources) |
|
267 |
} |
|
268 |
||
269 |
def old_document(directory: Directory): Option[Document_Output] = |
|
270 |
for {
|
|
|
75782
dba571dd0ba9
clarified signature: prefer Export.Session_Context over Sessions.Database_Context;
wenzelm
parents:
75748
diff
changeset
|
271 |
db <- session_context.session_db() |
|
dba571dd0ba9
clarified signature: prefer Export.Session_Context over Sessions.Database_Context;
wenzelm
parents:
75748
diff
changeset
|
272 |
old_doc <- read_document(db, session, directory.doc.name) |
| 73719 | 273 |
if old_doc.sources == directory.sources |
274 |
} |
|
| 73720 | 275 |
yield old_doc |
| 73719 | 276 |
} |
277 |
||
278 |
sealed case class Directory( |
|
279 |
doc_dir: Path, |
|
280 |
doc: Document_Variant, |
|
281 |
root_name: String, |
|
| 75393 | 282 |
sources: SHA1.Digest |
283 |
) {
|
|
|
73737
6638323d2774
clarified bash scripts, with public interfaces for user-defined Document_Build.Engine;
wenzelm
parents:
73735
diff
changeset
|
284 |
def root_name_script(ext: String = ""): String = |
|
6638323d2774
clarified bash scripts, with public interfaces for user-defined Document_Build.Engine;
wenzelm
parents:
73735
diff
changeset
|
285 |
Bash.string(if (ext.isEmpty) root_name else root_name + "." + ext) |
|
6638323d2774
clarified bash scripts, with public interfaces for user-defined Document_Build.Engine;
wenzelm
parents:
73735
diff
changeset
|
286 |
|
|
6638323d2774
clarified bash scripts, with public interfaces for user-defined Document_Build.Engine;
wenzelm
parents:
73735
diff
changeset
|
287 |
def conditional_script(ext: String, exe: String, after: String = ""): String = |
|
6638323d2774
clarified bash scripts, with public interfaces for user-defined Document_Build.Engine;
wenzelm
parents:
73735
diff
changeset
|
288 |
"if [ -f " + root_name_script(ext) + " ]\n" + |
|
6638323d2774
clarified bash scripts, with public interfaces for user-defined Document_Build.Engine;
wenzelm
parents:
73735
diff
changeset
|
289 |
"then\n" + |
|
6638323d2774
clarified bash scripts, with public interfaces for user-defined Document_Build.Engine;
wenzelm
parents:
73735
diff
changeset
|
290 |
" " + exe + " " + root_name_script() + "\n" + |
|
6638323d2774
clarified bash scripts, with public interfaces for user-defined Document_Build.Engine;
wenzelm
parents:
73735
diff
changeset
|
291 |
(if (after.isEmpty) "" else " " + after) + |
|
6638323d2774
clarified bash scripts, with public interfaces for user-defined Document_Build.Engine;
wenzelm
parents:
73735
diff
changeset
|
292 |
"fi\n" |
|
6638323d2774
clarified bash scripts, with public interfaces for user-defined Document_Build.Engine;
wenzelm
parents:
73735
diff
changeset
|
293 |
|
| 73719 | 294 |
def log_errors(): List[String] = |
295 |
Latex.latex_errors(doc_dir, root_name) ::: |
|
296 |
Bibtex.bibtex_errors(doc_dir, root_name) |
|
297 |
||
| 75393 | 298 |
def make_document(log: List[String], errors: List[String]): Document_Output = {
|
| 73719 | 299 |
val root_pdf = Path.basic(root_name).pdf |
300 |
val result_pdf = doc_dir + root_pdf |
|
301 |
||
302 |
if (errors.nonEmpty) {
|
|
303 |
val errors1 = errors ::: List("Failed to build document " + quote(doc.name))
|
|
304 |
throw new Build_Error(log, Exn.cat_message(errors1: _*)) |
|
305 |
} |
|
306 |
else if (!result_pdf.is_file) {
|
|
307 |
val message = "Bad document result: expected to find " + root_pdf |
|
308 |
throw new Build_Error(log, message) |
|
309 |
} |
|
310 |
else {
|
|
311 |
val log_xz = Bytes(cat_lines(log)).compress() |
|
312 |
val pdf = Bytes.read(result_pdf) |
|
313 |
Document_Output(doc.name, sources, log_xz, pdf) |
|
314 |
} |
|
315 |
} |
|
316 |
} |
|
317 |
||
| 73718 | 318 |
|
|
73721
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
319 |
/* build engines */ |
|
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
320 |
|
| 75393 | 321 |
abstract class Engine(val name: String) extends Isabelle_System.Service {
|
|
73721
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
322 |
override def toString: String = name |
|
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
323 |
|
|
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
324 |
def prepare_directory(context: Context, dir: Path, doc: Document_Variant): Directory |
|
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
325 |
def build_document(context: Context, directory: Directory, verbose: Boolean): Document_Output |
|
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
326 |
} |
|
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
327 |
|
| 75393 | 328 |
abstract class Bash_Engine(name: String) extends Engine(name) {
|
|
73721
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
329 |
def prepare_directory(context: Context, dir: Path, doc: Document_Variant): Directory = |
|
74824
6424f74fd9d4
Latex.Output.latex_heading depends on option document_heading_prefix, e.g. relevant for Dagstuhl LIPIcs which prefers unaliased \section etc.;
wenzelm
parents:
74811
diff
changeset
|
330 |
context.prepare_directory(dir, doc, new Latex.Output(context.options)) |
|
73721
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
331 |
|
| 73759 | 332 |
def use_pdflatex: Boolean = false |
|
73737
6638323d2774
clarified bash scripts, with public interfaces for user-defined Document_Build.Engine;
wenzelm
parents:
73735
diff
changeset
|
333 |
def latex_script(context: Context, directory: Directory): String = |
| 73759 | 334 |
(if (use_pdflatex) "$ISABELLE_PDFLATEX" else "$ISABELLE_LUALATEX") + |
|
73737
6638323d2774
clarified bash scripts, with public interfaces for user-defined Document_Build.Engine;
wenzelm
parents:
73735
diff
changeset
|
335 |
" " + directory.root_name_script() + "\n" |
|
6638323d2774
clarified bash scripts, with public interfaces for user-defined Document_Build.Engine;
wenzelm
parents:
73735
diff
changeset
|
336 |
|
| 75393 | 337 |
def bibtex_script(context: Context, directory: Directory, latex: Boolean = false): String = {
|
| 73743 | 338 |
val ext = if (context.document_bibliography) "aux" else "bib" |
339 |
directory.conditional_script(ext, "$ISABELLE_BIBTEX", |
|
|
73737
6638323d2774
clarified bash scripts, with public interfaces for user-defined Document_Build.Engine;
wenzelm
parents:
73735
diff
changeset
|
340 |
after = if (latex) latex_script(context, directory) else "") |
| 73743 | 341 |
} |
|
73737
6638323d2774
clarified bash scripts, with public interfaces for user-defined Document_Build.Engine;
wenzelm
parents:
73735
diff
changeset
|
342 |
|
|
6638323d2774
clarified bash scripts, with public interfaces for user-defined Document_Build.Engine;
wenzelm
parents:
73735
diff
changeset
|
343 |
def makeindex_script(context: Context, directory: Directory, latex: Boolean = false): String = |
|
6638323d2774
clarified bash scripts, with public interfaces for user-defined Document_Build.Engine;
wenzelm
parents:
73735
diff
changeset
|
344 |
directory.conditional_script("idx", "$ISABELLE_MAKEINDEX",
|
|
6638323d2774
clarified bash scripts, with public interfaces for user-defined Document_Build.Engine;
wenzelm
parents:
73735
diff
changeset
|
345 |
after = if (latex) latex_script(context, directory) else "") |
|
6638323d2774
clarified bash scripts, with public interfaces for user-defined Document_Build.Engine;
wenzelm
parents:
73735
diff
changeset
|
346 |
|
| 73759 | 347 |
def use_build_script: Boolean = false |
| 75393 | 348 |
def build_script(context: Context, directory: Directory): String = {
|
| 73759 | 349 |
val has_build_script = (directory.doc_dir + Path.explode("build")).is_file
|
|
73721
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
350 |
|
| 73759 | 351 |
if (!use_build_script && has_build_script) {
|
|
73721
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
352 |
error("Unexpected document build script for option document_build=" +
|
|
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
353 |
quote(context.document_build)) |
|
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
354 |
} |
| 73759 | 355 |
else if (use_build_script && !has_build_script) error("Missing document build script")
|
356 |
else if (has_build_script) "./build pdf " + Bash.string(directory.doc.name) |
|
|
73721
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
357 |
else {
|
|
73737
6638323d2774
clarified bash scripts, with public interfaces for user-defined Document_Build.Engine;
wenzelm
parents:
73735
diff
changeset
|
358 |
"set -e\n" + |
|
6638323d2774
clarified bash scripts, with public interfaces for user-defined Document_Build.Engine;
wenzelm
parents:
73735
diff
changeset
|
359 |
latex_script(context, directory) + |
|
6638323d2774
clarified bash scripts, with public interfaces for user-defined Document_Build.Engine;
wenzelm
parents:
73735
diff
changeset
|
360 |
bibtex_script(context, directory, latex = true) + |
|
73738
d701bd96e323
more robust: allow \printindex within the document;
wenzelm
parents:
73737
diff
changeset
|
361 |
makeindex_script(context, directory) + |
|
73737
6638323d2774
clarified bash scripts, with public interfaces for user-defined Document_Build.Engine;
wenzelm
parents:
73735
diff
changeset
|
362 |
latex_script(context, directory) + |
|
6638323d2774
clarified bash scripts, with public interfaces for user-defined Document_Build.Engine;
wenzelm
parents:
73735
diff
changeset
|
363 |
makeindex_script(context, directory, latex = true) |
|
73721
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
364 |
} |
|
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
365 |
} |
|
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
366 |
|
| 75393 | 367 |
def build_document( |
368 |
context: Context, |
|
369 |
directory: Directory, |
|
370 |
verbose: Boolean |
|
371 |
): Document_Output = {
|
|
|
73721
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
372 |
val result = |
|
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
373 |
context.progress.bash( |
|
73737
6638323d2774
clarified bash scripts, with public interfaces for user-defined Document_Build.Engine;
wenzelm
parents:
73735
diff
changeset
|
374 |
build_script(context, directory), |
|
73721
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
375 |
cwd = directory.doc_dir.file, |
|
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
376 |
echo = verbose, |
|
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
377 |
watchdog = Time.seconds(0.5)) |
|
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
378 |
|
|
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
379 |
val log = result.out_lines ::: result.err_lines |
|
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
380 |
val errors = (if (result.ok) Nil else List(result.err)) ::: directory.log_errors() |
|
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
381 |
directory.make_document(log, errors) |
|
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
382 |
} |
|
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
383 |
} |
|
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
384 |
|
|
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
385 |
class LuaLaTeX_Engine extends Bash_Engine("lualatex")
|
| 73759 | 386 |
class PDFLaTeX_Engine extends Bash_Engine("pdflatex") { override def use_pdflatex: Boolean = true }
|
387 |
class Build_Engine extends Bash_Engine("build") { override def use_build_script: Boolean = true }
|
|
|
73721
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
388 |
|
|
76454
f2d17e69e520
clarified options: support lualatex as well, but prefer old pdflatex for demos;
wenzelm
parents:
76451
diff
changeset
|
389 |
class LIPIcs_Engine(name: String) extends Bash_Engine(name) {
|
|
76451
87cd8506e000
document_build engine for "lipics", with options and document_files;
wenzelm
parents:
76450
diff
changeset
|
390 |
def lipics_options(options: Options): Options = |
|
87cd8506e000
document_build engine for "lipics", with options and document_files;
wenzelm
parents:
76450
diff
changeset
|
391 |
options + "document_heading_prefix=" + "document_comment_latex" |
|
87cd8506e000
document_build engine for "lipics", with options and document_files;
wenzelm
parents:
76450
diff
changeset
|
392 |
|
|
87cd8506e000
document_build engine for "lipics", with options and document_files;
wenzelm
parents:
76450
diff
changeset
|
393 |
override def use_pdflatex: Boolean = true |
|
87cd8506e000
document_build engine for "lipics", with options and document_files;
wenzelm
parents:
76450
diff
changeset
|
394 |
|
|
87cd8506e000
document_build engine for "lipics", with options and document_files;
wenzelm
parents:
76450
diff
changeset
|
395 |
override def prepare_directory(context: Context, dir: Path, doc: Document_Variant): Directory = {
|
|
87cd8506e000
document_build engine for "lipics", with options and document_files;
wenzelm
parents:
76450
diff
changeset
|
396 |
val doc_dir = context.make_directory(dir, doc) |
|
87cd8506e000
document_build engine for "lipics", with options and document_files;
wenzelm
parents:
76450
diff
changeset
|
397 |
Build_LIPIcs.document_files.foreach(Isabelle_System.copy_file(_, doc_dir)) |
|
87cd8506e000
document_build engine for "lipics", with options and document_files;
wenzelm
parents:
76450
diff
changeset
|
398 |
|
|
87cd8506e000
document_build engine for "lipics", with options and document_files;
wenzelm
parents:
76450
diff
changeset
|
399 |
val latex_output = new Latex.Output(lipics_options(context.options)) |
|
87cd8506e000
document_build engine for "lipics", with options and document_files;
wenzelm
parents:
76450
diff
changeset
|
400 |
context.prepare_directory(dir, doc, latex_output) |
|
87cd8506e000
document_build engine for "lipics", with options and document_files;
wenzelm
parents:
76450
diff
changeset
|
401 |
} |
|
87cd8506e000
document_build engine for "lipics", with options and document_files;
wenzelm
parents:
76450
diff
changeset
|
402 |
} |
|
76454
f2d17e69e520
clarified options: support lualatex as well, but prefer old pdflatex for demos;
wenzelm
parents:
76451
diff
changeset
|
403 |
class LIPIcs_LuaLaTeX_Engine extends LIPIcs_Engine("lipics")
|
|
f2d17e69e520
clarified options: support lualatex as well, but prefer old pdflatex for demos;
wenzelm
parents:
76451
diff
changeset
|
404 |
class LIPIcs_PDFLaTeX_Engine extends LIPIcs_Engine("lipics_pdflatex") {
|
|
f2d17e69e520
clarified options: support lualatex as well, but prefer old pdflatex for demos;
wenzelm
parents:
76451
diff
changeset
|
405 |
override def use_pdflatex: Boolean = true |
|
f2d17e69e520
clarified options: support lualatex as well, but prefer old pdflatex for demos;
wenzelm
parents:
76451
diff
changeset
|
406 |
} |
|
76451
87cd8506e000
document_build engine for "lipics", with options and document_files;
wenzelm
parents:
76450
diff
changeset
|
407 |
|
|
73721
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
408 |
|
| 73718 | 409 |
/* build documents */ |
410 |
||
411 |
def tex_name(name: Document.Node.Name): String = name.theory_base_name + ".tex" |
|
412 |
||
413 |
class Build_Error(val log_lines: List[String], val message: String) |
|
414 |
extends Exn.User_Error(message) |
|
415 |
||
416 |
def build_documents( |
|
| 73719 | 417 |
context: Context, |
| 73718 | 418 |
output_sources: Option[Path] = None, |
419 |
output_pdf: Option[Path] = None, |
|
| 75393 | 420 |
verbose: Boolean = false |
421 |
): List[Document_Output] = {
|
|
| 73719 | 422 |
val progress = context.progress |
|
73721
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
423 |
val engine = context.get_engine() |
| 73718 | 424 |
|
425 |
val documents = |
|
| 73719 | 426 |
for (doc <- context.documents) |
| 73718 | 427 |
yield {
|
| 75394 | 428 |
Isabelle_System.with_tmp_dir("document") { tmp_dir =>
|
| 73719 | 429 |
progress.echo("Preparing " + context.session + "/" + doc.name + " ...")
|
| 73718 | 430 |
val start = Time.now() |
431 |
||
|
73721
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
432 |
output_sources.foreach(engine.prepare_directory(context, _, doc)) |
|
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
433 |
val directory = engine.prepare_directory(context, tmp_dir, doc) |
| 73718 | 434 |
|
|
73721
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
435 |
val document = |
|
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
436 |
context.old_document(directory) getOrElse |
|
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
437 |
engine.build_document(context, directory, verbose) |
| 73718 | 438 |
|
|
73721
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
439 |
val stop = Time.now() |
|
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
440 |
val timing = stop - start |
| 73718 | 441 |
|
|
73721
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
442 |
progress.echo("Finished " + context.session + "/" + doc.name +
|
|
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
443 |
" (" + timing.message_hms + " elapsed time)")
|
| 73718 | 444 |
|
|
73721
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
445 |
document |
| 75394 | 446 |
} |
| 73718 | 447 |
} |
448 |
||
449 |
for (dir <- output_pdf; doc <- documents) {
|
|
| 73719 | 450 |
val path = doc.write(dir) |
| 73718 | 451 |
progress.echo("Document at " + path.absolute)
|
452 |
} |
|
453 |
||
454 |
documents |
|
455 |
} |
|
456 |
||
457 |
||
458 |
/* Isabelle tool wrapper */ |
|
459 |
||
460 |
val isabelle_tool = |
|
| 75394 | 461 |
Isabelle_Tool("document", "prepare session theory document", Scala_Project.here,
|
462 |
{ args =>
|
|
463 |
var output_sources: Option[Path] = None |
|
464 |
var output_pdf: Option[Path] = None |
|
465 |
var verbose_latex = false |
|
466 |
var dirs: List[Path] = Nil |
|
467 |
var options = Options.init() |
|
468 |
var verbose_build = false |
|
| 73718 | 469 |
|
| 75394 | 470 |
val getopts = Getopts("""
|
| 73718 | 471 |
Usage: isabelle document [OPTIONS] SESSION |
472 |
||
473 |
Options are: |
|
474 |
-O DIR output directory for LaTeX sources and resulting PDF |
|
475 |
-P DIR output directory for resulting PDF |
|
476 |
-S DIR output directory for LaTeX sources |
|
477 |
-V verbose latex |
|
478 |
-d DIR include session directory |
|
479 |
-o OPTION override Isabelle system OPTION (via NAME=VAL or NAME) |
|
480 |
-v verbose build |
|
481 |
||
482 |
Prepare the theory document of a session. |
|
483 |
""", |
|
| 75394 | 484 |
"O:" -> (arg => |
485 |
{
|
|
486 |
val dir = Path.explode(arg) |
|
487 |
output_sources = Some(dir) |
|
488 |
output_pdf = Some(dir) |
|
489 |
}), |
|
490 |
"P:" -> (arg => { output_pdf = Some(Path.explode(arg)) }),
|
|
491 |
"S:" -> (arg => { output_sources = Some(Path.explode(arg)) }),
|
|
492 |
"V" -> (_ => verbose_latex = true), |
|
493 |
"d:" -> (arg => dirs = dirs ::: List(Path.explode(arg))), |
|
494 |
"o:" -> (arg => options = options + arg), |
|
495 |
"v" -> (_ => verbose_build = true)) |
|
| 73718 | 496 |
|
| 75394 | 497 |
val more_args = getopts(args) |
498 |
val session = |
|
499 |
more_args match {
|
|
500 |
case List(a) => a |
|
501 |
case _ => getopts.usage() |
|
502 |
} |
|
| 73718 | 503 |
|
| 75394 | 504 |
val progress = new Console_Progress(verbose = verbose_build) |
| 73718 | 505 |
|
| 75394 | 506 |
progress.interrupt_handler {
|
|
76206
769a7cd5a16a
clarified signature: re-use store/cache from build results;
wenzelm
parents:
75941
diff
changeset
|
507 |
val build_results = |
| 75394 | 508 |
Build.build(options, selection = Sessions.Selection.session(session), |
509 |
dirs = dirs, progress = progress, verbose = verbose_build) |
|
|
76206
769a7cd5a16a
clarified signature: re-use store/cache from build results;
wenzelm
parents:
75941
diff
changeset
|
510 |
if (!build_results.ok) error("Failed to build session " + quote(session))
|
| 73718 | 511 |
|
| 75394 | 512 |
val deps = |
513 |
Sessions.load_structure(options + "document=pdf", dirs = dirs). |
|
514 |
selection_deps(Sessions.Selection.session(session)) |
|
| 73718 | 515 |
|
|
75782
dba571dd0ba9
clarified signature: prefer Export.Session_Context over Sessions.Database_Context;
wenzelm
parents:
75748
diff
changeset
|
516 |
val session_base_info = deps.base_info(session) |
|
dba571dd0ba9
clarified signature: prefer Export.Session_Context over Sessions.Database_Context;
wenzelm
parents:
75748
diff
changeset
|
517 |
|
| 75394 | 518 |
if (output_sources.isEmpty && output_pdf.isEmpty) {
|
519 |
progress.echo_warning("No output directory")
|
|
520 |
} |
|
| 73718 | 521 |
|
|
76206
769a7cd5a16a
clarified signature: re-use store/cache from build results;
wenzelm
parents:
75941
diff
changeset
|
522 |
using(Export.open_session_context(build_results.store, session_base_info)) {
|
|
769a7cd5a16a
clarified signature: re-use store/cache from build results;
wenzelm
parents:
75941
diff
changeset
|
523 |
session_context => |
|
769a7cd5a16a
clarified signature: re-use store/cache from build results;
wenzelm
parents:
75941
diff
changeset
|
524 |
build_documents( |
|
769a7cd5a16a
clarified signature: re-use store/cache from build results;
wenzelm
parents:
75941
diff
changeset
|
525 |
context(session_context, progress = progress), |
|
769a7cd5a16a
clarified signature: re-use store/cache from build results;
wenzelm
parents:
75941
diff
changeset
|
526 |
output_sources = output_sources, output_pdf = output_pdf, |
|
769a7cd5a16a
clarified signature: re-use store/cache from build results;
wenzelm
parents:
75941
diff
changeset
|
527 |
verbose = verbose_latex) |
| 75394 | 528 |
} |
529 |
} |
|
530 |
}) |
|
| 73718 | 531 |
} |