author | wenzelm |
Thu, 06 Dec 2018 12:55:53 +0100 | |
changeset 69409 | e7a5340128f0 |
parent 69283 | 39044da8bb5a |
child 71397 | 028edb1e5b99 |
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 |
import scala.util.matching.Regex |
|
11 |
||
12 |
||
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 |
2cfe6656d6d6
slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents:
52429
diff
changeset
|
28 |
line <- split_lines(Library.trim_line(File.read(catalog))) |
56422 | 29 |
} yield (dir, line) |
52444
2cfe6656d6d6
slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents:
52429
diff
changeset
|
30 |
|
52427 | 31 |
sealed abstract class Entry |
56423
c2f52824dbb2
explicit indication of important doc sections ("!"), which are expanded in the tree view;
wenzelm
parents:
56422
diff
changeset
|
32 |
case class Section(text: String, important: Boolean) extends 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 |
private val Variable_Path = new Regex("""^\$[^/]+/+(.+)$""") |
37 |
||
38 |
def text_file(path: Path): Option[Text_File] = |
|
53777 | 39 |
{ |
69283 | 40 |
if (path.is_file) { |
41 |
val name = |
|
42 |
path.implode match { |
|
43 |
case Variable_Path(name) => name |
|
44 |
case name => name |
|
45 |
} |
|
46 |
Some(Text_File(name, path)) |
|
47 |
} |
|
53777 | 48 |
else None |
49 |
} |
|
50 |
||
52444
2cfe6656d6d6
slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents:
52429
diff
changeset
|
51 |
private val Section_Entry = new Regex("""^(\S.*)\s*$""") |
2cfe6656d6d6
slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents:
52429
diff
changeset
|
52 |
private val Doc_Entry = new Regex("""^\s+(\S+)\s+(.+)\s*$""") |
52427 | 53 |
|
52542
19d674acb764
more release notes according to availability in proper release vs. repository clone;
wenzelm
parents:
52541
diff
changeset
|
54 |
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
|
55 |
Section("Release Notes", true) :: |
56424
7032378cc097
proper settings instead of hard-wired information;
wenzelm
parents:
56423
diff
changeset
|
56 |
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
|
57 |
|
53777 | 58 |
private def examples(): List[Entry] = |
56424
7032378cc097
proper settings instead of hard-wired information;
wenzelm
parents:
56423
diff
changeset
|
59 |
Section("Examples", true) :: |
7032378cc097
proper settings instead of hard-wired information;
wenzelm
parents:
56423
diff
changeset
|
60 |
Path.split(Isabelle_System.getenv_strict("ISABELLE_DOCS_EXAMPLES")).map(file => |
7032378cc097
proper settings instead of hard-wired information;
wenzelm
parents:
56423
diff
changeset
|
61 |
text_file(file) match { |
7032378cc097
proper settings instead of hard-wired information;
wenzelm
parents:
56423
diff
changeset
|
62 |
case Some(entry) => entry |
56425 | 63 |
case None => error("Bad entry in ISABELLE_DOCS_EXAMPLES: " + file) |
56424
7032378cc097
proper settings instead of hard-wired information;
wenzelm
parents:
56423
diff
changeset
|
64 |
}) |
53777 | 65 |
|
52444
2cfe6656d6d6
slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents:
52429
diff
changeset
|
66 |
def contents(): List[Entry] = |
61157 | 67 |
{ |
68 |
val main_contents = |
|
69 |
for { |
|
70 |
(dir, line) <- contents_lines() |
|
71 |
entry <- |
|
72 |
line match { |
|
73 |
case Section_Entry(text) => |
|
74 |
Library.try_unsuffix("!", text) match { |
|
75 |
case None => Some(Section(text, false)) |
|
76 |
case Some(txt) => Some(Section(txt, true)) |
|
77 |
} |
|
78 |
case Doc_Entry(name, title) => Some(Doc(name, title, dir + Path.basic(name))) |
|
79 |
case _ => None |
|
80 |
} |
|
81 |
} yield entry |
|
82 |
||
83 |
examples() ::: release_notes() ::: main_contents |
|
84 |
} |
|
52427 | 85 |
|
67471 | 86 |
def doc_names(): List[String] = |
87 |
for (Doc(name, _, _) <- contents()) yield name |
|
88 |
||
52427 | 89 |
|
90 |
/* view */ |
|
91 |
||
56422 | 92 |
def view(path: Path) |
52427 | 93 |
{ |
67178 | 94 |
if (path.is_file) Output.writeln(Library.trim_line(File.read(path)), stdout = true) |
56422 | 95 |
else { |
96 |
val pdf = path.ext("pdf") |
|
97 |
if (pdf.is_file) Isabelle_System.pdf_viewer(pdf) |
|
98 |
else error("Bad Isabelle documentation file: " + pdf) |
|
52427 | 99 |
} |
100 |
} |
|
52444
2cfe6656d6d6
slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents:
52429
diff
changeset
|
101 |
|
2cfe6656d6d6
slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents:
52429
diff
changeset
|
102 |
|
62831 | 103 |
/* Isabelle tool wrapper */ |
52444
2cfe6656d6d6
slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents:
52429
diff
changeset
|
104 |
|
62831 | 105 |
val isabelle_tool = Isabelle_Tool("doc", "view Isabelle documentation", args => |
52444
2cfe6656d6d6
slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents:
52429
diff
changeset
|
106 |
{ |
62831 | 107 |
val getopts = Getopts(""" |
62438 | 108 |
Usage: isabelle doc [DOC ...] |
109 |
||
110 |
View Isabelle documentation. |
|
111 |
""") |
|
62831 | 112 |
val docs = getopts(args) |
62438 | 113 |
|
62831 | 114 |
val entries = contents() |
67178 | 115 |
if (docs.isEmpty) Output.writeln(cat_lines(contents_lines().map(_._2)), stdout = true) |
62831 | 116 |
else { |
117 |
docs.foreach(doc => |
|
118 |
entries.collectFirst { case Doc(name, _, path) if doc == name => path } match { |
|
119 |
case Some(path) => view(path) |
|
120 |
case None => error("No Isabelle documentation entry: " + quote(doc)) |
|
121 |
} |
|
122 |
) |
|
52444
2cfe6656d6d6
slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents:
52429
diff
changeset
|
123 |
} |
62831 | 124 |
}) |
52427 | 125 |
} |