author | wenzelm |
Sat, 24 Dec 2022 13:54:24 +0100 | |
changeset 76769 | 0438622a7b9c |
parent 76758 | 8fc7157485d8 |
child 76826 | eb3b946bdeff |
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 |
||
76677 | 112 |
/* background context */ |
113 |
||
114 |
def session_background( |
|
115 |
options: Options, |
|
116 |
session: String, |
|
117 |
dirs: List[Path] = Nil, |
|
76717 | 118 |
progress: Progress = new Progress, |
119 |
verbose: Boolean = false |
|
76677 | 120 |
): Sessions.Background = { |
76741 | 121 |
Sessions.load_structure(options + "document", dirs = dirs). |
76717 | 122 |
selection_deps(Sessions.Selection.session(session), progress = progress, verbose = verbose). |
76677 | 123 |
background(session) |
124 |
} |
|
125 |
||
126 |
||
127 |
/* document context */ |
|
73719 | 128 |
|
74840
4faf0ec33cbf
option document_comment_latex supports e.g. Dagstuhl LIPIcs;
wenzelm
parents:
74839
diff
changeset
|
129 |
val texinputs: Path = Path.explode("~~/lib/texinputs") |
4faf0ec33cbf
option document_comment_latex supports e.g. Dagstuhl LIPIcs;
wenzelm
parents:
74839
diff
changeset
|
130 |
|
73724 | 131 |
val isabelle_styles: List[Path] = |
74840
4faf0ec33cbf
option document_comment_latex supports e.g. Dagstuhl LIPIcs;
wenzelm
parents:
74839
diff
changeset
|
132 |
List("isabelle.sty", "isabellesym.sty", "pdfsetup.sty", "railsetup.sty"). |
4faf0ec33cbf
option document_comment_latex supports e.g. Dagstuhl LIPIcs;
wenzelm
parents:
74839
diff
changeset
|
133 |
map(name => texinputs + Path.basic(name)) |
73724 | 134 |
|
76769 | 135 |
def running_script(title: String): String = "echo 'Running " + quote(title) + " ...'; " |
136 |
def is_running_script(msg: String): Boolean = |
|
137 |
msg.startsWith("Running \"") && msg.endsWith("\" ...") |
|
138 |
||
76757 | 139 |
sealed case class Document_Latex(name: Document.Node.Name, body: XML.Body) { |
140 |
def content: File.Content_XML = File.content(Path.basic(tex_name(name)), body) |
|
141 |
def file_pos: String = name.path.implode_symbolic |
|
142 |
def write(latex_output: Latex.Output, dir: Path): Unit = |
|
143 |
content.output(latex_output(_, file_pos = file_pos)).write(dir) |
|
144 |
} |
|
145 |
||
73719 | 146 |
def context( |
75782
dba571dd0ba9
clarified signature: prefer Export.Session_Context over Sessions.Database_Context;
wenzelm
parents:
75748
diff
changeset
|
147 |
session_context: Export.Session_Context, |
75821
affd69bad2d4
clarified signature: support different document_session, e.g. within running PIDE session;
wenzelm
parents:
75782
diff
changeset
|
148 |
document_session: Option[Sessions.Base] = None, |
76732 | 149 |
document_selection: Document.Node.Name => Boolean = _ => true, |
75393 | 150 |
progress: Progress = new Progress |
76732 | 151 |
): Context = new Context(session_context, document_session, document_selection, progress) |
73719 | 152 |
|
153 |
final class Context private[Document_Build]( |
|
76370 | 154 |
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
|
155 |
document_session: Option[Sessions.Base], |
76732 | 156 |
document_selection: Document.Node.Name => Boolean, |
76731 | 157 |
val progress: Progress |
75393 | 158 |
) { |
75826 | 159 |
context => |
160 |
||
161 |
||
73719 | 162 |
/* session info */ |
163 |
||
75821
affd69bad2d4
clarified signature: support different document_session, e.g. within running PIDE session;
wenzelm
parents:
75782
diff
changeset
|
164 |
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
|
165 |
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
|
166 |
|
75821
affd69bad2d4
clarified signature: support different document_session, e.g. within running PIDE session;
wenzelm
parents:
75782
diff
changeset
|
167 |
def session: String = info.name |
affd69bad2d4
clarified signature: support different document_session, e.g. within running PIDE session;
wenzelm
parents:
75782
diff
changeset
|
168 |
def options: Options = info.options |
affd69bad2d4
clarified signature: support different document_session, e.g. within running PIDE session;
wenzelm
parents:
75782
diff
changeset
|
169 |
|
affd69bad2d4
clarified signature: support different document_session, e.g. within running PIDE session;
wenzelm
parents:
75782
diff
changeset
|
170 |
override def toString: String = session |
75782
dba571dd0ba9
clarified signature: prefer Export.Session_Context over Sessions.Database_Context;
wenzelm
parents:
75748
diff
changeset
|
171 |
|
75825 | 172 |
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
|
173 |
|
73743 | 174 |
def document_bibliography: Boolean = options.bool("document_bibliography") |
175 |
||
73723 | 176 |
def document_logo: Option[String] = |
177 |
options.string("document_logo") match { |
|
178 |
case "" => None |
|
179 |
case "_" => Some("") |
|
180 |
case name => Some(name) |
|
181 |
} |
|
182 |
||
73721
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
183 |
def document_build: String = options.string("document_build") |
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
184 |
|
75393 | 185 |
def get_engine(): Engine = { |
73721
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
186 |
val name = document_build |
75702 | 187 |
Classpath(jar_contents = classpath).make_services(classOf[Engine]) |
75697 | 188 |
.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
|
189 |
} |
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
190 |
|
73719 | 191 |
|
192 |
/* document content */ |
|
193 |
||
194 |
def documents: List[Document_Variant] = info.documents |
|
195 |
||
76628 | 196 |
def session_document_theories: List[Document.Node.Name] = base.proper_session_theories |
197 |
def all_document_theories: List[Document.Node.Name] = base.all_document_theories |
|
73719 | 198 |
|
76758 | 199 |
lazy val isabelle_logo: Option[File.Content] = { |
200 |
document_logo.map(logo_name => |
|
201 |
Isabelle_System.with_tmp_file("logo", ext = "pdf") { tmp_path => |
|
202 |
Logo.create_logo(logo_name, output_file = tmp_path, quiet = true) |
|
203 |
val path = Path.basic("isabelle_logo.pdf") |
|
204 |
val content = Bytes.read(tmp_path) |
|
205 |
File.content(path, content) |
|
206 |
}) |
|
207 |
} |
|
73719 | 208 |
|
75393 | 209 |
lazy val session_graph: File.Content = { |
75941 | 210 |
val path = Browser_Info.session_graph_path |
73778 | 211 |
val content = graphview.Graph_File.make_pdf(options, base.session_graph_display) |
75824 | 212 |
File.content(path, content) |
73719 | 213 |
} |
214 |
||
75393 | 215 |
lazy val session_tex: File.Content = { |
73719 | 216 |
val path = Path.basic("session.tex") |
73778 | 217 |
val content = |
73719 | 218 |
Library.terminate_lines( |
76628 | 219 |
session_document_theories.map(name => "\\input{" + tex_name(name) + "}")) |
75824 | 220 |
File.content(path, content) |
73719 | 221 |
} |
222 |
||
76758 | 223 |
lazy val document_latex: List[Document_Latex] = |
224 |
for (name <- all_document_theories) |
|
225 |
yield { |
|
226 |
val body = |
|
227 |
if (document_selection(name)) { |
|
228 |
val entry = session_context(name.theory, Export.DOCUMENT_LATEX, permissive = true) |
|
229 |
YXML.parse_body(entry.text) |
|
230 |
} |
|
231 |
else Nil |
|
232 |
Document_Latex(name, body) |
|
233 |
} |
|
73723 | 234 |
|
73719 | 235 |
|
75826 | 236 |
/* build document */ |
237 |
||
238 |
def build_document(doc: Document_Variant, verbose: Boolean = false): Document_Output = { |
|
239 |
Isabelle_System.with_tmp_dir("document") { tmp_dir => |
|
240 |
val engine = get_engine() |
|
241 |
val directory = engine.prepare_directory(context, tmp_dir, doc) |
|
242 |
engine.build_document(context, directory, verbose) |
|
243 |
} |
|
244 |
} |
|
245 |
||
246 |
||
73719 | 247 |
/* document directory */ |
248 |
||
76451
87cd8506e000
document_build engine for "lipics", with options and document_files;
wenzelm
parents:
76450
diff
changeset
|
249 |
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
|
250 |
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
|
251 |
|
75393 | 252 |
def prepare_directory( |
253 |
dir: Path, |
|
254 |
doc: Document_Variant, |
|
255 |
latex_output: Latex.Output |
|
256 |
): Directory = { |
|
76451
87cd8506e000
document_build engine for "lipics", with options and document_files;
wenzelm
parents:
76450
diff
changeset
|
257 |
val doc_dir = make_directory(dir, doc) |
73719 | 258 |
|
259 |
||
74747 | 260 |
/* actual sources: with SHA1 digest */ |
73719 | 261 |
|
73724 | 262 |
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
|
263 |
|
76450
107d8203fbd7
clarified signature: allow to change options in instances of Document_Build.Engine;
wenzelm
parents:
76370
diff
changeset
|
264 |
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
|
265 |
if (!comment_latex) { |
4faf0ec33cbf
option document_comment_latex supports e.g. Dagstuhl LIPIcs;
wenzelm
parents:
74839
diff
changeset
|
266 |
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
|
267 |
} |
4faf0ec33cbf
option document_comment_latex supports e.g. Dagstuhl LIPIcs;
wenzelm
parents:
74839
diff
changeset
|
268 |
doc.tags.sty(comment_latex).write(doc_dir) |
73724 | 269 |
|
73719 | 270 |
for ((base_dir, src) <- info.document_files) { |
271 |
Isabelle_System.copy_file_base(info.dir + base_dir, src, doc_dir) |
|
272 |
} |
|
273 |
||
274 |
session_tex.write(doc_dir) |
|
76757 | 275 |
document_latex.foreach(_.write(latex_output, doc_dir)) |
73719 | 276 |
|
277 |
val root_name1 = "root_" + doc.name |
|
278 |
val root_name = if ((doc_dir + Path.explode(root_name1).tex).is_file) root_name1 else "root" |
|
279 |
||
73723 | 280 |
val digests1 = List(doc.print, document_logo.toString, document_build).map(SHA1.digest) |
281 |
val digests2 = File.find_files(doc_dir.file, follow_links = true).map(SHA1.digest) |
|
282 |
val sources = SHA1.digest_set(digests1 ::: digests2) |
|
73719 | 283 |
|
284 |
||
74747 | 285 |
/* derived material: without SHA1 digest */ |
73723 | 286 |
|
287 |
isabelle_logo.foreach(_.write(doc_dir)) |
|
73719 | 288 |
session_graph.write(doc_dir) |
289 |
||
290 |
Directory(doc_dir, doc, root_name, sources) |
|
291 |
} |
|
292 |
||
293 |
def old_document(directory: Directory): Option[Document_Output] = |
|
294 |
for { |
|
75782
dba571dd0ba9
clarified signature: prefer Export.Session_Context over Sessions.Database_Context;
wenzelm
parents:
75748
diff
changeset
|
295 |
db <- session_context.session_db() |
dba571dd0ba9
clarified signature: prefer Export.Session_Context over Sessions.Database_Context;
wenzelm
parents:
75748
diff
changeset
|
296 |
old_doc <- read_document(db, session, directory.doc.name) |
73719 | 297 |
if old_doc.sources == directory.sources |
298 |
} |
|
73720 | 299 |
yield old_doc |
73719 | 300 |
} |
301 |
||
302 |
sealed case class Directory( |
|
303 |
doc_dir: Path, |
|
304 |
doc: Document_Variant, |
|
305 |
root_name: String, |
|
75393 | 306 |
sources: SHA1.Digest |
307 |
) { |
|
73737
6638323d2774
clarified bash scripts, with public interfaces for user-defined Document_Build.Engine;
wenzelm
parents:
73735
diff
changeset
|
308 |
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
|
309 |
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
|
310 |
|
76769 | 311 |
def conditional_script( |
312 |
ext: String, exe: String, title: String = "", after: String = "" |
|
313 |
): String = { |
|
73737
6638323d2774
clarified bash scripts, with public interfaces for user-defined Document_Build.Engine;
wenzelm
parents:
73735
diff
changeset
|
314 |
"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
|
315 |
"then\n" + |
76769 | 316 |
" " + (if (title.nonEmpty) running_script(title) else "") + |
317 |
exe + " " + root_name_script() + "\n" + |
|
73737
6638323d2774
clarified bash scripts, with public interfaces for user-defined Document_Build.Engine;
wenzelm
parents:
73735
diff
changeset
|
318 |
(if (after.isEmpty) "" else " " + after) + |
6638323d2774
clarified bash scripts, with public interfaces for user-defined Document_Build.Engine;
wenzelm
parents:
73735
diff
changeset
|
319 |
"fi\n" |
76769 | 320 |
} |
73737
6638323d2774
clarified bash scripts, with public interfaces for user-defined Document_Build.Engine;
wenzelm
parents:
73735
diff
changeset
|
321 |
|
73719 | 322 |
def log_errors(): List[String] = |
323 |
Latex.latex_errors(doc_dir, root_name) ::: |
|
324 |
Bibtex.bibtex_errors(doc_dir, root_name) |
|
325 |
||
75393 | 326 |
def make_document(log: List[String], errors: List[String]): Document_Output = { |
73719 | 327 |
val root_pdf = Path.basic(root_name).pdf |
328 |
val result_pdf = doc_dir + root_pdf |
|
329 |
||
330 |
if (errors.nonEmpty) { |
|
76734 | 331 |
val message = "Failed to build document " + quote(doc.name) |
332 |
throw new Build_Error(log, errors ::: List(message)) |
|
73719 | 333 |
} |
334 |
else if (!result_pdf.is_file) { |
|
335 |
val message = "Bad document result: expected to find " + root_pdf |
|
76734 | 336 |
throw new Build_Error(log, List(message)) |
73719 | 337 |
} |
338 |
else { |
|
339 |
val log_xz = Bytes(cat_lines(log)).compress() |
|
340 |
val pdf = Bytes.read(result_pdf) |
|
341 |
Document_Output(doc.name, sources, log_xz, pdf) |
|
342 |
} |
|
343 |
} |
|
344 |
} |
|
345 |
||
73718 | 346 |
|
73721
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
347 |
/* build engines */ |
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
348 |
|
75393 | 349 |
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
|
350 |
override def toString: String = name |
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
351 |
|
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
352 |
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
|
353 |
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
|
354 |
} |
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
355 |
|
75393 | 356 |
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
|
357 |
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
|
358 |
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
|
359 |
|
73759 | 360 |
def use_pdflatex: Boolean = false |
76769 | 361 |
def running_latex: String = running_script(if (use_pdflatex) "pdflatex" else "lualatex") |
73737
6638323d2774
clarified bash scripts, with public interfaces for user-defined Document_Build.Engine;
wenzelm
parents:
73735
diff
changeset
|
362 |
def latex_script(context: Context, directory: Directory): String = |
76769 | 363 |
running_latex + (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
|
364 |
" " + directory.root_name_script() + "\n" |
6638323d2774
clarified bash scripts, with public interfaces for user-defined Document_Build.Engine;
wenzelm
parents:
73735
diff
changeset
|
365 |
|
75393 | 366 |
def bibtex_script(context: Context, directory: Directory, latex: Boolean = false): String = { |
73743 | 367 |
val ext = if (context.document_bibliography) "aux" else "bib" |
76769 | 368 |
directory.conditional_script(ext, "$ISABELLE_BIBTEX", title = "bibtex", |
73737
6638323d2774
clarified bash scripts, with public interfaces for user-defined Document_Build.Engine;
wenzelm
parents:
73735
diff
changeset
|
369 |
after = if (latex) latex_script(context, directory) else "") |
73743 | 370 |
} |
73737
6638323d2774
clarified bash scripts, with public interfaces for user-defined Document_Build.Engine;
wenzelm
parents:
73735
diff
changeset
|
371 |
|
6638323d2774
clarified bash scripts, with public interfaces for user-defined Document_Build.Engine;
wenzelm
parents:
73735
diff
changeset
|
372 |
def makeindex_script(context: Context, directory: Directory, latex: Boolean = false): String = |
76769 | 373 |
directory.conditional_script("idx", "$ISABELLE_MAKEINDEX", title = "makeindex", |
73737
6638323d2774
clarified bash scripts, with public interfaces for user-defined Document_Build.Engine;
wenzelm
parents:
73735
diff
changeset
|
374 |
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
|
375 |
|
73759 | 376 |
def use_build_script: Boolean = false |
75393 | 377 |
def build_script(context: Context, directory: Directory): String = { |
73759 | 378 |
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
|
379 |
|
73759 | 380 |
if (!use_build_script && has_build_script) { |
73721
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
381 |
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
|
382 |
quote(context.document_build)) |
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
383 |
} |
73759 | 384 |
else if (use_build_script && !has_build_script) error("Missing document build script") |
385 |
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
|
386 |
else { |
73737
6638323d2774
clarified bash scripts, with public interfaces for user-defined Document_Build.Engine;
wenzelm
parents:
73735
diff
changeset
|
387 |
"set -e\n" + |
6638323d2774
clarified bash scripts, with public interfaces for user-defined Document_Build.Engine;
wenzelm
parents:
73735
diff
changeset
|
388 |
latex_script(context, directory) + |
6638323d2774
clarified bash scripts, with public interfaces for user-defined Document_Build.Engine;
wenzelm
parents:
73735
diff
changeset
|
389 |
bibtex_script(context, directory, latex = true) + |
73738
d701bd96e323
more robust: allow \printindex within the document;
wenzelm
parents:
73737
diff
changeset
|
390 |
makeindex_script(context, directory) + |
73737
6638323d2774
clarified bash scripts, with public interfaces for user-defined Document_Build.Engine;
wenzelm
parents:
73735
diff
changeset
|
391 |
latex_script(context, directory) + |
6638323d2774
clarified bash scripts, with public interfaces for user-defined Document_Build.Engine;
wenzelm
parents:
73735
diff
changeset
|
392 |
makeindex_script(context, directory, latex = true) |
73721
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
393 |
} |
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
394 |
} |
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
395 |
|
75393 | 396 |
def build_document( |
397 |
context: Context, |
|
398 |
directory: Directory, |
|
399 |
verbose: Boolean |
|
400 |
): Document_Output = { |
|
73721
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
401 |
val result = |
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
402 |
context.progress.bash( |
73737
6638323d2774
clarified bash scripts, with public interfaces for user-defined Document_Build.Engine;
wenzelm
parents:
73735
diff
changeset
|
403 |
build_script(context, directory), |
73721
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
404 |
cwd = directory.doc_dir.file, |
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
405 |
echo = verbose, |
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
406 |
watchdog = Time.seconds(0.5)) |
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
407 |
|
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
408 |
val log = result.out_lines ::: result.err_lines |
76734 | 409 |
val err = result.err |
410 |
||
411 |
val errors1 = directory.log_errors() |
|
412 |
val errors2 = |
|
413 |
if (result.ok) errors1 |
|
414 |
else if (err.nonEmpty) err :: errors1 |
|
415 |
else if (errors1.nonEmpty) errors1 |
|
416 |
else List("Error") |
|
417 |
directory.make_document(log, errors2) |
|
73721
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
418 |
} |
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
419 |
} |
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
420 |
|
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
421 |
class LuaLaTeX_Engine extends Bash_Engine("lualatex") |
73759 | 422 |
class PDFLaTeX_Engine extends Bash_Engine("pdflatex") { override def use_pdflatex: Boolean = true } |
423 |
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
|
424 |
|
76454
f2d17e69e520
clarified options: support lualatex as well, but prefer old pdflatex for demos;
wenzelm
parents:
76451
diff
changeset
|
425 |
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
|
426 |
def lipics_options(options: Options): Options = |
87cd8506e000
document_build engine for "lipics", with options and document_files;
wenzelm
parents:
76450
diff
changeset
|
427 |
options + "document_heading_prefix=" + "document_comment_latex" |
87cd8506e000
document_build engine for "lipics", with options and document_files;
wenzelm
parents:
76450
diff
changeset
|
428 |
|
87cd8506e000
document_build engine for "lipics", with options and document_files;
wenzelm
parents:
76450
diff
changeset
|
429 |
override def use_pdflatex: Boolean = true |
87cd8506e000
document_build engine for "lipics", with options and document_files;
wenzelm
parents:
76450
diff
changeset
|
430 |
|
87cd8506e000
document_build engine for "lipics", with options and document_files;
wenzelm
parents:
76450
diff
changeset
|
431 |
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
|
432 |
val doc_dir = context.make_directory(dir, doc) |
87cd8506e000
document_build engine for "lipics", with options and document_files;
wenzelm
parents:
76450
diff
changeset
|
433 |
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
|
434 |
|
87cd8506e000
document_build engine for "lipics", with options and document_files;
wenzelm
parents:
76450
diff
changeset
|
435 |
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
|
436 |
context.prepare_directory(dir, doc, latex_output) |
87cd8506e000
document_build engine for "lipics", with options and document_files;
wenzelm
parents:
76450
diff
changeset
|
437 |
} |
87cd8506e000
document_build engine for "lipics", with options and document_files;
wenzelm
parents:
76450
diff
changeset
|
438 |
} |
76454
f2d17e69e520
clarified options: support lualatex as well, but prefer old pdflatex for demos;
wenzelm
parents:
76451
diff
changeset
|
439 |
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
|
440 |
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
|
441 |
override def use_pdflatex: Boolean = true |
f2d17e69e520
clarified options: support lualatex as well, but prefer old pdflatex for demos;
wenzelm
parents:
76451
diff
changeset
|
442 |
} |
76451
87cd8506e000
document_build engine for "lipics", with options and document_files;
wenzelm
parents:
76450
diff
changeset
|
443 |
|
73721
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
444 |
|
73718 | 445 |
/* build documents */ |
446 |
||
447 |
def tex_name(name: Document.Node.Name): String = name.theory_base_name + ".tex" |
|
448 |
||
76734 | 449 |
class Build_Error(val log_lines: List[String], val log_errors: List[String]) |
450 |
extends Exn.User_Error(Exn.cat_message(log_errors: _*)) |
|
73718 | 451 |
|
452 |
def build_documents( |
|
73719 | 453 |
context: Context, |
73718 | 454 |
output_sources: Option[Path] = None, |
455 |
output_pdf: Option[Path] = None, |
|
75393 | 456 |
verbose: Boolean = false |
457 |
): List[Document_Output] = { |
|
73719 | 458 |
val progress = context.progress |
73721
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
459 |
val engine = context.get_engine() |
73718 | 460 |
|
461 |
val documents = |
|
73719 | 462 |
for (doc <- context.documents) |
73718 | 463 |
yield { |
75394 | 464 |
Isabelle_System.with_tmp_dir("document") { tmp_dir => |
73719 | 465 |
progress.echo("Preparing " + context.session + "/" + doc.name + " ...") |
73718 | 466 |
val start = Time.now() |
467 |
||
73721
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
468 |
output_sources.foreach(engine.prepare_directory(context, _, doc)) |
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
469 |
val directory = engine.prepare_directory(context, tmp_dir, doc) |
73718 | 470 |
|
73721
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
471 |
val document = |
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
472 |
context.old_document(directory) getOrElse |
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
473 |
engine.build_document(context, directory, verbose) |
73718 | 474 |
|
73721
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
475 |
val stop = Time.now() |
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
476 |
val timing = stop - start |
73718 | 477 |
|
73721
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
478 |
progress.echo("Finished " + context.session + "/" + doc.name + |
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
479 |
" (" + timing.message_hms + " elapsed time)") |
73718 | 480 |
|
73721
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
wenzelm
parents:
73720
diff
changeset
|
481 |
document |
75394 | 482 |
} |
73718 | 483 |
} |
484 |
||
485 |
for (dir <- output_pdf; doc <- documents) { |
|
73719 | 486 |
val path = doc.write(dir) |
73718 | 487 |
progress.echo("Document at " + path.absolute) |
488 |
} |
|
489 |
||
490 |
documents |
|
491 |
} |
|
492 |
||
493 |
||
494 |
/* Isabelle tool wrapper */ |
|
495 |
||
496 |
val isabelle_tool = |
|
75394 | 497 |
Isabelle_Tool("document", "prepare session theory document", Scala_Project.here, |
498 |
{ args => |
|
499 |
var output_sources: Option[Path] = None |
|
500 |
var output_pdf: Option[Path] = None |
|
501 |
var verbose_latex = false |
|
502 |
var dirs: List[Path] = Nil |
|
503 |
var options = Options.init() |
|
504 |
var verbose_build = false |
|
73718 | 505 |
|
75394 | 506 |
val getopts = Getopts(""" |
73718 | 507 |
Usage: isabelle document [OPTIONS] SESSION |
508 |
||
509 |
Options are: |
|
510 |
-O DIR output directory for LaTeX sources and resulting PDF |
|
511 |
-P DIR output directory for resulting PDF |
|
512 |
-S DIR output directory for LaTeX sources |
|
513 |
-V verbose latex |
|
514 |
-d DIR include session directory |
|
515 |
-o OPTION override Isabelle system OPTION (via NAME=VAL or NAME) |
|
516 |
-v verbose build |
|
517 |
||
518 |
Prepare the theory document of a session. |
|
519 |
""", |
|
75394 | 520 |
"O:" -> (arg => |
521 |
{ |
|
522 |
val dir = Path.explode(arg) |
|
523 |
output_sources = Some(dir) |
|
524 |
output_pdf = Some(dir) |
|
525 |
}), |
|
526 |
"P:" -> (arg => { output_pdf = Some(Path.explode(arg)) }), |
|
527 |
"S:" -> (arg => { output_sources = Some(Path.explode(arg)) }), |
|
528 |
"V" -> (_ => verbose_latex = true), |
|
529 |
"d:" -> (arg => dirs = dirs ::: List(Path.explode(arg))), |
|
530 |
"o:" -> (arg => options = options + arg), |
|
531 |
"v" -> (_ => verbose_build = true)) |
|
73718 | 532 |
|
75394 | 533 |
val more_args = getopts(args) |
534 |
val session = |
|
535 |
more_args match { |
|
536 |
case List(a) => a |
|
537 |
case _ => getopts.usage() |
|
538 |
} |
|
73718 | 539 |
|
75394 | 540 |
val progress = new Console_Progress(verbose = verbose_build) |
73718 | 541 |
|
75394 | 542 |
progress.interrupt_handler { |
76206
769a7cd5a16a
clarified signature: re-use store/cache from build results;
wenzelm
parents:
75941
diff
changeset
|
543 |
val build_results = |
75394 | 544 |
Build.build(options, selection = Sessions.Selection.session(session), |
545 |
dirs = dirs, progress = progress, verbose = verbose_build) |
|
76206
769a7cd5a16a
clarified signature: re-use store/cache from build results;
wenzelm
parents:
75941
diff
changeset
|
546 |
if (!build_results.ok) error("Failed to build session " + quote(session)) |
73718 | 547 |
|
75394 | 548 |
if (output_sources.isEmpty && output_pdf.isEmpty) { |
549 |
progress.echo_warning("No output directory") |
|
550 |
} |
|
73718 | 551 |
|
76731 | 552 |
val session_background = Document_Build.session_background(options, session, dirs = dirs) |
553 |
using(Export.open_session_context(build_results.store, session_background)) { |
|
76206
769a7cd5a16a
clarified signature: re-use store/cache from build results;
wenzelm
parents:
75941
diff
changeset
|
554 |
session_context => |
769a7cd5a16a
clarified signature: re-use store/cache from build results;
wenzelm
parents:
75941
diff
changeset
|
555 |
build_documents( |
769a7cd5a16a
clarified signature: re-use store/cache from build results;
wenzelm
parents:
75941
diff
changeset
|
556 |
context(session_context, progress = progress), |
769a7cd5a16a
clarified signature: re-use store/cache from build results;
wenzelm
parents:
75941
diff
changeset
|
557 |
output_sources = output_sources, output_pdf = output_pdf, |
769a7cd5a16a
clarified signature: re-use store/cache from build results;
wenzelm
parents:
75941
diff
changeset
|
558 |
verbose = verbose_latex) |
75394 | 559 |
} |
560 |
} |
|
561 |
}) |
|
73718 | 562 |
} |