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