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