src/Pure/Tools/doc.scala
author wenzelm
Sat Dec 07 13:10:56 2013 +0100 (2013-12-07)
changeset 54690 cd88b44623bf
parent 53935 59c6dbdf0a38
child 56276 9e2d5e3debd3
permissions -rw-r--r--
more direct Isabelle_System.pdf_viewer;
generic Isabelle_System.open, e.g. for URLs;
     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 = Path.basic(name + ".pdf")
    85     dirs().find(dir => (dir + doc).is_file) match {
    86       case Some(dir) => Isabelle_System.pdf_viewer(dir + doc)
    87       case None => error("Missing Isabelle documentation file: " + doc)
    88     }
    89   }
    90 
    91 
    92   /* command line entry point */
    93 
    94   def main(args: Array[String])
    95   {
    96     Command_Line.tool {
    97       if (args.isEmpty) Console.println(cat_lines(contents_lines()))
    98       else args.foreach(view)
    99       0
   100     }
   101   }
   102 }
   103