src/Pure/Tools/doc.scala
author wenzelm
Thu Sep 26 22:34:43 2013 +0200 (2013-09-26)
changeset 53935 59c6dbdf0a38
parent 53777 06a6216f733e
child 54690 cd88b44623bf
permissions -rw-r--r--
added Isabelle/ML example;
     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[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 line
    32 
    33   sealed abstract class Entry
    34   case class Section(text: String) extends Entry
    35   case class Doc(name: String, title: String) 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") ::
    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     Section("Examples") :: names.map(name => text_file(name).get)
    66   }
    67 
    68   def contents(): List[Entry] =
    69     (for {
    70       line <- contents_lines()
    71       entry <-
    72         line match {
    73           case Section_Entry(text) => Some(Section(text))
    74           case Doc_Entry(name, title) => Some(Doc(name, title))
    75           case _ => None
    76         }
    77     } yield entry) ::: release_notes() ::: examples()
    78 
    79 
    80   /* view */
    81 
    82   def view(name: String)
    83   {
    84     val doc = name + ".pdf"
    85     dirs().find(dir => (dir + Path.basic(doc)).is_file) match {
    86       case Some(dir) =>
    87         Isabelle_System.bash_env(dir.file, null,
    88           "\"$ISABELLE_TOOL\" display " + quote(doc) + " >/dev/null 2>/dev/null &")
    89       case None => error("Missing Isabelle documentation file: " + quote(doc))
    90     }
    91   }
    92 
    93 
    94   /* command line entry point */
    95 
    96   def main(args: Array[String])
    97   {
    98     Command_Line.tool {
    99       if (args.isEmpty) Console.println(cat_lines(contents_lines()))
   100       else args.foreach(view)
   101       0
   102     }
   103   }
   104 }
   105