| author | wenzelm |
| Mon, 27 Oct 2025 15:16:32 +0100 | |
| changeset 83417 | b51e4a526897 |
| parent 83387 | 65d22b27471a |
| permissions | -rw-r--r-- |
|
52444
2cfe6656d6d6
slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents:
52429
diff
changeset
|
1 |
/* Title: Pure/Tools/doc.scala |
| 52427 | 2 |
Author: Makarius |
3 |
||
|
81353
4829e4c68d7c
tuned description: plain text documentation is also supported;
wenzelm
parents:
81350
diff
changeset
|
4 |
Access to Isabelle examples and documentation. |
| 52427 | 5 |
*/ |
6 |
||
7 |
package isabelle |
|
8 |
||
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 |
|
| 75393 | 13 |
object Doc {
|
|
81350
1818358373e2
misc tuning and clarification: Doc.Entry supports both plain files and pdf documents;
wenzelm
parents:
75394
diff
changeset
|
14 |
/* entries */ |
|
1818358373e2
misc tuning and clarification: Doc.Entry supports both plain files and pdf documents;
wenzelm
parents:
75394
diff
changeset
|
15 |
|
| 83383 | 16 |
case class Section(title: String, important: Boolean, entries: List[Entry]) {
|
17 |
def print_title: String = title + if_proper(important, "!") |
|
18 |
} |
|
|
81350
1818358373e2
misc tuning and clarification: Doc.Entry supports both plain files and pdf documents;
wenzelm
parents:
75394
diff
changeset
|
19 |
case class Entry(name: String, path: Path, title: String = "") {
|
|
1818358373e2
misc tuning and clarification: Doc.Entry supports both plain files and pdf documents;
wenzelm
parents:
75394
diff
changeset
|
20 |
def view(): Unit = Doc.view(path) |
| 83387 | 21 |
|
22 |
def print_html: String = {
|
|
23 |
val style = GUI.Style_HTML |
|
24 |
if (title.nonEmpty) style.make_bold(name) + style.make_text(": " + title)
|
|
25 |
else style.make_text(name) |
|
26 |
} |
|
27 |
||
28 |
override def toString: String = // Swing GUI label |
|
29 |
if (title.nonEmpty) GUI.Style_HTML.enclose(print_html) else name |
|
|
81350
1818358373e2
misc tuning and clarification: Doc.Entry supports both plain files and pdf documents;
wenzelm
parents:
75394
diff
changeset
|
30 |
} |
|
1818358373e2
misc tuning and clarification: Doc.Entry supports both plain files and pdf documents;
wenzelm
parents:
75394
diff
changeset
|
31 |
|
|
82720
956ecf2c07a0
more flexible ML_Settings in Isabelle/Scala, depending on system options and some default settings;
wenzelm
parents:
81657
diff
changeset
|
32 |
def plain_file(path0: Path, |
|
956ecf2c07a0
more flexible ML_Settings in Isabelle/Scala, depending on system options and some default settings;
wenzelm
parents:
81657
diff
changeset
|
33 |
env: Isabelle_System.Settings = Isabelle_System.Settings() |
|
956ecf2c07a0
more flexible ML_Settings in Isabelle/Scala, depending on system options and some default settings;
wenzelm
parents:
81657
diff
changeset
|
34 |
): Option[Entry] = {
|
|
956ecf2c07a0
more flexible ML_Settings in Isabelle/Scala, depending on system options and some default settings;
wenzelm
parents:
81657
diff
changeset
|
35 |
val path1 = path0.expand_env(env) |
|
956ecf2c07a0
more flexible ML_Settings in Isabelle/Scala, depending on system options and some default settings;
wenzelm
parents:
81657
diff
changeset
|
36 |
if (path1.is_file && !path1.is_pdf) {
|
|
956ecf2c07a0
more flexible ML_Settings in Isabelle/Scala, depending on system options and some default settings;
wenzelm
parents:
81657
diff
changeset
|
37 |
val a = path0.implode |
|
81350
1818358373e2
misc tuning and clarification: Doc.Entry supports both plain files and pdf documents;
wenzelm
parents:
75394
diff
changeset
|
38 |
val b = Library.try_unprefix("$ISABELLE_HOME/", a).getOrElse(a)
|
|
82720
956ecf2c07a0
more flexible ML_Settings in Isabelle/Scala, depending on system options and some default settings;
wenzelm
parents:
81657
diff
changeset
|
39 |
Some(Entry(b, path1)) |
|
81350
1818358373e2
misc tuning and clarification: Doc.Entry supports both plain files and pdf documents;
wenzelm
parents:
75394
diff
changeset
|
40 |
} |
|
1818358373e2
misc tuning and clarification: Doc.Entry supports both plain files and pdf documents;
wenzelm
parents:
75394
diff
changeset
|
41 |
else None |
|
82720
956ecf2c07a0
more flexible ML_Settings in Isabelle/Scala, depending on system options and some default settings;
wenzelm
parents:
81657
diff
changeset
|
42 |
} |
|
81350
1818358373e2
misc tuning and clarification: Doc.Entry supports both plain files and pdf documents;
wenzelm
parents:
75394
diff
changeset
|
43 |
|
|
1818358373e2
misc tuning and clarification: Doc.Entry supports both plain files and pdf documents;
wenzelm
parents:
75394
diff
changeset
|
44 |
|
|
1818358373e2
misc tuning and clarification: Doc.Entry supports both plain files and pdf documents;
wenzelm
parents:
75394
diff
changeset
|
45 |
/* contents */ |
| 52427 | 46 |
|
47 |
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
|
48 |
Path.split(Isabelle_System.getenv("ISABELLE_DOCS"))
|
| 52740 | 49 |
|
| 56422 | 50 |
private def contents_lines(): List[(Path, String)] = |
|
52444
2cfe6656d6d6
slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents:
52429
diff
changeset
|
51 |
for {
|
|
2cfe6656d6d6
slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents:
52429
diff
changeset
|
52 |
dir <- dirs() |
|
2cfe6656d6d6
slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents:
52429
diff
changeset
|
53 |
catalog = dir + Path.basic("Contents")
|
|
2cfe6656d6d6
slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents:
52429
diff
changeset
|
54 |
if catalog.is_file |
| 73276 | 55 |
line <- Library.trim_split_lines(File.read(catalog)) |
| 56422 | 56 |
} yield (dir, line) |
|
52444
2cfe6656d6d6
slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents:
52429
diff
changeset
|
57 |
|
| 75393 | 58 |
object Contents {
|
| 75118 | 59 |
def apply(sections: List[Section]): Contents = new Contents(sections) |
60 |
||
61 |
def section(title: String, important: Boolean, entries: List[Entry]): Contents = |
|
62 |
apply(List(Section(title, important, entries))) |
|
63 |
} |
|
| 75393 | 64 |
class Contents private(val sections: List[Section]) {
|
| 83383 | 65 |
override def toString: String = |
66 |
sections.map(_.print_title).mkString("Doc.Contents(", ", ", ")")
|
|
67 |
||
| 75118 | 68 |
def ++ (other: Contents): Contents = new Contents(sections ::: other.sections) |
|
81350
1818358373e2
misc tuning and clarification: Doc.Entry supports both plain files and pdf documents;
wenzelm
parents:
75394
diff
changeset
|
69 |
def entries(name: String => Boolean = _ => true, pdf: Boolean = false): List[Entry] = |
|
1818358373e2
misc tuning and clarification: Doc.Entry supports both plain files and pdf documents;
wenzelm
parents:
75394
diff
changeset
|
70 |
sections.flatMap(s => s.entries.filter(e => name(e.name) && (!pdf || e.path.is_pdf))) |
| 75118 | 71 |
} |
| 53777 | 72 |
|
| 75118 | 73 |
def release_notes(): Contents = |
74 |
Contents.section("Release Notes", true,
|
|
|
82720
956ecf2c07a0
more flexible ML_Settings in Isabelle/Scala, depending on system options and some default settings;
wenzelm
parents:
81657
diff
changeset
|
75 |
Path.split(Isabelle_System.getenv_strict("ISABELLE_DOCS_RELEASE_NOTES"))
|
|
956ecf2c07a0
more flexible ML_Settings in Isabelle/Scala, depending on system options and some default settings;
wenzelm
parents:
81657
diff
changeset
|
76 |
.flatMap(plain_file(_))) |
| 52427 | 77 |
|
| 82759 | 78 |
def examples(ml_settings: ML_Settings): Contents = {
|
|
82720
956ecf2c07a0
more flexible ML_Settings in Isabelle/Scala, depending on system options and some default settings;
wenzelm
parents:
81657
diff
changeset
|
79 |
val env = Isabelle_System.Settings(putenv = List(ml_settings.ml_sources_root)) |
| 75118 | 80 |
Contents.section("Examples", true,
|
|
56424
7032378cc097
proper settings instead of hard-wired information;
wenzelm
parents:
56423
diff
changeset
|
81 |
Path.split(Isabelle_System.getenv_strict("ISABELLE_DOCS_EXAMPLES")).map(file =>
|
|
82720
956ecf2c07a0
more flexible ML_Settings in Isabelle/Scala, depending on system options and some default settings;
wenzelm
parents:
81657
diff
changeset
|
82 |
plain_file(file, env = env) match {
|
|
56424
7032378cc097
proper settings instead of hard-wired information;
wenzelm
parents:
56423
diff
changeset
|
83 |
case Some(entry) => entry |
| 56425 | 84 |
case None => error("Bad entry in ISABELLE_DOCS_EXAMPLES: " + file)
|
|
75115
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
85 |
})) |
|
82720
956ecf2c07a0
more flexible ML_Settings in Isabelle/Scala, depending on system options and some default settings;
wenzelm
parents:
81657
diff
changeset
|
86 |
} |
|
75115
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
87 |
|
| 75393 | 88 |
def main_contents(): Contents = {
|
|
75115
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
89 |
val result = new mutable.ListBuffer[Section] |
|
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
90 |
val entries = new mutable.ListBuffer[Entry] |
|
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
91 |
var section: Option[Section] = None |
|
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 |
def flush(): Unit = |
|
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
94 |
if (section.nonEmpty) {
|
|
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
95 |
result += section.get.copy(entries = entries.toList) |
|
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
96 |
entries.clear() |
|
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
97 |
section = None |
|
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
98 |
} |
|
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
99 |
|
| 75393 | 100 |
def begin(s: Section): Unit = {
|
|
75115
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
101 |
flush() |
|
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
102 |
section = Some(s) |
|
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
103 |
} |
|
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
104 |
|
|
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
105 |
val Section_ = """^(\S.*)\s*$""".r |
|
81350
1818358373e2
misc tuning and clarification: Doc.Entry supports both plain files and pdf documents;
wenzelm
parents:
75394
diff
changeset
|
106 |
val Entry_ = """^\s+(\S+)\s+(.+)\s*$""".r |
| 53777 | 107 |
|
|
75115
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
108 |
for ((dir, line) <- contents_lines()) {
|
|
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
109 |
line match {
|
|
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
110 |
case Section_(text) => |
|
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
111 |
Library.try_unsuffix("!", text) match {
|
|
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
112 |
case None => begin(Section(text, false, Nil)) |
|
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
113 |
case Some(txt) => begin(Section(txt, true, Nil)) |
|
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
114 |
} |
|
81350
1818358373e2
misc tuning and clarification: Doc.Entry supports both plain files and pdf documents;
wenzelm
parents:
75394
diff
changeset
|
115 |
case Entry_(name, title) => |
|
1818358373e2
misc tuning and clarification: Doc.Entry supports both plain files and pdf documents;
wenzelm
parents:
75394
diff
changeset
|
116 |
val path = dir + Path.basic(name) |
|
1818358373e2
misc tuning and clarification: Doc.Entry supports both plain files and pdf documents;
wenzelm
parents:
75394
diff
changeset
|
117 |
entries += Entry(name, if (path.is_file) path else path.pdf, title = title) |
|
75115
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
118 |
case _ => |
|
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
119 |
} |
|
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
120 |
} |
| 75114 | 121 |
|
|
75115
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
122 |
flush() |
| 75118 | 123 |
Contents(result.toList) |
|
75115
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
124 |
} |
|
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
125 |
|
| 82759 | 126 |
def contents(ml_settings: ML_Settings): Contents = {
|
127 |
examples(ml_settings) ++ release_notes() ++ main_contents() |
|
| 61157 | 128 |
} |
| 52427 | 129 |
|
| 75393 | 130 |
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
|
131 |
val here = Scala_Project.here |
| 82759 | 132 |
def apply(arg: String): String = {
|
| 82762 | 133 |
val ml_settings = ML_Settings.init(ml_platform = arg) |
| 82759 | 134 |
cat_lines((for (entry <- contents(ml_settings).entries(pdf = true)) yield entry.name).sorted) |
135 |
} |
|
|
72760
042180540068
clarified protocol: Doc.check at run-time via Scala function;
wenzelm
parents:
71601
diff
changeset
|
136 |
} |
| 67471 | 137 |
|
| 52427 | 138 |
|
139 |
/* view */ |
|
140 |
||
| 75393 | 141 |
def view(path: Path): Unit = {
|
| 75120 | 142 |
if (!path.is_file) error("Bad Isabelle documentation file: " + path)
|
143 |
else if (path.is_pdf) Isabelle_System.pdf_viewer(path) |
|
144 |
else Output.writeln(Library.trim_line(File.read(path)), stdout = true) |
|
| 52427 | 145 |
} |
|
52444
2cfe6656d6d6
slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents:
52429
diff
changeset
|
146 |
|
|
2cfe6656d6d6
slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents:
52429
diff
changeset
|
147 |
|
| 62831 | 148 |
/* Isabelle tool wrapper */ |
|
52444
2cfe6656d6d6
slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents:
52429
diff
changeset
|
149 |
|
|
81353
4829e4c68d7c
tuned description: plain text documentation is also supported;
wenzelm
parents:
81350
diff
changeset
|
150 |
val isabelle_tool = Isabelle_Tool("doc", "view Isabelle documentation",
|
| 75394 | 151 |
Scala_Project.here, |
152 |
{ args =>
|
|
153 |
val getopts = Getopts("""
|
|
| 62438 | 154 |
Usage: isabelle doc [DOC ...] |
155 |
||
|
81353
4829e4c68d7c
tuned description: plain text documentation is also supported;
wenzelm
parents:
81350
diff
changeset
|
156 |
View Isabelle documentation. |
| 62438 | 157 |
""") |
| 75394 | 158 |
val docs = getopts(args) |
| 62438 | 159 |
|
| 82762 | 160 |
val ml_settings = ML_Settings.init() |
| 82759 | 161 |
|
| 75394 | 162 |
if (docs.isEmpty) Output.writeln(cat_lines(contents_lines().map(_._2)), stdout = true) |
163 |
else {
|
|
164 |
docs.foreach(name => |
|
| 82759 | 165 |
contents(ml_settings).entries(name = docs.contains).headOption match {
|
|
81350
1818358373e2
misc tuning and clarification: Doc.Entry supports both plain files and pdf documents;
wenzelm
parents:
75394
diff
changeset
|
166 |
case Some(entry) => entry.view() |
| 75394 | 167 |
case None => error("No Isabelle documentation entry: " + quote(name))
|
168 |
} |
|
169 |
) |
|
170 |
} |
|
171 |
}) |
|
| 52427 | 172 |
} |