src/Pure/Tools/doc.scala
author wenzelm
Sat Apr 05 18:52:03 2014 +0200 (2014-04-05)
changeset 56423 c2f52824dbb2
parent 56422 7490555d7dff
child 56424 7032378cc097
permissions -rw-r--r--
explicit indication of important doc sections ("!"), which are expanded in the tree view;
     1 /*  Title:      Pure/Tools/doc.scala
     2     Author:     Makarius
     3 
     4 View Isabelle documentation.
     5 */
     6 
     7 package isabelle
     8 
     9 
    10 import scala.util.matching.Regex
    11 
    12 
    13 object Doc
    14 {
    15   /* dirs */
    16 
    17   def dirs(): List[Path] =
    18     Path.split(Isabelle_System.getenv("ISABELLE_DOCS")).map(dir =>
    19       if (dir.is_dir) dir
    20       else error("Bad documentation directory: " + dir))
    21 
    22 
    23   /* contents */
    24 
    25   private def contents_lines(): List[(Path, String)] =
    26     for {
    27       dir <- dirs()
    28       catalog = dir + Path.basic("Contents")
    29       if catalog.is_file
    30       line <- split_lines(Library.trim_line(File.read(catalog)))
    31     } yield (dir, line)
    32 
    33   sealed abstract class Entry
    34   case class Section(text: String, important: Boolean) extends Entry
    35   case class Doc(name: String, title: String, path: Path) extends Entry
    36   case class Text_File(name: String, path: Path) extends Entry
    37 
    38   def text_file(name: String): Option[Text_File] =
    39   {
    40     val path = Path.variable("ISABELLE_HOME") + Path.explode(name)
    41     if (path.is_file) Some(Text_File(name, path))
    42     else None
    43   }
    44 
    45   private val Section_Entry = new Regex("""^(\S.*)\s*$""")
    46   private val Doc_Entry = new Regex("""^\s+(\S+)\s+(.+)\s*$""")
    47 
    48   private def release_notes(): List[Entry] =
    49   {
    50     val names =
    51       List("ANNOUNCE", "README", "NEWS", "COPYRIGHT", "CONTRIBUTORS",
    52         "contrib/README", "README_REPOSITORY")
    53     Section("Release notes", true) ::
    54       (for (name <- names; entry <- text_file(name)) yield entry)
    55   }
    56 
    57   private def examples(): List[Entry] =
    58   {
    59     val names =
    60       List(
    61         "src/HOL/ex/Seq.thy",
    62         "src/HOL/ex/ML.thy",
    63         "src/HOL/Unix/Unix.thy",
    64         "src/HOL/Isar_Examples/Drinker.thy",
    65         "src/Tools/SML/Examples.thy")
    66     Section("Examples", true) :: names.map(name => text_file(name).get)
    67   }
    68 
    69   def contents(): List[Entry] =
    70     (for {
    71       (dir, line) <- contents_lines()
    72       entry <-
    73         line match {
    74           case Section_Entry(text) =>
    75             Library.try_unsuffix("!", text) match {
    76               case None => Some(Section(text, false))
    77               case Some(txt) => Some(Section(txt, true))
    78             }
    79           case Doc_Entry(name, title) => Some(Doc(name, title, dir + Path.basic(name)))
    80           case _ => None
    81         }
    82     } yield entry) ::: release_notes() ::: examples()
    83 
    84 
    85   /* view */
    86 
    87   def view(path: Path)
    88   {
    89     if (path.is_file) Console.println(File.read(path))
    90     else {
    91       val pdf = path.ext("pdf")
    92       if (pdf.is_file) Isabelle_System.pdf_viewer(pdf)
    93       else error("Bad Isabelle documentation file: " + pdf)
    94     }
    95   }
    96 
    97 
    98   /* command line entry point */
    99 
   100   def main(args: Array[String])
   101   {
   102     Command_Line.tool {
   103       if (args.isEmpty) Console.println(cat_lines(contents_lines().map(_._2)))
   104       else {
   105         val entries = contents()
   106         args.foreach(arg =>
   107           entries.collectFirst { case Doc(name, _, path) if arg == name => path } match {
   108             case Some(path) => view(path)
   109             case None => error("No Isabelle documentation entry: " + quote(arg))
   110           }
   111         )
   112       }
   113       0
   114     }
   115   }
   116 }
   117