src/Pure/Thy/latex.scala
changeset 74748 95643a0bff49
parent 73783 e4d50a660140
child 74777 2fd0c33fe440
equal deleted inserted replaced
74747:10991d115fff 74748:95643a0bff49
     8 
     8 
     9 
     9 
    10 import java.io.{File => JFile}
    10 import java.io.{File => JFile}
    11 
    11 
    12 import scala.annotation.tailrec
    12 import scala.annotation.tailrec
       
    13 import scala.collection.mutable
    13 import scala.util.matching.Regex
    14 import scala.util.matching.Regex
    14 
    15 
    15 
    16 
    16 object Latex
    17 object Latex
    17 {
    18 {
    19 
    20 
    20   type Text = XML.Body
    21   type Text = XML.Body
    21 
    22 
    22   def output(latex_text: Text, file_pos: String = ""): String =
    23   def output(latex_text: Text, file_pos: String = ""): String =
    23   {
    24   {
    24     def position(a: String, b: String): String = "%:%" + a + "=" + b + "%:%"
       
    25 
       
    26     var positions: List[String] =
       
    27       if (file_pos.isEmpty) Nil
       
    28       else List(position(Markup.FILE, file_pos), "\\endinput")
       
    29 
       
    30     var line = 1
    25     var line = 1
    31     var result = List.empty[String]
    26     val result = new mutable.ListBuffer[String]
       
    27     val positions = new mutable.ListBuffer[String]
       
    28 
       
    29     def position(a: String, b: String): String = "%:%" + a + "=" + b + "%:%\n"
       
    30 
       
    31     if (file_pos.nonEmpty) {
       
    32       positions += "\\endinput\n"
       
    33       positions += position(Markup.FILE, file_pos)
       
    34     }
    32 
    35 
    33     def traverse(body: XML.Body): Unit =
    36     def traverse(body: XML.Body): Unit =
    34     {
    37     {
    35       body.foreach {
    38       body.foreach {
    36         case XML.Wrapped_Elem(_, _, _) =>
    39         case XML.Wrapped_Elem(_, _, _) =>
    37         case XML.Elem(markup, body) =>
    40         case XML.Elem(markup, body) =>
    38           if (markup.name == Markup.DOCUMENT_LATEX) {
    41           if (markup.name == Markup.DOCUMENT_LATEX) {
    39             for { l <- Position.Line.unapply(markup.properties) if positions.nonEmpty } {
    42             for { l <- Position.Line.unapply(markup.properties) if positions.nonEmpty } {
    40               val s = position(Value.Int(line), Value.Int(l))
    43               val s = position(Value.Int(line), Value.Int(l))
    41               if (positions.head != s) positions ::= s
    44               if (positions.last != s) positions += s
    42             }
    45             }
    43             traverse(body)
    46             traverse(body)
    44           }
    47           }
    45         case XML.Text(s) =>
    48         case XML.Text(s) =>
    46           line += s.count(_ == '\n')
    49           line += s.count(_ == '\n')
    47           result ::= s
    50           result += s
    48       }
    51       }
    49     }
    52     }
    50     traverse(latex_text)
    53     traverse(latex_text)
    51 
    54 
    52     result.reverse.mkString + cat_lines(positions.reverse)
    55     result ++= positions
       
    56     result.mkString
    53   }
    57   }
    54 
    58 
    55 
    59 
    56   /* generated .tex file */
    60   /* generated .tex file */
    57 
    61