src/Pure/Tools/doc.scala
author wenzelm
Tue, 25 Jun 2013 12:17:19 +0200
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";
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
52444
2cfe6656d6d6 slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents: 52429
diff changeset
     1
/*  Title:      Pure/Tools/doc.scala
52427
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
     3
52444
2cfe6656d6d6 slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents: 52429
diff changeset
     4
View Isabelle documentation.
52427
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
     5
*/
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
     6
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
     7
package isabelle
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
     8
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
     9
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    10
import scala.util.matching.Regex
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    11
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    12
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    13
object Doc
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    14
{
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    15
  /* dirs */
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    16
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    17
  def dirs(): List[Path] =
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    18
    Path.split(Isabelle_System.getenv("ISABELLE_DOCS")).map(dir =>
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    19
      if (dir.is_dir) dir
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    20
      else error("Bad documentation directory: " + dir))
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    21
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    22
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    23
  /* contents */
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    24
52444
2cfe6656d6d6 slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents: 52429
diff changeset
    25
  private def contents_lines(): List[String] =
2cfe6656d6d6 slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents: 52429
diff changeset
    26
    for {
2cfe6656d6d6 slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents: 52429
diff changeset
    27
      dir <- dirs()
2cfe6656d6d6 slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents: 52429
diff changeset
    28
      catalog = dir + Path.basic("Contents")
2cfe6656d6d6 slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents: 52429
diff changeset
    29
      if catalog.is_file
2cfe6656d6d6 slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents: 52429
diff changeset
    30
      line <- split_lines(Library.trim_line(File.read(catalog)))
2cfe6656d6d6 slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents: 52429
diff changeset
    31
    } yield line
2cfe6656d6d6 slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents: 52429
diff changeset
    32
52427
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    33
  sealed abstract class Entry
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    34
  case class Section(text: String) extends Entry
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    35
  case class Doc(name: String, title: String) extends Entry
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    36
52444
2cfe6656d6d6 slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents: 52429
diff changeset
    37
  private val Section_Entry = new Regex("""^(\S.*)\s*$""")
2cfe6656d6d6 slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents: 52429
diff changeset
    38
  private val Doc_Entry = new Regex("""^\s+(\S+)\s+(.+)\s*$""")
52427
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    39
52444
2cfe6656d6d6 slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents: 52429
diff changeset
    40
  def contents(): List[Entry] =
52427
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    41
    for {
52444
2cfe6656d6d6 slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents: 52429
diff changeset
    42
      line <- contents_lines()
52427
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    43
      entry <-
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    44
        line match {
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    45
          case Section_Entry(text) => Some(Section(text))
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    46
          case Doc_Entry(name, title) => Some(Doc(name, title))
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    47
          case _ => None
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    48
        }
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    49
    } yield entry
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    50
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    51
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    52
  /* view */
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    53
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    54
  def view(name: String)
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    55
  {
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    56
    val formats = List(Isabelle_System.getenv_strict("ISABELLE_DOC_FORMAT"), "dvi")
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    57
    val docs =
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    58
      for {
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    59
        dir <- dirs()
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    60
        fmt <- formats
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    61
        doc = name + "." + fmt
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    62
        if (dir + Path.basic(doc)).is_file
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    63
      } yield (dir, doc)
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    64
    docs match {
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    65
      case (dir, doc) :: _ =>
52429
921f22c8890e detach process more reliably -- NB: viewer might be synchronous or asynchronous depending on settings;
wenzelm
parents: 52427
diff changeset
    66
        Isabelle_System.bash_env(dir.file, null,
921f22c8890e detach process more reliably -- NB: viewer might be synchronous or asynchronous depending on settings;
wenzelm
parents: 52427
diff changeset
    67
          "\"$ISABELLE_TOOL\" display " + quote(doc) + " >/dev/null 2>/dev/null &")
52427
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    68
      case Nil => error("Missing Isabelle documentation: " + quote(name))
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    69
    }
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    70
  }
52444
2cfe6656d6d6 slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents: 52429
diff changeset
    71
2cfe6656d6d6 slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents: 52429
diff changeset
    72
2cfe6656d6d6 slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents: 52429
diff changeset
    73
  /* command line entry point */
2cfe6656d6d6 slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents: 52429
diff changeset
    74
2cfe6656d6d6 slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents: 52429
diff changeset
    75
  def main(args: Array[String])
2cfe6656d6d6 slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents: 52429
diff changeset
    76
  {
2cfe6656d6d6 slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents: 52429
diff changeset
    77
    Command_Line.tool {
2cfe6656d6d6 slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents: 52429
diff changeset
    78
      if (args.isEmpty) Console.println(cat_lines(contents_lines()))
2cfe6656d6d6 slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents: 52429
diff changeset
    79
      else args.foreach(view)
2cfe6656d6d6 slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents: 52429
diff changeset
    80
      0
2cfe6656d6d6 slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents: 52429
diff changeset
    81
    }
2cfe6656d6d6 slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents: 52429
diff changeset
    82
  }
52427
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    83
}
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    84