src/Pure/Tools/doc.scala
author wenzelm
Mon, 21 Feb 2022 21:15:05 +0100
changeset 75120 488c7e8923b2
parent 75118 6fd8e482c9ce
child 75393 87ebf5a50283
permissions -rw-r--r--
clarified pdf path;
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
75115
c212435866d6 clarified signature: more explicit section structure;
wenzelm
parents: 75114
diff changeset
    10
import scala.collection.mutable
c212435866d6 clarified signature: more explicit section structure;
wenzelm
parents: 75114
diff changeset
    11
c212435866d6 clarified signature: more explicit section structure;
wenzelm
parents: 75114
diff changeset
    12
52427
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] =
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
    18
    Path.split(Isabelle_System.getenv("ISABELLE_DOCS"))
52740
bceec99254b0 documentation is always in PDF;
wenzelm
parents: 52542
diff changeset
    19
52427
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    20
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    21
  /* contents */
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    22
56422
7490555d7dff clarified Doc entry: more explicit path;
wenzelm
parents: 56276
diff changeset
    23
  private def contents_lines(): List[(Path, String)] =
52444
2cfe6656d6d6 slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents: 52429
diff changeset
    24
    for {
2cfe6656d6d6 slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents: 52429
diff changeset
    25
      dir <- dirs()
2cfe6656d6d6 slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents: 52429
diff changeset
    26
      catalog = dir + Path.basic("Contents")
2cfe6656d6d6 slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents: 52429
diff changeset
    27
      if catalog.is_file
73276
wenzelm
parents: 72770
diff changeset
    28
      line <- Library.trim_split_lines(File.read(catalog))
56422
7490555d7dff clarified Doc entry: more explicit path;
wenzelm
parents: 56276
diff changeset
    29
    } yield (dir, line)
52444
2cfe6656d6d6 slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents: 52429
diff changeset
    30
75118
6fd8e482c9ce clarified signature;
wenzelm
parents: 75116
diff changeset
    31
  object Contents
6fd8e482c9ce clarified signature;
wenzelm
parents: 75116
diff changeset
    32
  {
6fd8e482c9ce clarified signature;
wenzelm
parents: 75116
diff changeset
    33
    def apply(sections: List[Section]): Contents = new Contents(sections)
6fd8e482c9ce clarified signature;
wenzelm
parents: 75116
diff changeset
    34
6fd8e482c9ce clarified signature;
wenzelm
parents: 75116
diff changeset
    35
    def section(title: String, important: Boolean, entries: List[Entry]): Contents =
6fd8e482c9ce clarified signature;
wenzelm
parents: 75116
diff changeset
    36
      apply(List(Section(title, important, entries)))
6fd8e482c9ce clarified signature;
wenzelm
parents: 75116
diff changeset
    37
  }
6fd8e482c9ce clarified signature;
wenzelm
parents: 75116
diff changeset
    38
  class Contents private(val sections: List[Section])
6fd8e482c9ce clarified signature;
wenzelm
parents: 75116
diff changeset
    39
  {
6fd8e482c9ce clarified signature;
wenzelm
parents: 75116
diff changeset
    40
    def ++ (other: Contents): Contents = new Contents(sections ::: other.sections)
6fd8e482c9ce clarified signature;
wenzelm
parents: 75116
diff changeset
    41
    def entries: List[Entry] = sections.flatMap(_.entries)
6fd8e482c9ce clarified signature;
wenzelm
parents: 75116
diff changeset
    42
    def docs: List[Doc] = entries.collect({ case doc: Doc => doc })
6fd8e482c9ce clarified signature;
wenzelm
parents: 75116
diff changeset
    43
  }
6fd8e482c9ce clarified signature;
wenzelm
parents: 75116
diff changeset
    44
75115
c212435866d6 clarified signature: more explicit section structure;
wenzelm
parents: 75114
diff changeset
    45
  case class Section(title: String, important: Boolean, entries: List[Entry])
52427
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    46
  sealed abstract class Entry
75118
6fd8e482c9ce clarified signature;
wenzelm
parents: 75116
diff changeset
    47
  {
6fd8e482c9ce clarified signature;
wenzelm
parents: 75116
diff changeset
    48
    def name: String
6fd8e482c9ce clarified signature;
wenzelm
parents: 75116
diff changeset
    49
    def path: Path
6fd8e482c9ce clarified signature;
wenzelm
parents: 75116
diff changeset
    50
  }
56422
7490555d7dff clarified Doc entry: more explicit path;
wenzelm
parents: 56276
diff changeset
    51
  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
    52
  case class Text_File(name: String, path: Path) extends Entry
52427
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    53
69283
39044da8bb5a clarified display name;
wenzelm
parents: 67604
diff changeset
    54
  def text_file(path: Path): Option[Text_File] =
39044da8bb5a clarified display name;
wenzelm
parents: 67604
diff changeset
    55
    if (path.is_file) {
71397
028edb1e5b99 clarified file names;
wenzelm
parents: 69409
diff changeset
    56
      val a = path.implode
028edb1e5b99 clarified file names;
wenzelm
parents: 69409
diff changeset
    57
      val b = Library.try_unprefix("$ISABELLE_HOME/", a).getOrElse(a)
028edb1e5b99 clarified file names;
wenzelm
parents: 69409
diff changeset
    58
      Some(Text_File(b, path))
69283
39044da8bb5a clarified display name;
wenzelm
parents: 67604
diff changeset
    59
    }
53777
06a6216f733e immediate access to some elementary examples;
wenzelm
parents: 52740
diff changeset
    60
    else None
06a6216f733e immediate access to some elementary examples;
wenzelm
parents: 52740
diff changeset
    61
75118
6fd8e482c9ce clarified signature;
wenzelm
parents: 75116
diff changeset
    62
  def release_notes(): Contents =
6fd8e482c9ce clarified signature;
wenzelm
parents: 75116
diff changeset
    63
    Contents.section("Release Notes", true,
75115
c212435866d6 clarified signature: more explicit section structure;
wenzelm
parents: 75114
diff changeset
    64
      Path.split(Isabelle_System.getenv_strict("ISABELLE_DOCS_RELEASE_NOTES")).flatMap(text_file))
52427
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
    65
75118
6fd8e482c9ce clarified signature;
wenzelm
parents: 75116
diff changeset
    66
  def examples(): Contents =
6fd8e482c9ce clarified signature;
wenzelm
parents: 75116
diff changeset
    67
    Contents.section("Examples", true,
56424
7032378cc097 proper settings instead of hard-wired information;
wenzelm
parents: 56423
diff changeset
    68
      Path.split(Isabelle_System.getenv_strict("ISABELLE_DOCS_EXAMPLES")).map(file =>
7032378cc097 proper settings instead of hard-wired information;
wenzelm
parents: 56423
diff changeset
    69
        text_file(file) match {
7032378cc097 proper settings instead of hard-wired information;
wenzelm
parents: 56423
diff changeset
    70
          case Some(entry) => entry
56425
d12653fbd5b1 tuned error;
wenzelm
parents: 56424
diff changeset
    71
          case None => error("Bad entry in ISABELLE_DOCS_EXAMPLES: " + file)
75115
c212435866d6 clarified signature: more explicit section structure;
wenzelm
parents: 75114
diff changeset
    72
        }))
c212435866d6 clarified signature: more explicit section structure;
wenzelm
parents: 75114
diff changeset
    73
75118
6fd8e482c9ce clarified signature;
wenzelm
parents: 75116
diff changeset
    74
  def main_contents(): Contents =
75115
c212435866d6 clarified signature: more explicit section structure;
wenzelm
parents: 75114
diff changeset
    75
  {
c212435866d6 clarified signature: more explicit section structure;
wenzelm
parents: 75114
diff changeset
    76
    val result = new mutable.ListBuffer[Section]
c212435866d6 clarified signature: more explicit section structure;
wenzelm
parents: 75114
diff changeset
    77
    val entries = new mutable.ListBuffer[Entry]
c212435866d6 clarified signature: more explicit section structure;
wenzelm
parents: 75114
diff changeset
    78
    var section: Option[Section] = None
c212435866d6 clarified signature: more explicit section structure;
wenzelm
parents: 75114
diff changeset
    79
c212435866d6 clarified signature: more explicit section structure;
wenzelm
parents: 75114
diff changeset
    80
    def flush(): Unit =
c212435866d6 clarified signature: more explicit section structure;
wenzelm
parents: 75114
diff changeset
    81
      if (section.nonEmpty) {
c212435866d6 clarified signature: more explicit section structure;
wenzelm
parents: 75114
diff changeset
    82
        result += section.get.copy(entries = entries.toList)
c212435866d6 clarified signature: more explicit section structure;
wenzelm
parents: 75114
diff changeset
    83
        entries.clear()
c212435866d6 clarified signature: more explicit section structure;
wenzelm
parents: 75114
diff changeset
    84
        section = None
c212435866d6 clarified signature: more explicit section structure;
wenzelm
parents: 75114
diff changeset
    85
      }
c212435866d6 clarified signature: more explicit section structure;
wenzelm
parents: 75114
diff changeset
    86
c212435866d6 clarified signature: more explicit section structure;
wenzelm
parents: 75114
diff changeset
    87
    def begin(s: Section): Unit =
c212435866d6 clarified signature: more explicit section structure;
wenzelm
parents: 75114
diff changeset
    88
    {
c212435866d6 clarified signature: more explicit section structure;
wenzelm
parents: 75114
diff changeset
    89
      flush()
c212435866d6 clarified signature: more explicit section structure;
wenzelm
parents: 75114
diff changeset
    90
      section = Some(s)
c212435866d6 clarified signature: more explicit section structure;
wenzelm
parents: 75114
diff changeset
    91
    }
c212435866d6 clarified signature: more explicit section structure;
wenzelm
parents: 75114
diff changeset
    92
c212435866d6 clarified signature: more explicit section structure;
wenzelm
parents: 75114
diff changeset
    93
    val Section_ = """^(\S.*)\s*$""".r
c212435866d6 clarified signature: more explicit section structure;
wenzelm
parents: 75114
diff changeset
    94
    val Doc_ = """^\s+(\S+)\s+(.+)\s*$""".r
53777
06a6216f733e immediate access to some elementary examples;
wenzelm
parents: 52740
diff changeset
    95
75115
c212435866d6 clarified signature: more explicit section structure;
wenzelm
parents: 75114
diff changeset
    96
    for ((dir, line) <- contents_lines()) {
c212435866d6 clarified signature: more explicit section structure;
wenzelm
parents: 75114
diff changeset
    97
      line match {
c212435866d6 clarified signature: more explicit section structure;
wenzelm
parents: 75114
diff changeset
    98
        case Section_(text) =>
c212435866d6 clarified signature: more explicit section structure;
wenzelm
parents: 75114
diff changeset
    99
          Library.try_unsuffix("!", text) match {
c212435866d6 clarified signature: more explicit section structure;
wenzelm
parents: 75114
diff changeset
   100
            case None => begin(Section(text, false, Nil))
c212435866d6 clarified signature: more explicit section structure;
wenzelm
parents: 75114
diff changeset
   101
            case Some(txt) => begin(Section(txt, true, Nil))
c212435866d6 clarified signature: more explicit section structure;
wenzelm
parents: 75114
diff changeset
   102
          }
c212435866d6 clarified signature: more explicit section structure;
wenzelm
parents: 75114
diff changeset
   103
        case Doc_(name, title) =>
75120
488c7e8923b2 clarified pdf path;
wenzelm
parents: 75118
diff changeset
   104
          entries += Doc(name, title, dir + Path.basic(name).pdf)
75115
c212435866d6 clarified signature: more explicit section structure;
wenzelm
parents: 75114
diff changeset
   105
        case _ =>
c212435866d6 clarified signature: more explicit section structure;
wenzelm
parents: 75114
diff changeset
   106
      }
c212435866d6 clarified signature: more explicit section structure;
wenzelm
parents: 75114
diff changeset
   107
    }
75114
1fd78367c96f clarified signature;
wenzelm
parents: 73565
diff changeset
   108
75115
c212435866d6 clarified signature: more explicit section structure;
wenzelm
parents: 75114
diff changeset
   109
    flush()
75118
6fd8e482c9ce clarified signature;
wenzelm
parents: 75116
diff changeset
   110
    Contents(result.toList)
75115
c212435866d6 clarified signature: more explicit section structure;
wenzelm
parents: 75114
diff changeset
   111
  }
c212435866d6 clarified signature: more explicit section structure;
wenzelm
parents: 75114
diff changeset
   112
75118
6fd8e482c9ce clarified signature;
wenzelm
parents: 75116
diff changeset
   113
  def contents(): Contents =
61157
13f4056c42d7 clarified order;
wenzelm
parents: 56831
diff changeset
   114
  {
75118
6fd8e482c9ce clarified signature;
wenzelm
parents: 75116
diff changeset
   115
    examples() ++ release_notes() ++ main_contents()
61157
13f4056c42d7 clarified order;
wenzelm
parents: 56831
diff changeset
   116
  }
52427
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
   117
73565
1aa92bc4d356 clarified signature for Scala functions;
wenzelm
parents: 73340
diff changeset
   118
  object Doc_Names extends Scala.Fun_String("doc_names")
72760
042180540068 clarified protocol: Doc.check at run-time via Scala function;
wenzelm
parents: 71601
diff changeset
   119
  {
042180540068 clarified protocol: Doc.check at run-time via Scala function;
wenzelm
parents: 71601
diff changeset
   120
    val here = Scala_Project.here
042180540068 clarified protocol: Doc.check at run-time via Scala function;
wenzelm
parents: 71601
diff changeset
   121
    def apply(arg: String): String =
042180540068 clarified protocol: Doc.check at run-time via Scala function;
wenzelm
parents: 71601
diff changeset
   122
      if (arg.nonEmpty) error("Bad argument: " + quote(arg))
75118
6fd8e482c9ce clarified signature;
wenzelm
parents: 75116
diff changeset
   123
      else cat_lines((for (doc <- contents().docs) yield doc.name).sorted)
72760
042180540068 clarified protocol: Doc.check at run-time via Scala function;
wenzelm
parents: 71601
diff changeset
   124
  }
67471
bddfa23a4ea9 formal treatment of documentation names;
wenzelm
parents: 67178
diff changeset
   125
52427
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
   126
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
   127
  /* view */
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
   128
73340
0ffcad1f6130 tuned --- fewer warnings;
wenzelm
parents: 73276
diff changeset
   129
  def view(path: Path): Unit =
52427
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
   130
  {
75120
488c7e8923b2 clarified pdf path;
wenzelm
parents: 75118
diff changeset
   131
    if (!path.is_file) error("Bad Isabelle documentation file: " + path)
488c7e8923b2 clarified pdf path;
wenzelm
parents: 75118
diff changeset
   132
    else if (path.is_pdf) Isabelle_System.pdf_viewer(path)
488c7e8923b2 clarified pdf path;
wenzelm
parents: 75118
diff changeset
   133
    else Output.writeln(Library.trim_line(File.read(path)), stdout = true)
52427
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
   134
  }
52444
2cfe6656d6d6 slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents: 52429
diff changeset
   135
2cfe6656d6d6 slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents: 52429
diff changeset
   136
62831
5560905a32ae prefer internal tool;
wenzelm
parents: 62454
diff changeset
   137
  /* Isabelle tool wrapper */
52444
2cfe6656d6d6 slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents: 52429
diff changeset
   138
75116
001b74439ad4 tuned message;
wenzelm
parents: 75115
diff changeset
   139
  val isabelle_tool = Isabelle_Tool("doc", "view Isabelle PDF documentation",
72763
3cc73d00553c added document antiquotation @{tool};
wenzelm
parents: 72760
diff changeset
   140
    Scala_Project.here, args =>
52444
2cfe6656d6d6 slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents: 52429
diff changeset
   141
  {
62831
5560905a32ae prefer internal tool;
wenzelm
parents: 62454
diff changeset
   142
    val getopts = Getopts("""
62438
42e13a4f52f5 moved getopts to Scala;
wenzelm
parents: 61157
diff changeset
   143
Usage: isabelle doc [DOC ...]
42e13a4f52f5 moved getopts to Scala;
wenzelm
parents: 61157
diff changeset
   144
75116
001b74439ad4 tuned message;
wenzelm
parents: 75115
diff changeset
   145
  View Isabelle PDF documentation.
62438
42e13a4f52f5 moved getopts to Scala;
wenzelm
parents: 61157
diff changeset
   146
""")
62831
5560905a32ae prefer internal tool;
wenzelm
parents: 62454
diff changeset
   147
    val docs = getopts(args)
62438
42e13a4f52f5 moved getopts to Scala;
wenzelm
parents: 61157
diff changeset
   148
67178
70576478bda9 avoid println with its extra CR on Windows;
wenzelm
parents: 62831
diff changeset
   149
    if (docs.isEmpty) Output.writeln(cat_lines(contents_lines().map(_._2)), stdout = true)
62831
5560905a32ae prefer internal tool;
wenzelm
parents: 62454
diff changeset
   150
    else {
75115
c212435866d6 clarified signature: more explicit section structure;
wenzelm
parents: 75114
diff changeset
   151
      docs.foreach(name =>
75118
6fd8e482c9ce clarified signature;
wenzelm
parents: 75116
diff changeset
   152
        contents().docs.find(_.name == name) match {
75115
c212435866d6 clarified signature: more explicit section structure;
wenzelm
parents: 75114
diff changeset
   153
          case Some(doc) => view(doc.path)
c212435866d6 clarified signature: more explicit section structure;
wenzelm
parents: 75114
diff changeset
   154
          case None => error("No Isabelle documentation entry: " + quote(name))
62831
5560905a32ae prefer internal tool;
wenzelm
parents: 62454
diff changeset
   155
        }
5560905a32ae prefer internal tool;
wenzelm
parents: 62454
diff changeset
   156
      )
52444
2cfe6656d6d6 slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents: 52429
diff changeset
   157
    }
62831
5560905a32ae prefer internal tool;
wenzelm
parents: 62454
diff changeset
   158
  })
52427
9d1cc9a22177 Scala version of "isabelle doc";
wenzelm
parents:
diff changeset
   159
}