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