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