src/Pure/Tools/doc.scala
author wenzelm
Sun, 29 Nov 2020 13:59:18 +0100
changeset 72770 0c86c29767b2
parent 72763 3cc73d00553c
child 73276 54065cbf7134
permissions -rw-r--r--
tuned;
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
56424
7032378cc097 proper settings instead of hard-wired information;
wenzelm
parents: 56423
diff changeset
     4
Access to 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
object Doc
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    11
{
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    12
  /* dirs */
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    13
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    14
  def dirs(): List[Path] =
67604
02cf352cbc4c permissive Doc.dirs: some entries may be absent due to distribution bootstrap, e.g. $JEDIT_HOME/dist/doc;
wenzelm
parents: 67471
diff changeset
    15
    Path.split(Isabelle_System.getenv("ISABELLE_DOCS"))
52740
bceec99254b0 documentation is always in PDF;
wenzelm
parents: 52542
diff changeset
    16
52427
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    17
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    18
  /* contents */
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    19
56422
7490555d7dff clarified Doc entry: more explicit path;
wenzelm
parents: 56276
diff changeset
    20
  private def contents_lines(): List[(Path, String)] =
52444
2cfe6656d6d6 slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents: 52429
diff changeset
    21
    for {
2cfe6656d6d6 slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents: 52429
diff changeset
    22
      dir <- dirs()
2cfe6656d6d6 slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents: 52429
diff changeset
    23
      catalog = dir + Path.basic("Contents")
2cfe6656d6d6 slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents: 52429
diff changeset
    24
      if catalog.is_file
2cfe6656d6d6 slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents: 52429
diff changeset
    25
      line <- split_lines(Library.trim_line(File.read(catalog)))
56422
7490555d7dff clarified Doc entry: more explicit path;
wenzelm
parents: 56276
diff changeset
    26
    } yield (dir, line)
52444
2cfe6656d6d6 slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents: 52429
diff changeset
    27
52427
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    28
  sealed abstract class Entry
56423
c2f52824dbb2 explicit indication of important doc sections ("!"), which are expanded in the tree view;
wenzelm
parents: 56422
diff changeset
    29
  case class Section(text: String, important: Boolean) extends Entry
56422
7490555d7dff clarified Doc entry: more explicit path;
wenzelm
parents: 56276
diff changeset
    30
  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
    31
  case class Text_File(name: String, path: Path) extends Entry
52427
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    32
69283
39044da8bb5a clarified display name;
wenzelm
parents: 67604
diff changeset
    33
  def text_file(path: Path): Option[Text_File] =
39044da8bb5a clarified display name;
wenzelm
parents: 67604
diff changeset
    34
    if (path.is_file) {
71397
028edb1e5b99 clarified file names;
wenzelm
parents: 69409
diff changeset
    35
      val a = path.implode
028edb1e5b99 clarified file names;
wenzelm
parents: 69409
diff changeset
    36
      val b = Library.try_unprefix("$ISABELLE_HOME/", a).getOrElse(a)
028edb1e5b99 clarified file names;
wenzelm
parents: 69409
diff changeset
    37
      Some(Text_File(b, path))
69283
39044da8bb5a clarified display name;
wenzelm
parents: 67604
diff changeset
    38
    }
53777
06a6216f733e immediate access to some elementary examples;
wenzelm
parents: 52740
diff changeset
    39
    else None
06a6216f733e immediate access to some elementary examples;
wenzelm
parents: 52740
diff changeset
    40
72770
wenzelm
parents: 72763
diff changeset
    41
  private val Section_Entry = """^(\S.*)\s*$""".r
wenzelm
parents: 72763
diff changeset
    42
  private val Doc_Entry = """^\s+(\S+)\s+(.+)\s*$""".r
52427
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    43
52542
19d674acb764 more release notes according to availability in proper release vs. repository clone;
wenzelm
parents: 52541
diff changeset
    44
  private def release_notes(): List[Entry] =
69409
e7a5340128f0 clarified doc sections: add-on components may focus their own application name;
wenzelm
parents: 69283
diff changeset
    45
    Section("Release Notes", true) ::
71601
97ccf48c2f0c misc tuning based on hints by IntelliJ IDEA;
wenzelm
parents: 71397
diff changeset
    46
      Path.split(Isabelle_System.getenv_strict("ISABELLE_DOCS_RELEASE_NOTES")).flatMap(text_file)
52541
97c950217d7f quick access to release notes (imitating website/documentation.html);
wenzelm
parents: 52448
diff changeset
    47
53777
06a6216f733e immediate access to some elementary examples;
wenzelm
parents: 52740
diff changeset
    48
  private def examples(): List[Entry] =
56424
7032378cc097 proper settings instead of hard-wired information;
wenzelm
parents: 56423
diff changeset
    49
    Section("Examples", true) ::
7032378cc097 proper settings instead of hard-wired information;
wenzelm
parents: 56423
diff changeset
    50
      Path.split(Isabelle_System.getenv_strict("ISABELLE_DOCS_EXAMPLES")).map(file =>
7032378cc097 proper settings instead of hard-wired information;
wenzelm
parents: 56423
diff changeset
    51
        text_file(file) match {
7032378cc097 proper settings instead of hard-wired information;
wenzelm
parents: 56423
diff changeset
    52
          case Some(entry) => entry
56425
d12653fbd5b1 tuned error;
wenzelm
parents: 56424
diff changeset
    53
          case None => error("Bad entry in ISABELLE_DOCS_EXAMPLES: " + file)
56424
7032378cc097 proper settings instead of hard-wired information;
wenzelm
parents: 56423
diff changeset
    54
        })
53777
06a6216f733e immediate access to some elementary examples;
wenzelm
parents: 52740
diff changeset
    55
52444
2cfe6656d6d6 slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents: 52429
diff changeset
    56
  def contents(): List[Entry] =
61157
13f4056c42d7 clarified order;
wenzelm
parents: 56831
diff changeset
    57
  {
13f4056c42d7 clarified order;
wenzelm
parents: 56831
diff changeset
    58
    val main_contents =
13f4056c42d7 clarified order;
wenzelm
parents: 56831
diff changeset
    59
      for {
13f4056c42d7 clarified order;
wenzelm
parents: 56831
diff changeset
    60
        (dir, line) <- contents_lines()
13f4056c42d7 clarified order;
wenzelm
parents: 56831
diff changeset
    61
        entry <-
13f4056c42d7 clarified order;
wenzelm
parents: 56831
diff changeset
    62
          line match {
13f4056c42d7 clarified order;
wenzelm
parents: 56831
diff changeset
    63
            case Section_Entry(text) =>
13f4056c42d7 clarified order;
wenzelm
parents: 56831
diff changeset
    64
              Library.try_unsuffix("!", text) match {
13f4056c42d7 clarified order;
wenzelm
parents: 56831
diff changeset
    65
                case None => Some(Section(text, false))
13f4056c42d7 clarified order;
wenzelm
parents: 56831
diff changeset
    66
                case Some(txt) => Some(Section(txt, true))
13f4056c42d7 clarified order;
wenzelm
parents: 56831
diff changeset
    67
              }
13f4056c42d7 clarified order;
wenzelm
parents: 56831
diff changeset
    68
            case Doc_Entry(name, title) => Some(Doc(name, title, dir + Path.basic(name)))
13f4056c42d7 clarified order;
wenzelm
parents: 56831
diff changeset
    69
            case _ => None
13f4056c42d7 clarified order;
wenzelm
parents: 56831
diff changeset
    70
          }
13f4056c42d7 clarified order;
wenzelm
parents: 56831
diff changeset
    71
      } yield entry
13f4056c42d7 clarified order;
wenzelm
parents: 56831
diff changeset
    72
13f4056c42d7 clarified order;
wenzelm
parents: 56831
diff changeset
    73
    examples() ::: release_notes() ::: main_contents
13f4056c42d7 clarified order;
wenzelm
parents: 56831
diff changeset
    74
  }
52427
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    75
72760
042180540068 clarified protocol: Doc.check at run-time via Scala function;
wenzelm
parents: 71601
diff changeset
    76
  object Doc_Names extends Scala.Fun("doc_names")
042180540068 clarified protocol: Doc.check at run-time via Scala function;
wenzelm
parents: 71601
diff changeset
    77
  {
042180540068 clarified protocol: Doc.check at run-time via Scala function;
wenzelm
parents: 71601
diff changeset
    78
    val here = Scala_Project.here
042180540068 clarified protocol: Doc.check at run-time via Scala function;
wenzelm
parents: 71601
diff changeset
    79
    def apply(arg: String): String =
042180540068 clarified protocol: Doc.check at run-time via Scala function;
wenzelm
parents: 71601
diff changeset
    80
      if (arg.nonEmpty) error("Bad argument: " + quote(arg))
042180540068 clarified protocol: Doc.check at run-time via Scala function;
wenzelm
parents: 71601
diff changeset
    81
      else cat_lines((for (Doc(name, _, _) <- contents()) yield name).sorted)
042180540068 clarified protocol: Doc.check at run-time via Scala function;
wenzelm
parents: 71601
diff changeset
    82
  }
67471
bddfa23a4ea9 formal treatment of documentation names;
wenzelm
parents: 67178
diff changeset
    83
52427
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    84
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    85
  /* view */
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    86
56422
7490555d7dff clarified Doc entry: more explicit path;
wenzelm
parents: 56276
diff changeset
    87
  def view(path: Path)
52427
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    88
  {
67178
70576478bda9 avoid println with its extra CR on Windows;
wenzelm
parents: 62831
diff changeset
    89
    if (path.is_file) Output.writeln(Library.trim_line(File.read(path)), stdout = true)
56422
7490555d7dff clarified Doc entry: more explicit path;
wenzelm
parents: 56276
diff changeset
    90
    else {
7490555d7dff clarified Doc entry: more explicit path;
wenzelm
parents: 56276
diff changeset
    91
      val pdf = path.ext("pdf")
7490555d7dff clarified Doc entry: more explicit path;
wenzelm
parents: 56276
diff changeset
    92
      if (pdf.is_file) Isabelle_System.pdf_viewer(pdf)
7490555d7dff clarified Doc entry: more explicit path;
wenzelm
parents: 56276
diff changeset
    93
      else error("Bad Isabelle documentation file: " + pdf)
52427
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    94
    }
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    95
  }
52444
2cfe6656d6d6 slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents: 52429
diff changeset
    96
2cfe6656d6d6 slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents: 52429
diff changeset
    97
62831
5560905a32ae prefer internal tool;
wenzelm
parents: 62454
diff changeset
    98
  /* Isabelle tool wrapper */
52444
2cfe6656d6d6 slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents: 52429
diff changeset
    99
72763
3cc73d00553c added document antiquotation @{tool};
wenzelm
parents: 72760
diff changeset
   100
  val isabelle_tool = Isabelle_Tool("doc", "view Isabelle documentation",
3cc73d00553c added document antiquotation @{tool};
wenzelm
parents: 72760
diff changeset
   101
    Scala_Project.here, args =>
52444
2cfe6656d6d6 slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents: 52429
diff changeset
   102
  {
62831
5560905a32ae prefer internal tool;
wenzelm
parents: 62454
diff changeset
   103
    val getopts = Getopts("""
62438
42e13a4f52f5 moved getopts to Scala;
wenzelm
parents: 61157
diff changeset
   104
Usage: isabelle doc [DOC ...]
42e13a4f52f5 moved getopts to Scala;
wenzelm
parents: 61157
diff changeset
   105
42e13a4f52f5 moved getopts to Scala;
wenzelm
parents: 61157
diff changeset
   106
  View Isabelle documentation.
42e13a4f52f5 moved getopts to Scala;
wenzelm
parents: 61157
diff changeset
   107
""")
62831
5560905a32ae prefer internal tool;
wenzelm
parents: 62454
diff changeset
   108
    val docs = getopts(args)
62438
42e13a4f52f5 moved getopts to Scala;
wenzelm
parents: 61157
diff changeset
   109
62831
5560905a32ae prefer internal tool;
wenzelm
parents: 62454
diff changeset
   110
    val entries = contents()
67178
70576478bda9 avoid println with its extra CR on Windows;
wenzelm
parents: 62831
diff changeset
   111
    if (docs.isEmpty) Output.writeln(cat_lines(contents_lines().map(_._2)), stdout = true)
62831
5560905a32ae prefer internal tool;
wenzelm
parents: 62454
diff changeset
   112
    else {
5560905a32ae prefer internal tool;
wenzelm
parents: 62454
diff changeset
   113
      docs.foreach(doc =>
5560905a32ae prefer internal tool;
wenzelm
parents: 62454
diff changeset
   114
        entries.collectFirst { case Doc(name, _, path) if doc == name => path } match {
5560905a32ae prefer internal tool;
wenzelm
parents: 62454
diff changeset
   115
          case Some(path) => view(path)
5560905a32ae prefer internal tool;
wenzelm
parents: 62454
diff changeset
   116
          case None => error("No Isabelle documentation entry: " + quote(doc))
5560905a32ae prefer internal tool;
wenzelm
parents: 62454
diff changeset
   117
        }
5560905a32ae prefer internal tool;
wenzelm
parents: 62454
diff changeset
   118
      )
52444
2cfe6656d6d6 slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents: 52429
diff changeset
   119
    }
62831
5560905a32ae prefer internal tool;
wenzelm
parents: 62454
diff changeset
   120
  })
52427
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
   121
}