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