src/Pure/Tools/doc.scala
author wenzelm
Tue Mar 25 14:52:35 2014 +0100 (2014-03-25)
changeset 56276 9e2d5e3debd3
parent 54690 cd88b44623bf
child 56422 7490555d7dff
permissions -rw-r--r--
some SML examples;
wenzelm@52444
     1
/*  Title:      Pure/Tools/doc.scala
wenzelm@52427
     2
    Author:     Makarius
wenzelm@52427
     3
wenzelm@52444
     4
View Isabelle documentation.
wenzelm@52427
     5
*/
wenzelm@52427
     6
wenzelm@52427
     7
package isabelle
wenzelm@52427
     8
wenzelm@52427
     9
wenzelm@52427
    10
import scala.util.matching.Regex
wenzelm@52427
    11
wenzelm@52427
    12
wenzelm@52427
    13
object Doc
wenzelm@52427
    14
{
wenzelm@52427
    15
  /* dirs */
wenzelm@52427
    16
wenzelm@52427
    17
  def dirs(): List[Path] =
wenzelm@52427
    18
    Path.split(Isabelle_System.getenv("ISABELLE_DOCS")).map(dir =>
wenzelm@52427
    19
      if (dir.is_dir) dir
wenzelm@52427
    20
      else error("Bad documentation directory: " + dir))
wenzelm@52740
    21
wenzelm@52427
    22
wenzelm@52427
    23
  /* contents */
wenzelm@52427
    24
wenzelm@52444
    25
  private def contents_lines(): List[String] =
wenzelm@52444
    26
    for {
wenzelm@52444
    27
      dir <- dirs()
wenzelm@52444
    28
      catalog = dir + Path.basic("Contents")
wenzelm@52444
    29
      if catalog.is_file
wenzelm@52444
    30
      line <- split_lines(Library.trim_line(File.read(catalog)))
wenzelm@52444
    31
    } yield line
wenzelm@52444
    32
wenzelm@52427
    33
  sealed abstract class Entry
wenzelm@52427
    34
  case class Section(text: String) extends Entry
wenzelm@52427
    35
  case class Doc(name: String, title: String) extends Entry
wenzelm@52542
    36
  case class Text_File(name: String, path: Path) extends Entry
wenzelm@52427
    37
wenzelm@53777
    38
  def text_file(name: String): Option[Text_File] =
wenzelm@53777
    39
  {
wenzelm@53777
    40
    val path = Path.variable("ISABELLE_HOME") + Path.explode(name)
wenzelm@53777
    41
    if (path.is_file) Some(Text_File(name, path))
wenzelm@53777
    42
    else None
wenzelm@53777
    43
  }
wenzelm@53777
    44
wenzelm@52444
    45
  private val Section_Entry = new Regex("""^(\S.*)\s*$""")
wenzelm@52444
    46
  private val Doc_Entry = new Regex("""^\s+(\S+)\s+(.+)\s*$""")
wenzelm@52427
    47
wenzelm@52542
    48
  private def release_notes(): List[Entry] =
wenzelm@52542
    49
  {
wenzelm@52542
    50
    val names =
wenzelm@52542
    51
      List("ANNOUNCE", "README", "NEWS", "COPYRIGHT", "CONTRIBUTORS",
wenzelm@52542
    52
        "contrib/README", "README_REPOSITORY")
wenzelm@52542
    53
    Section("Release notes") ::
wenzelm@52542
    54
      (for (name <- names; entry <- text_file(name)) yield entry)
wenzelm@52542
    55
  }
wenzelm@52541
    56
wenzelm@53777
    57
  private def examples(): List[Entry] =
wenzelm@53777
    58
  {
wenzelm@53777
    59
    val names =
wenzelm@53777
    60
      List(
wenzelm@53777
    61
        "src/HOL/ex/Seq.thy",
wenzelm@53935
    62
        "src/HOL/ex/ML.thy",
wenzelm@53777
    63
        "src/HOL/Unix/Unix.thy",
wenzelm@56276
    64
        "src/HOL/Isar_Examples/Drinker.thy",
wenzelm@56276
    65
        "src/Tools/SML/Examples.thy")
wenzelm@53777
    66
    Section("Examples") :: names.map(name => text_file(name).get)
wenzelm@53777
    67
  }
wenzelm@53777
    68
wenzelm@52444
    69
  def contents(): List[Entry] =
wenzelm@52541
    70
    (for {
wenzelm@52444
    71
      line <- contents_lines()
wenzelm@52427
    72
      entry <-
wenzelm@52427
    73
        line match {
wenzelm@52427
    74
          case Section_Entry(text) => Some(Section(text))
wenzelm@52427
    75
          case Doc_Entry(name, title) => Some(Doc(name, title))
wenzelm@52427
    76
          case _ => None
wenzelm@52427
    77
        }
wenzelm@53777
    78
    } yield entry) ::: release_notes() ::: examples()
wenzelm@52427
    79
wenzelm@52427
    80
wenzelm@52427
    81
  /* view */
wenzelm@52427
    82
wenzelm@52427
    83
  def view(name: String)
wenzelm@52427
    84
  {
wenzelm@54690
    85
    val doc = Path.basic(name + ".pdf")
wenzelm@54690
    86
    dirs().find(dir => (dir + doc).is_file) match {
wenzelm@54690
    87
      case Some(dir) => Isabelle_System.pdf_viewer(dir + doc)
wenzelm@54690
    88
      case None => error("Missing Isabelle documentation file: " + doc)
wenzelm@52427
    89
    }
wenzelm@52427
    90
  }
wenzelm@52444
    91
wenzelm@52444
    92
wenzelm@52444
    93
  /* command line entry point */
wenzelm@52444
    94
wenzelm@52444
    95
  def main(args: Array[String])
wenzelm@52444
    96
  {
wenzelm@52444
    97
    Command_Line.tool {
wenzelm@52444
    98
      if (args.isEmpty) Console.println(cat_lines(contents_lines()))
wenzelm@52444
    99
      else args.foreach(view)
wenzelm@52444
   100
      0
wenzelm@52444
   101
    }
wenzelm@52444
   102
  }
wenzelm@52427
   103
}
wenzelm@52427
   104