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