Changeset 1107


Ignore:
Timestamp:
Jun 25, 2011, 5:23:59 PM (6 years ago)
Author:
stefan
Message:

improved sty parsing and added cls parsing

Location:
trunk/tools
Files:
4 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/pom.xml

    r1073 r1107  
    4747      <artifactId>scala-library</artifactId>
    4848      <version>${scala.version}</version>
     49    </dependency>
     50    <dependency>
     51      <groupId>commons-lang</groupId>
     52      <artifactId>commons-lang</artifactId>
     53      <version>2.5</version>
    4954    </dependency>
    5055    <dependency>
  • trunk/tools/scripts/findTexFile

    r1091 r1107  
    22
    33/usr/bin/find /usr/share/texmf* -name $1
    4 
     4/usr/bin/find /usr/share/texmf* -name $1.tex
  • trunk/tools/src/jlatexeditor/tools/PackageParser.scala

    r1096 r1107  
    55import java.io._
    66import collection.mutable._
     7import org.apache.commons.lang.StringEscapeUtils
    78
    89/**
     
    1112object PackageParser {
    1213  val Def = ".*\\\\def\\s*\\\\(\\w+)([#\\[\\]\\d]*)\\s*\\{.*".r
    13   val NewCommand = ".*\\\\newcommand\\{\\\\(\\w+)\\}(?:\\[(\\d+)\\])?(?:\\[([^\\]]+)\\])?.*".r
     14  val DefArgCount = "#+".r
     15  val NewCommand = ".*\\\\newcommand\\{?\\\\(\\w+)\\}?(?:\\[(\\d+)\\])?(?:\\[([^\\]]+)\\])?.*".r
    1416  val NewEnvironment = ".*\\\\newenvironment\\{(\\w+)\\}(?:\\[(\\d+)\\])?(?:\\[([^\\]]+)\\])?.*".r
    1517  val Input = ".*\\\\input\\{([^}]+)\\}".r
     
    3638
    3739    val packs = new MutableList[Package]
     40    val classes = new MutableList[Package]
    3841
    39     texmfDirs.foreach(dir => parse(packs, dir))
     42    texmfDirs.foreach(dir => parse(packs, classes, dir))
    4043
    41     writeXmlFile(packs)
     44    writeXmlFile("packages.xml", packs)
     45    writeXmlFile("docclasses.xml", classes)
    4246
    4347    /*
     
    5660  }
    5761
    58   def writeXmlFile(packages: MutableList[Package]) {
    59     val out = new PrintStream("packages.xml")
     62  def writeXmlFile(file: String, packages: MutableList[Package]) {
     63    val out = new PrintStream(file)
    6064    out.println("<packages>")
    6165    for (pack <- packages) {
     
    6367      out.println("  <package name=\"" + pack.name + "\"" + debPackageString + ">")
    6468      for (command <- pack.commands.values) {
    65         val optArgString = if (command.optionalArgs.isEmpty) "" else " optionalArg=\"" + command.optionalArgs(0) + "\""
     69        val optArgString = if (command.optionalArgs.isEmpty) "" else " optionalArg=\"" + escape(command.optionalArgs(0)) + "\""
    6670        out.println("    <command name=\"" + command.name + "\" argCount=\"" + command.argCount + "\"" + optArgString + " />")
    6771      }
    6872      for (env <- pack.environments.values) {
    69         val optArgString = if (env.optionalArgs.isEmpty) "" else " optionalArg=\"" + env.optionalArgs(0) + "\""
     73        val optArgString = if (env.optionalArgs.isEmpty) "" else " optionalArg=\"" + escape(env.optionalArgs(0)) + "\""
    7074        out.println("    <environment name=\"" + env.name + "\" argCount=\"" + env.argCount + "\"" + optArgString + " />")
    7175      }
     
    7781  }
    7882
    79   def parse (packs: MutableList[Package], dir: File) {
     83  def escape(text: String) = StringEscapeUtils.escapeHtml(text)
     84
     85  def parse (packs: MutableList[Package], classes: MutableList[Package], dir: File) {
    8086    for (file <- dir.listFiles()) {
    8187      if (file.isDirectory) {
    82         parse(packs, file)
     88        parse(packs, classes, file)
    8389      } else
    84       if (file.isFile && file.getName.endsWith(".sty")) {
    85         val packageName = file.getName.substring(0, file.getName.length() - 4)
    86         //println(packageName)
    87         val pack = new Package(file, packageName)
    88         packs += pack
    89         print(packs.size + "\r")
    90         parseFile(pack, file);
     90      if (file.isFile) {
     91        if (file.getName.endsWith(".sty") || file.getName.endsWith(".cls")) {
     92          val packageName = file.getName.substring(0, file.getName.length() - 4)
     93          val pack = new Package(file, file.getName.endsWith(".cls"), packageName)
     94          pack.cls match {
     95            case false => packs   += pack
     96            case true  => classes += pack
     97          }
     98          print(packs.size + " | " + classes.size + "\r")
     99          parseFile(pack, file);
     100        }
    91101      }
    92102    }
     
    97107      line match {
    98108        case Def(cmd, args) =>
    99           pack.commands += cmd -> Command(pack, cmd, args.count(_ == '#'))
     109          pack.commands += cmd -> new Command(pack, cmd, DefArgCount.findAllIn(args).size)
    100110        case NewCommand(cmd, args, optArg) =>
    101111          val argCount = if (args == null) 0 else args.toInt
    102           pack.commands += cmd -> Command(pack, cmd, argCount, if (optArg == null) List() else List(optArg))
    103 //          println(cmd + " with " + args + " arguments and " + optArgs)
     112          pack.commands += cmd -> new Command(pack, cmd, argCount, if (optArg == null) List() else List(optArg))
    104113        case NewEnvironment(cmd, args, optArg) =>
    105114          val argCount = if (args == null) 0 else args.toInt
    106           pack.environments += cmd -> Environment(pack, cmd, argCount, if (optArg == null) List() else List(optArg))
    107 //          println("  environment " + cmd + " with " + args + " arguments and " + optArgs)
     115          pack.environments += cmd -> new Environment(pack, cmd, argCount, if (optArg == null) List() else List(optArg))
    108116        case Input(fileName) =>
    109117          try {
     
    116124            case e: FileNotFoundException =>
    117125          }
    118 //          parseFile(pack, )
    119126        case _ =>
    120127      }
     
    156163    }
    157164
    158     DebPackage(name, files)
     165    new DebPackage(name, files)
    159166  }
    160167
    161   case class Command(pack: Package, name: String, argCount: Int, optionalArgs: List[String] = List())
    162   case class Environment(pack: Package, name: String, argCount: Int, optionalArgs: List[String] = List())
    163   case class Package(file: File, name: String, commands: LinkedHashMap[String, Command] = new LinkedHashMap[String, Command],
    164                     environments: LinkedHashMap[String, Environment] = new LinkedHashMap[String, Environment]) {
     168  class Command(val pack: Package, val name: String, val argCount: Int, val optionalArgs: List[String] = List())
     169  class Environment(val pack: Package, val name: String, val argCount: Int, val optionalArgs: List[String] = List())
     170  class Package(val file: File, val cls: Boolean, val name: String, val commands: LinkedHashMap[String, Command] = new LinkedHashMap[String, Command],
     171                val environments: LinkedHashMap[String, Environment] = new LinkedHashMap[String, Environment]) {
    165172    val debPackage = try {
    166173      Some(findDebPackage(file.getAbsolutePath))
     
    169176    }
    170177  }
    171   case class DebPackage(name: String, files: MutableList[String])
     178  class DebPackage(val name: String, val files: MutableList[String])
    172179}
Note: See TracChangeset for help on using the changeset viewer.