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