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