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