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