diff --git a/lib/texinputs/isabelle.sty b/lib/texinputs/isabelle.sty --- a/lib/texinputs/isabelle.sty +++ b/lib/texinputs/isabelle.sty @@ -1,303 +1,282 @@ %% %% macros for Isabelle generated LaTeX output %% %%% Simple document preparation (based on theory token language and symbols) % isabelle environments \newcommand{\isabellecontext}{UNKNOWN} \newcommand{\setisabellecontext}[1]{\def\isabellecontext{#1}} \newcommand{\isastyle}{\UNDEF} \newcommand{\isastylett}{\UNDEF} \newcommand{\isastyleminor}{\UNDEF} \newcommand{\isastyleminortt}{\UNDEF} \newcommand{\isastylescript}{\UNDEF} \newcommand{\isastyletext}{\normalsize\normalfont\rmfamily} \newcommand{\isastyletxt}{\normalfont\rmfamily} \newcommand{\isastylecmt}{\normalfont\rmfamily} \newcommand{\isaspacing}{% \sfcode 42 1000 % . \sfcode 63 1000 % ? \sfcode 33 1000 % ! \sfcode 58 1000 % : \sfcode 59 1000 % ; \sfcode 44 1000 % , } %symbol markup -- \emph achieves decent spacing via italic corrections \newcommand{\isamath}[1]{\emph{$#1$}} \newcommand{\isatext}[1]{\emph{#1}} \DeclareRobustCommand{\isascriptstyle}{\def\isamath##1{##1}\def\isatext##1{\mbox{\isaspacing\isastylescript##1}}} \newcommand{\isactrlsub}[1]{\emph{\isascriptstyle${}\sb{#1}$}} \newcommand{\isactrlsup}[1]{\emph{\isascriptstyle${}\sp{#1}$}} \DeclareRobustCommand{\isactrlbsub}{\emph\bgroup\math{}\sb\bgroup\mbox\bgroup\isaspacing\isastylescript} \DeclareRobustCommand{\isactrlesub}{\egroup\egroup\endmath\egroup} \DeclareRobustCommand{\isactrlbsup}{\emph\bgroup\math{}\sp\bgroup\mbox\bgroup\isaspacing\isastylescript} \DeclareRobustCommand{\isactrlesup}{\egroup\egroup\endmath\egroup} \newcommand{\isactrlbold}[1]{{\bfseries\upshape\boldmath#1}} %blackboard-bold (requires font txmia from pxfonts) \DeclareSymbolFont{bbbfont}{U}{txmia}{m}{it} \SetSymbolFont{bbbfont}{bold}{U}{txmia}{bx}{it} \DeclareMathSymbol{\bbbA}{\mathord}{bbbfont}{129} \DeclareMathSymbol{\bbbB}{\mathord}{bbbfont}{130} \DeclareMathSymbol{\bbbC}{\mathord}{bbbfont}{131} \DeclareMathSymbol{\bbbD}{\mathord}{bbbfont}{132} \DeclareMathSymbol{\bbbE}{\mathord}{bbbfont}{133} \DeclareMathSymbol{\bbbF}{\mathord}{bbbfont}{134} \DeclareMathSymbol{\bbbG}{\mathord}{bbbfont}{135} \DeclareMathSymbol{\bbbH}{\mathord}{bbbfont}{136} \DeclareMathSymbol{\bbbI}{\mathord}{bbbfont}{137} \DeclareMathSymbol{\bbbJ}{\mathord}{bbbfont}{138} \DeclareMathSymbol{\bbbK}{\mathord}{bbbfont}{139} \DeclareMathSymbol{\bbbL}{\mathord}{bbbfont}{140} \DeclareMathSymbol{\bbbM}{\mathord}{bbbfont}{141} \DeclareMathSymbol{\bbbN}{\mathord}{bbbfont}{142} \DeclareMathSymbol{\bbbO}{\mathord}{bbbfont}{143} \DeclareMathSymbol{\bbbP}{\mathord}{bbbfont}{144} \DeclareMathSymbol{\bbbQ}{\mathord}{bbbfont}{145} \DeclareMathSymbol{\bbbR}{\mathord}{bbbfont}{146} \DeclareMathSymbol{\bbbS}{\mathord}{bbbfont}{147} \DeclareMathSymbol{\bbbT}{\mathord}{bbbfont}{148} \DeclareMathSymbol{\bbbU}{\mathord}{bbbfont}{149} \DeclareMathSymbol{\bbbV}{\mathord}{bbbfont}{150} \DeclareMathSymbol{\bbbW}{\mathord}{bbbfont}{151} \DeclareMathSymbol{\bbbX}{\mathord}{bbbfont}{152} \DeclareMathSymbol{\bbbY}{\mathord}{bbbfont}{153} \DeclareMathSymbol{\bbbZ}{\mathord}{bbbfont}{154} \newenvironment{isaantiq}{{\isacharat\isacharbraceleft}}{{\isacharbraceright}} \newdimen\isa@parindent\newdimen\isa@parskip \newenvironment{isabellebody}{% \isamarkuptrue\par% \isa@parindent\parindent\parindent0pt% \isa@parskip\parskip\parskip0pt% \isaspacing\isastyle}{\par} \newenvironment{isabellebodytt}{% \isamarkuptrue\par% \isa@parindent\parindent\parindent0pt% \isa@parskip\parskip\parskip0pt% \isaspacing\isastylett}{\par} \newenvironment{isabelle} {\begin{trivlist}\begin{isabellebody}\item\relax} {\end{isabellebody}\end{trivlist}} \newenvironment{isabellett} {\begin{trivlist}\begin{isabellebodytt}\item\relax} {\end{isabellebodytt}\end{trivlist}} \newcommand{\isa}[1]{\emph{\isaspacing\isastyleminor #1}} \newcommand{\isatt}[1]{\emph{\isaspacing\isastyleminortt #1}} \newcommand{\isaindent}[1]{\hphantom{#1}} \newcommand{\isanewline}{\mbox{}\par\mbox{}} \newcommand{\isasep}{} \newcommand{\isadigit}[1]{#1} \newcommand{\isachardefaults}{% \def\isacharbell{\isamath{\bigbox}}%requires stmaryrd \chardef\isacharbang=`\!% \chardef\isachardoublequote=`\"% \chardef\isachardoublequoteopen=`\"% \chardef\isachardoublequoteclose=`\"% \chardef\isacharhash=`\#% \chardef\isachardollar=`\$% \chardef\isacharpercent=`\%% \chardef\isacharampersand=`\&% \chardef\isacharprime=`\'% \chardef\isacharparenleft=`\(% \chardef\isacharparenright=`\)% \chardef\isacharasterisk=`\*% \chardef\isacharplus=`\+% \chardef\isacharcomma=`\,% \chardef\isacharminus=`\-% \chardef\isachardot=`\.% \chardef\isacharslash=`\/% \chardef\isacharcolon=`\:% \chardef\isacharsemicolon=`\;% \chardef\isacharless=`\<% \chardef\isacharequal=`\=% \chardef\isachargreater=`\>% \chardef\isacharquery=`\?% \chardef\isacharat=`\@% \chardef\isacharbrackleft=`\[% \chardef\isacharbackslash=`\\% \chardef\isacharbrackright=`\]% \chardef\isacharcircum=`\^% \chardef\isacharunderscore=`\_% \def\isacharunderscorekeyword{\_}% \chardef\isacharbackquote=`\`% \chardef\isacharbackquoteopen=`\`% \chardef\isacharbackquoteclose=`\`% \chardef\isacharbraceleft=`\{% \chardef\isacharbar=`\|% \chardef\isacharbraceright=`\}% \chardef\isachartilde=`\~% \def\isacharverbatimopen{\isacharbraceleft\isacharasterisk}% \def\isacharverbatimclose{\isacharasterisk\isacharbraceright}% \def\isacartoucheopen{\isatext{\guilsinglleft}}% \def\isacartoucheclose{\isatext{\guilsinglright}}% } % keyword and section markup \newcommand{\isakeyword}[1] {\emph{\normalfont\bfseries\def\isachardot{.}\def\isacharunderscore{\isacharunderscorekeyword}% \def\isacharbraceleft{\{}\def\isacharbraceright{\}}#1}} \newcommand{\isacommand}[1]{\isakeyword{#1}} \newcommand{\isakeywordcontrol}[1] {\emph{\normalfont\bfseries\itshape\def\isacharunderscore{\isacharunderscorekeyword}#1\,}} \newcommand{\isamarkupchapter}[1]{\chapter{#1}} \newcommand{\isamarkupsection}[1]{\section{#1}} \newcommand{\isamarkupsubsection}[1]{\subsection{#1}} \newcommand{\isamarkupsubsubsection}[1]{\subsubsection{#1}} \newcommand{\isamarkupparagraph}[1]{\paragraph{#1}} \newcommand{\isamarkupsubparagraph}[1]{\subparagraph{#1}} \newif\ifisamarkup \newcommand{\isabeginpar}{\par\ifisamarkup\relax\else\medskip\fi} \newcommand{\isaendpar}{\par\medskip} \newenvironment{isapar}{\parindent\isa@parindent\parskip\isa@parskip\isabeginpar}{\isaendpar} \newenvironment{isamarkuptext}{\par\isastyletext\begin{isapar}}{\end{isapar}} \newenvironment{isamarkuptxt}{\par\isastyletxt\begin{isapar}}{\end{isapar}} \newcommand{\isamarkupcmt}[1]{{\isastylecmt--- #1}} % index entries \newcommand{\isaindexdef}[1]{\textbf{#1}} \newcommand{\isaindexref}[1]{#1} % styles \def\isabellestyle#1{\csname isabellestyle#1\endcsname} \newcommand{\isabellestyledefault}{% \def\isastyle{\small\normalfont\ttfamily\slshape}% \def\isastylett{\small\normalfont\ttfamily}% \def\isastyleminor{\small\normalfont\ttfamily\slshape}% \def\isastyleminortt{\small\normalfont\ttfamily}% \def\isastylescript{\footnotesize\normalfont\ttfamily\slshape}% \isachardefaults% } \isabellestyledefault \newcommand{\isabellestylett}{% \def\isastyle{\small\normalfont\ttfamily}% \def\isastylett{\small\normalfont\ttfamily}% \def\isastyleminor{\small\normalfont\ttfamily}% \def\isastyleminortt{\small\normalfont\ttfamily}% \def\isastylescript{\footnotesize\normalfont\ttfamily}% \isachardefaults% } \newcommand{\isabellestyleit}{% \def\isastyle{\small\normalfont\itshape}% \def\isastylett{\small\normalfont\ttfamily}% \def\isastyleminor{\normalfont\itshape}% \def\isastyleminortt{\normalfont\ttfamily}% \def\isastylescript{\footnotesize\normalfont\itshape}% \isachardefaults% \def\isacharunderscorekeyword{\mbox{-}}% \def\isacharbang{\isamath{!}}% \def\isachardoublequote{}% \def\isachardoublequoteopen{}% \def\isachardoublequoteclose{}% \def\isacharhash{\isamath{\#}}% \def\isachardollar{\isamath{\$}}% \def\isacharpercent{\isamath{\%}}% \def\isacharampersand{\isamath{\&}}% \def\isacharprime{\isamath{\mskip2mu{'}\mskip-2mu}}% \def\isacharparenleft{\isamath{(}}% \def\isacharparenright{\isamath{)}}% \def\isacharasterisk{\isamath{*}}% \def\isacharplus{\isamath{+}}% \def\isacharcomma{\isamath{\mathord,}}% \def\isacharminus{\isamath{-}}% \def\isachardot{\isamath{\mathord.}}% \def\isacharslash{\isamath{/}}% \def\isacharcolon{\isamath{\mathord:}}% \def\isacharsemicolon{\isamath{\mathord;}}% \def\isacharless{\isamath{<}}% \def\isacharequal{\isamath{=}}% \def\isachargreater{\isamath{>}}% \def\isacharat{\isamath{@}}% \def\isacharbrackleft{\isamath{[}}% \def\isacharbackslash{\isamath{\backslash}}% \def\isacharbrackright{\isamath{]}}% \def\isacharunderscore{\mbox{-}}% \def\isacharbraceleft{\isamath{\{}}% \def\isacharbar{\isamath{\mid}}% \def\isacharbraceright{\isamath{\}}}% \def\isachartilde{\isamath{{}\sp{\sim}}}% \def\isacharbackquoteopen{\isatext{\guilsinglleft}}% \def\isacharbackquoteclose{\isatext{\guilsinglright}}% } \newcommand{\isabellestyleliteral}{% \isabellestyleit% \def\isacharunderscore{\_}% \def\isacharunderscorekeyword{\_}% \chardef\isacharbackquoteopen=`\`% \chardef\isacharbackquoteclose=`\`% } \newcommand{\isabellestyleliteralunderscore}{% \isabellestyleliteral% \def\isacharunderscore{\textunderscore}% \def\isacharunderscorekeyword{\textunderscore}% } \newcommand{\isabellestylesl}{% \isabellestyleit% \def\isastyle{\small\normalfont\slshape}% \def\isastylett{\small\normalfont\ttfamily}% \def\isastyleminor{\normalfont\slshape}% \def\isastyleminortt{\normalfont\ttfamily}% \def\isastylescript{\footnotesize\normalfont\slshape}% } % cancel text \usepackage[normalem]{ulem} \newcommand{\isamarkupcancel}[1]{\isa{\xout{#1}}} -% tagged regions - -%plain TeX version of comment package -- much faster! -\let\isafmtname\fmtname\def\fmtname{plain} -\usepackage{comment} -\let\fmtname\isafmtname +% tags \newcommand{\isafold}[1]{\emph{$\langle\mathord{\mathit{#1}}\rangle$}} -\newcommand{\isakeeptag}[1]% -{\includecomment{isadelim#1}\includecomment{isatag#1}\csarg\def{isafold#1}{}} -\newcommand{\isadroptag}[1]% -{\excludecomment{isadelim#1}\excludecomment{isatag#1}\csarg\def{isafold#1}{}} -\newcommand{\isafoldtag}[1]% -{\includecomment{isadelim#1}\excludecomment{isatag#1}\csarg\def{isafold#1}{\isafold{#1}}} - -\isakeeptag{document} -\isakeeptag{theory} -\isakeeptag{proof} -\isakeeptag{ML} -\isakeeptag{visible} -\isadroptag{invisible} -\isakeeptag{important} -\isakeeptag{unimportant} - \IfFileExists{isabelletags.sty}{\usepackage{isabelletags}}{} diff --git a/src/Pure/Thy/document_build.scala b/src/Pure/Thy/document_build.scala --- a/src/Pure/Thy/document_build.scala +++ b/src/Pure/Thy/document_build.scala @@ -1,519 +1,500 @@ /* Title: Pure/Thy/document_build.scala Author: Makarius Build theory document (PDF) from session database. */ package isabelle object Document_Build { /* document variants */ abstract class Document_Name { def name: String def path: Path = Path.basic(name) override def toString: String = name } object Document_Variant { - def parse(name: String, tags: String): Document_Variant = - Document_Variant(name, Library.space_explode(',', tags)) - def parse(opt: String): Document_Variant = Library.space_explode('=', opt) match { - case List(name) => Document_Variant(name, Nil) - case List(name, tags) => parse(name, tags) + case List(name) => Document_Variant(name, Latex.Tags.empty) + case List(name, tags) => Document_Variant(name, Latex.Tags(tags)) case _ => error("Malformed document variant: " + quote(opt)) } } - sealed case class Document_Variant(name: String, tags: List[String]) extends Document_Name + sealed case class Document_Variant(name: String, tags: Latex.Tags) extends Document_Name { - def print_tags: String = tags.mkString(",") - def print: String = if (tags.isEmpty) name else name + "=" + print_tags - - def isabelletags: File.Content = - { - val path = Path.explode("isabelletags.sty") - val content = - Library.terminate_lines( - tags.map(tag => - tag.toList match { - case '/' :: cs => "\\isafoldtag{" + cs.mkString + "}" - case '-' :: cs => "\\isadroptag{" + cs.mkString + "}" - case '+' :: cs => "\\isakeeptag{" + cs.mkString + "}" - case cs => "\\isakeeptag{" + cs.mkString + "}" - })) - File.Content(path, content) - } + def print: String = if (tags.toString.isEmpty) name else name + "=" + tags.toString } sealed case class Document_Input(name: String, sources: SHA1.Digest) extends Document_Name sealed case class Document_Output(name: String, sources: SHA1.Digest, log_xz: Bytes, pdf: Bytes) extends Document_Name { def log: String = log_xz.uncompress().text def log_lines: List[String] = split_lines(log) def write(db: SQL.Database, session_name: String): Unit = write_document(db, session_name, this) def write(dir: Path): Path = { val path = dir + Path.basic(name).pdf Isabelle_System.make_directory(path.expand.dir) Bytes.write(path, pdf) path } } /* SQL data model */ object Data { val session_name = SQL.Column.string("session_name").make_primary_key val name = SQL.Column.string("name").make_primary_key val sources = SQL.Column.string("sources") val log_xz = SQL.Column.bytes("log_xz") val pdf = SQL.Column.bytes("pdf") val table = SQL.Table("isabelle_documents", List(session_name, name, sources, log_xz, pdf)) def where_equal(session_name: String, name: String = ""): SQL.Source = "WHERE " + Data.session_name.equal(session_name) + (if (name == "") "" else " AND " + Data.name.equal(name)) } def read_documents(db: SQL.Database, session_name: String): List[Document_Input] = { val select = Data.table.select(List(Data.name, Data.sources), Data.where_equal(session_name)) db.using_statement(select)(stmt => stmt.execute_query().iterator(res => { val name = res.string(Data.name) val sources = res.string(Data.sources) Document_Input(name, SHA1.fake(sources)) }).toList) } def read_document(db: SQL.Database, session_name: String, name: String): Option[Document_Output] = { val select = Data.table.select(sql = Data.where_equal(session_name, name)) db.using_statement(select)(stmt => { val res = stmt.execute_query() if (res.next()) { val name = res.string(Data.name) val sources = res.string(Data.sources) val log_xz = res.bytes(Data.log_xz) val pdf = res.bytes(Data.pdf) Some(Document_Output(name, SHA1.fake(sources), log_xz, pdf)) } else None }) } def write_document(db: SQL.Database, session_name: String, doc: Document_Output): Unit = { db.using_statement(Data.table.insert())(stmt => { stmt.string(1) = session_name stmt.string(2) = doc.name stmt.string(3) = doc.sources.toString stmt.bytes(4) = doc.log_xz stmt.bytes(5) = doc.pdf stmt.execute() }) } /* context */ val isabelle_styles: List[Path] = List("comment.sty", "isabelle.sty", "isabellesym.sty", "pdfsetup.sty", "railsetup.sty"). map(name => Path.explode("~~/lib/texinputs") + Path.basic(name)) def context( session: String, deps: Sessions.Deps, db_context: Sessions.Database_Context, progress: Progress = new Progress): Context = { val info = deps.sessions_structure(session) val base = deps(session) val hierarchy = deps.sessions_structure.build_hierarchy(session) new Context(info, base, hierarchy, db_context, progress) } final class Context private[Document_Build]( info: Sessions.Info, base: Sessions.Base, hierarchy: List[String], db_context: Sessions.Database_Context, val progress: Progress = new Progress) { /* session info */ def session: String = info.name def options: Options = info.options def document_bibliography: Boolean = options.bool("document_bibliography") def document_logo: Option[String] = options.string("document_logo") match { case "" => None case "_" => Some("") case name => Some(name) } def document_build: String = options.string("document_build") def get_engine(): Engine = { val name = document_build engines.find(_.name == name).getOrElse(error("Bad document_build engine " + quote(name))) } def get_export(theory: String, name: String): Export.Entry = db_context.get_export(hierarchy, theory, name) /* document content */ def documents: List[Document_Variant] = info.documents def session_theories: List[Document.Node.Name] = base.session_theories def document_theories: List[Document.Node.Name] = session_theories ::: base.document_theories lazy val document_latex: List[File.Content_XML] = for (name <- document_theories) yield { val path = Path.basic(tex_name(name)) val content = YXML.parse_body(get_export(name.theory, Export.DOCUMENT_LATEX).text) File.Content(path, content) } lazy val session_graph: File.Content = { val path = Presentation.session_graph_path val content = graphview.Graph_File.make_pdf(options, base.session_graph_display) File.Content(path, content) } lazy val session_tex: File.Content = { val path = Path.basic("session.tex") val content = Library.terminate_lines( base.session_theories.map(name => "\\input{" + tex_name(name) + "}")) File.Content(path, content) } lazy val isabelle_logo: Option[File.Content] = { document_logo.map(logo_name => Isabelle_System.with_tmp_file("logo", ext = "pdf")(tmp_path => { Logo.create_logo(logo_name, output_file = tmp_path, quiet = true) val path = Path.basic("isabelle_logo.pdf") val content = Bytes.read(tmp_path) File.Content(path, content) })) } /* document directory */ def prepare_directory(dir: Path, doc: Document_Variant, latex_output: Latex.Output): Directory = { val doc_dir = Isabelle_System.make_directory(dir + Path.basic(doc.name)) /* actual sources: with SHA1 digest */ isabelle_styles.foreach(Isabelle_System.copy_file(_, doc_dir)) - doc.isabelletags.write(doc_dir) + doc.tags.sty.write(doc_dir) for ((base_dir, src) <- info.document_files) { Isabelle_System.copy_file_base(info.dir + base_dir, src, doc_dir) } session_tex.write(doc_dir) for (content <- document_latex) { content.output(latex_output(_, file_pos = content.path.implode_symbolic)) .write(doc_dir) } val root_name1 = "root_" + doc.name val root_name = if ((doc_dir + Path.explode(root_name1).tex).is_file) root_name1 else "root" val digests1 = List(doc.print, document_logo.toString, document_build).map(SHA1.digest) val digests2 = File.find_files(doc_dir.file, follow_links = true).map(SHA1.digest) val sources = SHA1.digest_set(digests1 ::: digests2) /* derived material: without SHA1 digest */ isabelle_logo.foreach(_.write(doc_dir)) session_graph.write(doc_dir) Directory(doc_dir, doc, root_name, sources) } def old_document(directory: Directory): Option[Document_Output] = for { old_doc <- db_context.input_database(session)(read_document(_, _, directory.doc.name)) if old_doc.sources == directory.sources } yield old_doc } sealed case class Directory( doc_dir: Path, doc: Document_Variant, root_name: String, sources: SHA1.Digest) { def root_name_script(ext: String = ""): String = Bash.string(if (ext.isEmpty) root_name else root_name + "." + ext) def conditional_script(ext: String, exe: String, after: String = ""): String = "if [ -f " + root_name_script(ext) + " ]\n" + "then\n" + " " + exe + " " + root_name_script() + "\n" + (if (after.isEmpty) "" else " " + after) + "fi\n" def log_errors(): List[String] = Latex.latex_errors(doc_dir, root_name) ::: Bibtex.bibtex_errors(doc_dir, root_name) def make_document(log: List[String], errors: List[String]): Document_Output = { val root_pdf = Path.basic(root_name).pdf val result_pdf = doc_dir + root_pdf if (errors.nonEmpty) { val errors1 = errors ::: List("Failed to build document " + quote(doc.name)) throw new Build_Error(log, Exn.cat_message(errors1: _*)) } else if (!result_pdf.is_file) { val message = "Bad document result: expected to find " + root_pdf throw new Build_Error(log, message) } else { val log_xz = Bytes(cat_lines(log)).compress() val pdf = Bytes.read(result_pdf) Document_Output(doc.name, sources, log_xz, pdf) } } } /* build engines */ lazy val engines: List[Engine] = Isabelle_System.make_services(classOf[Engine]) abstract class Engine(val name: String) extends Isabelle_System.Service { override def toString: String = name def prepare_directory(context: Context, dir: Path, doc: Document_Variant): Directory def build_document(context: Context, directory: Directory, verbose: Boolean): Document_Output } abstract class Bash_Engine(name: String) extends Engine(name) { def prepare_directory(context: Context, dir: Path, doc: Document_Variant): Directory = context.prepare_directory(dir, doc, new Latex.Output(context.options)) def use_pdflatex: Boolean = false def latex_script(context: Context, directory: Directory): String = (if (use_pdflatex) "$ISABELLE_PDFLATEX" else "$ISABELLE_LUALATEX") + " " + directory.root_name_script() + "\n" def bibtex_script(context: Context, directory: Directory, latex: Boolean = false): String = { val ext = if (context.document_bibliography) "aux" else "bib" directory.conditional_script(ext, "$ISABELLE_BIBTEX", after = if (latex) latex_script(context, directory) else "") } def makeindex_script(context: Context, directory: Directory, latex: Boolean = false): String = directory.conditional_script("idx", "$ISABELLE_MAKEINDEX", after = if (latex) latex_script(context, directory) else "") def use_build_script: Boolean = false def build_script(context: Context, directory: Directory): String = { val has_build_script = (directory.doc_dir + Path.explode("build")).is_file if (!use_build_script && has_build_script) { error("Unexpected document build script for option document_build=" + quote(context.document_build)) } else if (use_build_script && !has_build_script) error("Missing document build script") else if (has_build_script) "./build pdf " + Bash.string(directory.doc.name) else { "set -e\n" + latex_script(context, directory) + bibtex_script(context, directory, latex = true) + makeindex_script(context, directory) + latex_script(context, directory) + makeindex_script(context, directory, latex = true) } } def build_document(context: Context, directory: Directory, verbose: Boolean): Document_Output = { val result = context.progress.bash( build_script(context, directory), cwd = directory.doc_dir.file, echo = verbose, watchdog = Time.seconds(0.5)) val log = result.out_lines ::: result.err_lines val errors = (if (result.ok) Nil else List(result.err)) ::: directory.log_errors() directory.make_document(log, errors) } } class LuaLaTeX_Engine extends Bash_Engine("lualatex") class PDFLaTeX_Engine extends Bash_Engine("pdflatex") { override def use_pdflatex: Boolean = true } class Build_Engine extends Bash_Engine("build") { override def use_build_script: Boolean = true } /* build documents */ def tex_name(name: Document.Node.Name): String = name.theory_base_name + ".tex" class Build_Error(val log_lines: List[String], val message: String) extends Exn.User_Error(message) def build_documents( context: Context, output_sources: Option[Path] = None, output_pdf: Option[Path] = None, verbose: Boolean = false): List[Document_Output] = { val progress = context.progress val engine = context.get_engine() val documents = for (doc <- context.documents) yield { Isabelle_System.with_tmp_dir("document")(tmp_dir => { progress.echo("Preparing " + context.session + "/" + doc.name + " ...") val start = Time.now() output_sources.foreach(engine.prepare_directory(context, _, doc)) val directory = engine.prepare_directory(context, tmp_dir, doc) val document = context.old_document(directory) getOrElse engine.build_document(context, directory, verbose) val stop = Time.now() val timing = stop - start progress.echo("Finished " + context.session + "/" + doc.name + " (" + timing.message_hms + " elapsed time)") document }) } for (dir <- output_pdf; doc <- documents) { val path = doc.write(dir) progress.echo("Document at " + path.absolute) } documents } /* Isabelle tool wrapper */ val isabelle_tool = Isabelle_Tool("document", "prepare session theory document", Scala_Project.here, args => { var output_sources: Option[Path] = None var output_pdf: Option[Path] = None var verbose_latex = false var dirs: List[Path] = Nil var options = Options.init() var verbose_build = false val getopts = Getopts( """ Usage: isabelle document [OPTIONS] SESSION Options are: -O DIR output directory for LaTeX sources and resulting PDF -P DIR output directory for resulting PDF -S DIR output directory for LaTeX sources -V verbose latex -d DIR include session directory -o OPTION override Isabelle system OPTION (via NAME=VAL or NAME) -v verbose build Prepare the theory document of a session. """, "O:" -> (arg => { val dir = Path.explode(arg) output_sources = Some(dir) output_pdf = Some(dir) }), "P:" -> (arg => { output_pdf = Some(Path.explode(arg)) }), "S:" -> (arg => { output_sources = Some(Path.explode(arg)) }), "V" -> (_ => verbose_latex = true), "d:" -> (arg => dirs = dirs ::: List(Path.explode(arg))), "o:" -> (arg => options = options + arg), "v" -> (_ => verbose_build = true)) val more_args = getopts(args) val session = more_args match { case List(a) => a case _ => getopts.usage() } val progress = new Console_Progress(verbose = verbose_build) val store = Sessions.store(options) progress.interrupt_handler { val res = Build.build(options, selection = Sessions.Selection.session(session), dirs = dirs, progress = progress, verbose = verbose_build) if (!res.ok) error("Failed to build session " + quote(session)) val deps = Sessions.load_structure(options + "document=pdf", dirs = dirs). selection_deps(Sessions.Selection.session(session)) if (output_sources.isEmpty && output_pdf.isEmpty) { progress.echo_warning("No output directory") } using(store.open_database_context())(db_context => { build_documents(context(session, deps, db_context, progress = progress), output_sources = output_sources, output_pdf = output_pdf, verbose = verbose_latex) }) } }) } diff --git a/src/Pure/Thy/latex.scala b/src/Pure/Thy/latex.scala --- a/src/Pure/Thy/latex.scala +++ b/src/Pure/Thy/latex.scala @@ -1,373 +1,433 @@ /* Title: Pure/Thy/latex.scala Author: Makarius Support for LaTeX. */ package isabelle import java.io.{File => JFile} import scala.annotation.tailrec import scala.collection.mutable +import scala.collection.immutable.TreeMap import scala.util.matching.Regex object Latex { /* output name for LaTeX macros */ private val output_name_map: Map[Char, String] = Map('_' -> "UNDERSCORE", '\'' -> "PRIME", '0' -> "ZERO", '1' -> "ONE", '2' -> "TWO", '3' -> "THREE", '4' -> "FOUR", '5' -> "FIVE", '6' -> "SIX", '7' -> "SEVEN", '8' -> "EIGHT", '9' -> "NINE") def output_name(name: String): String = if (name.exists(output_name_map.keySet)) { val res = new StringBuilder for (c <- name) { output_name_map.get(c) match { case None => res += c case Some(s) => res ++= s } } res.toString } else name /* index entries */ def index_escape(str: String): String = { val special1 = "!\"@|" val special2 = "\\{}#" if (str.exists(c => special1.contains(c) || special2.contains(c))) { val res = new StringBuilder for (c <- str) { if (special1.contains(c)) { res ++= "\\char" res ++= Value.Int(c) } else { if (special2.contains(c)) { res += '"'} res += c } } res.toString } else str } object Index_Item { sealed case class Value(text: Text, like: String) def unapply(tree: XML.Tree): Option[Value] = tree match { case XML.Wrapped_Elem(Markup.Latex_Index_Item(_), text, like) => Some(Value(text, XML.content(like))) case _ => None } } object Index_Entry { sealed case class Value(items: List[Index_Item.Value], kind: String) def unapply(tree: XML.Tree): Option[Value] = tree match { case XML.Elem(Markup.Latex_Index_Entry(kind), body) => val items = body.map(Index_Item.unapply) if (items.forall(_.isDefined)) Some(Value(items.map(_.get), kind)) else None case _ => None } } + /* tags */ + + object Tags + { + object Op extends Enumeration + { + val fold, drop, keep = Value + } + + val standard = "document,theory,proof,ML,visible,-invisible,important,unimportant" + + private def explode(spec: String): List[String] = + Library.space_explode(',', spec) + + def apply(spec: String): Tags = + new Tags(spec, + (explode(standard) ::: explode(spec)).foldLeft(TreeMap.empty[String, Op.Value]) { + case (m, tag) => + tag.toList match { + case '/' :: cs => m + (cs.mkString -> Op.fold) + case '-' :: cs => m + (cs.mkString -> Op.drop) + case '+' :: cs => m + (cs.mkString -> Op.keep) + case cs => m + (cs.mkString -> Op.keep) + } + }) + + val empty: Tags = apply("") + } + + class Tags private(spec: String, map: TreeMap[String, Tags.Op.Value]) + { + override def toString: String = spec + + def get(name: String): Option[Tags.Op.Value] = map.get(name) + + def sty: File.Content = + { + val path = Path.explode("isabelletags.sty") + val tags = + (for ((name, op) <- map.iterator) + yield "\\isa" + op + "tag{" + name + "}").toList + File.Content(path, """ +%plain TeX version of comment package -- much faster! +\let\isafmtname\fmtname\def\fmtname{plain} +\usepackage{comment} +\let\fmtname\isafmtname + +\newcommand{\isakeeptag}[1]% +{\includecomment{isadelim#1}\includecomment{isatag#1}\csarg\def{isafold#1}{}} +\newcommand{\isadroptag}[1]% +{\excludecomment{isadelim#1}\excludecomment{isatag#1}\csarg\def{isafold#1}{}} +\newcommand{\isafoldtag}[1]% +{\includecomment{isadelim#1}\excludecomment{isatag#1}\csarg\def{isafold#1}{\isafold{#1}}} + +""" + Library.terminate_lines(tags)) + } + } + + /* output text and positions */ type Text = XML.Body def position(a: String, b: String): String = "%:%" + a + "=" + b + "%:%\n" def init_position(file_pos: String): List[String] = if (file_pos.isEmpty) Nil else List("\\endinput\n", position(Markup.FILE, file_pos)) class Output(options: Options) { def latex_output(latex_text: Text): String = apply(latex_text) def latex_macro0(name: String, optional_argument: String = ""): Text = XML.string("\\" + name + optional_argument) def latex_macro(name: String, body: Text, optional_argument: String = ""): Text = XML.enclose("\\" + name + optional_argument + "{", "}", body) def latex_environment(name: String, body: Text, optional_argument: String = ""): Text = XML.enclose( "%\n\\begin{" + name + "}" + optional_argument + "%\n", "%\n\\end{" + name + "}", body) def latex_heading(kind: String, body: Text, optional_argument: String = ""): Text = XML.enclose( "%\n\\" + options.string("document_heading_prefix") + kind + optional_argument + "{", "%\n}\n", body) def latex_body(kind: String, body: Text, optional_argument: String = ""): Text = latex_environment("isamarkup" + kind, body, optional_argument) def latex_delim(name: String, body: Text): Text = { val s = output_name(name) XML.enclose("%\n\\isadelim" + s + "\n", "%\n\\endisadelim" + s + "\n", body) } def latex_tag(name: String, body: Text): Text = { val s = output_name(name) XML.enclose("%\n\\isatag" + s + "\n", "%\n\\endisatag" + s + "\n{\\isafold" + s + "}%\n", body) } def index_item(item: Index_Item.Value): String = { val like = if (item.like.isEmpty) "" else index_escape(item.like) + "@" val text = index_escape(latex_output(item.text)) like + text } def index_entry(entry: Index_Entry.Value): Text = { val items = entry.items.map(index_item).mkString("!") val kind = if (entry.kind.isEmpty) "" else "|" + index_escape(entry.kind) latex_macro("index", XML.string(items + kind)) } /* standard output of text with per-line positions */ def unknown_elem(elem: XML.Elem, pos: Position.T): XML.Body = error("Unknown latex markup element " + quote(elem.name) + Position.here(pos) + ":\n" + XML.string_of_tree(elem)) def apply(latex_text: Text, file_pos: String = ""): String = { var line = 1 val result = new mutable.ListBuffer[String] val positions = new mutable.ListBuffer[String] ++= init_position(file_pos) val file_position = if (file_pos.isEmpty) Position.none else Position.File(file_pos) def traverse(xml: XML.Body): Unit = { xml.foreach { case XML.Text(s) => line += s.count(_ == '\n') result += s case elem @ XML.Elem(markup, body) => val a = Markup.Optional_Argument.get(markup.properties) traverse { markup match { case Markup.Document_Latex(props) => for (l <- Position.Line.unapply(props) if positions.nonEmpty) { val s = position(Value.Int(line), Value.Int(l)) if (positions.last != s) positions += s } body case Markup.Latex_Output(_) => XML.string(latex_output(body)) case Markup.Latex_Macro0(name) if body.isEmpty => latex_macro0(name, a) case Markup.Latex_Macro(name) => latex_macro(name, body, a) case Markup.Latex_Environment(name) => latex_environment(name, body, a) case Markup.Latex_Heading(kind) => latex_heading(kind, body, a) case Markup.Latex_Body(kind) => latex_body(kind, body, a) case Markup.Latex_Delim(name) => latex_delim(name, body) case Markup.Latex_Tag(name) => latex_tag(name, body) case Markup.Latex_Index_Entry(_) => elem match { case Index_Entry(entry) => index_entry(entry) case _ => unknown_elem(elem, file_position) } case _ => unknown_elem(elem, file_position) } } } } traverse(latex_text) result ++= positions result.mkString } } /* generated .tex file */ private val File_Pattern = """^%:%file=(.+)%:%$""".r private val Line_Pattern = """^*%:%(\d+)=(\d+)%:%$""".r def read_tex_file(tex_file: Path): Tex_File = { val positions = Line.logical_lines(File.read(tex_file)).reverse. takeWhile(_ != "\\endinput").reverse val source_file = positions match { case File_Pattern(file) :: _ => Some(file) case _ => None } val source_lines = if (source_file.isEmpty) Nil else positions.flatMap(line => line match { case Line_Pattern(Value.Int(line), Value.Int(source_line)) => Some(line -> source_line) case _ => None }) new Tex_File(tex_file, source_file, source_lines) } final class Tex_File private[Latex]( tex_file: Path, source_file: Option[String], source_lines: List[(Int, Int)]) { override def toString: String = tex_file.toString def source_position(l: Int): Option[Position.T] = source_file match { case None => None case Some(file) => val source_line = source_lines.iterator.takeWhile({ case (m, _) => m <= l }). foldLeft(0) { case (_, (_, n)) => n } if (source_line == 0) None else Some(Position.Line_File(source_line, file)) } def position(line: Int): Position.T = source_position(line) getOrElse Position.Line_File(line, tex_file.implode) } /* latex log */ def latex_errors(dir: Path, root_name: String): List[String] = { val root_log_path = dir + Path.explode(root_name).ext("log") if (root_log_path.is_file) { for { (msg, pos) <- filter_errors(dir, File.read(root_log_path)) } yield "Latex error" + Position.here(pos) + ":\n" + Library.indent_lines(2, msg) } else Nil } def filter_errors(dir: Path, root_log: String): List[(String, Position.T)] = { val seen_files = Synchronized(Map.empty[JFile, Tex_File]) def check_tex_file(path: Path): Option[Tex_File] = seen_files.change_result(seen => seen.get(path.file) match { case None => if (path.is_file) { val tex_file = read_tex_file(path) (Some(tex_file), seen + (path.file -> tex_file)) } else (None, seen) case some => (some, seen) }) def tex_file_position(path: Path, line: Int): Position.T = check_tex_file(path) match { case Some(tex_file) => tex_file.position(line) case None => Position.Line_File(line, path.implode) } object File_Line_Error { val Pattern: Regex = """^(.*?\.\w\w\w):(\d+): (.*)$""".r def unapply(line: String): Option[(Path, Int, String)] = line match { case Pattern(file, Value.Int(line), message) => val path = File.standard_path(file) if (Path.is_wellformed(path)) { val file = (dir + Path.explode(path)).canonical val msg = Library.perhaps_unprefix("LaTeX Error: ", message) if (file.is_file) Some((file, line, msg)) else None } else None case _ => None } } val Line_Error = """^l\.\d+ (.*)$""".r val More_Error = List( "", "