src/Pure/Tools/doc.scala
changeset 75393 87ebf5a50283
parent 75120 488c7e8923b2
child 75394 42267c650205
equal deleted inserted replaced
75388:b3ca4a6ed74b 75393:87ebf5a50283
     8 
     8 
     9 
     9 
    10 import scala.collection.mutable
    10 import scala.collection.mutable
    11 
    11 
    12 
    12 
    13 object Doc
    13 object Doc {
    14 {
       
    15   /* dirs */
    14   /* dirs */
    16 
    15 
    17   def dirs(): List[Path] =
    16   def dirs(): List[Path] =
    18     Path.split(Isabelle_System.getenv("ISABELLE_DOCS"))
    17     Path.split(Isabelle_System.getenv("ISABELLE_DOCS"))
    19 
    18 
    26       catalog = dir + Path.basic("Contents")
    25       catalog = dir + Path.basic("Contents")
    27       if catalog.is_file
    26       if catalog.is_file
    28       line <- Library.trim_split_lines(File.read(catalog))
    27       line <- Library.trim_split_lines(File.read(catalog))
    29     } yield (dir, line)
    28     } yield (dir, line)
    30 
    29 
    31   object Contents
    30   object Contents {
    32   {
       
    33     def apply(sections: List[Section]): Contents = new Contents(sections)
    31     def apply(sections: List[Section]): Contents = new Contents(sections)
    34 
    32 
    35     def section(title: String, important: Boolean, entries: List[Entry]): Contents =
    33     def section(title: String, important: Boolean, entries: List[Entry]): Contents =
    36       apply(List(Section(title, important, entries)))
    34       apply(List(Section(title, important, entries)))
    37   }
    35   }
    38   class Contents private(val sections: List[Section])
    36   class Contents private(val sections: List[Section]) {
    39   {
       
    40     def ++ (other: Contents): Contents = new Contents(sections ::: other.sections)
    37     def ++ (other: Contents): Contents = new Contents(sections ::: other.sections)
    41     def entries: List[Entry] = sections.flatMap(_.entries)
    38     def entries: List[Entry] = sections.flatMap(_.entries)
    42     def docs: List[Doc] = entries.collect({ case doc: Doc => doc })
    39     def docs: List[Doc] = entries.collect({ case doc: Doc => doc })
    43   }
    40   }
    44 
    41 
    45   case class Section(title: String, important: Boolean, entries: List[Entry])
    42   case class Section(title: String, important: Boolean, entries: List[Entry])
    46   sealed abstract class Entry
    43   sealed abstract class Entry {
    47   {
       
    48     def name: String
    44     def name: String
    49     def path: Path
    45     def path: Path
    50   }
    46   }
    51   case class Doc(name: String, title: String, path: Path) extends Entry
    47   case class Doc(name: String, title: String, path: Path) extends Entry
    52   case class Text_File(name: String, path: Path) extends Entry
    48   case class Text_File(name: String, path: Path) extends Entry
    69         text_file(file) match {
    65         text_file(file) match {
    70           case Some(entry) => entry
    66           case Some(entry) => entry
    71           case None => error("Bad entry in ISABELLE_DOCS_EXAMPLES: " + file)
    67           case None => error("Bad entry in ISABELLE_DOCS_EXAMPLES: " + file)
    72         }))
    68         }))
    73 
    69 
    74   def main_contents(): Contents =
    70   def main_contents(): Contents = {
    75   {
       
    76     val result = new mutable.ListBuffer[Section]
    71     val result = new mutable.ListBuffer[Section]
    77     val entries = new mutable.ListBuffer[Entry]
    72     val entries = new mutable.ListBuffer[Entry]
    78     var section: Option[Section] = None
    73     var section: Option[Section] = None
    79 
    74 
    80     def flush(): Unit =
    75     def flush(): Unit =
    82         result += section.get.copy(entries = entries.toList)
    77         result += section.get.copy(entries = entries.toList)
    83         entries.clear()
    78         entries.clear()
    84         section = None
    79         section = None
    85       }
    80       }
    86 
    81 
    87     def begin(s: Section): Unit =
    82     def begin(s: Section): Unit = {
    88     {
       
    89       flush()
    83       flush()
    90       section = Some(s)
    84       section = Some(s)
    91     }
    85     }
    92 
    86 
    93     val Section_ = """^(\S.*)\s*$""".r
    87     val Section_ = """^(\S.*)\s*$""".r
   108 
   102 
   109     flush()
   103     flush()
   110     Contents(result.toList)
   104     Contents(result.toList)
   111   }
   105   }
   112 
   106 
   113   def contents(): Contents =
   107   def contents(): Contents = {
   114   {
       
   115     examples() ++ release_notes() ++ main_contents()
   108     examples() ++ release_notes() ++ main_contents()
   116   }
   109   }
   117 
   110 
   118   object Doc_Names extends Scala.Fun_String("doc_names")
   111   object Doc_Names extends Scala.Fun_String("doc_names") {
   119   {
       
   120     val here = Scala_Project.here
   112     val here = Scala_Project.here
   121     def apply(arg: String): String =
   113     def apply(arg: String): String =
   122       if (arg.nonEmpty) error("Bad argument: " + quote(arg))
   114       if (arg.nonEmpty) error("Bad argument: " + quote(arg))
   123       else cat_lines((for (doc <- contents().docs) yield doc.name).sorted)
   115       else cat_lines((for (doc <- contents().docs) yield doc.name).sorted)
   124   }
   116   }
   125 
   117 
   126 
   118 
   127   /* view */
   119   /* view */
   128 
   120 
   129   def view(path: Path): Unit =
   121   def view(path: Path): Unit = {
   130   {
       
   131     if (!path.is_file) error("Bad Isabelle documentation file: " + path)
   122     if (!path.is_file) error("Bad Isabelle documentation file: " + path)
   132     else if (path.is_pdf) Isabelle_System.pdf_viewer(path)
   123     else if (path.is_pdf) Isabelle_System.pdf_viewer(path)
   133     else Output.writeln(Library.trim_line(File.read(path)), stdout = true)
   124     else Output.writeln(Library.trim_line(File.read(path)), stdout = true)
   134   }
   125   }
   135 
   126 
   136 
   127 
   137   /* Isabelle tool wrapper */
   128   /* Isabelle tool wrapper */
   138 
   129 
   139   val isabelle_tool = Isabelle_Tool("doc", "view Isabelle PDF documentation",
   130   val isabelle_tool = Isabelle_Tool("doc", "view Isabelle PDF documentation",
   140     Scala_Project.here, args =>
   131     Scala_Project.here, args => {
   141   {
       
   142     val getopts = Getopts("""
   132     val getopts = Getopts("""
   143 Usage: isabelle doc [DOC ...]
   133 Usage: isabelle doc [DOC ...]
   144 
   134 
   145   View Isabelle PDF documentation.
   135   View Isabelle PDF documentation.
   146 """)
   136 """)