author | wenzelm |
Mon, 21 Feb 2022 21:15:05 +0100 | |
changeset 75120 | 488c7e8923b2 |
parent 75118 | 6fd8e482c9ce |
child 75393 | 87ebf5a50283 |
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 |
||
56424
7032378cc097
proper settings instead of hard-wired information;
wenzelm
parents:
56423
diff
changeset
|
4 |
Access to Isabelle 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 |
|
52427 | 13 |
object Doc |
14 |
{ |
|
15 |
/* dirs */ |
|
16 |
||
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 | 19 |
|
52427 | 20 |
|
21 |
/* contents */ |
|
22 |
||
56422 | 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 | 28 |
line <- Library.trim_split_lines(File.read(catalog)) |
56422 | 29 |
} yield (dir, line) |
52444
2cfe6656d6d6
slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents:
52429
diff
changeset
|
30 |
|
75118 | 31 |
object Contents |
32 |
{ |
|
33 |
def apply(sections: List[Section]): Contents = new Contents(sections) |
|
34 |
||
35 |
def section(title: String, important: Boolean, entries: List[Entry]): Contents = |
|
36 |
apply(List(Section(title, important, entries))) |
|
37 |
} |
|
38 |
class Contents private(val sections: List[Section]) |
|
39 |
{ |
|
40 |
def ++ (other: Contents): Contents = new Contents(sections ::: other.sections) |
|
41 |
def entries: List[Entry] = sections.flatMap(_.entries) |
|
42 |
def docs: List[Doc] = entries.collect({ case doc: Doc => doc }) |
|
43 |
} |
|
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 | 46 |
sealed abstract class Entry |
75118 | 47 |
{ |
48 |
def name: String |
|
49 |
def path: Path |
|
50 |
} |
|
56422 | 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 | 53 |
|
69283 | 54 |
def text_file(path: Path): Option[Text_File] = |
55 |
if (path.is_file) { |
|
71397 | 56 |
val a = path.implode |
57 |
val b = Library.try_unprefix("$ISABELLE_HOME/", a).getOrElse(a) |
|
58 |
Some(Text_File(b, path)) |
|
69283 | 59 |
} |
53777 | 60 |
else None |
61 |
||
75118 | 62 |
def release_notes(): Contents = |
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 | 65 |
|
75118 | 66 |
def examples(): Contents = |
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 | 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 | 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 | 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 | 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 | 108 |
|
75115
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
109 |
flush() |
75118 | 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 | 113 |
def contents(): Contents = |
61157 | 114 |
{ |
75118 | 115 |
examples() ++ release_notes() ++ main_contents() |
61157 | 116 |
} |
52427 | 117 |
|
73565 | 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 | 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 | 125 |
|
52427 | 126 |
|
127 |
/* view */ |
|
128 |
||
73340 | 129 |
def view(path: Path): Unit = |
52427 | 130 |
{ |
75120 | 131 |
if (!path.is_file) error("Bad Isabelle documentation file: " + path) |
132 |
else if (path.is_pdf) Isabelle_System.pdf_viewer(path) |
|
133 |
else Output.writeln(Library.trim_line(File.read(path)), stdout = true) |
|
52427 | 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 | 137 |
/* Isabelle tool wrapper */ |
52444
2cfe6656d6d6
slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents:
52429
diff
changeset
|
138 |
|
75116 | 139 |
val isabelle_tool = Isabelle_Tool("doc", "view Isabelle PDF documentation", |
72763 | 140 |
Scala_Project.here, args => |
52444
2cfe6656d6d6
slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents:
52429
diff
changeset
|
141 |
{ |
62831 | 142 |
val getopts = Getopts(""" |
62438 | 143 |
Usage: isabelle doc [DOC ...] |
144 |
||
75116 | 145 |
View Isabelle PDF documentation. |
62438 | 146 |
""") |
62831 | 147 |
val docs = getopts(args) |
62438 | 148 |
|
67178 | 149 |
if (docs.isEmpty) Output.writeln(cat_lines(contents_lines().map(_._2)), stdout = true) |
62831 | 150 |
else { |
75115
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
151 |
docs.foreach(name => |
75118 | 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 | 155 |
} |
156 |
) |
|
52444
2cfe6656d6d6
slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents:
52429
diff
changeset
|
157 |
} |
62831 | 158 |
}) |
52427 | 159 |
} |