| author | wenzelm | 
| Sun, 31 Dec 2023 12:33:13 +0100 | |
| changeset 79403 | 254b062ec54d | 
| parent 78396 | 7853d9072d1b | 
| child 79844 | ac40138234ce | 
| 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 = | 
| 77218 | 22 |       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 | ||
| 77210 
1ffee8893b12
prefer explicit shasum: more robust due to explicit file names, which often work implicitly in LaTeX;
 wenzelm parents: 
77197diff
changeset | 33 | sealed case class Document_Input(name: String, sources: SHA1.Shasum) | 
| 75822 | 34 |   extends Document_Name { override def toString: String = name }
 | 
| 73718 | 35 | |
| 77210 
1ffee8893b12
prefer explicit shasum: more robust due to explicit file names, which often work implicitly in LaTeX;
 wenzelm parents: 
77197diff
changeset | 36 | sealed case class Document_Output(name: String, sources: SHA1.Shasum, 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 | ||
| 78396 
7853d9072d1b
renamed object Data to private_data, to emphasize its intended scope (but it is publicly accessible in the database);
 wenzelm parents: 
78356diff
changeset | 57 |   object private_data extends SQL.Data("isabelle_documents") {
 | 
| 78260 
0a7f7abbe4f0
more robust transaction_lock: avoid overlapping data spaces;
 wenzelm parents: 
78153diff
changeset | 58 | override lazy val tables = SQL.Tables(Base.table) | 
| 73718 | 59 | |
| 78260 
0a7f7abbe4f0
more robust transaction_lock: avoid overlapping data spaces;
 wenzelm parents: 
78153diff
changeset | 60 |     object Base {
 | 
| 
0a7f7abbe4f0
more robust transaction_lock: avoid overlapping data spaces;
 wenzelm parents: 
78153diff
changeset | 61 |       val session_name = SQL.Column.string("session_name").make_primary_key
 | 
| 
0a7f7abbe4f0
more robust transaction_lock: avoid overlapping data spaces;
 wenzelm parents: 
78153diff
changeset | 62 |       val name = SQL.Column.string("name").make_primary_key
 | 
| 
0a7f7abbe4f0
more robust transaction_lock: avoid overlapping data spaces;
 wenzelm parents: 
78153diff
changeset | 63 |       val sources = SQL.Column.string("sources")
 | 
| 
0a7f7abbe4f0
more robust transaction_lock: avoid overlapping data spaces;
 wenzelm parents: 
78153diff
changeset | 64 |       val log_xz = SQL.Column.bytes("log_xz")
 | 
| 
0a7f7abbe4f0
more robust transaction_lock: avoid overlapping data spaces;
 wenzelm parents: 
78153diff
changeset | 65 |       val pdf = SQL.Column.bytes("pdf")
 | 
| 
0a7f7abbe4f0
more robust transaction_lock: avoid overlapping data spaces;
 wenzelm parents: 
78153diff
changeset | 66 | |
| 78266 | 67 | val table = make_table(List(session_name, name, sources, log_xz, pdf)) | 
| 78260 
0a7f7abbe4f0
more robust transaction_lock: avoid overlapping data spaces;
 wenzelm parents: 
78153diff
changeset | 68 | } | 
| 73718 | 69 | |
| 70 | def where_equal(session_name: String, name: String = ""): SQL.Source = | |
| 78153 | 71 | SQL.where_and( | 
| 78260 
0a7f7abbe4f0
more robust transaction_lock: avoid overlapping data spaces;
 wenzelm parents: 
78153diff
changeset | 72 | Base.session_name.equal(session_name), | 
| 
0a7f7abbe4f0
more robust transaction_lock: avoid overlapping data spaces;
 wenzelm parents: 
78153diff
changeset | 73 | if_proper(name, Base.name.equal(name))) | 
| 
0a7f7abbe4f0
more robust transaction_lock: avoid overlapping data spaces;
 wenzelm parents: 
78153diff
changeset | 74 | |
| 
0a7f7abbe4f0
more robust transaction_lock: avoid overlapping data spaces;
 wenzelm parents: 
78153diff
changeset | 75 | def clean_session(db: SQL.Database, session_name: String): Unit = | 
| 
0a7f7abbe4f0
more robust transaction_lock: avoid overlapping data spaces;
 wenzelm parents: 
78153diff
changeset | 76 | db.execute_statement(Base.table.delete(sql = Base.session_name.where_equal(session_name))) | 
| 
0a7f7abbe4f0
more robust transaction_lock: avoid overlapping data spaces;
 wenzelm parents: 
78153diff
changeset | 77 | |
| 
0a7f7abbe4f0
more robust transaction_lock: avoid overlapping data spaces;
 wenzelm parents: 
78153diff
changeset | 78 | def read_document( | 
| 
0a7f7abbe4f0
more robust transaction_lock: avoid overlapping data spaces;
 wenzelm parents: 
78153diff
changeset | 79 | db: SQL.Database, | 
| 
0a7f7abbe4f0
more robust transaction_lock: avoid overlapping data spaces;
 wenzelm parents: 
78153diff
changeset | 80 | session_name: String, | 
| 
0a7f7abbe4f0
more robust transaction_lock: avoid overlapping data spaces;
 wenzelm parents: 
78153diff
changeset | 81 | name: String | 
| 
0a7f7abbe4f0
more robust transaction_lock: avoid overlapping data spaces;
 wenzelm parents: 
78153diff
changeset | 82 |     ): Option[Document_Output] = {
 | 
| 
0a7f7abbe4f0
more robust transaction_lock: avoid overlapping data spaces;
 wenzelm parents: 
78153diff
changeset | 83 | db.execute_query_statementO[Document_Output]( | 
| 
0a7f7abbe4f0
more robust transaction_lock: avoid overlapping data spaces;
 wenzelm parents: 
78153diff
changeset | 84 | Base.table.select(sql = where_equal(session_name, name = name)), | 
| 
0a7f7abbe4f0
more robust transaction_lock: avoid overlapping data spaces;
 wenzelm parents: 
78153diff
changeset | 85 |         { res =>
 | 
| 
0a7f7abbe4f0
more robust transaction_lock: avoid overlapping data spaces;
 wenzelm parents: 
78153diff
changeset | 86 | val name = res.string(Base.name) | 
| 
0a7f7abbe4f0
more robust transaction_lock: avoid overlapping data spaces;
 wenzelm parents: 
78153diff
changeset | 87 | val sources = res.string(Base.sources) | 
| 
0a7f7abbe4f0
more robust transaction_lock: avoid overlapping data spaces;
 wenzelm parents: 
78153diff
changeset | 88 | val log_xz = res.bytes(Base.log_xz) | 
| 
0a7f7abbe4f0
more robust transaction_lock: avoid overlapping data spaces;
 wenzelm parents: 
78153diff
changeset | 89 | val pdf = res.bytes(Base.pdf) | 
| 
0a7f7abbe4f0
more robust transaction_lock: avoid overlapping data spaces;
 wenzelm parents: 
78153diff
changeset | 90 | Document_Output(name, SHA1.fake_shasum(sources), log_xz, pdf) | 
| 
0a7f7abbe4f0
more robust transaction_lock: avoid overlapping data spaces;
 wenzelm parents: 
78153diff
changeset | 91 | } | 
| 
0a7f7abbe4f0
more robust transaction_lock: avoid overlapping data spaces;
 wenzelm parents: 
78153diff
changeset | 92 | ) | 
| 
0a7f7abbe4f0
more robust transaction_lock: avoid overlapping data spaces;
 wenzelm parents: 
78153diff
changeset | 93 | } | 
| 
0a7f7abbe4f0
more robust transaction_lock: avoid overlapping data spaces;
 wenzelm parents: 
78153diff
changeset | 94 | |
| 
0a7f7abbe4f0
more robust transaction_lock: avoid overlapping data spaces;
 wenzelm parents: 
78153diff
changeset | 95 | def write_document(db: SQL.Database, session_name: String, doc: Document_Output): Unit = | 
| 
0a7f7abbe4f0
more robust transaction_lock: avoid overlapping data spaces;
 wenzelm parents: 
78153diff
changeset | 96 | db.execute_statement(Base.table.insert(), body = | 
| 
0a7f7abbe4f0
more robust transaction_lock: avoid overlapping data spaces;
 wenzelm parents: 
78153diff
changeset | 97 |         { stmt =>
 | 
| 
0a7f7abbe4f0
more robust transaction_lock: avoid overlapping data spaces;
 wenzelm parents: 
78153diff
changeset | 98 | stmt.string(1) = session_name | 
| 
0a7f7abbe4f0
more robust transaction_lock: avoid overlapping data spaces;
 wenzelm parents: 
78153diff
changeset | 99 | stmt.string(2) = doc.name | 
| 
0a7f7abbe4f0
more robust transaction_lock: avoid overlapping data spaces;
 wenzelm parents: 
78153diff
changeset | 100 | stmt.string(3) = doc.sources.toString | 
| 
0a7f7abbe4f0
more robust transaction_lock: avoid overlapping data spaces;
 wenzelm parents: 
78153diff
changeset | 101 | stmt.bytes(4) = doc.log_xz | 
| 
0a7f7abbe4f0
more robust transaction_lock: avoid overlapping data spaces;
 wenzelm parents: 
78153diff
changeset | 102 | stmt.bytes(5) = doc.pdf | 
| 
0a7f7abbe4f0
more robust transaction_lock: avoid overlapping data spaces;
 wenzelm parents: 
78153diff
changeset | 103 | }) | 
| 73718 | 104 | } | 
| 105 | ||
| 78260 
0a7f7abbe4f0
more robust transaction_lock: avoid overlapping data spaces;
 wenzelm parents: 
78153diff
changeset | 106 | def clean_session(db: SQL.Database, session_name: String): Unit = | 
| 78396 
7853d9072d1b
renamed object Data to private_data, to emphasize its intended scope (but it is publicly accessible in the database);
 wenzelm parents: 
78356diff
changeset | 107 |     private_data.transaction_lock(db, create = true, label = "Document_Build.clean_session") {
 | 
| 
7853d9072d1b
renamed object Data to private_data, to emphasize its intended scope (but it is publicly accessible in the database);
 wenzelm parents: 
78356diff
changeset | 108 | private_data.clean_session(db, session_name) | 
| 78356 | 109 | } | 
| 78260 
0a7f7abbe4f0
more robust transaction_lock: avoid overlapping data spaces;
 wenzelm parents: 
78153diff
changeset | 110 | |
| 
0a7f7abbe4f0
more robust transaction_lock: avoid overlapping data spaces;
 wenzelm parents: 
78153diff
changeset | 111 | def read_document(db: SQL.Database, session_name: String, name: String): Option[Document_Output] = | 
| 78396 
7853d9072d1b
renamed object Data to private_data, to emphasize its intended scope (but it is publicly accessible in the database);
 wenzelm parents: 
78356diff
changeset | 112 |     private_data.transaction_lock(db, label = "Document_Build.read_document") {
 | 
| 
7853d9072d1b
renamed object Data to private_data, to emphasize its intended scope (but it is publicly accessible in the database);
 wenzelm parents: 
78356diff
changeset | 113 | private_data.read_document(db, session_name, name) | 
| 78356 | 114 | } | 
| 73718 | 115 | |
| 77543 | 116 | def write_document(db: SQL.Database, session_name: String, doc: Document_Output): Unit = | 
| 78396 
7853d9072d1b
renamed object Data to private_data, to emphasize its intended scope (but it is publicly accessible in the database);
 wenzelm parents: 
78356diff
changeset | 117 |     private_data.transaction_lock(db, label = "Document_Build.write_document") {
 | 
| 
7853d9072d1b
renamed object Data to private_data, to emphasize its intended scope (but it is publicly accessible in the database);
 wenzelm parents: 
78356diff
changeset | 118 | private_data.write_document(db, session_name, doc) | 
| 78356 | 119 | } | 
| 73718 | 120 | |
| 121 | ||
| 76677 | 122 | /* background context */ | 
| 123 | ||
| 124 | def session_background( | |
| 125 | options: Options, | |
| 126 | session: String, | |
| 77520 
84aa349ed597
removed unused arguments: avoid ambiguity concerning progress/verbose;
 wenzelm parents: 
77517diff
changeset | 127 | dirs: List[Path] = Nil | 
| 76677 | 128 |   ): Sessions.Background = {
 | 
| 76741 | 129 | Sessions.load_structure(options + "document", dirs = dirs). | 
| 77520 
84aa349ed597
removed unused arguments: avoid ambiguity concerning progress/verbose;
 wenzelm parents: 
77517diff
changeset | 130 | selection_deps(Sessions.Selection.session(session)).background(session) | 
| 76677 | 131 | } | 
| 132 | ||
| 133 | ||
| 134 | /* document context */ | |
| 73719 | 135 | |
| 74840 
4faf0ec33cbf
option document_comment_latex supports e.g. Dagstuhl LIPIcs;
 wenzelm parents: 
74839diff
changeset | 136 |   val texinputs: Path = Path.explode("~~/lib/texinputs")
 | 
| 
4faf0ec33cbf
option document_comment_latex supports e.g. Dagstuhl LIPIcs;
 wenzelm parents: 
74839diff
changeset | 137 | |
| 73724 | 138 | val isabelle_styles: List[Path] = | 
| 74840 
4faf0ec33cbf
option document_comment_latex supports e.g. Dagstuhl LIPIcs;
 wenzelm parents: 
74839diff
changeset | 139 |     List("isabelle.sty", "isabellesym.sty", "pdfsetup.sty", "railsetup.sty").
 | 
| 
4faf0ec33cbf
option document_comment_latex supports e.g. Dagstuhl LIPIcs;
 wenzelm parents: 
74839diff
changeset | 140 | map(name => texinputs + Path.basic(name)) | 
| 73724 | 141 | |
| 77173 
f1063cdb0093
clarified terminology of inlined "PROGRAM START" messages;
 wenzelm parents: 
77025diff
changeset | 142 | def program_start(title: String): String = | 
| 
f1063cdb0093
clarified terminology of inlined "PROGRAM START" messages;
 wenzelm parents: 
77025diff
changeset | 143 | "PROGRAM START \"" + title + "\" ..." | 
| 76994 
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
 wenzelm parents: 
76884diff
changeset | 144 | |
| 77174 | 145 | def program_running_script(title: String): String = | 
| 146 |     "echo " + Bash.string(program_start("Running " + title)) + ";"
 | |
| 77173 
f1063cdb0093
clarified terminology of inlined "PROGRAM START" messages;
 wenzelm parents: 
77025diff
changeset | 147 | |
| 
f1063cdb0093
clarified terminology of inlined "PROGRAM START" messages;
 wenzelm parents: 
77025diff
changeset | 148 | def detect_program_start(s: String): Option[String] = | 
| 76994 
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
 wenzelm parents: 
76884diff
changeset | 149 |     for {
 | 
| 77173 
f1063cdb0093
clarified terminology of inlined "PROGRAM START" messages;
 wenzelm parents: 
77025diff
changeset | 150 |       s1 <- Library.try_unprefix("PROGRAM START \"", s)
 | 
| 76994 
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
 wenzelm parents: 
76884diff
changeset | 151 |       s2 <- Library.try_unsuffix("\" ...", s1)
 | 
| 
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
 wenzelm parents: 
76884diff
changeset | 152 | } yield s2 | 
| 76769 | 153 | |
| 77005 | 154 | sealed case class Document_Latex( | 
| 155 | name: Document.Node.Name, | |
| 156 | body: XML.Body, | |
| 157 | line_pos: Properties.T => Option[Int] | |
| 158 |   ) {
 | |
| 76757 | 159 | def content: File.Content_XML = File.content(Path.basic(tex_name(name)), body) | 
| 76884 | 160 | def file_pos: String = File.symbolic_path(name.path) | 
| 76757 | 161 | def write(latex_output: Latex.Output, dir: Path): Unit = | 
| 77005 | 162 | content.output(latex_output.make(_, file_pos = file_pos, line_pos = line_pos)) | 
| 163 | .write(dir) | |
| 76757 | 164 | } | 
| 165 | ||
| 73719 | 166 | def context( | 
| 75782 
dba571dd0ba9
clarified signature: prefer Export.Session_Context over Sessions.Database_Context;
 wenzelm parents: 
75748diff
changeset | 167 | session_context: Export.Session_Context, | 
| 75821 
affd69bad2d4
clarified signature: support different document_session, e.g. within running PIDE session;
 wenzelm parents: 
75782diff
changeset | 168 | document_session: Option[Sessions.Base] = None, | 
| 77197 
a541da01ba67
clarified signature selection: SortedSet[String], which fits better to stored json and works properly on Windows (NB: document theories have an authentic session-theory name);
 wenzelm parents: 
77186diff
changeset | 169 | document_selection: String => Boolean = _ => true, | 
| 75393 | 170 | progress: Progress = new Progress | 
| 76732 | 171 | ): Context = new Context(session_context, document_session, document_selection, progress) | 
| 73719 | 172 | |
| 173 | final class Context private[Document_Build]( | |
| 76370 | 174 | val session_context: Export.Session_Context, | 
| 75821 
affd69bad2d4
clarified signature: support different document_session, e.g. within running PIDE session;
 wenzelm parents: 
75782diff
changeset | 175 | document_session: Option[Sessions.Base], | 
| 77197 
a541da01ba67
clarified signature selection: SortedSet[String], which fits better to stored json and works properly on Windows (NB: document theories have an authentic session-theory name);
 wenzelm parents: 
77186diff
changeset | 176 | document_selection: String => Boolean, | 
| 76731 | 177 | val progress: Progress | 
| 75393 | 178 |   ) {
 | 
| 75826 | 179 | context => | 
| 180 | ||
| 181 | ||
| 73719 | 182 | /* session info */ | 
| 183 | ||
| 75821 
affd69bad2d4
clarified signature: support different document_session, e.g. within running PIDE session;
 wenzelm parents: 
75782diff
changeset | 184 | 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: 
75782diff
changeset | 185 | private val info = session_context.sessions_structure(base.session_name) | 
| 75782 
dba571dd0ba9
clarified signature: prefer Export.Session_Context over Sessions.Database_Context;
 wenzelm parents: 
75748diff
changeset | 186 | |
| 75821 
affd69bad2d4
clarified signature: support different document_session, e.g. within running PIDE session;
 wenzelm parents: 
75782diff
changeset | 187 | def session: String = info.name | 
| 
affd69bad2d4
clarified signature: support different document_session, e.g. within running PIDE session;
 wenzelm parents: 
75782diff
changeset | 188 | def options: Options = info.options | 
| 
affd69bad2d4
clarified signature: support different document_session, e.g. within running PIDE session;
 wenzelm parents: 
75782diff
changeset | 189 | |
| 
affd69bad2d4
clarified signature: support different document_session, e.g. within running PIDE session;
 wenzelm parents: 
75782diff
changeset | 190 | override def toString: String = session | 
| 75782 
dba571dd0ba9
clarified signature: prefer Export.Session_Context over Sessions.Database_Context;
 wenzelm parents: 
75748diff
changeset | 191 | |
| 75825 | 192 | val classpath: List[File.Content] = session_context.classpath() | 
| 75782 
dba571dd0ba9
clarified signature: prefer Export.Session_Context over Sessions.Database_Context;
 wenzelm parents: 
75748diff
changeset | 193 | |
| 73743 | 194 |     def document_bibliography: Boolean = options.bool("document_bibliography")
 | 
| 195 | ||
| 73723 | 196 | def document_logo: Option[String] = | 
| 197 |       options.string("document_logo") match {
 | |
| 198 | case "" => None | |
| 199 |         case "_" => Some("")
 | |
| 200 | case name => Some(name) | |
| 201 | } | |
| 202 | ||
| 73721 
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
 wenzelm parents: 
73720diff
changeset | 203 |     def document_build: String = options.string("document_build")
 | 
| 
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
 wenzelm parents: 
73720diff
changeset | 204 | |
| 75393 | 205 |     def get_engine(): Engine = {
 | 
| 73721 
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
 wenzelm parents: 
73720diff
changeset | 206 | val name = document_build | 
| 75702 | 207 | Classpath(jar_contents = classpath).make_services(classOf[Engine]) | 
| 75697 | 208 |         .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: 
73720diff
changeset | 209 | } | 
| 
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
 wenzelm parents: 
73720diff
changeset | 210 | |
| 73719 | 211 | |
| 212 | /* document content */ | |
| 213 | ||
| 214 | def documents: List[Document_Variant] = info.documents | |
| 215 | ||
| 76628 | 216 | def session_document_theories: List[Document.Node.Name] = base.proper_session_theories | 
| 217 | def all_document_theories: List[Document.Node.Name] = base.all_document_theories | |
| 73719 | 218 | |
| 76758 | 219 |     lazy val isabelle_logo: Option[File.Content] = {
 | 
| 220 | document_logo.map(logo_name => | |
| 221 |         Isabelle_System.with_tmp_file("logo", ext = "pdf") { tmp_path =>
 | |
| 222 | Logo.create_logo(logo_name, output_file = tmp_path, quiet = true) | |
| 223 |           val path = Path.basic("isabelle_logo.pdf")
 | |
| 224 | val content = Bytes.read(tmp_path) | |
| 225 | File.content(path, content) | |
| 226 | }) | |
| 227 | } | |
| 73719 | 228 | |
| 75393 | 229 |     lazy val session_graph: File.Content = {
 | 
| 75941 | 230 | val path = Browser_Info.session_graph_path | 
| 73778 | 231 | val content = graphview.Graph_File.make_pdf(options, base.session_graph_display) | 
| 75824 | 232 | File.content(path, content) | 
| 73719 | 233 | } | 
| 234 | ||
| 75393 | 235 |     lazy val session_tex: File.Content = {
 | 
| 73719 | 236 |       val path = Path.basic("session.tex")
 | 
| 73778 | 237 | val content = | 
| 77216 | 238 | terminate_lines( | 
| 76628 | 239 |           session_document_theories.map(name => "\\input{" + tex_name(name) + "}"))
 | 
| 75824 | 240 | File.content(path, content) | 
| 73719 | 241 | } | 
| 242 | ||
| 76758 | 243 | lazy val document_latex: List[Document_Latex] = | 
| 244 | for (name <- all_document_theories) | |
| 245 |       yield {
 | |
| 77197 
a541da01ba67
clarified signature selection: SortedSet[String], which fits better to stored json and works properly on Windows (NB: document theories have an authentic session-theory name);
 wenzelm parents: 
77186diff
changeset | 246 | val selected = document_selection(name.theory) | 
| 77018 | 247 | |
| 76758 | 248 | val body = | 
| 77018 | 249 |           if (selected) {
 | 
| 76758 | 250 | val entry = session_context(name.theory, Export.DOCUMENT_LATEX, permissive = true) | 
| 251 | YXML.parse_body(entry.text) | |
| 252 | } | |
| 77025 
34219d664854
proper citations for unselected theories, notably for the default selection of the GUI panel;
 wenzelm parents: 
77018diff
changeset | 253 |           else {
 | 
| 
34219d664854
proper citations for unselected theories, notably for the default selection of the GUI panel;
 wenzelm parents: 
77018diff
changeset | 254 | val text = | 
| 
34219d664854
proper citations for unselected theories, notably for the default selection of the GUI panel;
 wenzelm parents: 
77018diff
changeset | 255 | proper_string(session_context.theory_source(name.theory)) getOrElse | 
| 
34219d664854
proper citations for unselected theories, notably for the default selection of the GUI panel;
 wenzelm parents: 
77018diff
changeset | 256 | File.read(name.path) | 
| 
34219d664854
proper citations for unselected theories, notably for the default selection of the GUI panel;
 wenzelm parents: 
77018diff
changeset | 257 |             (for {
 | 
| 
34219d664854
proper citations for unselected theories, notably for the default selection of the GUI panel;
 wenzelm parents: 
77018diff
changeset | 258 | outer <- Bibtex.Cite.parse(Bibtex.cite_commands(options), text) | 
| 
34219d664854
proper citations for unselected theories, notably for the default selection of the GUI panel;
 wenzelm parents: 
77018diff
changeset | 259 | inner <- outer.parse | 
| 
34219d664854
proper citations for unselected theories, notably for the default selection of the GUI panel;
 wenzelm parents: 
77018diff
changeset | 260 | } yield inner.nocite.latex_text).flatten | 
| 
34219d664854
proper citations for unselected theories, notably for the default selection of the GUI panel;
 wenzelm parents: 
77018diff
changeset | 261 | } | 
| 77005 | 262 | |
| 263 | def line_pos(props: Properties.T): Option[Int] = | |
| 264 |           Position.Line.unapply(props) orElse {
 | |
| 77018 | 265 |             if (selected) {
 | 
| 266 |               for {
 | |
| 267 | snapshot <- session_context.document_snapshot | |
| 268 | id <- Position.Id.unapply(props) | |
| 269 | offset <- Position.Offset.unapply(props) | |
| 270 | line <- snapshot.find_command_line(id, offset) | |
| 271 | } yield line | |
| 272 | } | |
| 273 | else None | |
| 77005 | 274 | } | 
| 275 | ||
| 276 | Document_Latex(name, body, line_pos) | |
| 76758 | 277 | } | 
| 73723 | 278 | |
| 73719 | 279 | |
| 280 | /* document directory */ | |
| 281 | ||
| 76451 
87cd8506e000
document_build engine for "lipics", with options and document_files;
 wenzelm parents: 
76450diff
changeset | 282 | def make_directory(dir: Path, doc: Document_Variant): Path = | 
| 
87cd8506e000
document_build engine for "lipics", with options and document_files;
 wenzelm parents: 
76450diff
changeset | 283 | Isabelle_System.make_directory(dir + Path.basic(doc.name)) | 
| 
87cd8506e000
document_build engine for "lipics", with options and document_files;
 wenzelm parents: 
76450diff
changeset | 284 | |
| 75393 | 285 | def prepare_directory( | 
| 286 | dir: Path, | |
| 287 | doc: Document_Variant, | |
| 77178 
4aff4a84b8af
less verbosity by default, notably for regular "isabelle build -o document";
 wenzelm parents: 
77176diff
changeset | 288 | latex_output: Latex.Output, | 
| 
4aff4a84b8af
less verbosity by default, notably for regular "isabelle build -o document";
 wenzelm parents: 
77176diff
changeset | 289 | verbose: Boolean | 
| 75393 | 290 |     ): Directory = {
 | 
| 76451 
87cd8506e000
document_build engine for "lipics", with options and document_files;
 wenzelm parents: 
76450diff
changeset | 291 | val doc_dir = make_directory(dir, doc) | 
| 73719 | 292 | |
| 77178 
4aff4a84b8af
less verbosity by default, notably for regular "isabelle build -o document";
 wenzelm parents: 
77176diff
changeset | 293 |       if (verbose) progress.echo(program_start("Creating directory"))
 | 
| 77174 | 294 | |
| 73719 | 295 | |
| 74747 | 296 | /* actual sources: with SHA1 digest */ | 
| 73719 | 297 | |
| 77000 
ffc0774e0efe
clarified file positions: retain original source path;
 wenzelm parents: 
76994diff
changeset | 298 | isabelle_styles.foreach(Latex.copy_file(_, doc_dir)) | 
| 74840 
4faf0ec33cbf
option document_comment_latex supports e.g. Dagstuhl LIPIcs;
 wenzelm parents: 
74839diff
changeset | 299 | |
| 76450 
107d8203fbd7
clarified signature: allow to change options in instances of Document_Build.Engine;
 wenzelm parents: 
76370diff
changeset | 300 |       val comment_latex = latex_output.options.bool("document_comment_latex")
 | 
| 77000 
ffc0774e0efe
clarified file positions: retain original source path;
 wenzelm parents: 
76994diff
changeset | 301 |       if (!comment_latex) Latex.copy_file(texinputs + Path.basic("comment.sty"), doc_dir)
 | 
| 74840 
4faf0ec33cbf
option document_comment_latex supports e.g. Dagstuhl LIPIcs;
 wenzelm parents: 
74839diff
changeset | 302 | doc.tags.sty(comment_latex).write(doc_dir) | 
| 73724 | 303 | |
| 73719 | 304 |       for ((base_dir, src) <- info.document_files) {
 | 
| 77000 
ffc0774e0efe
clarified file positions: retain original source path;
 wenzelm parents: 
76994diff
changeset | 305 | Latex.copy_file_base(info.dir + base_dir, src, doc_dir) | 
| 73719 | 306 | } | 
| 307 | ||
| 308 | session_tex.write(doc_dir) | |
| 76757 | 309 | document_latex.foreach(_.write(latex_output, doc_dir)) | 
| 73719 | 310 | |
| 311 | val root_name1 = "root_" + doc.name | |
| 312 | val root_name = if ((doc_dir + Path.explode(root_name1).tex).is_file) root_name1 else "root" | |
| 313 | ||
| 77618 | 314 | val document_prefs = latex_output.options.make_prefs(filter = _.for_document) | 
| 315 | ||
| 77210 
1ffee8893b12
prefer explicit shasum: more robust due to explicit file names, which often work implicitly in LaTeX;
 wenzelm parents: 
77197diff
changeset | 316 | val meta_info = | 
| 
1ffee8893b12
prefer explicit shasum: more robust due to explicit file names, which often work implicitly in LaTeX;
 wenzelm parents: 
77197diff
changeset | 317 | SHA1.shasum_meta_info( | 
| 77618 | 318 | SHA1.digest( | 
| 319 | List(doc.print, document_logo.toString, document_build, document_prefs).toString)) | |
| 77210 
1ffee8893b12
prefer explicit shasum: more robust due to explicit file names, which often work implicitly in LaTeX;
 wenzelm parents: 
77197diff
changeset | 320 | |
| 
1ffee8893b12
prefer explicit shasum: more robust due to explicit file names, which often work implicitly in LaTeX;
 wenzelm parents: 
77197diff
changeset | 321 | val manifest = | 
| 77211 | 322 | SHA1.shasum_sorted( | 
| 323 | for (file <- File.find_files(doc_dir.file, follow_links = true)) | |
| 324 | yield SHA1.digest(file) -> File.path(doc_dir.java_path.relativize(file.toPath)).implode) | |
| 77210 
1ffee8893b12
prefer explicit shasum: more robust due to explicit file names, which often work implicitly in LaTeX;
 wenzelm parents: 
77197diff
changeset | 325 | |
| 77214 
df8d71edbc79
clarified signature, using right-associative operation;
 wenzelm parents: 
77211diff
changeset | 326 | val sources = meta_info ::: manifest | 
| 73719 | 327 | |
| 328 | ||
| 74747 | 329 | /* derived material: without SHA1 digest */ | 
| 73723 | 330 | |
| 331 | isabelle_logo.foreach(_.write(doc_dir)) | |
| 73719 | 332 | session_graph.write(doc_dir) | 
| 333 | ||
| 77178 
4aff4a84b8af
less verbosity by default, notably for regular "isabelle build -o document";
 wenzelm parents: 
77176diff
changeset | 334 |       if (verbose) {
 | 
| 
4aff4a84b8af
less verbosity by default, notably for regular "isabelle build -o document";
 wenzelm parents: 
77176diff
changeset | 335 |         progress.bash("ls -alR", echo = true, cwd = doc_dir.file).check
 | 
| 
4aff4a84b8af
less verbosity by default, notably for regular "isabelle build -o document";
 wenzelm parents: 
77176diff
changeset | 336 |         progress match {
 | 
| 
4aff4a84b8af
less verbosity by default, notably for regular "isabelle build -o document";
 wenzelm parents: 
77176diff
changeset | 337 | case program_progress: Program_Progress => program_progress.stop_program() | 
| 
4aff4a84b8af
less verbosity by default, notably for regular "isabelle build -o document";
 wenzelm parents: 
77176diff
changeset | 338 | case _ => | 
| 
4aff4a84b8af
less verbosity by default, notably for regular "isabelle build -o document";
 wenzelm parents: 
77176diff
changeset | 339 | } | 
| 77176 | 340 | } | 
| 77174 | 341 | |
| 73719 | 342 | Directory(doc_dir, doc, root_name, sources) | 
| 343 | } | |
| 344 | ||
| 345 | def old_document(directory: Directory): Option[Document_Output] = | |
| 346 |       for {
 | |
| 75782 
dba571dd0ba9
clarified signature: prefer Export.Session_Context over Sessions.Database_Context;
 wenzelm parents: 
75748diff
changeset | 347 | db <- session_context.session_db() | 
| 
dba571dd0ba9
clarified signature: prefer Export.Session_Context over Sessions.Database_Context;
 wenzelm parents: 
75748diff
changeset | 348 | old_doc <- read_document(db, session, directory.doc.name) | 
| 73719 | 349 | if old_doc.sources == directory.sources | 
| 350 | } | |
| 73720 | 351 | yield old_doc | 
| 73719 | 352 | } | 
| 353 | ||
| 354 | sealed case class Directory( | |
| 355 | doc_dir: Path, | |
| 356 | doc: Document_Variant, | |
| 357 | root_name: String, | |
| 77210 
1ffee8893b12
prefer explicit shasum: more robust due to explicit file names, which often work implicitly in LaTeX;
 wenzelm parents: 
77197diff
changeset | 358 | sources: SHA1.Shasum | 
| 75393 | 359 |   ) {
 | 
| 73737 
6638323d2774
clarified bash scripts, with public interfaces for user-defined Document_Build.Engine;
 wenzelm parents: 
73735diff
changeset | 360 | def root_name_script(ext: String = ""): String = | 
| 
6638323d2774
clarified bash scripts, with public interfaces for user-defined Document_Build.Engine;
 wenzelm parents: 
73735diff
changeset | 361 | 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: 
73735diff
changeset | 362 | |
| 76769 | 363 | def conditional_script( | 
| 364 | ext: String, exe: String, title: String = "", after: String = "" | |
| 365 |     ): String = {
 | |
| 73737 
6638323d2774
clarified bash scripts, with public interfaces for user-defined Document_Build.Engine;
 wenzelm parents: 
73735diff
changeset | 366 | "if [ -f " + root_name_script(ext) + " ]\n" + | 
| 
6638323d2774
clarified bash scripts, with public interfaces for user-defined Document_Build.Engine;
 wenzelm parents: 
73735diff
changeset | 367 | "then\n" + | 
| 77174 | 368 | " " + (if (title.nonEmpty) program_running_script(title) else "") + | 
| 76769 | 369 | exe + " " + root_name_script() + "\n" + | 
| 77368 | 370 | if_proper(after, " " + after) + | 
| 73737 
6638323d2774
clarified bash scripts, with public interfaces for user-defined Document_Build.Engine;
 wenzelm parents: 
73735diff
changeset | 371 | "fi\n" | 
| 76769 | 372 | } | 
| 73737 
6638323d2774
clarified bash scripts, with public interfaces for user-defined Document_Build.Engine;
 wenzelm parents: 
73735diff
changeset | 373 | |
| 73719 | 374 | def log_errors(): List[String] = | 
| 375 | Latex.latex_errors(doc_dir, root_name) ::: | |
| 376 | Bibtex.bibtex_errors(doc_dir, root_name) | |
| 377 | ||
| 75393 | 378 |     def make_document(log: List[String], errors: List[String]): Document_Output = {
 | 
| 73719 | 379 | val root_pdf = Path.basic(root_name).pdf | 
| 380 | val result_pdf = doc_dir + root_pdf | |
| 381 | ||
| 382 |       if (errors.nonEmpty) {
 | |
| 76734 | 383 | val message = "Failed to build document " + quote(doc.name) | 
| 384 | throw new Build_Error(log, errors ::: List(message)) | |
| 73719 | 385 | } | 
| 386 |       else if (!result_pdf.is_file) {
 | |
| 387 | val message = "Bad document result: expected to find " + root_pdf | |
| 76734 | 388 | throw new Build_Error(log, List(message)) | 
| 73719 | 389 | } | 
| 390 |       else {
 | |
| 391 | val log_xz = Bytes(cat_lines(log)).compress() | |
| 392 | val pdf = Bytes.read(result_pdf) | |
| 393 | Document_Output(doc.name, sources, log_xz, pdf) | |
| 394 | } | |
| 395 | } | |
| 396 | } | |
| 397 | ||
| 73718 | 398 | |
| 73721 
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
 wenzelm parents: 
73720diff
changeset | 399 | /* build engines */ | 
| 
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
 wenzelm parents: 
73720diff
changeset | 400 | |
| 75393 | 401 |   abstract class Engine(val name: String) extends Isabelle_System.Service {
 | 
| 73721 
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
 wenzelm parents: 
73720diff
changeset | 402 | override def toString: String = name | 
| 
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
 wenzelm parents: 
73720diff
changeset | 403 | |
| 77178 
4aff4a84b8af
less verbosity by default, notably for regular "isabelle build -o document";
 wenzelm parents: 
77176diff
changeset | 404 | def prepare_directory(context: Context, dir: Path, doc: Document_Variant, verbose: Boolean): Directory | 
| 73721 
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
 wenzelm parents: 
73720diff
changeset | 405 | def build_document(context: Context, directory: Directory, verbose: Boolean): Document_Output | 
| 
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
 wenzelm parents: 
73720diff
changeset | 406 | } | 
| 
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
 wenzelm parents: 
73720diff
changeset | 407 | |
| 75393 | 408 |   abstract class Bash_Engine(name: String) extends Engine(name) {
 | 
| 77178 
4aff4a84b8af
less verbosity by default, notably for regular "isabelle build -o document";
 wenzelm parents: 
77176diff
changeset | 409 | def prepare_directory(context: Context, dir: Path, doc: Document_Variant, verbose: Boolean): Directory = | 
| 
4aff4a84b8af
less verbosity by default, notably for regular "isabelle build -o document";
 wenzelm parents: 
77176diff
changeset | 410 | context.prepare_directory(dir, doc, new Latex.Output(context.options), verbose) | 
| 73721 
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
 wenzelm parents: 
73720diff
changeset | 411 | |
| 73759 | 412 | def use_pdflatex: Boolean = false | 
| 77174 | 413 | def running_latex: String = program_running_script(if (use_pdflatex) "pdflatex" else "lualatex") | 
| 73737 
6638323d2774
clarified bash scripts, with public interfaces for user-defined Document_Build.Engine;
 wenzelm parents: 
73735diff
changeset | 414 | def latex_script(context: Context, directory: Directory): String = | 
| 77174 | 415 | running_latex + (if (use_pdflatex) "$ISABELLE_PDFLATEX" else "$ISABELLE_LUALATEX") + | 
| 73737 
6638323d2774
clarified bash scripts, with public interfaces for user-defined Document_Build.Engine;
 wenzelm parents: 
73735diff
changeset | 416 | " " + directory.root_name_script() + "\n" | 
| 
6638323d2774
clarified bash scripts, with public interfaces for user-defined Document_Build.Engine;
 wenzelm parents: 
73735diff
changeset | 417 | |
| 75393 | 418 |     def bibtex_script(context: Context, directory: Directory, latex: Boolean = false): String = {
 | 
| 73743 | 419 | val ext = if (context.document_bibliography) "aux" else "bib" | 
| 76769 | 420 | directory.conditional_script(ext, "$ISABELLE_BIBTEX", title = "bibtex", | 
| 73737 
6638323d2774
clarified bash scripts, with public interfaces for user-defined Document_Build.Engine;
 wenzelm parents: 
73735diff
changeset | 421 | after = if (latex) latex_script(context, directory) else "") | 
| 73743 | 422 | } | 
| 73737 
6638323d2774
clarified bash scripts, with public interfaces for user-defined Document_Build.Engine;
 wenzelm parents: 
73735diff
changeset | 423 | |
| 
6638323d2774
clarified bash scripts, with public interfaces for user-defined Document_Build.Engine;
 wenzelm parents: 
73735diff
changeset | 424 | def makeindex_script(context: Context, directory: Directory, latex: Boolean = false): String = | 
| 76769 | 425 |       directory.conditional_script("idx", "$ISABELLE_MAKEINDEX", title = "makeindex",
 | 
| 73737 
6638323d2774
clarified bash scripts, with public interfaces for user-defined Document_Build.Engine;
 wenzelm parents: 
73735diff
changeset | 426 | after = if (latex) latex_script(context, directory) else "") | 
| 
6638323d2774
clarified bash scripts, with public interfaces for user-defined Document_Build.Engine;
 wenzelm parents: 
73735diff
changeset | 427 | |
| 73759 | 428 | def use_build_script: Boolean = false | 
| 75393 | 429 |     def build_script(context: Context, directory: Directory): String = {
 | 
| 73759 | 430 |       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: 
73720diff
changeset | 431 | |
| 73759 | 432 |       if (!use_build_script && has_build_script) {
 | 
| 73721 
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
 wenzelm parents: 
73720diff
changeset | 433 |         error("Unexpected document build script for option document_build=" +
 | 
| 
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
 wenzelm parents: 
73720diff
changeset | 434 | quote(context.document_build)) | 
| 
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
 wenzelm parents: 
73720diff
changeset | 435 | } | 
| 73759 | 436 |       else if (use_build_script && !has_build_script) error("Missing document build script")
 | 
| 437 | 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: 
73720diff
changeset | 438 |       else {
 | 
| 73737 
6638323d2774
clarified bash scripts, with public interfaces for user-defined Document_Build.Engine;
 wenzelm parents: 
73735diff
changeset | 439 | "set -e\n" + | 
| 
6638323d2774
clarified bash scripts, with public interfaces for user-defined Document_Build.Engine;
 wenzelm parents: 
73735diff
changeset | 440 | latex_script(context, directory) + | 
| 
6638323d2774
clarified bash scripts, with public interfaces for user-defined Document_Build.Engine;
 wenzelm parents: 
73735diff
changeset | 441 | bibtex_script(context, directory, latex = true) + | 
| 73738 
d701bd96e323
more robust: allow \printindex within the document;
 wenzelm parents: 
73737diff
changeset | 442 | makeindex_script(context, directory) + | 
| 73737 
6638323d2774
clarified bash scripts, with public interfaces for user-defined Document_Build.Engine;
 wenzelm parents: 
73735diff
changeset | 443 | latex_script(context, directory) + | 
| 
6638323d2774
clarified bash scripts, with public interfaces for user-defined Document_Build.Engine;
 wenzelm parents: 
73735diff
changeset | 444 | makeindex_script(context, directory, latex = true) | 
| 73721 
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
 wenzelm parents: 
73720diff
changeset | 445 | } | 
| 
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
 wenzelm parents: 
73720diff
changeset | 446 | } | 
| 
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
 wenzelm parents: 
73720diff
changeset | 447 | |
| 75393 | 448 | def build_document( | 
| 449 | context: Context, | |
| 450 | directory: Directory, | |
| 451 | verbose: Boolean | |
| 452 |     ): Document_Output = {
 | |
| 77517 | 453 | val progress = context.progress | 
| 73721 
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
 wenzelm parents: 
73720diff
changeset | 454 | val result = | 
| 77517 | 455 | progress.bash( | 
| 73737 
6638323d2774
clarified bash scripts, with public interfaces for user-defined Document_Build.Engine;
 wenzelm parents: 
73735diff
changeset | 456 | build_script(context, directory), | 
| 73721 
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
 wenzelm parents: 
73720diff
changeset | 457 | cwd = directory.doc_dir.file, | 
| 
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
 wenzelm parents: 
73720diff
changeset | 458 | echo = verbose, | 
| 
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
 wenzelm parents: 
73720diff
changeset | 459 | watchdog = Time.seconds(0.5)) | 
| 
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
 wenzelm parents: 
73720diff
changeset | 460 | |
| 
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
 wenzelm parents: 
73720diff
changeset | 461 | val log = result.out_lines ::: result.err_lines | 
| 76734 | 462 | val err = result.err | 
| 463 | ||
| 464 | val errors1 = directory.log_errors() | |
| 465 | val errors2 = | |
| 466 | if (result.ok) errors1 | |
| 467 | else if (err.nonEmpty) err :: errors1 | |
| 468 | else if (errors1.nonEmpty) errors1 | |
| 469 |         else List("Error")
 | |
| 470 | directory.make_document(log, errors2) | |
| 73721 
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
 wenzelm parents: 
73720diff
changeset | 471 | } | 
| 
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
 wenzelm parents: 
73720diff
changeset | 472 | } | 
| 
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
 wenzelm parents: 
73720diff
changeset | 473 | |
| 
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
 wenzelm parents: 
73720diff
changeset | 474 |   class LuaLaTeX_Engine extends Bash_Engine("lualatex")
 | 
| 73759 | 475 |   class PDFLaTeX_Engine extends Bash_Engine("pdflatex") { override def use_pdflatex: Boolean = true }
 | 
| 476 |   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: 
73720diff
changeset | 477 | |
| 76454 
f2d17e69e520
clarified options: support lualatex as well, but prefer old pdflatex for demos;
 wenzelm parents: 
76451diff
changeset | 478 |   class LIPIcs_Engine(name: String) extends Bash_Engine(name) {
 | 
| 76451 
87cd8506e000
document_build engine for "lipics", with options and document_files;
 wenzelm parents: 
76450diff
changeset | 479 | def lipics_options(options: Options): Options = | 
| 
87cd8506e000
document_build engine for "lipics", with options and document_files;
 wenzelm parents: 
76450diff
changeset | 480 | options + "document_heading_prefix=" + "document_comment_latex" | 
| 
87cd8506e000
document_build engine for "lipics", with options and document_files;
 wenzelm parents: 
76450diff
changeset | 481 | |
| 
87cd8506e000
document_build engine for "lipics", with options and document_files;
 wenzelm parents: 
76450diff
changeset | 482 | override def use_pdflatex: Boolean = true | 
| 
87cd8506e000
document_build engine for "lipics", with options and document_files;
 wenzelm parents: 
76450diff
changeset | 483 | |
| 77178 
4aff4a84b8af
less verbosity by default, notably for regular "isabelle build -o document";
 wenzelm parents: 
77176diff
changeset | 484 | override def prepare_directory( | 
| 
4aff4a84b8af
less verbosity by default, notably for regular "isabelle build -o document";
 wenzelm parents: 
77176diff
changeset | 485 | context: Context, dir: Path, doc: Document_Variant, verbose: Boolean | 
| 
4aff4a84b8af
less verbosity by default, notably for regular "isabelle build -o document";
 wenzelm parents: 
77176diff
changeset | 486 |     ): Directory = {
 | 
| 76451 
87cd8506e000
document_build engine for "lipics", with options and document_files;
 wenzelm parents: 
76450diff
changeset | 487 | val doc_dir = context.make_directory(dir, doc) | 
| 77566 
2a99fcb283ee
renamed administrative tools to build Isabelle components (unrelated to "isabelle build");
 wenzelm parents: 
77552diff
changeset | 488 | Component_LIPIcs.document_files.foreach(Latex.copy_file(_, doc_dir)) | 
| 76451 
87cd8506e000
document_build engine for "lipics", with options and document_files;
 wenzelm parents: 
76450diff
changeset | 489 | |
| 
87cd8506e000
document_build engine for "lipics", with options and document_files;
 wenzelm parents: 
76450diff
changeset | 490 | val latex_output = new Latex.Output(lipics_options(context.options)) | 
| 77178 
4aff4a84b8af
less verbosity by default, notably for regular "isabelle build -o document";
 wenzelm parents: 
77176diff
changeset | 491 | context.prepare_directory(dir, doc, latex_output, verbose) | 
| 76451 
87cd8506e000
document_build engine for "lipics", with options and document_files;
 wenzelm parents: 
76450diff
changeset | 492 | } | 
| 
87cd8506e000
document_build engine for "lipics", with options and document_files;
 wenzelm parents: 
76450diff
changeset | 493 | } | 
| 76454 
f2d17e69e520
clarified options: support lualatex as well, but prefer old pdflatex for demos;
 wenzelm parents: 
76451diff
changeset | 494 |   class LIPIcs_LuaLaTeX_Engine extends LIPIcs_Engine("lipics")
 | 
| 
f2d17e69e520
clarified options: support lualatex as well, but prefer old pdflatex for demos;
 wenzelm parents: 
76451diff
changeset | 495 |   class LIPIcs_PDFLaTeX_Engine extends LIPIcs_Engine("lipics_pdflatex") {
 | 
| 
f2d17e69e520
clarified options: support lualatex as well, but prefer old pdflatex for demos;
 wenzelm parents: 
76451diff
changeset | 496 | override def use_pdflatex: Boolean = true | 
| 
f2d17e69e520
clarified options: support lualatex as well, but prefer old pdflatex for demos;
 wenzelm parents: 
76451diff
changeset | 497 | } | 
| 76451 
87cd8506e000
document_build engine for "lipics", with options and document_files;
 wenzelm parents: 
76450diff
changeset | 498 | |
| 73721 
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
 wenzelm parents: 
73720diff
changeset | 499 | |
| 73718 | 500 | /* build documents */ | 
| 501 | ||
| 502 | def tex_name(name: Document.Node.Name): String = name.theory_base_name + ".tex" | |
| 503 | ||
| 76734 | 504 | class Build_Error(val log_lines: List[String], val log_errors: List[String]) | 
| 505 | extends Exn.User_Error(Exn.cat_message(log_errors: _*)) | |
| 73718 | 506 | |
| 507 | def build_documents( | |
| 73719 | 508 | context: Context, | 
| 73718 | 509 | output_sources: Option[Path] = None, | 
| 510 | output_pdf: Option[Path] = None, | |
| 75393 | 511 | verbose: Boolean = false | 
| 512 |   ): List[Document_Output] = {
 | |
| 73719 | 513 | val progress = context.progress | 
| 73721 
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
 wenzelm parents: 
73720diff
changeset | 514 | val engine = context.get_engine() | 
| 73718 | 515 | |
| 516 | val documents = | |
| 73719 | 517 | for (doc <- context.documents) | 
| 73718 | 518 |       yield {
 | 
| 75394 | 519 |         Isabelle_System.with_tmp_dir("document") { tmp_dir =>
 | 
| 73719 | 520 |           progress.echo("Preparing " + context.session + "/" + doc.name + " ...")
 | 
| 73718 | 521 | val start = Time.now() | 
| 522 | ||
| 77178 
4aff4a84b8af
less verbosity by default, notably for regular "isabelle build -o document";
 wenzelm parents: 
77176diff
changeset | 523 | output_sources.foreach(engine.prepare_directory(context, _, doc, false)) | 
| 
4aff4a84b8af
less verbosity by default, notably for regular "isabelle build -o document";
 wenzelm parents: 
77176diff
changeset | 524 | val directory = engine.prepare_directory(context, tmp_dir, doc, verbose) | 
| 73718 | 525 | |
| 73721 
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
 wenzelm parents: 
73720diff
changeset | 526 | val document = | 
| 
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
 wenzelm parents: 
73720diff
changeset | 527 | context.old_document(directory) getOrElse | 
| 
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
 wenzelm parents: 
73720diff
changeset | 528 | engine.build_document(context, directory, verbose) | 
| 73718 | 529 | |
| 73721 
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
 wenzelm parents: 
73720diff
changeset | 530 | val stop = Time.now() | 
| 
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
 wenzelm parents: 
73720diff
changeset | 531 | val timing = stop - start | 
| 73718 | 532 | |
| 73721 
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
 wenzelm parents: 
73720diff
changeset | 533 |           progress.echo("Finished " + context.session + "/" + doc.name +
 | 
| 
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
 wenzelm parents: 
73720diff
changeset | 534 |             " (" + timing.message_hms + " elapsed time)")
 | 
| 73718 | 535 | |
| 73721 
52030acb19ac
option document_build refers to build engine in Isabelle/Scala;
 wenzelm parents: 
73720diff
changeset | 536 | document | 
| 75394 | 537 | } | 
| 73718 | 538 | } | 
| 539 | ||
| 540 |     for (dir <- output_pdf; doc <- documents) {
 | |
| 73719 | 541 | val path = doc.write(dir) | 
| 73718 | 542 |       progress.echo("Document at " + path.absolute)
 | 
| 543 | } | |
| 544 | ||
| 545 | documents | |
| 546 | } | |
| 547 | ||
| 548 | ||
| 549 | /* Isabelle tool wrapper */ | |
| 550 | ||
| 551 | val isabelle_tool = | |
| 75394 | 552 |     Isabelle_Tool("document", "prepare session theory document", Scala_Project.here,
 | 
| 553 |       { args =>
 | |
| 554 | var output_sources: Option[Path] = None | |
| 555 | var output_pdf: Option[Path] = None | |
| 556 | var verbose_latex = false | |
| 557 | var dirs: List[Path] = Nil | |
| 558 | var options = Options.init() | |
| 559 | var verbose_build = false | |
| 73718 | 560 | |
| 75394 | 561 |         val getopts = Getopts("""
 | 
| 73718 | 562 | Usage: isabelle document [OPTIONS] SESSION | 
| 563 | ||
| 564 | Options are: | |
| 565 | -O DIR output directory for LaTeX sources and resulting PDF | |
| 566 | -P DIR output directory for resulting PDF | |
| 567 | -S DIR output directory for LaTeX sources | |
| 568 | -V verbose latex | |
| 569 | -d DIR include session directory | |
| 570 | -o OPTION override Isabelle system OPTION (via NAME=VAL or NAME) | |
| 571 | -v verbose build | |
| 572 | ||
| 573 | Prepare the theory document of a session. | |
| 574 | """, | |
| 75394 | 575 | "O:" -> (arg => | 
| 576 |             {
 | |
| 577 | val dir = Path.explode(arg) | |
| 578 | output_sources = Some(dir) | |
| 579 | output_pdf = Some(dir) | |
| 580 | }), | |
| 581 |           "P:" -> (arg => { output_pdf = Some(Path.explode(arg)) }),
 | |
| 582 |           "S:" -> (arg => { output_sources = Some(Path.explode(arg)) }),
 | |
| 583 | "V" -> (_ => verbose_latex = true), | |
| 584 | "d:" -> (arg => dirs = dirs ::: List(Path.explode(arg))), | |
| 585 | "o:" -> (arg => options = options + arg), | |
| 586 | "v" -> (_ => verbose_build = true)) | |
| 73718 | 587 | |
| 75394 | 588 | val more_args = getopts(args) | 
| 589 | val session = | |
| 590 |           more_args match {
 | |
| 591 | case List(a) => a | |
| 592 | case _ => getopts.usage() | |
| 593 | } | |
| 73718 | 594 | |
| 75394 | 595 | val progress = new Console_Progress(verbose = verbose_build) | 
| 73718 | 596 | |
| 75394 | 597 |         progress.interrupt_handler {
 | 
| 76206 
769a7cd5a16a
clarified signature: re-use store/cache from build results;
 wenzelm parents: 
75941diff
changeset | 598 | val build_results = | 
| 75394 | 599 | Build.build(options, selection = Sessions.Selection.session(session), | 
| 77521 
5642de4d225d
clarified signature: manage "verbose" flag via "progress";
 wenzelm parents: 
77520diff
changeset | 600 | dirs = dirs, progress = progress) | 
| 76206 
769a7cd5a16a
clarified signature: re-use store/cache from build results;
 wenzelm parents: 
75941diff
changeset | 601 |           if (!build_results.ok) error("Failed to build session " + quote(session))
 | 
| 73718 | 602 | |
| 75394 | 603 |           if (output_sources.isEmpty && output_pdf.isEmpty) {
 | 
| 604 |             progress.echo_warning("No output directory")
 | |
| 605 | } | |
| 73718 | 606 | |
| 76731 | 607 | val session_background = Document_Build.session_background(options, session, dirs = dirs) | 
| 608 |           using(Export.open_session_context(build_results.store, session_background)) {
 | |
| 76206 
769a7cd5a16a
clarified signature: re-use store/cache from build results;
 wenzelm parents: 
75941diff
changeset | 609 | session_context => | 
| 
769a7cd5a16a
clarified signature: re-use store/cache from build results;
 wenzelm parents: 
75941diff
changeset | 610 | build_documents( | 
| 
769a7cd5a16a
clarified signature: re-use store/cache from build results;
 wenzelm parents: 
75941diff
changeset | 611 | context(session_context, progress = progress), | 
| 
769a7cd5a16a
clarified signature: re-use store/cache from build results;
 wenzelm parents: 
75941diff
changeset | 612 | output_sources = output_sources, output_pdf = output_pdf, | 
| 
769a7cd5a16a
clarified signature: re-use store/cache from build results;
 wenzelm parents: 
75941diff
changeset | 613 | verbose = verbose_latex) | 
| 75394 | 614 | } | 
| 615 | } | |
| 616 | }) | |
| 73718 | 617 | } |