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