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;
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@53777
    64
        "src/HOL/Isar_Examples/Drinker.thy")
wenzelm@53777
    65
    Section("Examples") :: names.map(name => text_file(name).get)
wenzelm@53777
    66
  }
wenzelm@53777
    67
wenzelm@52444
    68
  def contents(): List[Entry] =
wenzelm@52541
    69
    (for {
wenzelm@52444
    70
      line <- contents_lines()
wenzelm@52427
    71
      entry <-
wenzelm@52427
    72
        line match {
wenzelm@52427
    73
          case Section_Entry(text) => Some(Section(text))
wenzelm@52427
    74
          case Doc_Entry(name, title) => Some(Doc(name, title))
wenzelm@52427
    75
          case _ => None
wenzelm@52427
    76
        }
wenzelm@53777
    77
    } yield entry) ::: release_notes() ::: examples()
wenzelm@52427
    78
wenzelm@52427
    79
wenzelm@52427
    80
  /* view */
wenzelm@52427
    81
wenzelm@52427
    82
  def view(name: String)
wenzelm@52427
    83
  {
wenzelm@52740
    84
    val doc = name + ".pdf"
wenzelm@52448
    85
    dirs().find(dir => (dir + Path.basic(doc)).is_file) match {
wenzelm@52448
    86
      case Some(dir) =>
wenzelm@52429
    87
        Isabelle_System.bash_env(dir.file, null,
wenzelm@52429
    88
          "\"$ISABELLE_TOOL\" display " + quote(doc) + " >/dev/null 2>/dev/null &")
wenzelm@52448
    89
      case None => error("Missing Isabelle documentation file: " + quote(doc))
wenzelm@52427
    90
    }
wenzelm@52427
    91
  }
wenzelm@52444
    92
wenzelm@52444
    93
wenzelm@52444
    94
  /* command line entry point */
wenzelm@52444
    95
wenzelm@52444
    96
  def main(args: Array[String])
wenzelm@52444
    97
  {
wenzelm@52444
    98
    Command_Line.tool {
wenzelm@52444
    99
      if (args.isEmpty) Console.println(cat_lines(contents_lines()))
wenzelm@52444
   100
      else args.foreach(view)
wenzelm@52444
   101
      0
wenzelm@52444
   102
    }
wenzelm@52444
   103
  }
wenzelm@52427
   104
}
wenzelm@52427
   105