src/Pure/Tools/doc.scala
author wenzelm
Tue Jun 25 12:17:19 2013 +0200 (2013-06-25)
changeset 52444 2cfe6656d6d6
parent 52429 src/Pure/System/doc.scala@921f22c8890e
child 52448 082a1c8c2c89
permissions -rw-r--r--
slightly improved "isabelle doc" based on Isabelle/Scala;
updated documentation of "isabelle display";
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@52427
    56
    val formats = List(Isabelle_System.getenv_strict("ISABELLE_DOC_FORMAT"), "dvi")
wenzelm@52427
    57
    val docs =
wenzelm@52427
    58
      for {
wenzelm@52427
    59
        dir <- dirs()
wenzelm@52427
    60
        fmt <- formats
wenzelm@52427
    61
        doc = name + "." + fmt
wenzelm@52427
    62
        if (dir + Path.basic(doc)).is_file
wenzelm@52427
    63
      } yield (dir, doc)
wenzelm@52427
    64
    docs match {
wenzelm@52427
    65
      case (dir, doc) :: _ =>
wenzelm@52429
    66
        Isabelle_System.bash_env(dir.file, null,
wenzelm@52429
    67
          "\"$ISABELLE_TOOL\" display " + quote(doc) + " >/dev/null 2>/dev/null &")
wenzelm@52427
    68
      case Nil => error("Missing Isabelle documentation: " + quote(name))
wenzelm@52427
    69
    }
wenzelm@52427
    70
  }
wenzelm@52444
    71
wenzelm@52444
    72
wenzelm@52444
    73
  /* command line entry point */
wenzelm@52444
    74
wenzelm@52444
    75
  def main(args: Array[String])
wenzelm@52444
    76
  {
wenzelm@52444
    77
    Command_Line.tool {
wenzelm@52444
    78
      if (args.isEmpty) Console.println(cat_lines(contents_lines()))
wenzelm@52444
    79
      else args.foreach(view)
wenzelm@52444
    80
      0
wenzelm@52444
    81
    }
wenzelm@52444
    82
  }
wenzelm@52427
    83
}
wenzelm@52427
    84