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