src/Pure/Thy/latex.scala
author wenzelm
Tue, 12 Dec 2017 17:47:23 +0100
changeset 67190 58ab7ddbdb04
parent 67188 bc7a6455e12a
child 67194 1c0a6a957114
permissions -rw-r--r--
clarified file pattern;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
67172
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/Thy/latex.scala
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
     3
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
     4
Support for LaTeX.
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
     5
*/
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
     6
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
     7
package isabelle
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
     8
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
     9
67173
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
    10
import java.io.{File => JFile}
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
    11
67172
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
    12
import scala.annotation.tailrec
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
    13
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
    14
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
    15
object Latex
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
    16
{
67173
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
    17
  /** latex errors **/
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
    18
67176
13b5c3ff1954 re-implemented "isabelle document" in Isabelle/Scala, include latex_errors here;
wenzelm
parents: 67174
diff changeset
    19
  def latex_errors(dir: Path, root_name: String): List[String] =
13b5c3ff1954 re-implemented "isabelle document" in Isabelle/Scala, include latex_errors here;
wenzelm
parents: 67174
diff changeset
    20
  {
13b5c3ff1954 re-implemented "isabelle document" in Isabelle/Scala, include latex_errors here;
wenzelm
parents: 67174
diff changeset
    21
    val root_log_path = dir + Path.explode(root_name).ext("log")
13b5c3ff1954 re-implemented "isabelle document" in Isabelle/Scala, include latex_errors here;
wenzelm
parents: 67174
diff changeset
    22
    if (root_log_path.is_file) {
13b5c3ff1954 re-implemented "isabelle document" in Isabelle/Scala, include latex_errors here;
wenzelm
parents: 67174
diff changeset
    23
      for { (msg, pos) <- filter_errors(dir, File.read(root_log_path)) }
13b5c3ff1954 re-implemented "isabelle document" in Isabelle/Scala, include latex_errors here;
wenzelm
parents: 67174
diff changeset
    24
      yield "Latex error" + Position.here(pos) + ":\n" + cat_lines(split_lines(msg).map("  " + _))
13b5c3ff1954 re-implemented "isabelle document" in Isabelle/Scala, include latex_errors here;
wenzelm
parents: 67174
diff changeset
    25
    }
13b5c3ff1954 re-implemented "isabelle document" in Isabelle/Scala, include latex_errors here;
wenzelm
parents: 67174
diff changeset
    26
    else Nil
13b5c3ff1954 re-implemented "isabelle document" in Isabelle/Scala, include latex_errors here;
wenzelm
parents: 67174
diff changeset
    27
  }
13b5c3ff1954 re-implemented "isabelle document" in Isabelle/Scala, include latex_errors here;
wenzelm
parents: 67174
diff changeset
    28
13b5c3ff1954 re-implemented "isabelle document" in Isabelle/Scala, include latex_errors here;
wenzelm
parents: 67174
diff changeset
    29
67173
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
    30
  /* generated .tex file */
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
    31
67184
ecc786cb3b7b more robust range on preceding comment-line;
wenzelm
parents: 67183
diff changeset
    32
  private val File_Pattern = """^%:%file=(.+)%:%$""".r
67188
bc7a6455e12a simplified positions -- line is also human-readable in generated .tex file;
wenzelm
parents: 67184
diff changeset
    33
  private val Line_Pattern = """^*%:%line=(\d+)%:%$""".r
67173
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
    34
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
    35
  def read_tex_file(tex_file: Path): Tex_File =
67174
258e1394e76a more robust Windows support;
wenzelm
parents: 67173
diff changeset
    36
    new Tex_File(tex_file, Line.logical_lines(File.read(tex_file)).toArray)
67173
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
    37
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
    38
  final class Tex_File private[Latex](val tex_file: Path, tex_lines: Array[String])
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
    39
  {
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
    40
    override def toString: String = tex_file.toString
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
    41
67188
bc7a6455e12a simplified positions -- line is also human-readable in generated .tex file;
wenzelm
parents: 67184
diff changeset
    42
    val source_file: Option[String] =
bc7a6455e12a simplified positions -- line is also human-readable in generated .tex file;
wenzelm
parents: 67184
diff changeset
    43
      if (tex_lines.nonEmpty) {
67173
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
    44
        tex_lines(0) match {
67188
bc7a6455e12a simplified positions -- line is also human-readable in generated .tex file;
wenzelm
parents: 67184
diff changeset
    45
          case File_Pattern(file) => Some(file)
67173
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
    46
          case _ => None
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
    47
        }
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
    48
      }
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
    49
      else None
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
    50
67184
ecc786cb3b7b more robust range on preceding comment-line;
wenzelm
parents: 67183
diff changeset
    51
    private def prev_lines_iterator(l: Int): Iterator[String] =
67188
bc7a6455e12a simplified positions -- line is also human-readable in generated .tex file;
wenzelm
parents: 67184
diff changeset
    52
      if (0 < l && l <= tex_lines.length)
bc7a6455e12a simplified positions -- line is also human-readable in generated .tex file;
wenzelm
parents: 67184
diff changeset
    53
        Range.inclusive(l - 1, 0, -1).iterator.map(tex_lines(_))
67184
ecc786cb3b7b more robust range on preceding comment-line;
wenzelm
parents: 67183
diff changeset
    54
      else Iterator.empty
ecc786cb3b7b more robust range on preceding comment-line;
wenzelm
parents: 67183
diff changeset
    55
67173
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
    56
    def source_position(l: Int): Option[Position.T] =
67184
ecc786cb3b7b more robust range on preceding comment-line;
wenzelm
parents: 67183
diff changeset
    57
      (for {
ecc786cb3b7b more robust range on preceding comment-line;
wenzelm
parents: 67183
diff changeset
    58
        file <- source_file.iterator
67188
bc7a6455e12a simplified positions -- line is also human-readable in generated .tex file;
wenzelm
parents: 67184
diff changeset
    59
        s <- prev_lines_iterator(l)
bc7a6455e12a simplified positions -- line is also human-readable in generated .tex file;
wenzelm
parents: 67184
diff changeset
    60
        line <-
bc7a6455e12a simplified positions -- line is also human-readable in generated .tex file;
wenzelm
parents: 67184
diff changeset
    61
          (s match {
bc7a6455e12a simplified positions -- line is also human-readable in generated .tex file;
wenzelm
parents: 67184
diff changeset
    62
            case Line_Pattern(Value.Int(line)) => Some(line)
67173
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
    63
            case _ => None
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
    64
          })
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
    65
      }
67188
bc7a6455e12a simplified positions -- line is also human-readable in generated .tex file;
wenzelm
parents: 67184
diff changeset
    66
      yield { Position.Line_File(line, file) }).toStream.headOption
67173
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
    67
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
    68
    def position(line: Int): Position.T =
67188
bc7a6455e12a simplified positions -- line is also human-readable in generated .tex file;
wenzelm
parents: 67184
diff changeset
    69
      source_position(line) getOrElse Position.Line_File(line, tex_file.implode)
67173
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
    70
  }
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
    71
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
    72
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
    73
  /* latex log */
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
    74
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
    75
  def filter_errors(dir: Path, root_log: String): List[(String, Position.T)] =
67172
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
    76
  {
67173
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
    77
    val seen_files = Synchronized(Map.empty[JFile, Tex_File])
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
    78
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
    79
    def check_tex_file(path: Path): Option[Tex_File] =
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
    80
      seen_files.change_result(seen =>
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
    81
        seen.get(path.file) match {
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
    82
          case None =>
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
    83
            if (path.is_file) {
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
    84
              val tex_file = read_tex_file(path)
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
    85
              (Some(tex_file), seen + (path.file -> tex_file))
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
    86
            }
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
    87
            else (None, seen)
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
    88
          case some => (some, seen)
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
    89
        })
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
    90
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
    91
    def tex_file_position(path: Path, line: Int): Position.T =
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
    92
      check_tex_file(path) match {
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
    93
        case Some(tex_file) => tex_file.position(line)
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
    94
        case None => Position.Line_File(line, path.implode)
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
    95
      }
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
    96
67172
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
    97
    object File_Line_Error
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
    98
    {
67190
58ab7ddbdb04 clarified file pattern;
wenzelm
parents: 67188
diff changeset
    99
      val Pattern = """^(.*?\.\w\w\w):(\d+): (.*)$""".r
67173
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
   100
      def unapply(line: String): Option[(Path, Int, String)] =
67172
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
   101
        line match {
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
   102
          case Pattern(file, Value.Int(line), message) =>
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
   103
            val path = File.standard_path(file)
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
   104
            if (Path.is_wellformed(path)) {
67183
28227b13a2f1 proper file;
wenzelm
parents: 67182
diff changeset
   105
              val file = (dir + Path.explode(path)).canonical
67173
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
   106
              if (file.is_file) Some((file, line, message)) else None
67172
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
   107
            }
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
   108
            else None
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
   109
          case _ => None
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
   110
        }
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
   111
    }
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
   112
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
   113
    object Line_Error
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
   114
    {
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
   115
      val Pattern = """^l\.(\d+) (.*)$""".r
67173
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
   116
      def unapply(line: String): Option[(Int, String)] =
67172
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
   117
        line match {
67173
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
   118
          case Pattern(Value.Int(line), message) => Some((line, message))
67172
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
   119
          case _ => None
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
   120
        }
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
   121
    }
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
   122
67173
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
   123
    @tailrec def filter(lines: List[String], result: List[(String, Position.T)])
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
   124
      : List[(String, Position.T)] =
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
   125
    {
67172
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
   126
      lines match {
67173
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
   127
        case File_Line_Error((file, line, msg1)) :: rest1 =>
67183
28227b13a2f1 proper file;
wenzelm
parents: 67182
diff changeset
   128
          val pos = tex_file_position(file, line)
67172
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
   129
          rest1 match {
67173
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
   130
            case Line_Error((line2, msg2)) :: rest2 if line == line2 =>
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
   131
              filter(rest2, (Exn.cat_message(msg1, msg2), pos) :: result)
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
   132
            case _ =>
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
   133
              filter(rest1, (msg1, pos) :: result)
67172
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
   134
          }
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
   135
        case _ :: rest => filter(rest, result)
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
   136
        case Nil => result.reverse
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
   137
      }
67173
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
   138
    }
67172
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
   139
67174
258e1394e76a more robust Windows support;
wenzelm
parents: 67173
diff changeset
   140
    filter(Line.logical_lines(root_log), Nil)
67173
e746db6db903 more explicit latex errors;
wenzelm
parents: 67172
diff changeset
   141
  }
67172
97d199699a6b some support for LaTeX;
wenzelm
parents:
diff changeset
   142
}