src/Pure/Tools/doc.scala
author wenzelm
Tue Jun 25 19:25:40 2013 +0200 (2013-06-25)
changeset 52448 082a1c8c2c89
parent 52444 2cfe6656d6d6
child 52541 97c950217d7f
permissions -rw-r--r--
clarified ISABELLE_DOC_FORMAT -- eliminated slightly odd fall-back;
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@52427
    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@52427
    36
wenzelm@52444
    37
  private val Section_Entry = new Regex("""^(\S.*)\s*$""")
wenzelm@52444
    38
  private val Doc_Entry = new Regex("""^\s+(\S+)\s+(.+)\s*$""")
wenzelm@52427
    39
wenzelm@52444
    40
  def contents(): List[Entry] =
wenzelm@52427
    41
    for {
wenzelm@52444
    42
      line <- contents_lines()
wenzelm@52427
    43
      entry <-
wenzelm@52427
    44
        line match {
wenzelm@52427
    45
          case Section_Entry(text) => Some(Section(text))
wenzelm@52427
    46
          case Doc_Entry(name, title) => Some(Doc(name, title))
wenzelm@52427
    47
          case _ => None
wenzelm@52427
    48
        }
wenzelm@52427
    49
    } yield entry
wenzelm@52427
    50
wenzelm@52427
    51
wenzelm@52427
    52
  /* view */
wenzelm@52427
    53
wenzelm@52427
    54
  def view(name: String)
wenzelm@52427
    55
  {
wenzelm@52448
    56
    val doc = name + "." + Isabelle_System.getenv_strict("ISABELLE_DOC_FORMAT")
wenzelm@52448
    57
    dirs().find(dir => (dir + Path.basic(doc)).is_file) match {
wenzelm@52448
    58
      case Some(dir) =>
wenzelm@52429
    59
        Isabelle_System.bash_env(dir.file, null,
wenzelm@52429
    60
          "\"$ISABELLE_TOOL\" display " + quote(doc) + " >/dev/null 2>/dev/null &")
wenzelm@52448
    61
      case None => error("Missing Isabelle documentation file: " + quote(doc))
wenzelm@52427
    62
    }
wenzelm@52427
    63
  }
wenzelm@52444
    64
wenzelm@52444
    65
wenzelm@52444
    66
  /* command line entry point */
wenzelm@52444
    67
wenzelm@52444
    68
  def main(args: Array[String])
wenzelm@52444
    69
  {
wenzelm@52444
    70
    Command_Line.tool {
wenzelm@52444
    71
      if (args.isEmpty) Console.println(cat_lines(contents_lines()))
wenzelm@52444
    72
      else args.foreach(view)
wenzelm@52444
    73
      0
wenzelm@52444
    74
    }
wenzelm@52444
    75
  }
wenzelm@52427
    76
}
wenzelm@52427
    77