author | wenzelm |
Wed, 24 Nov 2021 15:33:43 +0100 | |
changeset 74838 | 4c8d9479f916 |
parent 74836 | a97ec0954c50 |
child 74839 | 3bf746911da1 |
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 |
{ |
|
74826
0e4d8aa61ad7
more symbolic latex_output via XML (using YXML within text);
wenzelm
parents:
74824
diff
changeset
|
19 |
/* output name for LaTeX macros */ |
0e4d8aa61ad7
more symbolic latex_output via XML (using YXML within text);
wenzelm
parents:
74824
diff
changeset
|
20 |
|
0e4d8aa61ad7
more symbolic latex_output via XML (using YXML within text);
wenzelm
parents:
74824
diff
changeset
|
21 |
private val output_name_map: Map[Char, String] = |
0e4d8aa61ad7
more symbolic latex_output via XML (using YXML within text);
wenzelm
parents:
74824
diff
changeset
|
22 |
Map('_' -> "UNDERSCORE", |
0e4d8aa61ad7
more symbolic latex_output via XML (using YXML within text);
wenzelm
parents:
74824
diff
changeset
|
23 |
'\'' -> "PRIME", |
0e4d8aa61ad7
more symbolic latex_output via XML (using YXML within text);
wenzelm
parents:
74824
diff
changeset
|
24 |
'0' -> "ZERO", |
0e4d8aa61ad7
more symbolic latex_output via XML (using YXML within text);
wenzelm
parents:
74824
diff
changeset
|
25 |
'1' -> "ONE", |
0e4d8aa61ad7
more symbolic latex_output via XML (using YXML within text);
wenzelm
parents:
74824
diff
changeset
|
26 |
'2' -> "TWO", |
0e4d8aa61ad7
more symbolic latex_output via XML (using YXML within text);
wenzelm
parents:
74824
diff
changeset
|
27 |
'3' -> "THREE", |
0e4d8aa61ad7
more symbolic latex_output via XML (using YXML within text);
wenzelm
parents:
74824
diff
changeset
|
28 |
'4' -> "FOUR", |
0e4d8aa61ad7
more symbolic latex_output via XML (using YXML within text);
wenzelm
parents:
74824
diff
changeset
|
29 |
'5' -> "FIVE", |
0e4d8aa61ad7
more symbolic latex_output via XML (using YXML within text);
wenzelm
parents:
74824
diff
changeset
|
30 |
'6' -> "SIX", |
0e4d8aa61ad7
more symbolic latex_output via XML (using YXML within text);
wenzelm
parents:
74824
diff
changeset
|
31 |
'7' -> "SEVEN", |
0e4d8aa61ad7
more symbolic latex_output via XML (using YXML within text);
wenzelm
parents:
74824
diff
changeset
|
32 |
'8' -> "EIGHT", |
0e4d8aa61ad7
more symbolic latex_output via XML (using YXML within text);
wenzelm
parents:
74824
diff
changeset
|
33 |
'9' -> "NINE") |
0e4d8aa61ad7
more symbolic latex_output via XML (using YXML within text);
wenzelm
parents:
74824
diff
changeset
|
34 |
|
0e4d8aa61ad7
more symbolic latex_output via XML (using YXML within text);
wenzelm
parents:
74824
diff
changeset
|
35 |
def output_name(name: String): String = |
0e4d8aa61ad7
more symbolic latex_output via XML (using YXML within text);
wenzelm
parents:
74824
diff
changeset
|
36 |
if (name.exists(output_name_map.keySet)) { |
0e4d8aa61ad7
more symbolic latex_output via XML (using YXML within text);
wenzelm
parents:
74824
diff
changeset
|
37 |
val res = new StringBuilder |
0e4d8aa61ad7
more symbolic latex_output via XML (using YXML within text);
wenzelm
parents:
74824
diff
changeset
|
38 |
for (c <- name) { |
0e4d8aa61ad7
more symbolic latex_output via XML (using YXML within text);
wenzelm
parents:
74824
diff
changeset
|
39 |
output_name_map.get(c) match { |
0e4d8aa61ad7
more symbolic latex_output via XML (using YXML within text);
wenzelm
parents:
74824
diff
changeset
|
40 |
case None => res += c |
0e4d8aa61ad7
more symbolic latex_output via XML (using YXML within text);
wenzelm
parents:
74824
diff
changeset
|
41 |
case Some(s) => res ++= s |
0e4d8aa61ad7
more symbolic latex_output via XML (using YXML within text);
wenzelm
parents:
74824
diff
changeset
|
42 |
} |
0e4d8aa61ad7
more symbolic latex_output via XML (using YXML within text);
wenzelm
parents:
74824
diff
changeset
|
43 |
} |
0e4d8aa61ad7
more symbolic latex_output via XML (using YXML within text);
wenzelm
parents:
74824
diff
changeset
|
44 |
res.toString |
0e4d8aa61ad7
more symbolic latex_output via XML (using YXML within text);
wenzelm
parents:
74824
diff
changeset
|
45 |
} |
0e4d8aa61ad7
more symbolic latex_output via XML (using YXML within text);
wenzelm
parents:
74824
diff
changeset
|
46 |
else name |
0e4d8aa61ad7
more symbolic latex_output via XML (using YXML within text);
wenzelm
parents:
74824
diff
changeset
|
47 |
|
0e4d8aa61ad7
more symbolic latex_output via XML (using YXML within text);
wenzelm
parents:
74824
diff
changeset
|
48 |
|
74786 | 49 |
/* index entries */ |
50 |
||
51 |
def index_escape(str: String): String = |
|
52 |
{ |
|
53 |
val special1 = "!\"@|" |
|
54 |
val special2 = "\\{}#" |
|
55 |
if (str.exists(c => special1.contains(c) || special2.contains(c))) { |
|
56 |
val res = new StringBuilder |
|
57 |
for (c <- str) { |
|
58 |
if (special1.contains(c)) { |
|
59 |
res ++= "\\char" |
|
60 |
res ++= Value.Int(c) |
|
61 |
} |
|
62 |
else { |
|
63 |
if (special2.contains(c)) { res += '"'} |
|
64 |
res += c |
|
65 |
} |
|
66 |
} |
|
67 |
res.toString |
|
68 |
} |
|
69 |
else str |
|
70 |
} |
|
71 |
||
72 |
object Index_Item |
|
73 |
{ |
|
74 |
sealed case class Value(text: Text, like: String) |
|
75 |
def unapply(tree: XML.Tree): Option[Value] = |
|
76 |
tree match { |
|
77 |
case XML.Wrapped_Elem(Markup.Latex_Index_Item(_), text, like) => |
|
78 |
Some(Value(text, XML.content(like))) |
|
79 |
case _ => None |
|
80 |
} |
|
81 |
} |
|
82 |
||
83 |
object Index_Entry |
|
84 |
{ |
|
85 |
sealed case class Value(items: List[Index_Item.Value], kind: String) |
|
86 |
def unapply(tree: XML.Tree): Option[Value] = |
|
87 |
tree match { |
|
88 |
case XML.Elem(Markup.Latex_Index_Entry(kind), body) => |
|
89 |
val items = body.map(Index_Item.unapply) |
|
90 |
if (items.forall(_.isDefined)) Some(Value(items.map(_.get), kind)) else None |
|
91 |
case _ => None |
|
92 |
} |
|
93 |
} |
|
94 |
||
95 |
||
73780
466fae6bf22e
compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents:
73736
diff
changeset
|
96 |
/* output text and positions */ |
466fae6bf22e
compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents:
73736
diff
changeset
|
97 |
|
466fae6bf22e
compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents:
73736
diff
changeset
|
98 |
type Text = XML.Body |
466fae6bf22e
compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents:
73736
diff
changeset
|
99 |
|
74777
2fd0c33fe440
clarified signature: Latex.Output as parameter to Document_Build.Engine;
wenzelm
parents:
74748
diff
changeset
|
100 |
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
|
101 |
|
74777
2fd0c33fe440
clarified signature: Latex.Output as parameter to Document_Build.Engine;
wenzelm
parents:
74748
diff
changeset
|
102 |
def init_position(file_pos: String): List[String] = |
2fd0c33fe440
clarified signature: Latex.Output as parameter to Document_Build.Engine;
wenzelm
parents:
74748
diff
changeset
|
103 |
if (file_pos.isEmpty) Nil |
2fd0c33fe440
clarified signature: Latex.Output as parameter to Document_Build.Engine;
wenzelm
parents:
74748
diff
changeset
|
104 |
else List("\\endinput\n", position(Markup.FILE, file_pos)) |
73780
466fae6bf22e
compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents:
73736
diff
changeset
|
105 |
|
74824
6424f74fd9d4
Latex.Output.latex_heading depends on option document_heading_prefix, e.g. relevant for Dagstuhl LIPIcs which prefers unaliased \section etc.;
wenzelm
parents:
74823
diff
changeset
|
106 |
class Output(options: Options) |
74777
2fd0c33fe440
clarified signature: Latex.Output as parameter to Document_Build.Engine;
wenzelm
parents:
74748
diff
changeset
|
107 |
{ |
74786 | 108 |
def latex_output(latex_text: Text): String = apply(latex_text) |
109 |
||
74838
4c8d9479f916
more uniform treatment of optional_argument for Latex elements;
wenzelm
parents:
74836
diff
changeset
|
110 |
def latex_macro0(name: String, optional_argument: String = ""): Text = |
4c8d9479f916
more uniform treatment of optional_argument for Latex elements;
wenzelm
parents:
74836
diff
changeset
|
111 |
XML.string("\\" + name + optional_argument) |
74790 | 112 |
|
74838
4c8d9479f916
more uniform treatment of optional_argument for Latex elements;
wenzelm
parents:
74836
diff
changeset
|
113 |
def latex_macro(name: String, body: Text, optional_argument: String = ""): Text = |
4c8d9479f916
more uniform treatment of optional_argument for Latex elements;
wenzelm
parents:
74836
diff
changeset
|
114 |
XML.enclose("\\" + name + optional_argument + "{", "}", body) |
74790 | 115 |
|
74838
4c8d9479f916
more uniform treatment of optional_argument for Latex elements;
wenzelm
parents:
74836
diff
changeset
|
116 |
def latex_environment(name: String, body: Text, optional_argument: String = ""): Text = |
4c8d9479f916
more uniform treatment of optional_argument for Latex elements;
wenzelm
parents:
74836
diff
changeset
|
117 |
XML.enclose( |
4c8d9479f916
more uniform treatment of optional_argument for Latex elements;
wenzelm
parents:
74836
diff
changeset
|
118 |
"%\n\\begin{" + name + "}" + optional_argument + "%\n", |
4c8d9479f916
more uniform treatment of optional_argument for Latex elements;
wenzelm
parents:
74836
diff
changeset
|
119 |
"%\n\\end{" + name + "}", body) |
74823 | 120 |
|
74838
4c8d9479f916
more uniform treatment of optional_argument for Latex elements;
wenzelm
parents:
74836
diff
changeset
|
121 |
def latex_heading(kind: String, body: Text, optional_argument: String = ""): Text = |
4c8d9479f916
more uniform treatment of optional_argument for Latex elements;
wenzelm
parents:
74836
diff
changeset
|
122 |
XML.enclose( |
4c8d9479f916
more uniform treatment of optional_argument for Latex elements;
wenzelm
parents:
74836
diff
changeset
|
123 |
"%\n\\" + options.string("document_heading_prefix") + kind + optional_argument + "{", |
4c8d9479f916
more uniform treatment of optional_argument for Latex elements;
wenzelm
parents:
74836
diff
changeset
|
124 |
"%\n}\n", body) |
4c8d9479f916
more uniform treatment of optional_argument for Latex elements;
wenzelm
parents:
74836
diff
changeset
|
125 |
|
4c8d9479f916
more uniform treatment of optional_argument for Latex elements;
wenzelm
parents:
74836
diff
changeset
|
126 |
def latex_body(kind: String, body: Text, optional_argument: String = ""): Text = |
4c8d9479f916
more uniform treatment of optional_argument for Latex elements;
wenzelm
parents:
74836
diff
changeset
|
127 |
latex_environment("isamarkup" + kind, body, optional_argument) |
74823 | 128 |
|
74826
0e4d8aa61ad7
more symbolic latex_output via XML (using YXML within text);
wenzelm
parents:
74824
diff
changeset
|
129 |
def latex_delim(name: String, body: Text): Text = |
0e4d8aa61ad7
more symbolic latex_output via XML (using YXML within text);
wenzelm
parents:
74824
diff
changeset
|
130 |
{ |
0e4d8aa61ad7
more symbolic latex_output via XML (using YXML within text);
wenzelm
parents:
74824
diff
changeset
|
131 |
val s = output_name(name) |
0e4d8aa61ad7
more symbolic latex_output via XML (using YXML within text);
wenzelm
parents:
74824
diff
changeset
|
132 |
XML.enclose("%\n\\isadelim" + s + "\n", "%\n\\endisadelim" + s + "\n", body) |
0e4d8aa61ad7
more symbolic latex_output via XML (using YXML within text);
wenzelm
parents:
74824
diff
changeset
|
133 |
} |
0e4d8aa61ad7
more symbolic latex_output via XML (using YXML within text);
wenzelm
parents:
74824
diff
changeset
|
134 |
|
0e4d8aa61ad7
more symbolic latex_output via XML (using YXML within text);
wenzelm
parents:
74824
diff
changeset
|
135 |
def latex_tag(name: String, body: Text): Text = |
0e4d8aa61ad7
more symbolic latex_output via XML (using YXML within text);
wenzelm
parents:
74824
diff
changeset
|
136 |
{ |
0e4d8aa61ad7
more symbolic latex_output via XML (using YXML within text);
wenzelm
parents:
74824
diff
changeset
|
137 |
val s = output_name(name) |
0e4d8aa61ad7
more symbolic latex_output via XML (using YXML within text);
wenzelm
parents:
74824
diff
changeset
|
138 |
XML.enclose("%\n\\isatag" + s + "\n", "%\n\\endisatag" + s + "\n{\\isafold" + s + "}%\n", body) |
0e4d8aa61ad7
more symbolic latex_output via XML (using YXML within text);
wenzelm
parents:
74824
diff
changeset
|
139 |
} |
0e4d8aa61ad7
more symbolic latex_output via XML (using YXML within text);
wenzelm
parents:
74824
diff
changeset
|
140 |
|
74786 | 141 |
def index_item(item: Index_Item.Value): String = |
142 |
{ |
|
143 |
val like = if (item.like.isEmpty) "" else index_escape(item.like) + "@" |
|
144 |
val text = index_escape(latex_output(item.text)) |
|
145 |
like + text |
|
146 |
} |
|
147 |
||
148 |
def index_entry(entry: Index_Entry.Value): Text = |
|
149 |
{ |
|
150 |
val items = entry.items.map(index_item).mkString("!") |
|
151 |
val kind = if (entry.kind.isEmpty) "" else "|" + index_escape(entry.kind) |
|
152 |
latex_macro("index", XML.string(items + kind)) |
|
153 |
} |
|
154 |
||
155 |
||
156 |
/* standard output of text with per-line positions */ |
|
74784
d2522bb4db1b
symbolic latex_output via XML, interpreted in Isabelle/Scala;
wenzelm
parents:
74783
diff
changeset
|
157 |
|
74838
4c8d9479f916
more uniform treatment of optional_argument for Latex elements;
wenzelm
parents:
74836
diff
changeset
|
158 |
def unknown_elem(elem: XML.Elem, pos: Position.T): XML.Body = |
4c8d9479f916
more uniform treatment of optional_argument for Latex elements;
wenzelm
parents:
74836
diff
changeset
|
159 |
error("Unknown latex markup element " + quote(elem.name) + Position.here(pos) + |
4c8d9479f916
more uniform treatment of optional_argument for Latex elements;
wenzelm
parents:
74836
diff
changeset
|
160 |
":\n" + XML.string_of_tree(elem)) |
4c8d9479f916
more uniform treatment of optional_argument for Latex elements;
wenzelm
parents:
74836
diff
changeset
|
161 |
|
74777
2fd0c33fe440
clarified signature: Latex.Output as parameter to Document_Build.Engine;
wenzelm
parents:
74748
diff
changeset
|
162 |
def apply(latex_text: Text, file_pos: String = ""): String = |
73780
466fae6bf22e
compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents:
73736
diff
changeset
|
163 |
{ |
74777
2fd0c33fe440
clarified signature: Latex.Output as parameter to Document_Build.Engine;
wenzelm
parents:
74748
diff
changeset
|
164 |
var line = 1 |
2fd0c33fe440
clarified signature: Latex.Output as parameter to Document_Build.Engine;
wenzelm
parents:
74748
diff
changeset
|
165 |
val result = new mutable.ListBuffer[String] |
2fd0c33fe440
clarified signature: Latex.Output as parameter to Document_Build.Engine;
wenzelm
parents:
74748
diff
changeset
|
166 |
val positions = new mutable.ListBuffer[String] ++= init_position(file_pos) |
2fd0c33fe440
clarified signature: Latex.Output as parameter to Document_Build.Engine;
wenzelm
parents:
74748
diff
changeset
|
167 |
|
74838
4c8d9479f916
more uniform treatment of optional_argument for Latex elements;
wenzelm
parents:
74836
diff
changeset
|
168 |
val file_position = if (file_pos.isEmpty) Position.none else Position.File(file_pos) |
4c8d9479f916
more uniform treatment of optional_argument for Latex elements;
wenzelm
parents:
74836
diff
changeset
|
169 |
|
4c8d9479f916
more uniform treatment of optional_argument for Latex elements;
wenzelm
parents:
74836
diff
changeset
|
170 |
def traverse(xml: XML.Body): Unit = |
74777
2fd0c33fe440
clarified signature: Latex.Output as parameter to Document_Build.Engine;
wenzelm
parents:
74748
diff
changeset
|
171 |
{ |
74838
4c8d9479f916
more uniform treatment of optional_argument for Latex elements;
wenzelm
parents:
74836
diff
changeset
|
172 |
xml.foreach { |
74777
2fd0c33fe440
clarified signature: Latex.Output as parameter to Document_Build.Engine;
wenzelm
parents:
74748
diff
changeset
|
173 |
case XML.Text(s) => |
2fd0c33fe440
clarified signature: Latex.Output as parameter to Document_Build.Engine;
wenzelm
parents:
74748
diff
changeset
|
174 |
line += s.count(_ == '\n') |
2fd0c33fe440
clarified signature: Latex.Output as parameter to Document_Build.Engine;
wenzelm
parents:
74748
diff
changeset
|
175 |
result += s |
74838
4c8d9479f916
more uniform treatment of optional_argument for Latex elements;
wenzelm
parents:
74836
diff
changeset
|
176 |
case elem @ XML.Elem(markup, body) => |
4c8d9479f916
more uniform treatment of optional_argument for Latex elements;
wenzelm
parents:
74836
diff
changeset
|
177 |
val a = Markup.Optional_Argument.get(markup.properties) |
4c8d9479f916
more uniform treatment of optional_argument for Latex elements;
wenzelm
parents:
74836
diff
changeset
|
178 |
traverse { |
4c8d9479f916
more uniform treatment of optional_argument for Latex elements;
wenzelm
parents:
74836
diff
changeset
|
179 |
markup match { |
4c8d9479f916
more uniform treatment of optional_argument for Latex elements;
wenzelm
parents:
74836
diff
changeset
|
180 |
case Markup.Document_Latex(props) => |
4c8d9479f916
more uniform treatment of optional_argument for Latex elements;
wenzelm
parents:
74836
diff
changeset
|
181 |
for (l <- Position.Line.unapply(props) if positions.nonEmpty) { |
4c8d9479f916
more uniform treatment of optional_argument for Latex elements;
wenzelm
parents:
74836
diff
changeset
|
182 |
val s = position(Value.Int(line), Value.Int(l)) |
4c8d9479f916
more uniform treatment of optional_argument for Latex elements;
wenzelm
parents:
74836
diff
changeset
|
183 |
if (positions.last != s) positions += s |
4c8d9479f916
more uniform treatment of optional_argument for Latex elements;
wenzelm
parents:
74836
diff
changeset
|
184 |
} |
4c8d9479f916
more uniform treatment of optional_argument for Latex elements;
wenzelm
parents:
74836
diff
changeset
|
185 |
body |
4c8d9479f916
more uniform treatment of optional_argument for Latex elements;
wenzelm
parents:
74836
diff
changeset
|
186 |
case Markup.Latex_Output(_) => XML.string(latex_output(body)) |
4c8d9479f916
more uniform treatment of optional_argument for Latex elements;
wenzelm
parents:
74836
diff
changeset
|
187 |
case Markup.Latex_Macro0(name) if body.isEmpty => latex_macro0(name, a) |
4c8d9479f916
more uniform treatment of optional_argument for Latex elements;
wenzelm
parents:
74836
diff
changeset
|
188 |
case Markup.Latex_Macro(name) => latex_macro(name, body, a) |
4c8d9479f916
more uniform treatment of optional_argument for Latex elements;
wenzelm
parents:
74836
diff
changeset
|
189 |
case Markup.Latex_Environment(name) => latex_environment(name, body, a) |
4c8d9479f916
more uniform treatment of optional_argument for Latex elements;
wenzelm
parents:
74836
diff
changeset
|
190 |
case Markup.Latex_Heading(kind) => latex_heading(kind, body, a) |
4c8d9479f916
more uniform treatment of optional_argument for Latex elements;
wenzelm
parents:
74836
diff
changeset
|
191 |
case Markup.Latex_Body(kind) => latex_body(kind, body, a) |
4c8d9479f916
more uniform treatment of optional_argument for Latex elements;
wenzelm
parents:
74836
diff
changeset
|
192 |
case Markup.Latex_Delim(name) => latex_delim(name, body) |
4c8d9479f916
more uniform treatment of optional_argument for Latex elements;
wenzelm
parents:
74836
diff
changeset
|
193 |
case Markup.Latex_Tag(name) => latex_tag(name, body) |
4c8d9479f916
more uniform treatment of optional_argument for Latex elements;
wenzelm
parents:
74836
diff
changeset
|
194 |
case Markup.Latex_Index_Entry(_) => |
4c8d9479f916
more uniform treatment of optional_argument for Latex elements;
wenzelm
parents:
74836
diff
changeset
|
195 |
elem match { |
4c8d9479f916
more uniform treatment of optional_argument for Latex elements;
wenzelm
parents:
74836
diff
changeset
|
196 |
case Index_Entry(entry) => index_entry(entry) |
4c8d9479f916
more uniform treatment of optional_argument for Latex elements;
wenzelm
parents:
74836
diff
changeset
|
197 |
case _ => unknown_elem(elem, file_position) |
4c8d9479f916
more uniform treatment of optional_argument for Latex elements;
wenzelm
parents:
74836
diff
changeset
|
198 |
} |
4c8d9479f916
more uniform treatment of optional_argument for Latex elements;
wenzelm
parents:
74836
diff
changeset
|
199 |
case _ => unknown_elem(elem, file_position) |
4c8d9479f916
more uniform treatment of optional_argument for Latex elements;
wenzelm
parents:
74836
diff
changeset
|
200 |
} |
74783 | 201 |
} |
74777
2fd0c33fe440
clarified signature: Latex.Output as parameter to Document_Build.Engine;
wenzelm
parents:
74748
diff
changeset
|
202 |
} |
73780
466fae6bf22e
compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents:
73736
diff
changeset
|
203 |
} |
74777
2fd0c33fe440
clarified signature: Latex.Output as parameter to Document_Build.Engine;
wenzelm
parents:
74748
diff
changeset
|
204 |
traverse(latex_text) |
73780
466fae6bf22e
compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents:
73736
diff
changeset
|
205 |
|
74777
2fd0c33fe440
clarified signature: Latex.Output as parameter to Document_Build.Engine;
wenzelm
parents:
74748
diff
changeset
|
206 |
result ++= positions |
2fd0c33fe440
clarified signature: Latex.Output as parameter to Document_Build.Engine;
wenzelm
parents:
74748
diff
changeset
|
207 |
result.mkString |
2fd0c33fe440
clarified signature: Latex.Output as parameter to Document_Build.Engine;
wenzelm
parents:
74748
diff
changeset
|
208 |
} |
73780
466fae6bf22e
compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents:
73736
diff
changeset
|
209 |
} |
466fae6bf22e
compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents:
73736
diff
changeset
|
210 |
|
466fae6bf22e
compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents:
73736
diff
changeset
|
211 |
|
67173 | 212 |
/* generated .tex file */ |
213 |
||
67184 | 214 |
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
|
215 |
private val Line_Pattern = """^*%:%(\d+)=(\d+)%:%$""".r |
67173 | 216 |
|
217 |
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
|
218 |
{ |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
219 |
val positions = |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
220 |
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
|
221 |
takeWhile(_ != "\\endinput").reverse |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
222 |
|
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
223 |
val source_file = |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
224 |
positions match { |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
225 |
case File_Pattern(file) :: _ => Some(file) |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
226 |
case _ => None |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
227 |
} |
67173 | 228 |
|
67194
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
229 |
val source_lines = |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
230 |
if (source_file.isEmpty) Nil |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
231 |
else |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
232 |
positions.flatMap(line => |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
233 |
line match { |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
234 |
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
|
235 |
case _ => None |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
236 |
}) |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
237 |
|
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
238 |
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
|
239 |
} |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
240 |
|
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
241 |
final class Tex_File private[Latex]( |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
242 |
tex_file: Path, source_file: Option[String], source_lines: List[(Int, Int)]) |
67173 | 243 |
{ |
244 |
override def toString: String = tex_file.toString |
|
245 |
||
67194
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
246 |
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
|
247 |
source_file match { |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
248 |
case None => None |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
249 |
case Some(file) => |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
250 |
val source_line = |
73359 | 251 |
source_lines.iterator.takeWhile({ case (m, _) => m <= l }). |
252 |
foldLeft(0) { case (_, (_, n)) => n } |
|
67194
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67190
diff
changeset
|
253 |
if (source_line == 0) None else Some(Position.Line_File(source_line, file)) |
67173 | 254 |
} |
255 |
||
256 |
def position(line: Int): Position.T = |
|
67188
bc7a6455e12a
simplified positions -- line is also human-readable in generated .tex file;
wenzelm
parents:
67184
diff
changeset
|
257 |
source_position(line) getOrElse Position.Line_File(line, tex_file.implode) |
67173 | 258 |
} |
259 |
||
260 |
||
261 |
/* latex log */ |
|
262 |
||
73782 | 263 |
def latex_errors(dir: Path, root_name: String): List[String] = |
264 |
{ |
|
265 |
val root_log_path = dir + Path.explode(root_name).ext("log") |
|
266 |
if (root_log_path.is_file) { |
|
267 |
for { (msg, pos) <- filter_errors(dir, File.read(root_log_path)) } |
|
268 |
yield "Latex error" + Position.here(pos) + ":\n" + Library.indent_lines(2, msg) |
|
269 |
} |
|
270 |
else Nil |
|
271 |
} |
|
272 |
||
67173 | 273 |
def filter_errors(dir: Path, root_log: String): List[(String, Position.T)] = |
67172 | 274 |
{ |
67173 | 275 |
val seen_files = Synchronized(Map.empty[JFile, Tex_File]) |
276 |
||
277 |
def check_tex_file(path: Path): Option[Tex_File] = |
|
278 |
seen_files.change_result(seen => |
|
279 |
seen.get(path.file) match { |
|
280 |
case None => |
|
281 |
if (path.is_file) { |
|
282 |
val tex_file = read_tex_file(path) |
|
283 |
(Some(tex_file), seen + (path.file -> tex_file)) |
|
284 |
} |
|
285 |
else (None, seen) |
|
286 |
case some => (some, seen) |
|
287 |
}) |
|
288 |
||
289 |
def tex_file_position(path: Path, line: Int): Position.T = |
|
290 |
check_tex_file(path) match { |
|
291 |
case Some(tex_file) => tex_file.position(line) |
|
292 |
case None => Position.Line_File(line, path.implode) |
|
293 |
} |
|
294 |
||
67172 | 295 |
object File_Line_Error |
296 |
{ |
|
71601 | 297 |
val Pattern: Regex = """^(.*?\.\w\w\w):(\d+): (.*)$""".r |
67173 | 298 |
def unapply(line: String): Option[(Path, Int, String)] = |
67172 | 299 |
line match { |
300 |
case Pattern(file, Value.Int(line), message) => |
|
301 |
val path = File.standard_path(file) |
|
302 |
if (Path.is_wellformed(path)) { |
|
67183 | 303 |
val file = (dir + Path.explode(path)).canonical |
67423 | 304 |
val msg = Library.perhaps_unprefix("LaTeX Error: ", message) |
305 |
if (file.is_file) Some((file, line, msg)) else None |
|
67172 | 306 |
} |
307 |
else None |
|
308 |
case _ => None |
|
309 |
} |
|
310 |
} |
|
311 |
||
67418
5a6ed2e679fb
more general error suffixes, e.g. for messages that are broken over several lines;
wenzelm
parents:
67417
diff
changeset
|
312 |
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
|
313 |
val More_Error = |
eb29f4868d14
more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents:
67194
diff
changeset
|
314 |
List( |
eb29f4868d14
more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents:
67194
diff
changeset
|
315 |
"<argument>", |
eb29f4868d14
more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents:
67194
diff
changeset
|
316 |
"<template>", |
eb29f4868d14
more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents:
67194
diff
changeset
|
317 |
"<recently read>", |
eb29f4868d14
more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents:
67194
diff
changeset
|
318 |
"<to be read again>", |
eb29f4868d14
more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents:
67194
diff
changeset
|
319 |
"<inserted text>", |
eb29f4868d14
more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents:
67194
diff
changeset
|
320 |
"<output>", |
eb29f4868d14
more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents:
67194
diff
changeset
|
321 |
"<everypar>", |
eb29f4868d14
more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents:
67194
diff
changeset
|
322 |
"<everymath>", |
eb29f4868d14
more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents:
67194
diff
changeset
|
323 |
"<everydisplay>", |
eb29f4868d14
more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents:
67194
diff
changeset
|
324 |
"<everyhbox>", |
eb29f4868d14
more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents:
67194
diff
changeset
|
325 |
"<everyvbox>", |
eb29f4868d14
more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents:
67194
diff
changeset
|
326 |
"<everyjob>", |
eb29f4868d14
more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents:
67194
diff
changeset
|
327 |
"<everycr>", |
eb29f4868d14
more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents:
67194
diff
changeset
|
328 |
"<mark>", |
eb29f4868d14
more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents:
67194
diff
changeset
|
329 |
"<everyeof>", |
eb29f4868d14
more error information according to @<Print type of token list@> in pdfweb.tex;
wenzelm
parents:
67194
diff
changeset
|
330 |
"<write>").mkString("^(?:", "|", ") (.*)$").r |
67172 | 331 |
|
73474
4e12a6caefb3
turn LaTeX warning into error, for the sake of isabelle.sty/bbbfont;
wenzelm
parents:
73359
diff
changeset
|
332 |
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
|
333 |
val Bad_File = """^! LaTeX Error: (File `.*' not found\.)$""".r |
67482 | 334 |
|
67423 | 335 |
val error_ignore = |
336 |
Set( |
|
337 |
"See the LaTeX manual or LaTeX Companion for explanation.", |
|
338 |
"Type H <return> for immediate help.") |
|
339 |
||
67418
5a6ed2e679fb
more general error suffixes, e.g. for messages that are broken over several lines;
wenzelm
parents:
67417
diff
changeset
|
340 |
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
|
341 |
{ |
71784 | 342 |
val lines1 = |
343 |
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
|
344 |
lines1.zipWithIndex.collectFirst({ |
67423 | 345 |
case (Line_Error(msg), i) => |
346 |
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
|
347 |
} |
5a6ed2e679fb
more general error suffixes, e.g. for messages that are broken over several lines;
wenzelm
parents:
67417
diff
changeset
|
348 |
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
|
349 |
lines match { |
5a6ed2e679fb
more general error suffixes, e.g. for messages that are broken over several lines;
wenzelm
parents:
67417
diff
changeset
|
350 |
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
|
351 |
case _ => None |
5a6ed2e679fb
more general error suffixes, e.g. for messages that are broken over several lines;
wenzelm
parents:
67417
diff
changeset
|
352 |
} |
5a6ed2e679fb
more general error suffixes, e.g. for messages that are broken over several lines;
wenzelm
parents:
67417
diff
changeset
|
353 |
|
67173 | 354 |
@tailrec def filter(lines: List[String], result: List[(String, Position.T)]) |
355 |
: List[(String, Position.T)] = |
|
356 |
{ |
|
67172 | 357 |
lines match { |
67173 | 358 |
case File_Line_Error((file, line, msg1)) :: rest1 => |
67183 | 359 |
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
|
360 |
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
|
361 |
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
|
362 |
case Bad_Font(msg) :: rest => |
4e12a6caefb3
turn LaTeX warning into error, for the sake of isabelle.sty/bbbfont;
wenzelm
parents:
73359
diff
changeset
|
363 |
filter(rest, (msg, Position.none) :: result) |
67483
aae933ca6fbd
tuned message: same error may occur in different contexts;
wenzelm
parents:
67482
diff
changeset
|
364 |
case Bad_File(msg) :: rest => |
67482 | 365 |
filter(rest, (msg, Position.none) :: result) |
67172 | 366 |
case _ :: rest => filter(rest, result) |
367 |
case Nil => result.reverse |
|
368 |
} |
|
67173 | 369 |
} |
67172 | 370 |
|
67174 | 371 |
filter(Line.logical_lines(root_log), Nil) |
67173 | 372 |
} |
67172 | 373 |
} |