author | wenzelm |
Mon, 21 Feb 2022 16:23:11 +0100 | |
changeset 75115 | c212435866d6 |
parent 75114 | 1fd78367c96f |
child 75116 | 001b74439ad4 |
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 |
|
75115
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
31 |
case class Section(title: String, important: Boolean, entries: List[Entry]) |
52427 | 32 |
sealed abstract class Entry |
56422 | 33 |
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
|
34 |
case class Text_File(name: String, path: Path) extends Entry |
52427 | 35 |
|
69283 | 36 |
def text_file(path: Path): Option[Text_File] = |
37 |
if (path.is_file) { |
|
71397 | 38 |
val a = path.implode |
39 |
val b = Library.try_unprefix("$ISABELLE_HOME/", a).getOrElse(a) |
|
40 |
Some(Text_File(b, path)) |
|
69283 | 41 |
} |
53777 | 42 |
else None |
43 |
||
75115
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
44 |
def release_notes(): Section = |
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
45 |
Section("Release Notes", true, |
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
46 |
Path.split(Isabelle_System.getenv_strict("ISABELLE_DOCS_RELEASE_NOTES")).flatMap(text_file)) |
52427 | 47 |
|
75115
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
48 |
def examples(): Section = |
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
49 |
Section("Examples", true, |
56424
7032378cc097
proper settings instead of hard-wired information;
wenzelm
parents:
56423
diff
changeset
|
50 |
Path.split(Isabelle_System.getenv_strict("ISABELLE_DOCS_EXAMPLES")).map(file => |
7032378cc097
proper settings instead of hard-wired information;
wenzelm
parents:
56423
diff
changeset
|
51 |
text_file(file) match { |
7032378cc097
proper settings instead of hard-wired information;
wenzelm
parents:
56423
diff
changeset
|
52 |
case Some(entry) => entry |
56425 | 53 |
case None => error("Bad entry in ISABELLE_DOCS_EXAMPLES: " + file) |
75115
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
54 |
})) |
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
55 |
|
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
56 |
def main_contents(): List[Section] = |
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
57 |
{ |
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
58 |
val result = new mutable.ListBuffer[Section] |
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
59 |
val entries = new mutable.ListBuffer[Entry] |
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
60 |
var section: Option[Section] = None |
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
61 |
|
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
62 |
def flush(): Unit = |
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
63 |
if (section.nonEmpty) { |
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
64 |
result += section.get.copy(entries = entries.toList) |
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
65 |
entries.clear() |
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
66 |
section = None |
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
67 |
} |
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
68 |
|
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
69 |
def begin(s: Section): Unit = |
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
70 |
{ |
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
71 |
flush() |
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
72 |
section = Some(s) |
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
73 |
} |
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
74 |
|
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
75 |
val Section_ = """^(\S.*)\s*$""".r |
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
76 |
val Doc_ = """^\s+(\S+)\s+(.+)\s*$""".r |
53777 | 77 |
|
75115
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
78 |
for ((dir, line) <- contents_lines()) { |
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
79 |
line match { |
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
80 |
case Section_(text) => |
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
81 |
Library.try_unsuffix("!", text) match { |
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
82 |
case None => begin(Section(text, false, Nil)) |
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
83 |
case Some(txt) => begin(Section(txt, true, Nil)) |
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
84 |
} |
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
85 |
case Doc_(name, title) => |
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
86 |
entries += Doc(name, title, dir + Path.basic(name)) |
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
87 |
case _ => |
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 |
} |
75114 | 90 |
|
75115
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
91 |
flush() |
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
92 |
result.toList |
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
93 |
} |
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
94 |
|
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
95 |
def contents(): List[Section] = |
61157 | 96 |
{ |
75115
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
97 |
examples() :: release_notes() :: main_contents() |
61157 | 98 |
} |
52427 | 99 |
|
75115
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
100 |
def doc_entries(sections: List[Section]): List[Doc] = |
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
101 |
sections.flatMap(_.entries).collect({ case doc: Doc => doc }) |
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
102 |
|
73565 | 103 |
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
|
104 |
{ |
042180540068
clarified protocol: Doc.check at run-time via Scala function;
wenzelm
parents:
71601
diff
changeset
|
105 |
val here = Scala_Project.here |
042180540068
clarified protocol: Doc.check at run-time via Scala function;
wenzelm
parents:
71601
diff
changeset
|
106 |
def apply(arg: String): String = |
042180540068
clarified protocol: Doc.check at run-time via Scala function;
wenzelm
parents:
71601
diff
changeset
|
107 |
if (arg.nonEmpty) error("Bad argument: " + quote(arg)) |
75115
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
108 |
else cat_lines((for (doc <- doc_entries(contents())) yield doc.name).sorted) |
72760
042180540068
clarified protocol: Doc.check at run-time via Scala function;
wenzelm
parents:
71601
diff
changeset
|
109 |
} |
67471 | 110 |
|
52427 | 111 |
|
112 |
/* view */ |
|
113 |
||
73340 | 114 |
def view(path: Path): Unit = |
52427 | 115 |
{ |
67178 | 116 |
if (path.is_file) Output.writeln(Library.trim_line(File.read(path)), stdout = true) |
56422 | 117 |
else { |
118 |
val pdf = path.ext("pdf") |
|
119 |
if (pdf.is_file) Isabelle_System.pdf_viewer(pdf) |
|
120 |
else error("Bad Isabelle documentation file: " + pdf) |
|
52427 | 121 |
} |
122 |
} |
|
52444
2cfe6656d6d6
slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents:
52429
diff
changeset
|
123 |
|
2cfe6656d6d6
slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents:
52429
diff
changeset
|
124 |
|
62831 | 125 |
/* Isabelle tool wrapper */ |
52444
2cfe6656d6d6
slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents:
52429
diff
changeset
|
126 |
|
72763 | 127 |
val isabelle_tool = Isabelle_Tool("doc", "view Isabelle documentation", |
128 |
Scala_Project.here, args => |
|
52444
2cfe6656d6d6
slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents:
52429
diff
changeset
|
129 |
{ |
62831 | 130 |
val getopts = Getopts(""" |
62438 | 131 |
Usage: isabelle doc [DOC ...] |
132 |
||
133 |
View Isabelle documentation. |
|
134 |
""") |
|
62831 | 135 |
val docs = getopts(args) |
62438 | 136 |
|
75115
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
137 |
val sections = contents() |
67178 | 138 |
if (docs.isEmpty) Output.writeln(cat_lines(contents_lines().map(_._2)), stdout = true) |
62831 | 139 |
else { |
75115
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
140 |
docs.foreach(name => |
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
141 |
doc_entries(sections).find(_.name == name) match { |
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
142 |
case Some(doc) => view(doc.path) |
c212435866d6
clarified signature: more explicit section structure;
wenzelm
parents:
75114
diff
changeset
|
143 |
case None => error("No Isabelle documentation entry: " + quote(name)) |
62831 | 144 |
} |
145 |
) |
|
52444
2cfe6656d6d6
slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents:
52429
diff
changeset
|
146 |
} |
62831 | 147 |
}) |
52427 | 148 |
} |