author | wenzelm |
Tue, 13 Nov 2018 20:57:54 +0100 | |
changeset 69294 | 085f31ae902d |
parent 69259 | 438e1a11445f |
child 69366 | b6dacf6eabe3 |
permissions | -rw-r--r-- |
67274 | 1 |
/* Title: Pure/Thy/bibtex.scala |
58523 | 2 |
Author: Makarius |
3 |
||
58544
340f130b3d38
bibtex support in ML: document antiquotation @{cite} with markup;
wenzelm
parents:
58543
diff
changeset
|
4 |
BibTeX support. |
58523 | 5 |
*/ |
6 |
||
7 |
package isabelle |
|
8 |
||
9 |
||
67290 | 10 |
import java.io.{File => JFile} |
11 |
||
58528 | 12 |
import scala.collection.mutable |
58523 | 13 |
import scala.util.parsing.combinator.RegexParsers |
64824 | 14 |
import scala.util.parsing.input.Reader |
58523 | 15 |
|
16 |
||
17 |
object Bibtex |
|
18 |
{ |
|
69255
800b1ce96fce
more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
68224
diff
changeset
|
19 |
/** file format **/ |
800b1ce96fce
more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
68224
diff
changeset
|
20 |
|
800b1ce96fce
more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
68224
diff
changeset
|
21 |
def is_bibtex(name: String): Boolean = name.endsWith(".bib") |
800b1ce96fce
more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
68224
diff
changeset
|
22 |
|
800b1ce96fce
more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
68224
diff
changeset
|
23 |
class File_Format extends isabelle.File_Format |
800b1ce96fce
more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
68224
diff
changeset
|
24 |
{ |
800b1ce96fce
more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
68224
diff
changeset
|
25 |
val format_name: String = "bibtex" |
69257 | 26 |
val file_ext: String = "bib" |
69255
800b1ce96fce
more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
68224
diff
changeset
|
27 |
|
69257 | 28 |
override def theory_suffix: String = "bibtex_file" |
69259 | 29 |
override def theory_content(name: String): String = |
30 |
"""theory "bib" imports Pure begin bibtex_file """ + quote(name) + """ end""" |
|
69255
800b1ce96fce
more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
68224
diff
changeset
|
31 |
|
800b1ce96fce
more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
68224
diff
changeset
|
32 |
override def make_preview(snapshot: Document.Snapshot): Option[Present.Preview] = |
800b1ce96fce
more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
68224
diff
changeset
|
33 |
{ |
800b1ce96fce
more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
68224
diff
changeset
|
34 |
val name = snapshot.node_name |
800b1ce96fce
more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
68224
diff
changeset
|
35 |
if (detect(name.node)) { |
800b1ce96fce
more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
68224
diff
changeset
|
36 |
val title = "Bibliography " + quote(snapshot.node_name.path.base_name) |
800b1ce96fce
more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
68224
diff
changeset
|
37 |
val content = |
800b1ce96fce
more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
68224
diff
changeset
|
38 |
Isabelle_System.with_tmp_file("bib", "bib") { bib => |
800b1ce96fce
more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
68224
diff
changeset
|
39 |
File.write(bib, snapshot.node.source) |
800b1ce96fce
more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
68224
diff
changeset
|
40 |
Bibtex.html_output(List(bib), style = "unsort", title = title) |
800b1ce96fce
more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
68224
diff
changeset
|
41 |
} |
800b1ce96fce
more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
68224
diff
changeset
|
42 |
Some(Present.Preview(title, content)) |
800b1ce96fce
more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
68224
diff
changeset
|
43 |
} |
800b1ce96fce
more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
68224
diff
changeset
|
44 |
else None |
800b1ce96fce
more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
68224
diff
changeset
|
45 |
} |
800b1ce96fce
more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
68224
diff
changeset
|
46 |
} |
800b1ce96fce
more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
68224
diff
changeset
|
47 |
|
800b1ce96fce
more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
68224
diff
changeset
|
48 |
|
800b1ce96fce
more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
68224
diff
changeset
|
49 |
|
67203 | 50 |
/** bibtex errors **/ |
51 |
||
52 |
def bibtex_errors(dir: Path, root_name: String): List[String] = |
|
53 |
{ |
|
54 |
val log_path = dir + Path.explode(root_name).ext("blg") |
|
55 |
if (log_path.is_file) { |
|
67300 | 56 |
val Error1 = """^(I couldn't open database file .+)$""".r |
57 |
val Error2 = """^(.+)---line (\d+) of file (.+)""".r |
|
67203 | 58 |
Line.logical_lines(File.read(log_path)).flatMap(line => |
59 |
line match { |
|
67300 | 60 |
case Error1(msg) => Some("Bibtex error: " + msg) |
61 |
case Error2(msg, Value.Int(l), file) => |
|
67203 | 62 |
val path = File.standard_path(file) |
63 |
if (Path.is_wellformed(path)) { |
|
64 |
val pos = Position.Line_File(l, (dir + Path.explode(path)).canonical.implode) |
|
65 |
Some("Bibtex error" + Position.here(pos) + ":\n " + msg) |
|
66 |
} |
|
67 |
else None |
|
68 |
case _ => None |
|
69 |
}) |
|
70 |
} |
|
71 |
else Nil |
|
72 |
} |
|
73 |
||
74 |
||
75 |
||
67272 | 76 |
/** check database **/ |
77 |
||
67301 | 78 |
def check_database(database: String): (List[(String, Position.T)], List[(String, Position.T)]) = |
67272 | 79 |
{ |
67275
5e427586cb57
check bibtex database on ML side -- for semantic PIDE editing;
wenzelm
parents:
67274
diff
changeset
|
80 |
val chunks = parse(Line.normalize(database)) |
5e427586cb57
check bibtex database on ML side -- for semantic PIDE editing;
wenzelm
parents:
67274
diff
changeset
|
81 |
var chunk_pos = Map.empty[String, Position.T] |
67272 | 82 |
val tokens = new mutable.ListBuffer[(Token, Position.T)] |
83 |
var line = 1 |
|
84 |
var offset = 1 |
|
85 |
||
67276 | 86 |
def make_pos(length: Int): Position.T = |
87 |
Position.Offset(offset) ::: Position.End_Offset(offset + length) ::: Position.Line(line) |
|
67272 | 88 |
|
89 |
def advance_pos(tok: Token) |
|
90 |
{ |
|
91 |
for (s <- Symbol.iterator(tok.source)) { |
|
92 |
if (Symbol.is_newline(s)) line += 1 |
|
93 |
offset += 1 |
|
94 |
} |
|
95 |
} |
|
96 |
||
97 |
def get_line_pos(l: Int): Position.T = |
|
98 |
if (0 < l && l <= tokens.length) tokens(l - 1)._2 else Position.none |
|
99 |
||
100 |
for (chunk <- chunks) { |
|
101 |
val name = chunk.name |
|
102 |
if (name != "" && !chunk_pos.isDefinedAt(name)) { |
|
67276 | 103 |
chunk_pos += (name -> make_pos(chunk.heading_length)) |
67272 | 104 |
} |
105 |
for (tok <- chunk.tokens) { |
|
67276 | 106 |
tokens += (tok.copy(source = tok.source.replace("\n", " ")) -> make_pos(tok.source.length)) |
67272 | 107 |
advance_pos(tok) |
108 |
} |
|
109 |
} |
|
110 |
||
111 |
Isabelle_System.with_tmp_dir("bibtex")(tmp_dir => |
|
112 |
{ |
|
113 |
File.write(tmp_dir + Path.explode("root.bib"), |
|
114 |
tokens.iterator.map(p => p._1.source).mkString("", "\n", "\n")) |
|
115 |
File.write(tmp_dir + Path.explode("root.aux"), |
|
116 |
"\\bibstyle{plain}\n\\bibdata{root}\n\\citation{*}") |
|
117 |
Isabelle_System.bash("\"$ISABELLE_BIBTEX\" root", cwd = tmp_dir.file) |
|
118 |
||
119 |
val Error = """^(.*)---line (\d+) of file root.bib$""".r |
|
120 |
val Warning = """^Warning--(.+)$""".r |
|
121 |
val Warning_Line = """--line (\d+) of file root.bib$""".r |
|
122 |
val Warning_in_Chunk = """^Warning--(.+) in (.+)$""".r |
|
123 |
||
124 |
val log_file = tmp_dir + Path.explode("root.blg") |
|
125 |
val lines = if (log_file.is_file) Line.logical_lines(File.read(log_file)) else Nil |
|
126 |
||
67301 | 127 |
val (errors, warnings) = |
69294 | 128 |
if (lines.isEmpty) (Nil, Nil) |
129 |
else { |
|
130 |
lines.zip(lines.tail ::: List("")).flatMap( |
|
131 |
{ |
|
132 |
case (Error(msg, Value.Int(l)), _) => |
|
133 |
Some((true, (msg, get_line_pos(l)))) |
|
134 |
case (Warning_in_Chunk(msg, name), _) if chunk_pos.isDefinedAt(name) => |
|
135 |
Some((false, (Word.capitalize(msg + " in entry " + quote(name)), chunk_pos(name)))) |
|
136 |
case (Warning(msg), Warning_Line(Value.Int(l))) => |
|
137 |
Some((false, (Word.capitalize(msg), get_line_pos(l)))) |
|
138 |
case (Warning(msg), _) => |
|
139 |
Some((false, (Word.capitalize(msg), Position.none))) |
|
140 |
case _ => None |
|
141 |
} |
|
142 |
).partition(_._1) |
|
143 |
} |
|
67301 | 144 |
(errors.map(_._2), warnings.map(_._2)) |
67272 | 145 |
}) |
146 |
} |
|
147 |
||
67275
5e427586cb57
check bibtex database on ML side -- for semantic PIDE editing;
wenzelm
parents:
67274
diff
changeset
|
148 |
def check_database_yxml(database: String): String = |
5e427586cb57
check bibtex database on ML side -- for semantic PIDE editing;
wenzelm
parents:
67274
diff
changeset
|
149 |
{ |
67301 | 150 |
import XML.Encode._ |
151 |
YXML.string_of_body(pair(list(pair(string, properties)), list(pair(string, properties)))( |
|
152 |
check_database(database))) |
|
67275
5e427586cb57
check bibtex database on ML side -- for semantic PIDE editing;
wenzelm
parents:
67274
diff
changeset
|
153 |
} |
5e427586cb57
check bibtex database on ML side -- for semantic PIDE editing;
wenzelm
parents:
67274
diff
changeset
|
154 |
|
67272 | 155 |
|
156 |
||
64828 | 157 |
/** document model **/ |
158 |
||
67290 | 159 |
/* entries */ |
64828 | 160 |
|
66150 | 161 |
def entries(text: String): List[Text.Info[String]] = |
64828 | 162 |
{ |
64831 | 163 |
val result = new mutable.ListBuffer[Text.Info[String]] |
64828 | 164 |
var offset = 0 |
165 |
for (chunk <- Bibtex.parse(text)) { |
|
64831 | 166 |
val end_offset = offset + chunk.source.length |
167 |
if (chunk.name != "" && !chunk.is_command) |
|
168 |
result += Text.Info(Text.Range(offset, end_offset), chunk.name) |
|
169 |
offset = end_offset |
|
64828 | 170 |
} |
171 |
result.toList |
|
172 |
} |
|
173 |
||
66150 | 174 |
def entries_iterator[A, B <: Document.Model](models: Map[A, B]) |
175 |
: Iterator[Text.Info[(String, B)]] = |
|
176 |
{ |
|
177 |
for { |
|
178 |
(_, model) <- models.iterator |
|
179 |
info <- model.bibtex_entries.iterator |
|
180 |
} yield info.map((_, model)) |
|
181 |
} |
|
182 |
||
64828 | 183 |
|
66152 | 184 |
/* completion */ |
185 |
||
186 |
def completion[A, B <: Document.Model]( |
|
187 |
history: Completion.History, rendering: Rendering, caret: Text.Offset, |
|
188 |
models: Map[A, B]): Option[Completion.Result] = |
|
189 |
{ |
|
190 |
for { |
|
191 |
Text.Info(r, name) <- rendering.citation(rendering.before_caret_range(caret)) |
|
192 |
name1 <- Completion.clean_name(name) |
|
193 |
||
67014 | 194 |
original <- rendering.model.get_text(r) |
66152 | 195 |
original1 <- Completion.clean_name(Library.perhaps_unquote(original)) |
196 |
||
197 |
entries = |
|
198 |
(for { |
|
199 |
Text.Info(_, (entry, _)) <- entries_iterator(models) |
|
200 |
if entry.toLowerCase.containsSlice(name1.toLowerCase) && entry != original1 |
|
201 |
} yield entry).toList |
|
202 |
if entries.nonEmpty |
|
203 |
||
204 |
items = |
|
205 |
entries.sorted.map({ |
|
206 |
case entry => |
|
207 |
val full_name = Long_Name.qualify(Markup.CITATION, entry) |
|
208 |
val description = List(entry, "(BibTeX entry)") |
|
209 |
val replacement = quote(entry) |
|
210 |
Completion.Item(r, original, full_name, description, replacement, 0, false) |
|
211 |
}).sorted(history.ordering).take(rendering.options.int("completion_limit")) |
|
212 |
} yield Completion.Result(r, original, false, items) |
|
213 |
} |
|
214 |
||
215 |
||
64828 | 216 |
|
58523 | 217 |
/** content **/ |
218 |
||
58529 | 219 |
private val months = List( |
58523 | 220 |
"jan", |
221 |
"feb", |
|
222 |
"mar", |
|
223 |
"apr", |
|
224 |
"may", |
|
225 |
"jun", |
|
226 |
"jul", |
|
227 |
"aug", |
|
228 |
"sep", |
|
229 |
"oct", |
|
230 |
"nov", |
|
231 |
"dec") |
|
58529 | 232 |
def is_month(s: String): Boolean = months.contains(s.toLowerCase) |
58523 | 233 |
|
58529 | 234 |
private val commands = List("preamble", "string") |
235 |
def is_command(s: String): Boolean = commands.contains(s.toLowerCase) |
|
58523 | 236 |
|
58524 | 237 |
sealed case class Entry( |
58526 | 238 |
kind: String, |
58523 | 239 |
required: List[String], |
240 |
optional_crossref: List[String], |
|
58529 | 241 |
optional_other: List[String]) |
58524 | 242 |
{ |
58533 | 243 |
def is_required(s: String): Boolean = required.contains(s.toLowerCase) |
58529 | 244 |
def is_optional(s: String): Boolean = |
58533 | 245 |
optional_crossref.contains(s.toLowerCase) || optional_other.contains(s.toLowerCase) |
58529 | 246 |
|
247 |
def fields: List[String] = required ::: optional_crossref ::: optional_other |
|
58524 | 248 |
def template: String = |
58526 | 249 |
"@" + kind + "{,\n" + fields.map(x => " " + x + " = {},\n").mkString + "}\n" |
58524 | 250 |
} |
58523 | 251 |
|
66150 | 252 |
val known_entries: List[Entry] = |
58524 | 253 |
List( |
254 |
Entry("Article", |
|
255 |
List("author", "title"), |
|
256 |
List("journal", "year"), |
|
257 |
List("volume", "number", "pages", "month", "note")), |
|
258 |
Entry("InProceedings", |
|
259 |
List("author", "title"), |
|
260 |
List("booktitle", "year"), |
|
261 |
List("editor", "volume", "number", "series", "pages", "month", "address", |
|
262 |
"organization", "publisher", "note")), |
|
263 |
Entry("InCollection", |
|
264 |
List("author", "title", "booktitle"), |
|
265 |
List("publisher", "year"), |
|
266 |
List("editor", "volume", "number", "series", "type", "chapter", "pages", |
|
267 |
"edition", "month", "address", "note")), |
|
268 |
Entry("InBook", |
|
269 |
List("author", "editor", "title", "chapter"), |
|
270 |
List("publisher", "year"), |
|
271 |
List("volume", "number", "series", "type", "address", "edition", "month", "pages", "note")), |
|
272 |
Entry("Proceedings", |
|
273 |
List("title", "year"), |
|
274 |
List(), |
|
275 |
List("booktitle", "editor", "volume", "number", "series", "address", "month", |
|
276 |
"organization", "publisher", "note")), |
|
277 |
Entry("Book", |
|
278 |
List("author", "editor", "title"), |
|
279 |
List("publisher", "year"), |
|
280 |
List("volume", "number", "series", "address", "edition", "month", "note")), |
|
281 |
Entry("Booklet", |
|
282 |
List("title"), |
|
283 |
List(), |
|
284 |
List("author", "howpublished", "address", "month", "year", "note")), |
|
285 |
Entry("PhdThesis", |
|
286 |
List("author", "title", "school", "year"), |
|
287 |
List(), |
|
288 |
List("type", "address", "month", "note")), |
|
289 |
Entry("MastersThesis", |
|
290 |
List("author", "title", "school", "year"), |
|
291 |
List(), |
|
292 |
List("type", "address", "month", "note")), |
|
293 |
Entry("TechReport", |
|
294 |
List("author", "title", "institution", "year"), |
|
295 |
List(), |
|
296 |
List("type", "number", "address", "month", "note")), |
|
297 |
Entry("Manual", |
|
298 |
List("title"), |
|
299 |
List(), |
|
300 |
List("author", "organization", "address", "edition", "month", "year", "note")), |
|
301 |
Entry("Unpublished", |
|
302 |
List("author", "title", "note"), |
|
303 |
List(), |
|
304 |
List("month", "year")), |
|
305 |
Entry("Misc", |
|
306 |
List(), |
|
307 |
List(), |
|
308 |
List("author", "title", "howpublished", "month", "year", "note"))) |
|
58523 | 309 |
|
58529 | 310 |
def get_entry(kind: String): Option[Entry] = |
66150 | 311 |
known_entries.find(entry => entry.kind.toLowerCase == kind.toLowerCase) |
58529 | 312 |
|
58530 | 313 |
def is_entry(kind: String): Boolean = get_entry(kind).isDefined |
314 |
||
58523 | 315 |
|
316 |
||
317 |
/** tokens and chunks **/ |
|
318 |
||
319 |
object Token |
|
320 |
{ |
|
321 |
object Kind extends Enumeration |
|
322 |
{ |
|
58529 | 323 |
val COMMAND = Value("command") |
324 |
val ENTRY = Value("entry") |
|
58523 | 325 |
val KEYWORD = Value("keyword") |
326 |
val NAT = Value("natural number") |
|
58529 | 327 |
val STRING = Value("string") |
58531 | 328 |
val NAME = Value("name") |
58523 | 329 |
val IDENT = Value("identifier") |
58535 | 330 |
val SPACE = Value("white space") |
331 |
val COMMENT = Value("ignored text") |
|
58523 | 332 |
val ERROR = Value("bad input") |
333 |
} |
|
334 |
} |
|
335 |
||
60215 | 336 |
sealed case class Token(kind: Token.Kind.Value, source: String) |
58523 | 337 |
{ |
58530 | 338 |
def is_kind: Boolean = |
339 |
kind == Token.Kind.COMMAND || |
|
340 |
kind == Token.Kind.ENTRY || |
|
341 |
kind == Token.Kind.IDENT |
|
58531 | 342 |
def is_name: Boolean = |
343 |
kind == Token.Kind.NAME || |
|
344 |
kind == Token.Kind.IDENT |
|
58535 | 345 |
def is_ignored: Boolean = |
346 |
kind == Token.Kind.SPACE || |
|
347 |
kind == Token.Kind.COMMENT |
|
67276 | 348 |
def is_malformed: Boolean = |
349 |
kind == Token.Kind.ERROR |
|
350 |
def is_open: Boolean = |
|
351 |
kind == Token.Kind.KEYWORD && (source == "{" || source == "(") |
|
58523 | 352 |
} |
353 |
||
58530 | 354 |
case class Chunk(kind: String, tokens: List[Token]) |
58523 | 355 |
{ |
58529 | 356 |
val source = tokens.map(_.source).mkString |
58526 | 357 |
|
58530 | 358 |
private val content: Option[List[Token]] = |
58523 | 359 |
tokens match { |
59319 | 360 |
case Token(Token.Kind.KEYWORD, "@") :: body if body.nonEmpty => |
58529 | 361 |
(body.init.filterNot(_.is_ignored), body.last) match { |
58530 | 362 |
case (tok :: Token(Token.Kind.KEYWORD, "{") :: toks, Token(Token.Kind.KEYWORD, "}")) |
363 |
if tok.is_kind => Some(toks) |
|
364 |
||
365 |
case (tok :: Token(Token.Kind.KEYWORD, "(") :: toks, Token(Token.Kind.KEYWORD, ")")) |
|
366 |
if tok.is_kind => Some(toks) |
|
367 |
||
58528 | 368 |
case _ => None |
58523 | 369 |
} |
58528 | 370 |
case _ => None |
58526 | 371 |
} |
372 |
||
373 |
def name: String = |
|
58530 | 374 |
content match { |
58531 | 375 |
case Some(tok :: _) if tok.is_name => tok.source |
58523 | 376 |
case _ => "" |
377 |
} |
|
58530 | 378 |
|
67276 | 379 |
def heading_length: Int = |
380 |
if (name == "") 1 |
|
381 |
else (0 /: tokens.takeWhile(tok => !tok.is_open)){ case (n, tok) => n + tok.source.length } |
|
382 |
||
58530 | 383 |
def is_ignored: Boolean = kind == "" && tokens.forall(_.is_ignored) |
384 |
def is_malformed: Boolean = kind == "" || tokens.exists(_.is_malformed) |
|
58543 | 385 |
def is_command: Boolean = Bibtex.is_command(kind) && name != "" && content.isDefined |
386 |
def is_entry: Boolean = Bibtex.is_entry(kind) && name != "" && content.isDefined |
|
58523 | 387 |
} |
388 |
||
389 |
||
390 |
||
391 |
/** parsing **/ |
|
392 |
||
393 |
// context of partial line-oriented scans |
|
394 |
abstract class Line_Context |
|
58589 | 395 |
case object Ignored extends Line_Context |
58590
472b9fbcc7f0
back to liberal spaces of official syntax (in contrast to 4b190c763097), e.g. relevant for easychair.bib;
wenzelm
parents:
58589
diff
changeset
|
396 |
case object At extends Line_Context |
472b9fbcc7f0
back to liberal spaces of official syntax (in contrast to 4b190c763097), e.g. relevant for easychair.bib;
wenzelm
parents:
58589
diff
changeset
|
397 |
case class Item_Start(kind: String) extends Line_Context |
58591 | 398 |
case class Item_Open(kind: String, end: String) extends Line_Context |
399 |
case class Item(kind: String, end: String, delim: Delimited) extends Line_Context |
|
58590
472b9fbcc7f0
back to liberal spaces of official syntax (in contrast to 4b190c763097), e.g. relevant for easychair.bib;
wenzelm
parents:
58589
diff
changeset
|
400 |
|
58528 | 401 |
case class Delimited(quoted: Boolean, depth: Int) |
402 |
val Closed = Delimited(false, 0) |
|
58523 | 403 |
|
404 |
private def token(kind: Token.Kind.Value)(source: String): Token = Token(kind, source) |
|
405 |
private def keyword(source: String): Token = Token(Token.Kind.KEYWORD, source) |
|
406 |
||
407 |
||
68224 | 408 |
// See also https://ctan.org/tex-archive/biblio/bibtex/base/bibtex.web |
58523 | 409 |
// module @<Scan for and process a \.{.bib} command or database entry@>. |
410 |
||
411 |
object Parsers extends RegexParsers |
|
412 |
{ |
|
413 |
/* white space and comments */ |
|
414 |
||
415 |
override val whiteSpace = "".r |
|
416 |
||
58535 | 417 |
private val space = """[ \t\n\r]+""".r ^^ token(Token.Kind.SPACE) |
58590
472b9fbcc7f0
back to liberal spaces of official syntax (in contrast to 4b190c763097), e.g. relevant for easychair.bib;
wenzelm
parents:
58589
diff
changeset
|
418 |
private val spaces = rep(space) |
58523 | 419 |
|
58528 | 420 |
|
58535 | 421 |
/* ignored text */ |
58528 | 422 |
|
423 |
private val ignored: Parser[Chunk] = |
|
58609 | 424 |
rep1("""(?i)([^@]+|@[ \t]*comment)""".r) ^^ { |
58535 | 425 |
case ss => Chunk("", List(Token(Token.Kind.COMMENT, ss.mkString))) } |
58523 | 426 |
|
58536
402a8e8107a7
more total chunk_line: recovery via ignored_line;
wenzelm
parents:
58535
diff
changeset
|
427 |
private def ignored_line: Parser[(Chunk, Line_Context)] = |
58589 | 428 |
ignored ^^ { case a => (a, Ignored) } |
58536
402a8e8107a7
more total chunk_line: recovery via ignored_line;
wenzelm
parents:
58535
diff
changeset
|
429 |
|
58523 | 430 |
|
431 |
/* delimited string: outermost "..." or {...} and body with balanced {...} */ |
|
432 |
||
58534 | 433 |
// see also bibtex.web: scan_a_field_token_and_eat_white, scan_balanced_braces |
58523 | 434 |
private def delimited_depth(delim: Delimited): Parser[(String, Delimited)] = |
435 |
new Parser[(String, Delimited)] |
|
436 |
{ |
|
437 |
require(if (delim.quoted) delim.depth > 0 else delim.depth >= 0) |
|
438 |
||
439 |
def apply(in: Input) = |
|
440 |
{ |
|
441 |
val start = in.offset |
|
442 |
val end = in.source.length |
|
443 |
||
444 |
var i = start |
|
445 |
var q = delim.quoted |
|
446 |
var d = delim.depth |
|
447 |
var finished = false |
|
448 |
while (!finished && i < end) { |
|
449 |
val c = in.source.charAt(i) |
|
58534 | 450 |
|
58523 | 451 |
if (c == '"' && d == 0) { i += 1; d = 1; q = true } |
58532 | 452 |
else if (c == '"' && d == 1 && q) { |
453 |
i += 1; d = 0; q = false; finished = true |
|
454 |
} |
|
58523 | 455 |
else if (c == '{') { i += 1; d += 1 } |
58534 | 456 |
else if (c == '}') { |
457 |
if (d == 1 && !q || d > 1) { i += 1; d -= 1; if (d == 0) finished = true } |
|
458 |
else {i = start; finished = true } |
|
58532 | 459 |
} |
58523 | 460 |
else if (d > 0) i += 1 |
461 |
else finished = true |
|
462 |
} |
|
463 |
if (i == start) Failure("bad input", in) |
|
58528 | 464 |
else { |
465 |
val s = in.source.subSequence(start, i).toString |
|
466 |
Success((s, Delimited(q, d)), in.drop(i - start)) |
|
467 |
} |
|
58523 | 468 |
} |
469 |
}.named("delimited_depth") |
|
470 |
||
58528 | 471 |
private def delimited: Parser[Token] = |
472 |
delimited_depth(Closed) ^? |
|
473 |
{ case (s, delim) if delim == Closed => Token(Token.Kind.STRING, s) } |
|
58523 | 474 |
|
58534 | 475 |
private def recover_delimited: Parser[Token] = |
58609 | 476 |
"""["{][^@]*""".r ^^ token(Token.Kind.ERROR) |
58534 | 477 |
|
58590
472b9fbcc7f0
back to liberal spaces of official syntax (in contrast to 4b190c763097), e.g. relevant for easychair.bib;
wenzelm
parents:
58589
diff
changeset
|
478 |
def delimited_line(ctxt: Item): Parser[(Chunk, Line_Context)] = |
472b9fbcc7f0
back to liberal spaces of official syntax (in contrast to 4b190c763097), e.g. relevant for easychair.bib;
wenzelm
parents:
58589
diff
changeset
|
479 |
delimited_depth(ctxt.delim) ^^ { case (s, delim1) => |
472b9fbcc7f0
back to liberal spaces of official syntax (in contrast to 4b190c763097), e.g. relevant for easychair.bib;
wenzelm
parents:
58589
diff
changeset
|
480 |
(Chunk(ctxt.kind, List(Token(Token.Kind.STRING, s))), ctxt.copy(delim = delim1)) } | |
472b9fbcc7f0
back to liberal spaces of official syntax (in contrast to 4b190c763097), e.g. relevant for easychair.bib;
wenzelm
parents:
58589
diff
changeset
|
481 |
recover_delimited ^^ { case a => (Chunk(ctxt.kind, List(a)), Ignored) } |
58523 | 482 |
|
483 |
||
484 |
/* other tokens */ |
|
485 |
||
486 |
private val at = "@" ^^ keyword |
|
487 |
||
488 |
private val nat = "[0-9]+".r ^^ token(Token.Kind.NAT) |
|
489 |
||
58591 | 490 |
private val name = """[\x21-\x7f&&[^"#%'(),={}]]+""".r ^^ token(Token.Kind.NAME) |
491 |
||
58529 | 492 |
private val identifier = |
493 |
"""[\x21-\x7f&&[^"#%'(),={}0-9]][\x21-\x7f&&[^"#%'(),={}]]*""".r |
|
494 |
||
495 |
private val ident = identifier ^^ token(Token.Kind.IDENT) |
|
58523 | 496 |
|
58528 | 497 |
val other_token = "[=#,]".r ^^ keyword | (nat | (ident | space)) |
498 |
||
499 |
||
58591 | 500 |
/* body */ |
501 |
||
502 |
private val body = |
|
503 |
delimited | (recover_delimited | other_token) |
|
504 |
||
505 |
private def body_line(ctxt: Item) = |
|
506 |
if (ctxt.delim.depth > 0) |
|
507 |
delimited_line(ctxt) |
|
508 |
else |
|
509 |
delimited_line(ctxt) | |
|
510 |
other_token ^^ { case a => (Chunk(ctxt.kind, List(a)), ctxt) } | |
|
511 |
ctxt.end ^^ { case a => (Chunk(ctxt.kind, List(keyword(a))), Ignored) } |
|
512 |
||
513 |
||
58530 | 514 |
/* items: command or entry */ |
58528 | 515 |
|
58530 | 516 |
private val item_kind = |
58529 | 517 |
identifier ^^ { case a => |
518 |
val kind = |
|
519 |
if (is_command(a)) Token.Kind.COMMAND |
|
58530 | 520 |
else if (is_entry(a)) Token.Kind.ENTRY |
58529 | 521 |
else Token.Kind.IDENT |
522 |
Token(kind, a) |
|
523 |
} |
|
524 |
||
58591 | 525 |
private val item_begin = |
526 |
"{" ^^ { case a => ("}", keyword(a)) } | |
|
527 |
"(" ^^ { case a => (")", keyword(a)) } |
|
528 |
||
529 |
private def item_name(kind: String) = |
|
530 |
kind.toLowerCase match { |
|
531 |
case "preamble" => failure("") |
|
532 |
case "string" => identifier ^^ token(Token.Kind.NAME) |
|
533 |
case _ => name |
|
534 |
} |
|
535 |
||
58531 | 536 |
private val item_start = |
58590
472b9fbcc7f0
back to liberal spaces of official syntax (in contrast to 4b190c763097), e.g. relevant for easychair.bib;
wenzelm
parents:
58589
diff
changeset
|
537 |
at ~ spaces ~ item_kind ~ spaces ^^ |
58528 | 538 |
{ case a ~ b ~ c ~ d => (c.source, List(a) ::: b ::: List(c) ::: d) } |
539 |
||
58530 | 540 |
private val item: Parser[Chunk] = |
58591 | 541 |
(item_start ~ item_begin ~ spaces) into |
542 |
{ case (kind, a) ~ ((end, b)) ~ c => |
|
543 |
opt(item_name(kind)) ~ rep(body) ~ opt(end ^^ keyword) ^^ { |
|
544 |
case d ~ e ~ f => Chunk(kind, a ::: List(b) ::: c ::: d.toList ::: e ::: f.toList) } } |
|
58528 | 545 |
|
58530 | 546 |
private val recover_item: Parser[Chunk] = |
58609 | 547 |
at ~ "[^@]*".r ^^ { case a ~ b => Chunk("", List(a, Token(Token.Kind.ERROR, b))) } |
58528 | 548 |
|
58591 | 549 |
|
550 |
/* chunks */ |
|
58523 | 551 |
|
58528 | 552 |
val chunk: Parser[Chunk] = ignored | (item | recover_item) |
58523 | 553 |
|
58528 | 554 |
def chunk_line(ctxt: Line_Context): Parser[(Chunk, Line_Context)] = |
58530 | 555 |
{ |
556 |
ctxt match { |
|
58589 | 557 |
case Ignored => |
58538
299b82d12d53
proper treatment of @comment (amending 402a8e8107a7);
wenzelm
parents:
58536
diff
changeset
|
558 |
ignored_line | |
58590
472b9fbcc7f0
back to liberal spaces of official syntax (in contrast to 4b190c763097), e.g. relevant for easychair.bib;
wenzelm
parents:
58589
diff
changeset
|
559 |
at ^^ { case a => (Chunk("", List(a)), At) } |
472b9fbcc7f0
back to liberal spaces of official syntax (in contrast to 4b190c763097), e.g. relevant for easychair.bib;
wenzelm
parents:
58589
diff
changeset
|
560 |
|
472b9fbcc7f0
back to liberal spaces of official syntax (in contrast to 4b190c763097), e.g. relevant for easychair.bib;
wenzelm
parents:
58589
diff
changeset
|
561 |
case At => |
472b9fbcc7f0
back to liberal spaces of official syntax (in contrast to 4b190c763097), e.g. relevant for easychair.bib;
wenzelm
parents:
58589
diff
changeset
|
562 |
space ^^ { case a => (Chunk("", List(a)), ctxt) } | |
472b9fbcc7f0
back to liberal spaces of official syntax (in contrast to 4b190c763097), e.g. relevant for easychair.bib;
wenzelm
parents:
58589
diff
changeset
|
563 |
item_kind ^^ { case a => (Chunk(a.source, List(a)), Item_Start(a.source)) } | |
472b9fbcc7f0
back to liberal spaces of official syntax (in contrast to 4b190c763097), e.g. relevant for easychair.bib;
wenzelm
parents:
58589
diff
changeset
|
564 |
recover_item ^^ { case a => (a, Ignored) } | |
472b9fbcc7f0
back to liberal spaces of official syntax (in contrast to 4b190c763097), e.g. relevant for easychair.bib;
wenzelm
parents:
58589
diff
changeset
|
565 |
ignored_line |
472b9fbcc7f0
back to liberal spaces of official syntax (in contrast to 4b190c763097), e.g. relevant for easychair.bib;
wenzelm
parents:
58589
diff
changeset
|
566 |
|
472b9fbcc7f0
back to liberal spaces of official syntax (in contrast to 4b190c763097), e.g. relevant for easychair.bib;
wenzelm
parents:
58589
diff
changeset
|
567 |
case Item_Start(kind) => |
472b9fbcc7f0
back to liberal spaces of official syntax (in contrast to 4b190c763097), e.g. relevant for easychair.bib;
wenzelm
parents:
58589
diff
changeset
|
568 |
space ^^ { case a => (Chunk(kind, List(a)), ctxt) } | |
58591 | 569 |
item_begin ^^ { case (end, a) => (Chunk(kind, List(a)), Item_Open(kind, end)) } | |
58596 | 570 |
recover_item ^^ { case a => (a, Ignored) } | |
58590
472b9fbcc7f0
back to liberal spaces of official syntax (in contrast to 4b190c763097), e.g. relevant for easychair.bib;
wenzelm
parents:
58589
diff
changeset
|
571 |
ignored_line |
472b9fbcc7f0
back to liberal spaces of official syntax (in contrast to 4b190c763097), e.g. relevant for easychair.bib;
wenzelm
parents:
58589
diff
changeset
|
572 |
|
58591 | 573 |
case Item_Open(kind, end) => |
58590
472b9fbcc7f0
back to liberal spaces of official syntax (in contrast to 4b190c763097), e.g. relevant for easychair.bib;
wenzelm
parents:
58589
diff
changeset
|
574 |
space ^^ { case a => (Chunk(kind, List(a)), ctxt) } | |
58591 | 575 |
item_name(kind) ^^ { case a => (Chunk(kind, List(a)), Item(kind, end, Closed)) } | |
576 |
body_line(Item(kind, end, Closed)) | |
|
58590
472b9fbcc7f0
back to liberal spaces of official syntax (in contrast to 4b190c763097), e.g. relevant for easychair.bib;
wenzelm
parents:
58589
diff
changeset
|
577 |
ignored_line |
472b9fbcc7f0
back to liberal spaces of official syntax (in contrast to 4b190c763097), e.g. relevant for easychair.bib;
wenzelm
parents:
58589
diff
changeset
|
578 |
|
472b9fbcc7f0
back to liberal spaces of official syntax (in contrast to 4b190c763097), e.g. relevant for easychair.bib;
wenzelm
parents:
58589
diff
changeset
|
579 |
case item_ctxt: Item => |
58591 | 580 |
body_line(item_ctxt) | |
58590
472b9fbcc7f0
back to liberal spaces of official syntax (in contrast to 4b190c763097), e.g. relevant for easychair.bib;
wenzelm
parents:
58589
diff
changeset
|
581 |
ignored_line |
472b9fbcc7f0
back to liberal spaces of official syntax (in contrast to 4b190c763097), e.g. relevant for easychair.bib;
wenzelm
parents:
58589
diff
changeset
|
582 |
|
58530 | 583 |
case _ => failure("") |
584 |
} |
|
585 |
} |
|
58528 | 586 |
} |
58523 | 587 |
|
588 |
||
58528 | 589 |
/* parse */ |
58523 | 590 |
|
591 |
def parse(input: CharSequence): List[Chunk] = |
|
64824 | 592 |
Parsers.parseAll(Parsers.rep(Parsers.chunk), Scan.char_reader(input)) match { |
58523 | 593 |
case Parsers.Success(result, _) => result |
58526 | 594 |
case _ => error("Unexpected failure to parse input:\n" + input.toString) |
58523 | 595 |
} |
58528 | 596 |
|
597 |
def parse_line(input: CharSequence, context: Line_Context): (List[Chunk], Line_Context) = |
|
598 |
{ |
|
64824 | 599 |
var in: Reader[Char] = Scan.char_reader(input) |
58528 | 600 |
val chunks = new mutable.ListBuffer[Chunk] |
601 |
var ctxt = context |
|
602 |
while (!in.atEnd) { |
|
603 |
Parsers.parse(Parsers.chunk_line(ctxt), in) match { |
|
60215 | 604 |
case Parsers.Success((x, c), rest) => chunks += x; ctxt = c; in = rest |
58528 | 605 |
case Parsers.NoSuccess(_, rest) => |
606 |
error("Unepected failure to parse input:\n" + rest.source.toString) |
|
607 |
} |
|
608 |
} |
|
609 |
(chunks.toList, ctxt) |
|
610 |
} |
|
67243 | 611 |
|
612 |
||
613 |
||
614 |
/** HTML output **/ |
|
615 |
||
616 |
private val output_styles = |
|
617 |
List( |
|
67257 | 618 |
"" -> "html-n", |
67243 | 619 |
"plain" -> "html-n", |
620 |
"alpha" -> "html-a", |
|
621 |
"named" -> "html-n", |
|
622 |
"paragraph" -> "html-n", |
|
623 |
"unsort" -> "html-u", |
|
624 |
"unsortlist" -> "html-u") |
|
625 |
||
626 |
def html_output(bib: List[Path], |
|
67256 | 627 |
title: String = "Bibliography", |
67248
68177abb2988
isabelle.preview presents bibtex database files as well;
wenzelm
parents:
67243
diff
changeset
|
628 |
body: Boolean = false, |
67243 | 629 |
citations: List[String] = List("*"), |
67257 | 630 |
style: String = "", |
67243 | 631 |
chronological: Boolean = false): String = |
632 |
{ |
|
633 |
Isabelle_System.with_tmp_dir("bibtex")(tmp_dir => |
|
634 |
{ |
|
635 |
/* database files */ |
|
636 |
||
637 |
val bib_files = bib.map(path => path.split_ext._1) |
|
638 |
val bib_names = |
|
639 |
{ |
|
640 |
val names0 = bib_files.map(_.base_name) |
|
641 |
if (Library.duplicates(names0).isEmpty) names0 |
|
642 |
else names0.zipWithIndex.map({ case (name, i) => (i + 1).toString + "-" + name }) |
|
643 |
} |
|
644 |
||
645 |
for ((a, b) <- bib_files zip bib_names) { |
|
646 |
File.copy(a.ext("bib"), tmp_dir + Path.basic(b).ext("bib")) |
|
647 |
} |
|
648 |
||
649 |
||
650 |
/* style file */ |
|
651 |
||
652 |
val bst = |
|
653 |
output_styles.toMap.get(style) match { |
|
654 |
case Some(base) => base + (if (chronological) "c" else "") + ".bst" |
|
655 |
case None => |
|
656 |
error("Bad style for bibtex HTML output: " + quote(style) + |
|
657 |
"\n(expected: " + commas_quote(output_styles.map(_._1)) + ")") |
|
658 |
} |
|
659 |
File.copy(Path.explode("$BIB2XHTML_HOME/bst") + Path.explode(bst), tmp_dir) |
|
660 |
||
661 |
||
662 |
/* result */ |
|
663 |
||
664 |
val in_file = Path.explode("bib.aux") |
|
665 |
val out_file = Path.explode("bib.html") |
|
666 |
||
667 |
File.write(tmp_dir + in_file, |
|
668 |
bib_names.mkString("\\bibdata{", ",", "}\n") + |
|
669 |
citations.map(cite => "\\citation{" + cite + "}\n").mkString) |
|
670 |
||
671 |
Isabelle_System.bash( |
|
672 |
"\"$BIB2XHTML_HOME/main/bib2xhtml.pl\" -B \"$ISABELLE_BIBTEX\"" + |
|
67257 | 673 |
" -u -s " + Bash.string(proper_string(style) getOrElse "empty") + |
674 |
(if (chronological) " -c" else "") + |
|
67256 | 675 |
(if (title != "") " -h " + Bash.string(title) + " " else "") + |
676 |
" " + File.bash_path(in_file) + " " + File.bash_path(out_file), |
|
67243 | 677 |
cwd = tmp_dir.file).check |
678 |
||
679 |
val html = File.read(tmp_dir + out_file) |
|
680 |
||
67248
68177abb2988
isabelle.preview presents bibtex database files as well;
wenzelm
parents:
67243
diff
changeset
|
681 |
if (body) { |
68177abb2988
isabelle.preview presents bibtex database files as well;
wenzelm
parents:
67243
diff
changeset
|
682 |
cat_lines( |
68177abb2988
isabelle.preview presents bibtex database files as well;
wenzelm
parents:
67243
diff
changeset
|
683 |
split_lines(html). |
68177abb2988
isabelle.preview presents bibtex database files as well;
wenzelm
parents:
67243
diff
changeset
|
684 |
dropWhile(line => !line.startsWith("<!-- BEGIN BIBLIOGRAPHY")).reverse. |
68177abb2988
isabelle.preview presents bibtex database files as well;
wenzelm
parents:
67243
diff
changeset
|
685 |
dropWhile(line => !line.startsWith("<!-- END BIBLIOGRAPHY")).reverse) |
68177abb2988
isabelle.preview presents bibtex database files as well;
wenzelm
parents:
67243
diff
changeset
|
686 |
} |
68177abb2988
isabelle.preview presents bibtex database files as well;
wenzelm
parents:
67243
diff
changeset
|
687 |
else html |
67243 | 688 |
}) |
689 |
} |
|
58523 | 690 |
} |