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