src/Pure/Thy/bibtex.scala
changeset 76972 6c542f2aab85
parent 76933 dd53bb198eb1
child 76984 29432d4a376d
equal deleted inserted replaced
76971:d1776c5ddc93 76972:6c542f2aab85
    10 import java.io.{File => JFile}
    10 import java.io.{File => JFile}
    11 
    11 
    12 import scala.collection.mutable
    12 import scala.collection.mutable
    13 import scala.util.parsing.combinator.RegexParsers
    13 import scala.util.parsing.combinator.RegexParsers
    14 import scala.util.parsing.input.Reader
    14 import scala.util.parsing.input.Reader
       
    15 import scala.util.matching.Regex
    15 
    16 
    16 
    17 
    17 object Bibtex {
    18 object Bibtex {
    18   /** file format **/
    19   /** file format **/
    19 
    20 
   713             dropWhile(line => !line.startsWith("<!-- END BIBLIOGRAPHY")).reverse)
   714             dropWhile(line => !line.startsWith("<!-- END BIBLIOGRAPHY")).reverse)
   714       }
   715       }
   715       else html
   716       else html
   716     }
   717     }
   717   }
   718   }
       
   719 
       
   720 
       
   721 
       
   722   /** cite commands and antiquotations **/
       
   723 
       
   724   def cite_antiquotation(name: String, body: String): String =
       
   725     """\<^""" + name + """>\<open>""" + body + """\<close>"""
       
   726 
       
   727   def cite_antiquotation(name: String, location: String, citations: List[String]): String = {
       
   728     val body =
       
   729       (if (location.isEmpty) "" else Symbol.cartouche(location) + " in ") +
       
   730       citations.map(quote).mkString(" and ")
       
   731     cite_antiquotation(name, body)
       
   732   }
       
   733 
       
   734   private val Cite_Command = """\\(cite|nocite|citet|citep)((?:\[[^\]]*\])?)\{([^}]*)\}""".r
       
   735   private val Cite_Antiq = """@\{cite\s*([^}]*)\}""".r
       
   736   private val Cite_Macro = """\[\s*cite_macro\s*=\s*"?(\w+)"?\]\s*""".r
       
   737 
       
   738   def update_cite(str: String): String = {
       
   739     val str1 =
       
   740       Cite_Command.replaceAllIn(str, { m =>
       
   741         val name = m.group(1)
       
   742         val loc = m.group(2)
       
   743         val location =
       
   744           if (loc.startsWith("[") && loc.endsWith("]")) loc.substring(1, loc.length - 1)
       
   745           else loc
       
   746         val citations = Library.space_explode(',', m.group(3)).map(_.trim)
       
   747         Regex.quoteReplacement(cite_antiquotation(name, location, citations))
       
   748       })
       
   749     val str2 =
       
   750       Cite_Antiq.replaceAllIn(str1, { m =>
       
   751         val body0 = m.group(1)
       
   752         val (name, body1) =
       
   753           Cite_Macro.findFirstMatchIn(body0) match {
       
   754             case None => ("cite", body0)
       
   755             case Some(m1) => (m1.group(1), Cite_Macro.replaceAllIn(body0, ""))
       
   756           }
       
   757         val body2 = body1.replace("""\<close>""", """\<close> in""")
       
   758         Regex.quoteReplacement(cite_antiquotation(name, body2))
       
   759       })
       
   760     str2
       
   761   }
   718 }
   762 }