author | wenzelm |
Sun, 29 Nov 2020 13:59:18 +0100 | |
changeset 72770 | 0c86c29767b2 |
parent 72763 | 3cc73d00553c |
child 73276 | 54065cbf7134 |
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 |
||
10 |
object Doc |
|
11 |
{ |
|
12 |
/* dirs */ |
|
13 |
||
14 |
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
|
15 |
Path.split(Isabelle_System.getenv("ISABELLE_DOCS")) |
52740 | 16 |
|
52427 | 17 |
|
18 |
/* contents */ |
|
19 |
||
56422 | 20 |
private def contents_lines(): List[(Path, String)] = |
52444
2cfe6656d6d6
slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents:
52429
diff
changeset
|
21 |
for { |
2cfe6656d6d6
slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents:
52429
diff
changeset
|
22 |
dir <- dirs() |
2cfe6656d6d6
slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents:
52429
diff
changeset
|
23 |
catalog = dir + Path.basic("Contents") |
2cfe6656d6d6
slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents:
52429
diff
changeset
|
24 |
if catalog.is_file |
2cfe6656d6d6
slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents:
52429
diff
changeset
|
25 |
line <- split_lines(Library.trim_line(File.read(catalog))) |
56422 | 26 |
} yield (dir, line) |
52444
2cfe6656d6d6
slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents:
52429
diff
changeset
|
27 |
|
52427 | 28 |
sealed abstract class Entry |
56423
c2f52824dbb2
explicit indication of important doc sections ("!"), which are expanded in the tree view;
wenzelm
parents:
56422
diff
changeset
|
29 |
case class Section(text: String, important: Boolean) extends Entry |
56422 | 30 |
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
|
31 |
case class Text_File(name: String, path: Path) extends Entry |
52427 | 32 |
|
69283 | 33 |
def text_file(path: Path): Option[Text_File] = |
34 |
if (path.is_file) { |
|
71397 | 35 |
val a = path.implode |
36 |
val b = Library.try_unprefix("$ISABELLE_HOME/", a).getOrElse(a) |
|
37 |
Some(Text_File(b, path)) |
|
69283 | 38 |
} |
53777 | 39 |
else None |
40 |
||
72770 | 41 |
private val Section_Entry = """^(\S.*)\s*$""".r |
42 |
private val Doc_Entry = """^\s+(\S+)\s+(.+)\s*$""".r |
|
52427 | 43 |
|
52542
19d674acb764
more release notes according to availability in proper release vs. repository clone;
wenzelm
parents:
52541
diff
changeset
|
44 |
private def release_notes(): List[Entry] = |
69409
e7a5340128f0
clarified doc sections: add-on components may focus their own application name;
wenzelm
parents:
69283
diff
changeset
|
45 |
Section("Release Notes", true) :: |
71601 | 46 |
Path.split(Isabelle_System.getenv_strict("ISABELLE_DOCS_RELEASE_NOTES")).flatMap(text_file) |
52541
97c950217d7f
quick access to release notes (imitating website/documentation.html);
wenzelm
parents:
52448
diff
changeset
|
47 |
|
53777 | 48 |
private def examples(): List[Entry] = |
56424
7032378cc097
proper settings instead of hard-wired information;
wenzelm
parents:
56423
diff
changeset
|
49 |
Section("Examples", true) :: |
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) |
56424
7032378cc097
proper settings instead of hard-wired information;
wenzelm
parents:
56423
diff
changeset
|
54 |
}) |
53777 | 55 |
|
52444
2cfe6656d6d6
slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents:
52429
diff
changeset
|
56 |
def contents(): List[Entry] = |
61157 | 57 |
{ |
58 |
val main_contents = |
|
59 |
for { |
|
60 |
(dir, line) <- contents_lines() |
|
61 |
entry <- |
|
62 |
line match { |
|
63 |
case Section_Entry(text) => |
|
64 |
Library.try_unsuffix("!", text) match { |
|
65 |
case None => Some(Section(text, false)) |
|
66 |
case Some(txt) => Some(Section(txt, true)) |
|
67 |
} |
|
68 |
case Doc_Entry(name, title) => Some(Doc(name, title, dir + Path.basic(name))) |
|
69 |
case _ => None |
|
70 |
} |
|
71 |
} yield entry |
|
72 |
||
73 |
examples() ::: release_notes() ::: main_contents |
|
74 |
} |
|
52427 | 75 |
|
72760
042180540068
clarified protocol: Doc.check at run-time via Scala function;
wenzelm
parents:
71601
diff
changeset
|
76 |
object Doc_Names extends Scala.Fun("doc_names") |
042180540068
clarified protocol: Doc.check at run-time via Scala function;
wenzelm
parents:
71601
diff
changeset
|
77 |
{ |
042180540068
clarified protocol: Doc.check at run-time via Scala function;
wenzelm
parents:
71601
diff
changeset
|
78 |
val here = Scala_Project.here |
042180540068
clarified protocol: Doc.check at run-time via Scala function;
wenzelm
parents:
71601
diff
changeset
|
79 |
def apply(arg: String): String = |
042180540068
clarified protocol: Doc.check at run-time via Scala function;
wenzelm
parents:
71601
diff
changeset
|
80 |
if (arg.nonEmpty) error("Bad argument: " + quote(arg)) |
042180540068
clarified protocol: Doc.check at run-time via Scala function;
wenzelm
parents:
71601
diff
changeset
|
81 |
else cat_lines((for (Doc(name, _, _) <- contents()) yield name).sorted) |
042180540068
clarified protocol: Doc.check at run-time via Scala function;
wenzelm
parents:
71601
diff
changeset
|
82 |
} |
67471 | 83 |
|
52427 | 84 |
|
85 |
/* view */ |
|
86 |
||
56422 | 87 |
def view(path: Path) |
52427 | 88 |
{ |
67178 | 89 |
if (path.is_file) Output.writeln(Library.trim_line(File.read(path)), stdout = true) |
56422 | 90 |
else { |
91 |
val pdf = path.ext("pdf") |
|
92 |
if (pdf.is_file) Isabelle_System.pdf_viewer(pdf) |
|
93 |
else error("Bad Isabelle documentation file: " + pdf) |
|
52427 | 94 |
} |
95 |
} |
|
52444
2cfe6656d6d6
slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents:
52429
diff
changeset
|
96 |
|
2cfe6656d6d6
slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents:
52429
diff
changeset
|
97 |
|
62831 | 98 |
/* Isabelle tool wrapper */ |
52444
2cfe6656d6d6
slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents:
52429
diff
changeset
|
99 |
|
72763 | 100 |
val isabelle_tool = Isabelle_Tool("doc", "view Isabelle documentation", |
101 |
Scala_Project.here, args => |
|
52444
2cfe6656d6d6
slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents:
52429
diff
changeset
|
102 |
{ |
62831 | 103 |
val getopts = Getopts(""" |
62438 | 104 |
Usage: isabelle doc [DOC ...] |
105 |
||
106 |
View Isabelle documentation. |
|
107 |
""") |
|
62831 | 108 |
val docs = getopts(args) |
62438 | 109 |
|
62831 | 110 |
val entries = contents() |
67178 | 111 |
if (docs.isEmpty) Output.writeln(cat_lines(contents_lines().map(_._2)), stdout = true) |
62831 | 112 |
else { |
113 |
docs.foreach(doc => |
|
114 |
entries.collectFirst { case Doc(name, _, path) if doc == name => path } match { |
|
115 |
case Some(path) => view(path) |
|
116 |
case None => error("No Isabelle documentation entry: " + quote(doc)) |
|
117 |
} |
|
118 |
) |
|
52444
2cfe6656d6d6
slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents:
52429
diff
changeset
|
119 |
} |
62831 | 120 |
}) |
52427 | 121 |
} |