src/Pure/Tools/doc.scala
author wenzelm
Sat, 05 Apr 2014 18:14:54 +0200
changeset 56422 7490555d7dff
parent 56276 9e2d5e3debd3
child 56423 c2f52824dbb2
permissions -rw-r--r--
clarified Doc entry: more explicit path; allow plain files as Doc; refer to official jEdit documentation;
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))
52740
bceec99254b0 documentation is always in PDF;
wenzelm
parents: 52542
diff changeset
    21
52427
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
56422
7490555d7dff clarified Doc entry: more explicit path;
wenzelm
parents: 56276
diff changeset
    25
  private def contents_lines(): List[(Path, String)] =
52444
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)))
56422
7490555d7dff clarified Doc entry: more explicit path;
wenzelm
parents: 56276
diff changeset
    31
    } yield (dir, line)
52444
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
56422
7490555d7dff clarified Doc entry: more explicit path;
wenzelm
parents: 56276
diff changeset
    35
  case class Doc(name: String, title: String, path: Path) extends Entry
52542
19d674acb764 more release notes according to availability in proper release vs. repository clone;
wenzelm
parents: 52541
diff changeset
    36
  case class Text_File(name: String, path: Path) extends Entry
52427
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    37
53777
06a6216f733e immediate access to some elementary examples;
wenzelm
parents: 52740
diff changeset
    38
  def text_file(name: String): Option[Text_File] =
06a6216f733e immediate access to some elementary examples;
wenzelm
parents: 52740
diff changeset
    39
  {
06a6216f733e immediate access to some elementary examples;
wenzelm
parents: 52740
diff changeset
    40
    val path = Path.variable("ISABELLE_HOME") + Path.explode(name)
06a6216f733e immediate access to some elementary examples;
wenzelm
parents: 52740
diff changeset
    41
    if (path.is_file) Some(Text_File(name, path))
06a6216f733e immediate access to some elementary examples;
wenzelm
parents: 52740
diff changeset
    42
    else None
06a6216f733e immediate access to some elementary examples;
wenzelm
parents: 52740
diff changeset
    43
  }
06a6216f733e immediate access to some elementary examples;
wenzelm
parents: 52740
diff changeset
    44
52444
2cfe6656d6d6 slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents: 52429
diff changeset
    45
  private val Section_Entry = new Regex("""^(\S.*)\s*$""")
2cfe6656d6d6 slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents: 52429
diff changeset
    46
  private val Doc_Entry = new Regex("""^\s+(\S+)\s+(.+)\s*$""")
52427
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    47
52542
19d674acb764 more release notes according to availability in proper release vs. repository clone;
wenzelm
parents: 52541
diff changeset
    48
  private def release_notes(): List[Entry] =
19d674acb764 more release notes according to availability in proper release vs. repository clone;
wenzelm
parents: 52541
diff changeset
    49
  {
19d674acb764 more release notes according to availability in proper release vs. repository clone;
wenzelm
parents: 52541
diff changeset
    50
    val names =
19d674acb764 more release notes according to availability in proper release vs. repository clone;
wenzelm
parents: 52541
diff changeset
    51
      List("ANNOUNCE", "README", "NEWS", "COPYRIGHT", "CONTRIBUTORS",
19d674acb764 more release notes according to availability in proper release vs. repository clone;
wenzelm
parents: 52541
diff changeset
    52
        "contrib/README", "README_REPOSITORY")
19d674acb764 more release notes according to availability in proper release vs. repository clone;
wenzelm
parents: 52541
diff changeset
    53
    Section("Release notes") ::
19d674acb764 more release notes according to availability in proper release vs. repository clone;
wenzelm
parents: 52541
diff changeset
    54
      (for (name <- names; entry <- text_file(name)) yield entry)
19d674acb764 more release notes according to availability in proper release vs. repository clone;
wenzelm
parents: 52541
diff changeset
    55
  }
52541
97c950217d7f quick access to release notes (imitating website/documentation.html);
wenzelm
parents: 52448
diff changeset
    56
53777
06a6216f733e immediate access to some elementary examples;
wenzelm
parents: 52740
diff changeset
    57
  private def examples(): List[Entry] =
06a6216f733e immediate access to some elementary examples;
wenzelm
parents: 52740
diff changeset
    58
  {
06a6216f733e immediate access to some elementary examples;
wenzelm
parents: 52740
diff changeset
    59
    val names =
06a6216f733e immediate access to some elementary examples;
wenzelm
parents: 52740
diff changeset
    60
      List(
06a6216f733e immediate access to some elementary examples;
wenzelm
parents: 52740
diff changeset
    61
        "src/HOL/ex/Seq.thy",
53935
59c6dbdf0a38 added Isabelle/ML example;
wenzelm
parents: 53777
diff changeset
    62
        "src/HOL/ex/ML.thy",
53777
06a6216f733e immediate access to some elementary examples;
wenzelm
parents: 52740
diff changeset
    63
        "src/HOL/Unix/Unix.thy",
56276
9e2d5e3debd3 some SML examples;
wenzelm
parents: 54690
diff changeset
    64
        "src/HOL/Isar_Examples/Drinker.thy",
9e2d5e3debd3 some SML examples;
wenzelm
parents: 54690
diff changeset
    65
        "src/Tools/SML/Examples.thy")
53777
06a6216f733e immediate access to some elementary examples;
wenzelm
parents: 52740
diff changeset
    66
    Section("Examples") :: names.map(name => text_file(name).get)
06a6216f733e immediate access to some elementary examples;
wenzelm
parents: 52740
diff changeset
    67
  }
06a6216f733e immediate access to some elementary examples;
wenzelm
parents: 52740
diff changeset
    68
52444
2cfe6656d6d6 slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents: 52429
diff changeset
    69
  def contents(): List[Entry] =
52541
97c950217d7f quick access to release notes (imitating website/documentation.html);
wenzelm
parents: 52448
diff changeset
    70
    (for {
56422
7490555d7dff clarified Doc entry: more explicit path;
wenzelm
parents: 56276
diff changeset
    71
      (dir, line) <- contents_lines()
52427
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    72
      entry <-
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    73
        line match {
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    74
          case Section_Entry(text) => Some(Section(text))
56422
7490555d7dff clarified Doc entry: more explicit path;
wenzelm
parents: 56276
diff changeset
    75
          case Doc_Entry(name, title) => Some(Doc(name, title, dir + Path.basic(name)))
52427
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    76
          case _ => None
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    77
        }
53777
06a6216f733e immediate access to some elementary examples;
wenzelm
parents: 52740
diff changeset
    78
    } yield entry) ::: release_notes() ::: examples()
52427
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    79
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    80
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    81
  /* view */
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    82
56422
7490555d7dff clarified Doc entry: more explicit path;
wenzelm
parents: 56276
diff changeset
    83
  def view(path: Path)
52427
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    84
  {
56422
7490555d7dff clarified Doc entry: more explicit path;
wenzelm
parents: 56276
diff changeset
    85
    if (path.is_file) Console.println(File.read(path))
7490555d7dff clarified Doc entry: more explicit path;
wenzelm
parents: 56276
diff changeset
    86
    else {
7490555d7dff clarified Doc entry: more explicit path;
wenzelm
parents: 56276
diff changeset
    87
      val pdf = path.ext("pdf")
7490555d7dff clarified Doc entry: more explicit path;
wenzelm
parents: 56276
diff changeset
    88
      if (pdf.is_file) Isabelle_System.pdf_viewer(pdf)
7490555d7dff clarified Doc entry: more explicit path;
wenzelm
parents: 56276
diff changeset
    89
      else error("Bad Isabelle documentation file: " + pdf)
52427
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    90
    }
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    91
  }
52444
2cfe6656d6d6 slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents: 52429
diff changeset
    92
2cfe6656d6d6 slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents: 52429
diff changeset
    93
2cfe6656d6d6 slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents: 52429
diff changeset
    94
  /* command line entry point */
2cfe6656d6d6 slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents: 52429
diff changeset
    95
2cfe6656d6d6 slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents: 52429
diff changeset
    96
  def main(args: Array[String])
2cfe6656d6d6 slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents: 52429
diff changeset
    97
  {
2cfe6656d6d6 slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents: 52429
diff changeset
    98
    Command_Line.tool {
56422
7490555d7dff clarified Doc entry: more explicit path;
wenzelm
parents: 56276
diff changeset
    99
      if (args.isEmpty) Console.println(cat_lines(contents_lines().map(_._2)))
7490555d7dff clarified Doc entry: more explicit path;
wenzelm
parents: 56276
diff changeset
   100
      else {
7490555d7dff clarified Doc entry: more explicit path;
wenzelm
parents: 56276
diff changeset
   101
        val entries = contents()
7490555d7dff clarified Doc entry: more explicit path;
wenzelm
parents: 56276
diff changeset
   102
        args.foreach(arg =>
7490555d7dff clarified Doc entry: more explicit path;
wenzelm
parents: 56276
diff changeset
   103
          entries.collectFirst { case Doc(name, _, path) if arg == name => path } match {
7490555d7dff clarified Doc entry: more explicit path;
wenzelm
parents: 56276
diff changeset
   104
            case Some(path) => view(path)
7490555d7dff clarified Doc entry: more explicit path;
wenzelm
parents: 56276
diff changeset
   105
            case None => error("No Isabelle documentation entry: " + quote(arg))
7490555d7dff clarified Doc entry: more explicit path;
wenzelm
parents: 56276
diff changeset
   106
          }
7490555d7dff clarified Doc entry: more explicit path;
wenzelm
parents: 56276
diff changeset
   107
        )
7490555d7dff clarified Doc entry: more explicit path;
wenzelm
parents: 56276
diff changeset
   108
      }
52444
2cfe6656d6d6 slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents: 52429
diff changeset
   109
      0
2cfe6656d6d6 slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents: 52429
diff changeset
   110
    }
2cfe6656d6d6 slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents: 52429
diff changeset
   111
  }
52427
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
   112
}
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
   113