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