author | wenzelm |
Wed, 10 Nov 2021 19:45:30 +0100 | |
changeset 74748 | 95643a0bff49 |
parent 73783 | e4d50a660140 |
child 74777 | 2fd0c33fe440 |
permissions | -rw-r--r-- |
67172 | 1 |
/* Title: Pure/Thy/latex.scala |
2 |
Author: Makarius |
|
3 |
||
4 |
Support for LaTeX. |
|
5 |
*/ |
|
6 |
||
7 |
package isabelle |
|
8 |
||
9 |
||
67173 | 10 |
import java.io.{File => JFile} |
11 |
||
67172 | 12 |
import scala.annotation.tailrec |
74748 | 13 |
import scala.collection.mutable |
71601 | 14 |
import scala.util.matching.Regex |
67172 | 15 |
|
16 |
||
17 |
object Latex |
|
18 |
{ |
|
73780
466fae6bf22e
compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents:
73736
diff
changeset
|
19 |
/* output text and positions */ |
466fae6bf22e
compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents:
73736
diff
changeset
|
20 |
|
466fae6bf22e
compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents:
73736
diff
changeset
|
21 |
type Text = XML.Body |
466fae6bf22e
compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents:
73736
diff
changeset
|
22 |
|
466fae6bf22e
compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents:
73736
diff
changeset
|
23 |
def output(latex_text: Text, file_pos: String = ""): String = |
466fae6bf22e
compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents:
73736
diff
changeset
|
24 |
{ |
74748 | 25 |
var line = 1 |
26 |
val result = new mutable.ListBuffer[String] |
|
27 |
val positions = new mutable.ListBuffer[String] |
|
73780
466fae6bf22e
compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents:
73736
diff
changeset
|
28 |
|
74748 | 29 |
def position(a: String, b: String): String = "%:%" + a + "=" + b + "%:%\n" |
73780
466fae6bf22e
compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents:
73736
diff
changeset
|
30 |
|
74748 | 31 |
if (file_pos.nonEmpty) { |
32 |
positions += "\\endinput\n" |
|
33 |
positions += position(Markup.FILE, file_pos) |
|
34 |
} |
|
73780
466fae6bf22e
compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents:
73736
diff
changeset
|
35 |
|
73783 | 36 |
def traverse(body: XML.Body): Unit = |
73780
466fae6bf22e
compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents:
73736
diff
changeset
|
37 |
{ |
466fae6bf22e
compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents:
73736
diff
changeset
|
38 |
body.foreach { |
466fae6bf22e
compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents:
73736
diff
changeset
|
39 |
case XML.Wrapped_Elem(_, _, _) => |
466fae6bf22e
compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents:
73736
diff
changeset
|
40 |
case XML.Elem(markup, body) => |
466fae6bf22e
compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents:
73736
diff
changeset
|
41 |
if (markup.name == Markup.DOCUMENT_LATEX) { |
466fae6bf22e
compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents:
73736
diff
changeset
|
42 |
for { l <- Position.Line.unapply(markup.properties) if positions.nonEmpty } { |
466fae6bf22e
compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents:
73736
diff
changeset
|
43 |
val s = position(Value.Int(line), Value.Int(l)) |
74748 | 44 |
if (positions.last != s) positions += s |
73780
466fae6bf22e
compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents:
73736
diff
changeset
|
45 |
} |
73783 | 46 |
traverse(body) |
73780
466fae6bf22e
compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents:
73736
diff
changeset
|
47 |
} |
466fae6bf22e
compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents:
73736
diff
changeset
|
48 |
case XML.Text(s) => |
466fae6bf22e
compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents:
73736
diff
changeset
|
49 |
line += s.count(_ == '\n') |
74748 | 50 |
result += s |
73780
466fae6bf22e
compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents:
73736
diff
changeset
|
51 |
} |
466fae6bf22e
compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents:
73736
diff
changeset
|
52 |
} |
73783 | 53 |
traverse(latex_text) |
73780
466fae6bf22e
compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents:
73736
diff
changeset
|
54 |
|
74748 | 55 |
result ++= positions |
56 |
result.mkString |
|
73780
466fae6bf22e
compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents:
73736
diff
changeset
|
57 |
} |
466fae6bf22e
compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents:
73736
diff
changeset
|
58 |
|
466fae6bf22e
compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents:
73736
diff
changeset
|
59 |
|
67173 | 60 |
/* generated .tex file */ |
61 |
||
67184 | 62 |
private val File_Pattern = """^%:%file=(.+)%:%$""".r |
67194
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
63 |
private val Line_Pattern = """^*%:%(\d+)=(\d+)%:%$""".r |
67173 | 64 |
|
65 |
def read_tex_file(tex_file: Path): Tex_File = |
|
67194
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
66 |
{ |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
67 |
val positions = |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
68 |
Line.logical_lines(File.read(tex_file)).reverse. |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
69 |
takeWhile(_ != "\\endinput").reverse |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
70 |
|
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
71 |
val source_file = |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
72 |
positions match { |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
73 |
case File_Pattern(file) :: _ => Some(file) |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
74 |
case _ => None |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
75 |
} |
67173 | 76 |
|
67194
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
77 |
val source_lines = |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
78 |
if (source_file.isEmpty) Nil |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
79 |
else |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
80 |
positions.flatMap(line => |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
81 |
line match { |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
82 |
case Line_Pattern(Value.Int(line), Value.Int(source_line)) => Some(line -> source_line) |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
83 |
case _ => None |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
84 |
}) |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
85 |
|
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
86 |
new Tex_File(tex_file, source_file, source_lines) |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
87 |
} |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
88 |
|
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
89 |
final class Tex_File private[Latex]( |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
90 |
tex_file: Path, source_file: Option[String], source_lines: List[(Int, Int)]) |
67173 | 91 |
{ |
92 |
override def toString: String = tex_file.toString |
|
93 |
||
67194
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
94 |
def source_position(l: Int): Option[Position.T] = |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
95 |
source_file match { |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
96 |
case None => None |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
97 |
case Some(file) => |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
98 |
val source_line = |
73359 | 99 |
source_lines.iterator.takeWhile({ case (m, _) => m <= l }). |
100 |
foldLeft(0) { case (_, (_, n)) => n } |
|
67194
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
101 |
if (source_line == 0) None else Some(Position.Line_File(source_line, file)) |
67173 | 102 |
} |
103 |
||
104 |
def position(line: Int): Position.T = |
|
67188
bc7a6455e12a
simplified positions -- line is also human-readable in generated .tex file;
wenzelm
parents:
67184
diff
changeset
|
105 |
source_position(line) getOrElse Position.Line_File(line, tex_file.implode) |
67173 | 106 |
} |
107 |
||
108 |
||
109 |
/* latex log */ |
|
110 |
||
73782 | 111 |
def latex_errors(dir: Path, root_name: String): List[String] = |
112 |
{ |
|
113 |
val root_log_path = dir + Path.explode(root_name).ext("log") |
|
114 |
if (root_log_path.is_file) { |
|
115 |
for { (msg, pos) <- filter_errors(dir, File.read(root_log_path)) } |
|
116 |
yield "Latex error" + Position.here(pos) + ":\n" + Library.indent_lines(2, msg) |
|
117 |
} |
|
118 |
else Nil |
|
119 |
} |
|
120 |
||
67173 | 121 |
def filter_errors(dir: Path, root_log: String): List[(String, Position.T)] = |
67172 | 122 |
{ |
67173 | 123 |
val seen_files = Synchronized(Map.empty[JFile, Tex_File]) |
124 |
||
125 |
def check_tex_file(path: Path): Option[Tex_File] = |
|
126 |
seen_files.change_result(seen => |
|
127 |
seen.get(path.file) match { |
|
128 |
case None => |
|
129 |
if (path.is_file) { |
|
130 |
val tex_file = read_tex_file(path) |
|
131 |
(Some(tex_file), seen + (path.file -> tex_file)) |
|
132 |
} |
|
133 |
else (None, seen) |
|
134 |
case some => (some, seen) |
|
135 |
}) |
|
136 |
||
137 |
def tex_file_position(path: Path, line: Int): Position.T = |
|
138 |
check_tex_file(path) match { |
|
139 |
case Some(tex_file) => tex_file.position(line) |
|
140 |
case None => Position.Line_File(line, path.implode) |
|
141 |
} |
|
142 |
||
67172 | 143 |
object File_Line_Error |
144 |
{ |
|
71601 | 145 |
val Pattern: Regex = """^(.*?\.\w\w\w):(\d+): (.*)$""".r |
67173 | 146 |
def unapply(line: String): Option[(Path, Int, String)] = |
67172 | 147 |
line match { |
148 |
case Pattern(file, Value.Int(line), message) => |
|
149 |
val path = File.standard_path(file) |
|
150 |
if (Path.is_wellformed(path)) { |
|
67183 | 151 |
val file = (dir + Path.explode(path)).canonical |
67423 | 152 |
val msg = Library.perhaps_unprefix("LaTeX Error: ", message) |
153 |
if (file.is_file) Some((file, line, msg)) else None |
|
67172 | 154 |
} |
155 |
else None |
|
156 |
case _ => None |
|
157 |
} |
|
158 |
} |
|
159 |
||
67418
5a6ed2e679fb
more general error suffixes, e.g. for messages that are broken over several lines;
wenzelm
parents:
67417
diff
changeset
|
160 |
val Line_Error = """^l\.\d+ (.*)$""".r |
67196
eb29f4868d14
more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents:
67194
diff
changeset
|
161 |
val More_Error = |
eb29f4868d14
more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents:
67194
diff
changeset
|
162 |
List( |
eb29f4868d14
more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents:
67194
diff
changeset
|
163 |
"<argument>", |
eb29f4868d14
more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents:
67194
diff
changeset
|
164 |
"<template>", |
eb29f4868d14
more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents:
67194
diff
changeset
|
165 |
"<recently read>", |
eb29f4868d14
more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents:
67194
diff
changeset
|
166 |
"<to be read again>", |
eb29f4868d14
more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents:
67194
diff
changeset
|
167 |
"<inserted text>", |
eb29f4868d14
more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents:
67194
diff
changeset
|
168 |
"<output>", |
eb29f4868d14
more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents:
67194
diff
changeset
|
169 |
"<everypar>", |
eb29f4868d14
more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents:
67194
diff
changeset
|
170 |
"<everymath>", |
eb29f4868d14
more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents:
67194
diff
changeset
|
171 |
"<everydisplay>", |
eb29f4868d14
more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents:
67194
diff
changeset
|
172 |
"<everyhbox>", |
eb29f4868d14
more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents:
67194
diff
changeset
|
173 |
"<everyvbox>", |
eb29f4868d14
more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents:
67194
diff
changeset
|
174 |
"<everyjob>", |
eb29f4868d14
more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents:
67194
diff
changeset
|
175 |
"<everycr>", |
eb29f4868d14
more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents:
67194
diff
changeset
|
176 |
"<mark>", |
eb29f4868d14
more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents:
67194
diff
changeset
|
177 |
"<everyeof>", |
eb29f4868d14
more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents:
67194
diff
changeset
|
178 |
"<write>").mkString("^(?:", "|", ") (.*)$").r |
67172 | 179 |
|
73474
4e12a6caefb3
turn LaTeX warning into error, for the sake of isabelle.sty/bbbfont;
wenzelm
parents:
73359
diff
changeset
|
180 |
val Bad_Font = """^LaTeX Font Warning: (Font .*\btxmia\b.* undefined).*$""".r |
67483
aae933ca6fbd
tuned message: same error may occur in different contexts;
wenzelm
parents:
67482
diff
changeset
|
181 |
val Bad_File = """^! LaTeX Error: (File `.*' not found\.)$""".r |
67482 | 182 |
|
67423 | 183 |
val error_ignore = |
184 |
Set( |
|
185 |
"See the LaTeX manual or LaTeX Companion for explanation.", |
|
186 |
"Type H <return> for immediate help.") |
|
187 |
||
67418
5a6ed2e679fb
more general error suffixes, e.g. for messages that are broken over several lines;
wenzelm
parents:
67417
diff
changeset
|
188 |
def error_suffix1(lines: List[String]): Option[String] = |
5a6ed2e679fb
more general error suffixes, e.g. for messages that are broken over several lines;
wenzelm
parents:
67417
diff
changeset
|
189 |
{ |
71784 | 190 |
val lines1 = |
191 |
lines.take(20).takeWhile({ case File_Line_Error((_, _, _)) => false case _ => true }) |
|
67418
5a6ed2e679fb
more general error suffixes, e.g. for messages that are broken over several lines;
wenzelm
parents:
67417
diff
changeset
|
192 |
lines1.zipWithIndex.collectFirst({ |
67423 | 193 |
case (Line_Error(msg), i) => |
194 |
cat_lines(lines1.take(i).filterNot(error_ignore) ::: List(msg)) }) |
|
67418
5a6ed2e679fb
more general error suffixes, e.g. for messages that are broken over several lines;
wenzelm
parents:
67417
diff
changeset
|
195 |
} |
5a6ed2e679fb
more general error suffixes, e.g. for messages that are broken over several lines;
wenzelm
parents:
67417
diff
changeset
|
196 |
def error_suffix2(lines: List[String]): Option[String] = |
5a6ed2e679fb
more general error suffixes, e.g. for messages that are broken over several lines;
wenzelm
parents:
67417
diff
changeset
|
197 |
lines match { |
5a6ed2e679fb
more general error suffixes, e.g. for messages that are broken over several lines;
wenzelm
parents:
67417
diff
changeset
|
198 |
case More_Error(msg) :: _ => Some(msg) |
5a6ed2e679fb
more general error suffixes, e.g. for messages that are broken over several lines;
wenzelm
parents:
67417
diff
changeset
|
199 |
case _ => None |
5a6ed2e679fb
more general error suffixes, e.g. for messages that are broken over several lines;
wenzelm
parents:
67417
diff
changeset
|
200 |
} |
5a6ed2e679fb
more general error suffixes, e.g. for messages that are broken over several lines;
wenzelm
parents:
67417
diff
changeset
|
201 |
|
67173 | 202 |
@tailrec def filter(lines: List[String], result: List[(String, Position.T)]) |
203 |
: List[(String, Position.T)] = |
|
204 |
{ |
|
67172 | 205 |
lines match { |
67173 | 206 |
case File_Line_Error((file, line, msg1)) :: rest1 => |
67183 | 207 |
val pos = tex_file_position(file, line) |
67418
5a6ed2e679fb
more general error suffixes, e.g. for messages that are broken over several lines;
wenzelm
parents:
67417
diff
changeset
|
208 |
val msg2 = error_suffix1(rest1) orElse error_suffix2(rest1) getOrElse "" |
5a6ed2e679fb
more general error suffixes, e.g. for messages that are broken over several lines;
wenzelm
parents:
67417
diff
changeset
|
209 |
filter(rest1, (Exn.cat_message(msg1, msg2), pos) :: result) |
73474
4e12a6caefb3
turn LaTeX warning into error, for the sake of isabelle.sty/bbbfont;
wenzelm
parents:
73359
diff
changeset
|
210 |
case Bad_Font(msg) :: rest => |
4e12a6caefb3
turn LaTeX warning into error, for the sake of isabelle.sty/bbbfont;
wenzelm
parents:
73359
diff
changeset
|
211 |
filter(rest, (msg, Position.none) :: result) |
67483
aae933ca6fbd
tuned message: same error may occur in different contexts;
wenzelm
parents:
67482
diff
changeset
|
212 |
case Bad_File(msg) :: rest => |
67482 | 213 |
filter(rest, (msg, Position.none) :: result) |
67172 | 214 |
case _ :: rest => filter(rest, result) |
215 |
case Nil => result.reverse |
|
216 |
} |
|
67173 | 217 |
} |
67172 | 218 |
|
67174 | 219 |
filter(Line.logical_lines(root_log), Nil) |
67173 | 220 |
} |
67172 | 221 |
} |