| 
58872
 | 
     1  | 
/*  Title:      Pure/Tools/update_header.scala
  | 
| 
 | 
     2  | 
    Author:     Makarius
  | 
| 
 | 
     3  | 
  | 
| 
 | 
     4  | 
Replace theory header command.
  | 
| 
 | 
     5  | 
*/
  | 
| 
 | 
     6  | 
  | 
| 
 | 
     7  | 
package isabelle
  | 
| 
 | 
     8  | 
  | 
| 
 | 
     9  | 
  | 
| 
 | 
    10  | 
object Update_Header
  | 
| 
 | 
    11  | 
{
 | 
| 
73340
 | 
    12  | 
  def update_header(section: String, path: Path): Unit =
  | 
| 
58872
 | 
    13  | 
  {
 | 
| 
 | 
    14  | 
    val text0 = File.read(path)
  | 
| 
 | 
    15  | 
    val text1 =
  | 
| 
59083
 | 
    16  | 
      (for (tok <- Token.explode(Keyword.Keywords.empty, text0).iterator)
  | 
| 
58872
 | 
    17  | 
        yield { if (tok.source == "header") section else tok.source }).mkString
 | 
| 
 | 
    18  | 
  | 
| 
 | 
    19  | 
    if (text0 != text1) {
 | 
| 
 | 
    20  | 
      Output.writeln("changing " + path)
 | 
| 
 | 
    21  | 
      File.write_backup2(path, text1)
  | 
| 
 | 
    22  | 
    }
  | 
| 
 | 
    23  | 
  }
  | 
| 
 | 
    24  | 
  | 
| 
 | 
    25  | 
  | 
| 
62836
 | 
    26  | 
  /* Isabelle tool wrapper */
  | 
| 
58872
 | 
    27  | 
  | 
| 
61463
 | 
    28  | 
  private val headings =
  | 
| 
 | 
    29  | 
    Set("chapter", "section", "subsection", "subsubsection", "paragraph", "subparagraph")
 | 
| 
 | 
    30  | 
  | 
| 
62836
 | 
    31  | 
  val isabelle_tool =
  | 
| 
72763
 | 
    32  | 
    Isabelle_Tool("update_header", "replace obsolete theory header command",
 | 
| 
 | 
    33  | 
      Scala_Project.here, args =>
  | 
| 
62836
 | 
    34  | 
    {
 | 
| 
62446
 | 
    35  | 
      var section = "section"
  | 
| 
 | 
    36  | 
  | 
| 
62454
 | 
    37  | 
      val getopts = Getopts("""
 | 
| 
62446
 | 
    38  | 
Usage: isabelle update_header [FILES|DIRS...]
  | 
| 
 | 
    39  | 
  | 
| 
 | 
    40  | 
  Options are:
  | 
| 
 | 
    41  | 
    -s COMMAND   alternative heading command (default 'section')
  | 
| 
 | 
    42  | 
  | 
| 
 | 
    43  | 
  Recursively find .thy files and replace obsolete theory header commands
  | 
| 
 | 
    44  | 
  by 'chapter', 'section' (default), 'subsection', 'subsubsection',
  | 
| 
 | 
    45  | 
  'paragraph', 'subparagraph'.
  | 
| 
 | 
    46  | 
  | 
| 
 | 
    47  | 
  Old versions of files are preserved by appending "~~".
  | 
| 
 | 
    48  | 
""",
  | 
| 
 | 
    49  | 
        "s:" -> (arg => section = arg))
  | 
| 
 | 
    50  | 
  | 
| 
 | 
    51  | 
      val specs = getopts(args)
  | 
| 
 | 
    52  | 
      if (specs.isEmpty) getopts.usage()
  | 
| 
 | 
    53  | 
  | 
| 
 | 
    54  | 
      if (!headings.contains(section))
  | 
| 
 | 
    55  | 
        error("Bad heading command: " + quote(section))
 | 
| 
 | 
    56  | 
  | 
| 
 | 
    57  | 
      for {
 | 
| 
 | 
    58  | 
        spec <- specs
  | 
| 
 | 
    59  | 
        file <- File.find_files(Path.explode(spec).file, file => file.getName.endsWith(".thy"))
 | 
| 
68994
 | 
    60  | 
      } update_header(section, File.path(file))
  | 
| 
62836
 | 
    61  | 
    })
  | 
| 
58872
 | 
    62  | 
}
  |