src/Pure/Tools/doc.scala
author wenzelm
Tue Apr 29 14:50:40 2014 +0200 (2014-04-29)
changeset 56784 776890e0cf71
parent 56631 89269bb8e7ca
child 56831 e3ccf0809d51
permissions -rw-r--r--
clarified;
wenzelm@52444
     1
/*  Title:      Pure/Tools/doc.scala
wenzelm@52427
     2
    Author:     Makarius
wenzelm@52427
     3
wenzelm@56424
     4
Access to 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@56422
    25
  private def contents_lines(): List[(Path, 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@56422
    31
    } yield (dir, line)
wenzelm@52444
    32
wenzelm@52427
    33
  sealed abstract class Entry
wenzelm@56423
    34
  case class Section(text: String, important: Boolean) extends Entry
wenzelm@56422
    35
  case class Doc(name: String, title: String, path: Path) extends Entry
wenzelm@52542
    36
  case class Text_File(name: String, path: Path) extends Entry
wenzelm@52427
    37
wenzelm@56424
    38
  def text_file(name: Path): Option[Text_File] =
wenzelm@53777
    39
  {
wenzelm@56424
    40
    val path = Path.variable("ISABELLE_HOME") + name
wenzelm@56424
    41
    if (path.is_file) Some(Text_File(name.implode, 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@56423
    49
    Section("Release notes", true) ::
wenzelm@56424
    50
      Path.split(Isabelle_System.getenv_strict("ISABELLE_DOCS_RELEASE_NOTES")).flatMap(text_file(_))
wenzelm@52541
    51
wenzelm@53777
    52
  private def examples(): List[Entry] =
wenzelm@56424
    53
    Section("Examples", true) ::
wenzelm@56424
    54
      Path.split(Isabelle_System.getenv_strict("ISABELLE_DOCS_EXAMPLES")).map(file =>
wenzelm@56424
    55
        text_file(file) match {
wenzelm@56424
    56
          case Some(entry) => entry
wenzelm@56425
    57
          case None => error("Bad entry in ISABELLE_DOCS_EXAMPLES: " + file)
wenzelm@56424
    58
        })
wenzelm@53777
    59
wenzelm@52444
    60
  def contents(): List[Entry] =
wenzelm@52541
    61
    (for {
wenzelm@56422
    62
      (dir, line) <- contents_lines()
wenzelm@52427
    63
      entry <-
wenzelm@52427
    64
        line match {
wenzelm@56423
    65
          case Section_Entry(text) =>
wenzelm@56423
    66
            Library.try_unsuffix("!", text) match {
wenzelm@56423
    67
              case None => Some(Section(text, false))
wenzelm@56423
    68
              case Some(txt) => Some(Section(txt, true))
wenzelm@56423
    69
            }
wenzelm@56422
    70
          case Doc_Entry(name, title) => Some(Doc(name, title, dir + Path.basic(name)))
wenzelm@52427
    71
          case _ => None
wenzelm@52427
    72
        }
wenzelm@53777
    73
    } yield entry) ::: release_notes() ::: examples()
wenzelm@52427
    74
wenzelm@52427
    75
wenzelm@52427
    76
  /* view */
wenzelm@52427
    77
wenzelm@56422
    78
  def view(path: Path)
wenzelm@52427
    79
  {
wenzelm@56784
    80
    if (path.is_file) System.out.println(Library.trim_line(File.read(path)))
wenzelm@56422
    81
    else {
wenzelm@56422
    82
      val pdf = path.ext("pdf")
wenzelm@56422
    83
      if (pdf.is_file) Isabelle_System.pdf_viewer(pdf)
wenzelm@56422
    84
      else error("Bad Isabelle documentation file: " + pdf)
wenzelm@52427
    85
    }
wenzelm@52427
    86
  }
wenzelm@52444
    87
wenzelm@52444
    88
wenzelm@52444
    89
  /* command line entry point */
wenzelm@52444
    90
wenzelm@52444
    91
  def main(args: Array[String])
wenzelm@52444
    92
  {
wenzelm@56631
    93
    Command_Line.tool0 {
wenzelm@56425
    94
      val entries = contents()
wenzelm@56784
    95
      if (args.isEmpty) System.out.println(cat_lines(contents_lines().map(_._2)))
wenzelm@56422
    96
      else {
wenzelm@56422
    97
        args.foreach(arg =>
wenzelm@56422
    98
          entries.collectFirst { case Doc(name, _, path) if arg == name => path } match {
wenzelm@56422
    99
            case Some(path) => view(path)
wenzelm@56422
   100
            case None => error("No Isabelle documentation entry: " + quote(arg))
wenzelm@56422
   101
          }
wenzelm@56422
   102
        )
wenzelm@56422
   103
      }
wenzelm@52444
   104
    }
wenzelm@52444
   105
  }
wenzelm@52427
   106
}
wenzelm@52427
   107