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 |
|
|
10 |
object Document_Build
|
|
11 |
{
|
|
12 |
/* document variants */
|
|
13 |
|
|
14 |
abstract class Document_Name
|
|
15 |
{
|
|
16 |
def name: String
|
|
17 |
def path: Path = Path.basic(name)
|
|
18 |
|
|
19 |
override def toString: String = name
|
|
20 |
}
|
|
21 |
|
|
22 |
object Document_Variant
|
|
23 |
{
|
|
24 |
def parse(name: String, tags: String): Document_Variant =
|
|
25 |
Document_Variant(name, Library.space_explode(',', tags))
|
|
26 |
|
|
27 |
def parse(opt: String): Document_Variant =
|
|
28 |
Library.space_explode('=', opt) match {
|
|
29 |
case List(name) => Document_Variant(name, Nil)
|
|
30 |
case List(name, tags) => parse(name, tags)
|
|
31 |
case _ => error("Malformed document variant: " + quote(opt))
|
|
32 |
}
|
|
33 |
}
|
|
34 |
|
|
35 |
sealed case class Document_Variant(name: String, tags: List[String]) extends Document_Name
|
|
36 |
{
|
|
37 |
def print_tags: String = tags.mkString(",")
|
|
38 |
def print: String = if (tags.isEmpty) name else name + "=" + print_tags
|
|
39 |
|
|
40 |
def latex_sty: String =
|
|
41 |
Library.terminate_lines(
|
|
42 |
tags.map(tag =>
|
|
43 |
tag.toList match {
|
|
44 |
case '/' :: cs => "\\isafoldtag{" + cs.mkString + "}"
|
|
45 |
case '-' :: cs => "\\isadroptag{" + cs.mkString + "}"
|
|
46 |
case '+' :: cs => "\\isakeeptag{" + cs.mkString + "}"
|
|
47 |
case cs => "\\isakeeptag{" + cs.mkString + "}"
|
|
48 |
}))
|
|
49 |
}
|
|
50 |
|
|
51 |
sealed case class Document_Input(name: String, sources: SHA1.Digest)
|
|
52 |
extends Document_Name
|
|
53 |
|
|
54 |
sealed case class Document_Output(name: String, sources: SHA1.Digest, log_xz: Bytes, pdf: Bytes)
|
|
55 |
extends Document_Name
|
|
56 |
{
|
|
57 |
def log: String = log_xz.uncompress().text
|
|
58 |
def log_lines: List[String] = split_lines(log)
|
|
59 |
|
|
60 |
def write(db: SQL.Database, session_name: String): Unit =
|
|
61 |
write_document(db, session_name, this)
|
73719
|
62 |
|
|
63 |
def write(dir: Path): Path =
|
|
64 |
{
|
|
65 |
val path = dir + Path.basic(name).pdf
|
|
66 |
Isabelle_System.make_directory(path.expand.dir)
|
|
67 |
Bytes.write(path, pdf)
|
|
68 |
path
|
|
69 |
}
|
73718
|
70 |
}
|
|
71 |
|
|
72 |
|
|
73 |
/* SQL data model */
|
|
74 |
|
|
75 |
object Data
|
|
76 |
{
|
|
77 |
val session_name = SQL.Column.string("session_name").make_primary_key
|
|
78 |
val name = SQL.Column.string("name").make_primary_key
|
|
79 |
val sources = SQL.Column.string("sources")
|
|
80 |
val log_xz = SQL.Column.bytes("log_xz")
|
|
81 |
val pdf = SQL.Column.bytes("pdf")
|
|
82 |
|
|
83 |
val table = SQL.Table("isabelle_documents", List(session_name, name, sources, log_xz, pdf))
|
|
84 |
|
|
85 |
def where_equal(session_name: String, name: String = ""): SQL.Source =
|
|
86 |
"WHERE " + Data.session_name.equal(session_name) +
|
|
87 |
(if (name == "") "" else " AND " + Data.name.equal(name))
|
|
88 |
}
|
|
89 |
|
|
90 |
def read_documents(db: SQL.Database, session_name: String): List[Document_Input] =
|
|
91 |
{
|
|
92 |
val select = Data.table.select(List(Data.name, Data.sources), Data.where_equal(session_name))
|
|
93 |
db.using_statement(select)(stmt =>
|
|
94 |
stmt.execute_query().iterator(res =>
|
|
95 |
{
|
|
96 |
val name = res.string(Data.name)
|
|
97 |
val sources = res.string(Data.sources)
|
|
98 |
Document_Input(name, SHA1.fake(sources))
|
|
99 |
}).toList)
|
|
100 |
}
|
|
101 |
|
|
102 |
def read_document(db: SQL.Database, session_name: String, name: String): Option[Document_Output] =
|
|
103 |
{
|
|
104 |
val select = Data.table.select(sql = Data.where_equal(session_name, name))
|
|
105 |
db.using_statement(select)(stmt =>
|
|
106 |
{
|
|
107 |
val res = stmt.execute_query()
|
|
108 |
if (res.next()) {
|
|
109 |
val name = res.string(Data.name)
|
|
110 |
val sources = res.string(Data.sources)
|
|
111 |
val log_xz = res.bytes(Data.log_xz)
|
|
112 |
val pdf = res.bytes(Data.pdf)
|
|
113 |
Some(Document_Output(name, SHA1.fake(sources), log_xz, pdf))
|
|
114 |
}
|
|
115 |
else None
|
|
116 |
})
|
|
117 |
}
|
|
118 |
|
|
119 |
def write_document(db: SQL.Database, session_name: String, doc: Document_Output): Unit =
|
|
120 |
{
|
|
121 |
db.using_statement(Data.table.insert())(stmt =>
|
|
122 |
{
|
|
123 |
stmt.string(1) = session_name
|
|
124 |
stmt.string(2) = doc.name
|
|
125 |
stmt.string(3) = doc.sources.toString
|
|
126 |
stmt.bytes(4) = doc.log_xz
|
|
127 |
stmt.bytes(5) = doc.pdf
|
|
128 |
stmt.execute()
|
|
129 |
})
|
|
130 |
}
|
|
131 |
|
|
132 |
|
73719
|
133 |
/* context */
|
|
134 |
|
|
135 |
def context(
|
|
136 |
session: String,
|
|
137 |
deps: Sessions.Deps,
|
|
138 |
db_context: Sessions.Database_Context,
|
|
139 |
progress: Progress = new Progress): Context =
|
|
140 |
{
|
|
141 |
val info = deps.sessions_structure(session)
|
|
142 |
val base = deps(session)
|
|
143 |
val hierarchy = deps.sessions_structure.hierarchy(session)
|
|
144 |
new Context(info, base, hierarchy, db_context, progress)
|
|
145 |
}
|
|
146 |
|
|
147 |
final class Context private[Document_Build](
|
|
148 |
info: Sessions.Info,
|
|
149 |
base: Sessions.Base,
|
|
150 |
hierarchy: List[String],
|
|
151 |
db_context: Sessions.Database_Context,
|
|
152 |
val progress: Progress = new Progress)
|
|
153 |
{
|
|
154 |
/* session info */
|
|
155 |
|
|
156 |
def session: String = info.name
|
|
157 |
def options: Options = info.options
|
|
158 |
|
|
159 |
def get_export(theory: String, name: String): Export.Entry =
|
|
160 |
db_context.get_export(hierarchy, theory, name)
|
|
161 |
|
|
162 |
|
|
163 |
/* document content */
|
|
164 |
|
|
165 |
def documents: List[Document_Variant] = info.documents
|
|
166 |
|
|
167 |
def session_theories: List[Document.Node.Name] = base.session_theories
|
|
168 |
def document_theories: List[Document.Node.Name] = session_theories ::: base.document_theories
|
|
169 |
|
|
170 |
lazy val tex_files: List[Content] =
|
|
171 |
for (name <- document_theories)
|
|
172 |
yield {
|
|
173 |
val path = Path.basic(tex_name(name))
|
|
174 |
val bytes = get_export(name.theory, document_tex_name(name)).uncompressed
|
|
175 |
Content(path, bytes)
|
|
176 |
}
|
|
177 |
|
|
178 |
lazy val session_graph: Content =
|
|
179 |
{
|
|
180 |
val path = Presentation.session_graph_path
|
|
181 |
val bytes = graphview.Graph_File.make_pdf(options, base.session_graph_display)
|
|
182 |
Content(path, bytes)
|
|
183 |
}
|
|
184 |
|
|
185 |
lazy val session_tex: Content =
|
|
186 |
{
|
|
187 |
val path = Path.basic("session.tex")
|
|
188 |
val text =
|
|
189 |
Library.terminate_lines(
|
|
190 |
base.session_theories.map(name => "\\input{" + tex_name(name) + "}"))
|
|
191 |
Content(path, Bytes(text))
|
|
192 |
}
|
|
193 |
|
|
194 |
|
|
195 |
/* document directory */
|
|
196 |
|
|
197 |
def prepare_directory(dir: Path, doc: Document_Variant): Directory =
|
|
198 |
{
|
|
199 |
val doc_dir = dir + Path.basic(doc.name)
|
|
200 |
Isabelle_System.make_directory(doc_dir)
|
|
201 |
|
|
202 |
|
|
203 |
/* sources */
|
|
204 |
|
|
205 |
Isabelle_System.bash("isabelle latex -o sty", cwd = doc_dir.file).check
|
|
206 |
File.write(doc_dir + Path.explode("isabelletags.sty"), doc.latex_sty)
|
|
207 |
for ((base_dir, src) <- info.document_files) {
|
|
208 |
Isabelle_System.copy_file_base(info.dir + base_dir, src, doc_dir)
|
|
209 |
}
|
|
210 |
|
|
211 |
session_tex.write(doc_dir)
|
|
212 |
tex_files.foreach(_.write(doc_dir))
|
|
213 |
|
|
214 |
val root_name1 = "root_" + doc.name
|
|
215 |
val root_name = if ((doc_dir + Path.explode(root_name1).tex).is_file) root_name1 else "root"
|
|
216 |
|
|
217 |
val sources =
|
|
218 |
SHA1.digest_set(File.find_files(doc_dir.file, follow_links = true).map(SHA1.digest))
|
|
219 |
|
|
220 |
|
|
221 |
/* derived material */
|
|
222 |
|
|
223 |
session_graph.write(doc_dir)
|
|
224 |
|
|
225 |
Directory(doc_dir, doc, root_name, sources)
|
|
226 |
}
|
|
227 |
|
|
228 |
def old_document(directory: Directory): Option[Document_Output] =
|
|
229 |
for {
|
|
230 |
old_doc <- db_context.input_database(session)(read_document(_, _, directory.doc.name))
|
|
231 |
if old_doc.sources == directory.sources
|
|
232 |
}
|
73720
|
233 |
yield old_doc
|
73719
|
234 |
}
|
|
235 |
|
|
236 |
sealed case class Content(path: Path, bytes: Bytes)
|
|
237 |
{
|
|
238 |
def write(dir: Path): Unit = Bytes.write(dir + path, bytes)
|
|
239 |
}
|
|
240 |
|
|
241 |
sealed case class Directory(
|
|
242 |
doc_dir: Path,
|
|
243 |
doc: Document_Variant,
|
|
244 |
root_name: String,
|
|
245 |
sources: SHA1.Digest)
|
|
246 |
{
|
|
247 |
def log_errors(): List[String] =
|
|
248 |
Latex.latex_errors(doc_dir, root_name) :::
|
|
249 |
Bibtex.bibtex_errors(doc_dir, root_name)
|
|
250 |
|
|
251 |
def make_document(log: List[String], errors: List[String]): Document_Output =
|
|
252 |
{
|
|
253 |
val root_pdf = Path.basic(root_name).pdf
|
|
254 |
val result_pdf = doc_dir + root_pdf
|
|
255 |
|
|
256 |
if (errors.nonEmpty) {
|
|
257 |
val errors1 = errors ::: List("Failed to build document " + quote(doc.name))
|
|
258 |
throw new Build_Error(log, Exn.cat_message(errors1: _*))
|
|
259 |
}
|
|
260 |
else if (!result_pdf.is_file) {
|
|
261 |
val message = "Bad document result: expected to find " + root_pdf
|
|
262 |
throw new Build_Error(log, message)
|
|
263 |
}
|
|
264 |
else {
|
|
265 |
val log_xz = Bytes(cat_lines(log)).compress()
|
|
266 |
val pdf = Bytes.read(result_pdf)
|
|
267 |
Document_Output(doc.name, sources, log_xz, pdf)
|
|
268 |
}
|
|
269 |
}
|
|
270 |
}
|
|
271 |
|
73718
|
272 |
|
|
273 |
/* build documents */
|
|
274 |
|
|
275 |
def tex_name(name: Document.Node.Name): String = name.theory_base_name + ".tex"
|
|
276 |
def document_tex_name(name: Document.Node.Name): String = "document/" + tex_name(name)
|
|
277 |
|
|
278 |
class Build_Error(val log_lines: List[String], val message: String)
|
|
279 |
extends Exn.User_Error(message)
|
|
280 |
|
|
281 |
def build_documents(
|
73719
|
282 |
context: Context,
|
73718
|
283 |
output_sources: Option[Path] = None,
|
|
284 |
output_pdf: Option[Path] = None,
|
|
285 |
verbose: Boolean = false,
|
|
286 |
verbose_latex: Boolean = false): List[Document_Output] =
|
|
287 |
{
|
73719
|
288 |
val progress = context.progress
|
73718
|
289 |
|
|
290 |
val documents =
|
73719
|
291 |
for (doc <- context.documents)
|
73718
|
292 |
yield {
|
|
293 |
Isabelle_System.with_tmp_dir("document")(tmp_dir =>
|
|
294 |
{
|
73719
|
295 |
progress.echo("Preparing " + context.session + "/" + doc.name + " ...")
|
73718
|
296 |
val start = Time.now()
|
|
297 |
|
|
298 |
|
73719
|
299 |
// prepare directory
|
73718
|
300 |
|
73719
|
301 |
output_sources.foreach(context.prepare_directory(_, doc))
|
|
302 |
val directory = context.prepare_directory(tmp_dir, doc)
|
|
303 |
val doc_dir = directory.doc_dir
|
|
304 |
val root = directory.root_name
|
73718
|
305 |
|
|
306 |
|
73719
|
307 |
// prepare document
|
73718
|
308 |
|
73719
|
309 |
context.old_document(directory) getOrElse {
|
73718
|
310 |
// bash scripts
|
|
311 |
|
|
312 |
def root_bash(ext: String): String = Bash.string(root + "." + ext)
|
|
313 |
|
|
314 |
def latex_bash(fmt: String = "pdf", ext: String = "tex"): String =
|
|
315 |
"isabelle latex -o " + Bash.string(fmt) + " " + Bash.string(root + "." + ext)
|
|
316 |
|
|
317 |
def bash(items: String*): Process_Result =
|
|
318 |
progress.bash(items.mkString(" && "), cwd = doc_dir.file,
|
|
319 |
echo = verbose_latex, watchdog = Time.seconds(0.5))
|
|
320 |
|
|
321 |
|
|
322 |
// prepare document
|
|
323 |
|
|
324 |
val result =
|
|
325 |
if ((doc_dir + Path.explode("build")).is_file) {
|
|
326 |
bash("./build pdf " + Bash.string(doc.name))
|
|
327 |
}
|
|
328 |
else {
|
|
329 |
bash(
|
|
330 |
latex_bash(),
|
|
331 |
"{ [ ! -f " + root_bash("bib") + " ] || " + latex_bash("bbl") + "; }",
|
|
332 |
"{ [ ! -f " + root_bash("idx") + " ] || " + latex_bash("idx") + "; }",
|
|
333 |
latex_bash(),
|
|
334 |
latex_bash())
|
|
335 |
}
|
|
336 |
|
73719
|
337 |
val stop = Time.now()
|
|
338 |
val timing = stop - start
|
|
339 |
|
73718
|
340 |
|
|
341 |
// result
|
|
342 |
|
73719
|
343 |
val log = result.out_lines ::: result.err_lines
|
|
344 |
val errors = (if (result.ok) Nil else List(result.err)) ::: directory.log_errors()
|
|
345 |
val document = directory.make_document(log, errors)
|
73718
|
346 |
|
73719
|
347 |
progress.echo("Finished " + context.session + "/" + doc.name +
|
|
348 |
" (" + timing.message_hms + " elapsed time)")
|
73718
|
349 |
|
73719
|
350 |
document
|
73718
|
351 |
}
|
|
352 |
})
|
|
353 |
}
|
|
354 |
|
|
355 |
for (dir <- output_pdf; doc <- documents) {
|
73719
|
356 |
val path = doc.write(dir)
|
73718
|
357 |
progress.echo("Document at " + path.absolute)
|
|
358 |
}
|
|
359 |
|
|
360 |
documents
|
|
361 |
}
|
|
362 |
|
|
363 |
|
|
364 |
/* Isabelle tool wrapper */
|
|
365 |
|
|
366 |
val isabelle_tool =
|
|
367 |
Isabelle_Tool("document", "prepare session theory document", Scala_Project.here, args =>
|
|
368 |
{
|
|
369 |
var output_sources: Option[Path] = None
|
|
370 |
var output_pdf: Option[Path] = None
|
|
371 |
var verbose_latex = false
|
|
372 |
var dirs: List[Path] = Nil
|
|
373 |
var options = Options.init()
|
|
374 |
var verbose_build = false
|
|
375 |
|
|
376 |
val getopts = Getopts(
|
|
377 |
"""
|
|
378 |
Usage: isabelle document [OPTIONS] SESSION
|
|
379 |
|
|
380 |
Options are:
|
|
381 |
-O DIR output directory for LaTeX sources and resulting PDF
|
|
382 |
-P DIR output directory for resulting PDF
|
|
383 |
-S DIR output directory for LaTeX sources
|
|
384 |
-V verbose latex
|
|
385 |
-d DIR include session directory
|
|
386 |
-o OPTION override Isabelle system OPTION (via NAME=VAL or NAME)
|
|
387 |
-v verbose build
|
|
388 |
|
|
389 |
Prepare the theory document of a session.
|
|
390 |
""",
|
|
391 |
"O:" -> (arg =>
|
|
392 |
{
|
|
393 |
val dir = Path.explode(arg)
|
|
394 |
output_sources = Some(dir)
|
|
395 |
output_pdf = Some(dir)
|
|
396 |
}),
|
|
397 |
"P:" -> (arg => { output_pdf = Some(Path.explode(arg)) }),
|
|
398 |
"S:" -> (arg => { output_sources = Some(Path.explode(arg)) }),
|
|
399 |
"V" -> (_ => verbose_latex = true),
|
|
400 |
"d:" -> (arg => dirs = dirs ::: List(Path.explode(arg))),
|
|
401 |
"o:" -> (arg => options = options + arg),
|
|
402 |
"v" -> (_ => verbose_build = true))
|
|
403 |
|
|
404 |
val more_args = getopts(args)
|
|
405 |
val session =
|
|
406 |
more_args match {
|
|
407 |
case List(a) => a
|
|
408 |
case _ => getopts.usage()
|
|
409 |
}
|
|
410 |
|
|
411 |
val progress = new Console_Progress(verbose = verbose_build)
|
|
412 |
val store = Sessions.store(options)
|
|
413 |
|
|
414 |
progress.interrupt_handler {
|
|
415 |
val res =
|
|
416 |
Build.build(options, selection = Sessions.Selection.session(session),
|
|
417 |
dirs = dirs, progress = progress, verbose = verbose_build)
|
|
418 |
if (!res.ok) error("Failed to build session " + quote(session))
|
|
419 |
|
|
420 |
val deps =
|
|
421 |
Sessions.load_structure(options + "document=pdf", dirs = dirs).
|
|
422 |
selection_deps(Sessions.Selection.session(session))
|
|
423 |
|
|
424 |
if (output_sources.isEmpty && output_pdf.isEmpty) {
|
|
425 |
progress.echo_warning("No output directory")
|
|
426 |
}
|
|
427 |
|
|
428 |
using(store.open_database_context())(db_context =>
|
73719
|
429 |
{
|
|
430 |
build_documents(context(session, deps, db_context, progress = progress),
|
73718
|
431 |
output_sources = output_sources, output_pdf = output_pdf,
|
73719
|
432 |
verbose = true, verbose_latex = verbose_latex)
|
|
433 |
})
|
73718
|
434 |
}
|
|
435 |
})
|
|
436 |
}
|