author | wenzelm |
Thu, 04 Nov 2021 16:47:28 +0100 | |
changeset 74693 | f7525f5c84b6 |
parent 74688 | 7e31f7022c7b |
child 74694 | 2d9d92116fac |
permissions | -rw-r--r-- |
73718 | 1 |
/* Title: Pure/Thy/presentation.scala |
50707
5b2bf7611662
maintain session index on Scala side, for more determistic results;
wenzelm
parents:
diff
changeset
|
2 |
Author: Makarius |
5b2bf7611662
maintain session index on Scala side, for more determistic results;
wenzelm
parents:
diff
changeset
|
3 |
|
73718 | 4 |
HTML presentation of PIDE document content. |
50707
5b2bf7611662
maintain session index on Scala side, for more determistic results;
wenzelm
parents:
diff
changeset
|
5 |
*/ |
5b2bf7611662
maintain session index on Scala side, for more determistic results;
wenzelm
parents:
diff
changeset
|
6 |
|
5b2bf7611662
maintain session index on Scala side, for more determistic results;
wenzelm
parents:
diff
changeset
|
7 |
package isabelle |
5b2bf7611662
maintain session index on Scala side, for more determistic results;
wenzelm
parents:
diff
changeset
|
8 |
|
5b2bf7611662
maintain session index on Scala side, for more determistic results;
wenzelm
parents:
diff
changeset
|
9 |
|
74677 | 10 |
import scala.annotation.tailrec |
51402 | 11 |
import scala.collection.immutable.SortedMap |
74677 | 12 |
import scala.collection.mutable |
51402 | 13 |
|
14 |
||
72652 | 15 |
object Presentation |
50707
5b2bf7611662
maintain session index on Scala side, for more determistic results;
wenzelm
parents:
diff
changeset
|
16 |
{ |
72957 | 17 |
/** HTML documents **/ |
18 |
||
73718 | 19 |
/* HTML context */ |
20 |
||
72962 | 21 |
val fonts_path = Path.explode("fonts") |
22 |
||
72957 | 23 |
sealed case class HTML_Document(title: String, content: String) |
24 |
||
72959 | 25 |
def html_context(fonts_url: String => String = HTML.fonts_url()): HTML_Context = |
26 |
new HTML_Context(fonts_url) |
|
27 |
||
28 |
final class HTML_Context private[Presentation](fonts_url: String => String) |
|
29 |
{ |
|
73340 | 30 |
def init_fonts(dir: Path): Unit = |
72962 | 31 |
{ |
32 |
val fonts_dir = Isabelle_System.make_directory(dir + fonts_path) |
|
33 |
for (entry <- Isabelle_Fonts.fonts(hidden = true)) |
|
73317 | 34 |
Isabelle_System.copy_file(entry.path, fonts_dir) |
72962 | 35 |
} |
36 |
||
37 |
def head(title: String, rest: XML.Body = Nil): XML.Tree = |
|
38 |
HTML.div("head", HTML.chapter(title) :: rest) |
|
39 |
||
40 |
def source(body: XML.Body): XML.Tree = HTML.pre("source", body) |
|
41 |
||
74677 | 42 |
def offset_ref(offset: Text.Range): String = |
43 |
"offset/" + offset.start + ".." + offset.stop |
|
44 |
||
45 |
def export_ref(entity: Entity): String = |
|
46 |
Export_Theory.export_kind(entity.kind) + "/" + entity.name |
|
47 |
||
72962 | 48 |
def contents(heading: String, items: List[XML.Body], css_class: String = "contents") |
49 |
: List[XML.Elem] = |
|
50 |
{ |
|
51 |
if (items.isEmpty) Nil |
|
52 |
else List(HTML.div(css_class, List(HTML.section(heading), HTML.itemize(items)))) |
|
53 |
} |
|
54 |
||
72959 | 55 |
def output_document(title: String, body: XML.Body): String = |
56 |
HTML.output_document( |
|
57 |
List( |
|
58 |
HTML.style(HTML.fonts_css(fonts_url) + "\n\n" + File.read(HTML.isabelle_css)), |
|
59 |
HTML.title(title)), |
|
60 |
List(HTML.source(body)), css = "", structural = false) |
|
61 |
||
62 |
def html_document(title: String, body: XML.Body): HTML_Document = |
|
63 |
HTML_Document(title, output_document(title, body)) |
|
64 |
} |
|
65 |
||
72957 | 66 |
|
73055 | 67 |
/* presentation elements */ |
68 |
||
69 |
sealed case class Elements( |
|
70 |
html: Markup.Elements = Markup.Elements.empty, |
|
73056 | 71 |
entity: Markup.Elements = Markup.Elements.empty, |
73055 | 72 |
language: Markup.Elements = Markup.Elements.empty) |
72957 | 73 |
|
73055 | 74 |
val elements1: Elements = |
75 |
Elements( |
|
74677 | 76 |
html = Rendering.foreground_elements ++ Rendering.text_color_elements + |
73056 | 77 |
Markup.NUMERAL + Markup.COMMENT + Markup.ENTITY + Markup.LANGUAGE, |
74677 | 78 |
entity = Markup.Elements(Markup.THEORY, Markup.TYPE_NAME, Markup.CONSTANT, Markup.FACT, |
79 |
Markup.CLASS, Markup.LOCALE, Markup.FREE)) |
|
73036 | 80 |
|
73055 | 81 |
val elements2: Elements = |
82 |
Elements( |
|
83 |
html = elements1.html ++ Rendering.markdown_elements, |
|
73056 | 84 |
language = Markup.Elements(Markup.Language.DOCUMENT)) |
73055 | 85 |
|
74678 | 86 |
|
87 |
/* formal entities */ |
|
74677 | 88 |
|
89 |
type Entity = Export_Theory.Entity[Export_Theory.No_Content] |
|
90 |
||
91 |
case class Entity_Context(node: Document.Node.Name) |
|
92 |
{ |
|
93 |
val seen_ranges: mutable.Set[Symbol.Range] = mutable.Set.empty |
|
94 |
} |
|
73055 | 95 |
|
74678 | 96 |
|
73718 | 97 |
/* HTML output */ |
73036 | 98 |
|
72957 | 99 |
private val div_elements = |
100 |
Set(HTML.div.name, HTML.pre.name, HTML.par.name, HTML.list.name, HTML.enum.name, |
|
101 |
HTML.descr.name) |
|
102 |
||
73056 | 103 |
def make_html( |
74677 | 104 |
name: Document.Node.Name, |
73056 | 105 |
elements: Elements, |
74677 | 106 |
entity_anchor: (Entity_Context, Symbol.Range, XML.Body) => Option[XML.Tree], |
107 |
entity_link: (Entity_Context, Properties.T, XML.Body) => Option[XML.Tree], |
|
73056 | 108 |
xml: XML.Body): XML.Body = |
73054 | 109 |
{ |
74677 | 110 |
val context = Entity_Context(name) |
111 |
||
73054 | 112 |
def html_div(html: XML.Body): Boolean = |
113 |
html exists { |
|
114 |
case XML.Elem(markup, body) => div_elements.contains(markup.name) || html_div(body) |
|
115 |
case XML.Text(_) => false |
|
116 |
} |
|
72957 | 117 |
|
73055 | 118 |
def html_class(c: String, html: XML.Body): XML.Body = |
119 |
if (c == "") html |
|
120 |
else if (html_div(html)) List(HTML.div(c, html)) |
|
121 |
else List(HTML.span(c, html)) |
|
72957 | 122 |
|
74677 | 123 |
def html_body(xml_body: XML.Body, end_offset: Symbol.Offset): (XML.Body, Symbol.Offset) = |
124 |
xml_body.foldRight((List.empty[XML.Tree], end_offset)) { case (tree, (res, end_offset1)) => |
|
125 |
val (res1, offset) = html_body_single(tree, end_offset1) |
|
126 |
(res1 ++ res, offset) |
|
127 |
} |
|
128 |
||
129 |
@tailrec |
|
130 |
def html_body_single(xml_tree: XML.Tree, end_offset: Symbol.Offset): (XML.Body, Symbol.Offset) = |
|
131 |
xml_tree match { |
|
132 |
case XML.Wrapped_Elem(markup, _, body) => html_body_single(XML.Elem(markup, body), end_offset) |
|
73056 | 133 |
case XML.Elem(Markup(Markup.ENTITY, props @ Markup.Kind(kind)), body) => |
74677 | 134 |
val (body1, offset) = html_body(body, end_offset) |
73056 | 135 |
if (elements.entity(kind)) { |
74677 | 136 |
entity_link(context, props, body1) match { |
137 |
case Some(link) => (List(link), offset) |
|
138 |
case None => (body1, offset) |
|
73056 | 139 |
} |
140 |
} |
|
74677 | 141 |
else (body1, offset) |
73055 | 142 |
case XML.Elem(Markup(Markup.LANGUAGE, Markup.Name(name)), body) => |
74677 | 143 |
val (body1, offset) = html_body(body, end_offset) |
144 |
(html_class(if (elements.language(name)) name else "", body1), offset) |
|
73055 | 145 |
case XML.Elem(Markup(Markup.MARKDOWN_PARAGRAPH, _), body) => |
74677 | 146 |
val (body1, offset) = html_body(body, end_offset) |
147 |
(List(HTML.par(body1)), offset) |
|
73055 | 148 |
case XML.Elem(Markup(Markup.MARKDOWN_ITEM, _), body) => |
74677 | 149 |
val (body1, offset) = html_body(body, end_offset) |
150 |
(List(HTML.item(body1)), offset) |
|
151 |
case XML.Elem(Markup(Markup.Markdown_Bullet.name, _), text) => |
|
74683
c8327efc7af1
clarified signature: more direct XML.symbol_length;
wenzelm
parents:
74682
diff
changeset
|
152 |
(Nil, end_offset - XML.symbol_length(text)) |
73054 | 153 |
case XML.Elem(Markup.Markdown_List(kind), body) => |
74677 | 154 |
val (body1, offset) = html_body(body, end_offset) |
155 |
if (kind == Markup.ENUMERATE) (List(HTML.enum(body1)), offset) |
|
156 |
else (List(HTML.list(body1)), offset) |
|
73054 | 157 |
case XML.Elem(markup, body) => |
158 |
val name = markup.name |
|
74677 | 159 |
val (body1, offset) = html_body(body, end_offset) |
73054 | 160 |
val html = |
161 |
markup.properties match { |
|
162 |
case Markup.Kind(kind) if kind == Markup.COMMAND || kind == Markup.KEYWORD => |
|
74677 | 163 |
html_class(kind, body1) |
73054 | 164 |
case _ => |
74677 | 165 |
body1 |
73054 | 166 |
} |
167 |
Rendering.foreground.get(name) orElse Rendering.text_color.get(name) match { |
|
74677 | 168 |
case Some(c) => (html_class(c.toString, html), offset) |
169 |
case None => (html_class(name, html), offset) |
|
72957 | 170 |
} |
73054 | 171 |
case XML.Text(text) => |
74682
ce4adcc85f6c
more direct Symbol.length: Symbol.decode is redundant, symbol counts are invariant under it;
wenzelm
parents:
74679
diff
changeset
|
172 |
val offset = end_offset - Symbol.length(text) |
ce4adcc85f6c
more direct Symbol.length: Symbol.decode is redundant, symbol counts are invariant under it;
wenzelm
parents:
74679
diff
changeset
|
173 |
val body = HTML.text(Symbol.decode(text)) |
74677 | 174 |
entity_anchor(context, Text.Range(offset, end_offset), body) match { |
175 |
case Some(body1) => (List(body1), offset) |
|
176 |
case None => (body, offset) |
|
177 |
} |
|
73054 | 178 |
} |
179 |
||
74683
c8327efc7af1
clarified signature: more direct XML.symbol_length;
wenzelm
parents:
74682
diff
changeset
|
180 |
html_body(xml, XML.symbol_length(xml) + 1)._1 |
73054 | 181 |
} |
72957 | 182 |
|
183 |
||
184 |
/* PIDE HTML document */ |
|
185 |
||
186 |
def html_document( |
|
187 |
resources: Resources, |
|
188 |
snapshot: Document.Snapshot, |
|
72962 | 189 |
html_context: HTML_Context, |
73055 | 190 |
elements: Elements, |
72959 | 191 |
plain_text: Boolean = false): HTML_Document = |
72957 | 192 |
{ |
73120
c3589f2dff31
more informative errors: simplify diagnosis of spurious failures reported by users;
wenzelm
parents:
73059
diff
changeset
|
193 |
require(!snapshot.is_outdated, "document snapshot outdated") |
72957 | 194 |
|
195 |
val name = snapshot.node_name |
|
196 |
if (plain_text) { |
|
72962 | 197 |
val title = "File " + Symbol.cartouche_decoded(name.path.file_name) |
72959 | 198 |
val body = HTML.text(snapshot.node.source) |
72962 | 199 |
html_context.html_document(title, body) |
72957 | 200 |
} |
201 |
else { |
|
72962 | 202 |
resources.html_document(snapshot) getOrElse { |
203 |
val title = |
|
204 |
if (name.is_theory) "Theory " + quote(name.theory_base_name) |
|
205 |
else "File " + Symbol.cartouche_decoded(name.path.file_name) |
|
74677 | 206 |
val xml = snapshot.xml_markup(elements = elements.html) |
207 |
val body = make_html(name, elements, (_, _, _) => None, (_, _, _) => None, xml) |
|
72962 | 208 |
html_context.html_document(title, body) |
72957 | 209 |
} |
210 |
} |
|
211 |
} |
|
212 |
||
213 |
||
214 |
||
73718 | 215 |
/** HTML presentation **/ |
72649 | 216 |
|
72648 | 217 |
/* presentation context */ |
218 |
||
219 |
object Context |
|
220 |
{ |
|
221 |
val none: Context = new Context { def enabled: Boolean = false } |
|
222 |
val standard: Context = new Context { def enabled: Boolean = true } |
|
223 |
||
224 |
def dir(path: Path): Context = |
|
225 |
new Context { |
|
226 |
def enabled: Boolean = true |
|
227 |
override def dir(store: Sessions.Store): Path = path |
|
228 |
} |
|
229 |
||
230 |
def make(s: String): Context = |
|
231 |
if (s == ":") standard else dir(Path.explode(s)) |
|
232 |
} |
|
233 |
||
234 |
abstract class Context private |
|
235 |
{ |
|
236 |
def enabled: Boolean |
|
237 |
def enabled(info: Sessions.Info): Boolean = enabled || info.browser_info |
|
238 |
def dir(store: Sessions.Store): Path = store.presentation_dir |
|
239 |
def dir(store: Sessions.Store, info: Sessions.Info): Path = |
|
72670 | 240 |
dir(store) + Path.explode(info.chapter_session) |
72648 | 241 |
} |
242 |
||
243 |
||
72955
942bf91545fa
clarified comments: file-system access is always unsynchronized;
wenzelm
parents:
72882
diff
changeset
|
244 |
/* maintain chapter index */ |
50707
5b2bf7611662
maintain session index on Scala side, for more determistic results;
wenzelm
parents:
diff
changeset
|
245 |
|
51399
6ac3c29a300e
discontinued "isabelle usedir" option -r (reset session path);
wenzelm
parents:
50707
diff
changeset
|
246 |
private val sessions_path = Path.basic(".sessions") |
50707
5b2bf7611662
maintain session index on Scala side, for more determistic results;
wenzelm
parents:
diff
changeset
|
247 |
|
51402 | 248 |
private def read_sessions(dir: Path): List[(String, String)] = |
249 |
{ |
|
51416 | 250 |
val path = dir + sessions_path |
251 |
if (path.is_file) { |
|
252 |
import XML.Decode._ |
|
65344
b99283eed13c
clarified YXML vs. symbol encoding: operate on whole message;
wenzelm
parents:
65089
diff
changeset
|
253 |
list(pair(string, string))(Symbol.decode_yxml(File.read(path))) |
51416 | 254 |
} |
255 |
else Nil |
|
51402 | 256 |
} |
50707
5b2bf7611662
maintain session index on Scala side, for more determistic results;
wenzelm
parents:
diff
changeset
|
257 |
|
73340 | 258 |
private def write_sessions(dir: Path, sessions: List[(String, String)]): Unit = |
51402 | 259 |
{ |
260 |
import XML.Encode._ |
|
261 |
File.write(dir + sessions_path, YXML.string_of_body(list(pair(string, string))(sessions))) |
|
262 |
} |
|
50707
5b2bf7611662
maintain session index on Scala side, for more determistic results;
wenzelm
parents:
diff
changeset
|
263 |
|
73340 | 264 |
def update_chapter_index( |
265 |
browser_info: Path, chapter: String, new_sessions: List[(String, String)]): Unit = |
|
51399
6ac3c29a300e
discontinued "isabelle usedir" option -r (reset session path);
wenzelm
parents:
50707
diff
changeset
|
266 |
{ |
72376 | 267 |
val dir = Isabelle_System.make_directory(browser_info + Path.basic(chapter)) |
50707
5b2bf7611662
maintain session index on Scala side, for more determistic results;
wenzelm
parents:
diff
changeset
|
268 |
|
51399
6ac3c29a300e
discontinued "isabelle usedir" option -r (reset session path);
wenzelm
parents:
50707
diff
changeset
|
269 |
val sessions0 = |
51406
950b897f95bb
proper path -- I/O was hidden due to permissiveness;
wenzelm
parents:
51402
diff
changeset
|
270 |
try { read_sessions(dir) } |
51987 | 271 |
catch { case _: XML.Error => Nil } |
51399
6ac3c29a300e
discontinued "isabelle usedir" option -r (reset session path);
wenzelm
parents:
50707
diff
changeset
|
272 |
|
51402 | 273 |
val sessions = (SortedMap.empty[String, String] ++ sessions0 ++ new_sessions).toList |
65988 | 274 |
write_sessions(dir, sessions) |
51402 | 275 |
|
65988 | 276 |
val title = "Isabelle/" + chapter + " sessions" |
277 |
HTML.write_document(dir, "index.html", |
|
73523
2cd23d587db9
further clarification of Isabelle distribution identification -- avoid odd patching of sources;
wenzelm
parents:
73474
diff
changeset
|
278 |
List(HTML.title(title + Isabelle_System.isabelle_heading())), |
65988 | 279 |
HTML.chapter(title) :: |
280 |
(if (sessions.isEmpty) Nil |
|
281 |
else |
|
65992 | 282 |
List(HTML.div("sessions", |
283 |
List(HTML.description( |
|
65988 | 284 |
sessions.map({ case (name, description) => |
69318 | 285 |
val descr = Symbol.trim_blank_lines(description) |
65989 | 286 |
(List(HTML.link(name + "/index.html", HTML.text(name))), |
69318 | 287 |
if (descr == "") Nil |
288 |
else HTML.break ::: List(HTML.pre(HTML.text(descr)))) }))))))) |
|
51399
6ac3c29a300e
discontinued "isabelle usedir" option -r (reset session path);
wenzelm
parents:
50707
diff
changeset
|
289 |
} |
61372 | 290 |
|
73340 | 291 |
def make_global_index(browser_info: Path): Unit = |
61372 | 292 |
{ |
293 |
if (!(browser_info + Path.explode("index.html")).is_file) { |
|
72375 | 294 |
Isabelle_System.make_directory(browser_info) |
73317 | 295 |
Isabelle_System.copy_file(Path.explode("~~/lib/logo/isabelle.gif"), |
61372 | 296 |
browser_info + Path.explode("isabelle.gif")) |
73528
c337c798f64c
clarified HTML template (see also 04cb7e02ca38): avoid odd patching of sources;
wenzelm
parents:
73523
diff
changeset
|
297 |
val title = "The " + XML.text(Isabelle_System.isabelle_name()) + " Library" |
61372 | 298 |
File.write(browser_info + Path.explode("index.html"), |
73529 | 299 |
HTML.header + |
300 |
""" |
|
73528
c337c798f64c
clarified HTML template (see also 04cb7e02ca38): avoid odd patching of sources;
wenzelm
parents:
73523
diff
changeset
|
301 |
<head> |
73529 | 302 |
""" + HTML.head_meta + """ |
73528
c337c798f64c
clarified HTML template (see also 04cb7e02ca38): avoid odd patching of sources;
wenzelm
parents:
73523
diff
changeset
|
303 |
<title>""" + title + """</title> |
c337c798f64c
clarified HTML template (see also 04cb7e02ca38): avoid odd patching of sources;
wenzelm
parents:
73523
diff
changeset
|
304 |
</head> |
c337c798f64c
clarified HTML template (see also 04cb7e02ca38): avoid odd patching of sources;
wenzelm
parents:
73523
diff
changeset
|
305 |
|
c337c798f64c
clarified HTML template (see also 04cb7e02ca38): avoid odd patching of sources;
wenzelm
parents:
73523
diff
changeset
|
306 |
<body text="#000000" bgcolor="#FFFFFF" link="#0000FF" vlink="#000099" alink="#404040"> |
c337c798f64c
clarified HTML template (see also 04cb7e02ca38): avoid odd patching of sources;
wenzelm
parents:
73523
diff
changeset
|
307 |
<center> |
c337c798f64c
clarified HTML template (see also 04cb7e02ca38): avoid odd patching of sources;
wenzelm
parents:
73523
diff
changeset
|
308 |
<table width="100%" border="0" cellspacing="10" cellpadding="0"> |
c337c798f64c
clarified HTML template (see also 04cb7e02ca38): avoid odd patching of sources;
wenzelm
parents:
73523
diff
changeset
|
309 |
<tr> |
74676 | 310 |
<td width="20%" valign="middle" align="center"><a href="https://isabelle.in.tum.de/"><img align="bottom" src="isabelle.gif" width="100" height="86" alt="[Isabelle]" border="0" /></a></td> |
73528
c337c798f64c
clarified HTML template (see also 04cb7e02ca38): avoid odd patching of sources;
wenzelm
parents:
73523
diff
changeset
|
311 |
|
c337c798f64c
clarified HTML template (see also 04cb7e02ca38): avoid odd patching of sources;
wenzelm
parents:
73523
diff
changeset
|
312 |
<td width="80%" valign="middle" align="center"> |
c337c798f64c
clarified HTML template (see also 04cb7e02ca38): avoid odd patching of sources;
wenzelm
parents:
73523
diff
changeset
|
313 |
<table width="90%" border="0" cellspacing="0" cellpadding="20"> |
c337c798f64c
clarified HTML template (see also 04cb7e02ca38): avoid odd patching of sources;
wenzelm
parents:
73523
diff
changeset
|
314 |
<tr> |
c337c798f64c
clarified HTML template (see also 04cb7e02ca38): avoid odd patching of sources;
wenzelm
parents:
73523
diff
changeset
|
315 |
<td valign="middle" align="center" bgcolor="#AACCCC"><font face="Helvetica,Arial" size="+2">""" + title + """</font></td> |
c337c798f64c
clarified HTML template (see also 04cb7e02ca38): avoid odd patching of sources;
wenzelm
parents:
73523
diff
changeset
|
316 |
</tr> |
c337c798f64c
clarified HTML template (see also 04cb7e02ca38): avoid odd patching of sources;
wenzelm
parents:
73523
diff
changeset
|
317 |
</table> |
c337c798f64c
clarified HTML template (see also 04cb7e02ca38): avoid odd patching of sources;
wenzelm
parents:
73523
diff
changeset
|
318 |
</td> |
c337c798f64c
clarified HTML template (see also 04cb7e02ca38): avoid odd patching of sources;
wenzelm
parents:
73523
diff
changeset
|
319 |
</tr> |
c337c798f64c
clarified HTML template (see also 04cb7e02ca38): avoid odd patching of sources;
wenzelm
parents:
73523
diff
changeset
|
320 |
</table> |
c337c798f64c
clarified HTML template (see also 04cb7e02ca38): avoid odd patching of sources;
wenzelm
parents:
73523
diff
changeset
|
321 |
</center> |
c337c798f64c
clarified HTML template (see also 04cb7e02ca38): avoid odd patching of sources;
wenzelm
parents:
73523
diff
changeset
|
322 |
<hr /> |
74670
b2604cd4d131
recover library_index_content.template from c337c798f64c: required for website/build/main;
wenzelm
parents:
73718
diff
changeset
|
323 |
""" + File.read(Path.explode("~~/lib/html/library_index_content.template")) + |
b2604cd4d131
recover library_index_content.template from c337c798f64c: required for website/build/main;
wenzelm
parents:
73718
diff
changeset
|
324 |
""" |
73528
c337c798f64c
clarified HTML template (see also 04cb7e02ca38): avoid odd patching of sources;
wenzelm
parents:
73523
diff
changeset
|
325 |
</body> |
73529 | 326 |
""" + HTML.footer) |
61372 | 327 |
} |
328 |
} |
|
329 |
||
330 |
||
74677 | 331 |
/* cache */ |
332 |
||
74693 | 333 |
class Entity_Cache private(cache: mutable.Map[Document.Node.Name, List[Entity]]) |
74677 | 334 |
{ |
74693 | 335 |
def get_or_update(node: Document.Node.Name, e: => List[Entity]): List[Entity] = |
74677 | 336 |
cache.getOrElseUpdate(node, e) |
337 |
} |
|
338 |
object Entity_Cache |
|
339 |
{ |
|
340 |
def empty: Entity_Cache = new Entity_Cache(mutable.Map.empty) |
|
341 |
} |
|
342 |
||
72622
830222403681
HTML presentation in Isabelle/Scala, based on theory html exports from Isabelle/ML;
wenzelm
parents:
72600
diff
changeset
|
343 |
/* present session */ |
830222403681
HTML presentation in Isabelle/Scala, based on theory html exports from Isabelle/ML;
wenzelm
parents:
72600
diff
changeset
|
344 |
|
830222403681
HTML presentation in Isabelle/Scala, based on theory html exports from Isabelle/ML;
wenzelm
parents:
72600
diff
changeset
|
345 |
val session_graph_path = Path.explode("session_graph.pdf") |
72959 | 346 |
val readme_path = Path.explode("README.html") |
347 |
val files_path = Path.explode("files") |
|
72622
830222403681
HTML presentation in Isabelle/Scala, based on theory html exports from Isabelle/ML;
wenzelm
parents:
72600
diff
changeset
|
348 |
|
74677 | 349 |
def html_name(name: Document.Node.Name): String = Path.explode(name.theory_base_name).html.implode |
350 |
def html_path(path: Path): String = (files_path + path.squash.html).implode |
|
351 |
||
352 |
def session_relative(deps: Sessions.Deps, session0: String, session1: String): Option[String] = |
|
353 |
{ |
|
354 |
for { |
|
355 |
info0 <- deps.sessions_structure.get(session0) |
|
356 |
info1 <- deps.sessions_structure.get(session1) |
|
357 |
} yield info0.relative_path(info1) |
|
358 |
} |
|
359 |
||
360 |
def node_relative( |
|
361 |
deps: Sessions.Deps, |
|
362 |
session0: String, |
|
363 |
node_name: Document.Node.Name): Option[String] = |
|
364 |
{ |
|
365 |
val session1 = deps(session0).theory_qualifier(node_name) |
|
366 |
session_relative(deps, session0, session1) |
|
367 |
} |
|
72622
830222403681
HTML presentation in Isabelle/Scala, based on theory html exports from Isabelle/ML;
wenzelm
parents:
72600
diff
changeset
|
368 |
|
73042 | 369 |
def theory_link(deps: Sessions.Deps, session0: String, |
74677 | 370 |
name: Document.Node.Name, body: XML.Body, anchor: Option[String] = None): Option[XML.Tree] = |
73042 | 371 |
{ |
372 |
val session1 = deps(session0).theory_qualifier(name) |
|
73057 | 373 |
val info0 = deps.sessions_structure.get(session0) |
374 |
val info1 = deps.sessions_structure.get(session1) |
|
74677 | 375 |
val fragment = if (anchor.isDefined) "#" + anchor.get else "" |
73057 | 376 |
if (info0.isDefined && info1.isDefined) { |
74677 | 377 |
Some(HTML.link(info0.get.relative_path(info1.get) + html_name(name) + fragment, body)) |
73057 | 378 |
} |
379 |
else None |
|
73042 | 380 |
} |
381 |
||
72648 | 382 |
def session_html( |
72962 | 383 |
resources: Resources, |
72648 | 384 |
session: String, |
385 |
deps: Sessions.Deps, |
|
72683 | 386 |
db_context: Sessions.Database_Context, |
72992 | 387 |
progress: Progress = new Progress, |
73022
38528017e4c8
more verbosity for potentially bulky presentation;
wenzelm
parents:
72992
diff
changeset
|
388 |
verbose: Boolean = false, |
72962 | 389 |
html_context: HTML_Context, |
73055 | 390 |
elements: Elements, |
74677 | 391 |
presentation: Context, |
392 |
seen_nodes_cache: Entity_Cache = Entity_Cache.empty): Unit = |
|
61372 | 393 |
{ |
72622
830222403681
HTML presentation in Isabelle/Scala, based on theory html exports from Isabelle/ML;
wenzelm
parents:
72600
diff
changeset
|
394 |
val info = deps.sessions_structure(session) |
830222403681
HTML presentation in Isabelle/Scala, based on theory html exports from Isabelle/ML;
wenzelm
parents:
72600
diff
changeset
|
395 |
val options = info.options |
830222403681
HTML presentation in Isabelle/Scala, based on theory html exports from Isabelle/ML;
wenzelm
parents:
72600
diff
changeset
|
396 |
val base = deps(session) |
830222403681
HTML presentation in Isabelle/Scala, based on theory html exports from Isabelle/ML;
wenzelm
parents:
72600
diff
changeset
|
397 |
|
72683 | 398 |
val session_dir = presentation.dir(db_context.store, info) |
72962 | 399 |
|
400 |
html_context.init_fonts(session_dir) |
|
72622
830222403681
HTML presentation in Isabelle/Scala, based on theory html exports from Isabelle/ML;
wenzelm
parents:
72600
diff
changeset
|
401 |
|
830222403681
HTML presentation in Isabelle/Scala, based on theory html exports from Isabelle/ML;
wenzelm
parents:
72600
diff
changeset
|
402 |
Bytes.write(session_dir + session_graph_path, |
830222403681
HTML presentation in Isabelle/Scala, based on theory html exports from Isabelle/ML;
wenzelm
parents:
72600
diff
changeset
|
403 |
graphview.Graph_File.make_pdf(options, base.session_graph_display)) |
830222403681
HTML presentation in Isabelle/Scala, based on theory html exports from Isabelle/ML;
wenzelm
parents:
72600
diff
changeset
|
404 |
|
72673
8ff7a0e394f9
clarified PDF/HTML presentation, based on pdf blobs from session database (e.g. from earlier builds);
wenzelm
parents:
72670
diff
changeset
|
405 |
val documents = |
72683 | 406 |
for { |
407 |
doc <- info.document_variants |
|
73718 | 408 |
document <- db_context.input_database(session)(Document_Build.read_document(_, _, doc.name)) |
73022
38528017e4c8
more verbosity for potentially bulky presentation;
wenzelm
parents:
72992
diff
changeset
|
409 |
} yield { |
38528017e4c8
more verbosity for potentially bulky presentation;
wenzelm
parents:
72992
diff
changeset
|
410 |
if (verbose) progress.echo("Presenting document " + session + "/" + doc.name) |
38528017e4c8
more verbosity for potentially bulky presentation;
wenzelm
parents:
72992
diff
changeset
|
411 |
Bytes.write(session_dir + doc.path.pdf, document.pdf) |
38528017e4c8
more verbosity for potentially bulky presentation;
wenzelm
parents:
72992
diff
changeset
|
412 |
doc |
38528017e4c8
more verbosity for potentially bulky presentation;
wenzelm
parents:
72992
diff
changeset
|
413 |
} |
72673
8ff7a0e394f9
clarified PDF/HTML presentation, based on pdf blobs from session database (e.g. from earlier builds);
wenzelm
parents:
72670
diff
changeset
|
414 |
|
73041 | 415 |
val view_links = |
72622
830222403681
HTML presentation in Isabelle/Scala, based on theory html exports from Isabelle/ML;
wenzelm
parents:
72600
diff
changeset
|
416 |
{ |
830222403681
HTML presentation in Isabelle/Scala, based on theory html exports from Isabelle/ML;
wenzelm
parents:
72600
diff
changeset
|
417 |
val deps_link = |
830222403681
HTML presentation in Isabelle/Scala, based on theory html exports from Isabelle/ML;
wenzelm
parents:
72600
diff
changeset
|
418 |
HTML.link(session_graph_path, HTML.text("theory dependencies")) |
61372 | 419 |
|
72622
830222403681
HTML presentation in Isabelle/Scala, based on theory html exports from Isabelle/ML;
wenzelm
parents:
72600
diff
changeset
|
420 |
val readme_links = |
830222403681
HTML presentation in Isabelle/Scala, based on theory html exports from Isabelle/ML;
wenzelm
parents:
72600
diff
changeset
|
421 |
if ((info.dir + readme_path).is_file) { |
73317 | 422 |
Isabelle_System.copy_file(info.dir + readme_path, session_dir + readme_path) |
72622
830222403681
HTML presentation in Isabelle/Scala, based on theory html exports from Isabelle/ML;
wenzelm
parents:
72600
diff
changeset
|
423 |
List(HTML.link(readme_path, HTML.text("README"))) |
830222403681
HTML presentation in Isabelle/Scala, based on theory html exports from Isabelle/ML;
wenzelm
parents:
72600
diff
changeset
|
424 |
} |
830222403681
HTML presentation in Isabelle/Scala, based on theory html exports from Isabelle/ML;
wenzelm
parents:
72600
diff
changeset
|
425 |
else Nil |
62971
087e36ce0593
back to exact copy of non-text file (amending dcc8e1d34b18);
wenzelm
parents:
62631
diff
changeset
|
426 |
|
72622
830222403681
HTML presentation in Isabelle/Scala, based on theory html exports from Isabelle/ML;
wenzelm
parents:
72600
diff
changeset
|
427 |
val document_links = |
72673
8ff7a0e394f9
clarified PDF/HTML presentation, based on pdf blobs from session database (e.g. from earlier builds);
wenzelm
parents:
72670
diff
changeset
|
428 |
documents.map(doc => HTML.link(doc.path.pdf, HTML.text(doc.name))) |
72622
830222403681
HTML presentation in Isabelle/Scala, based on theory html exports from Isabelle/ML;
wenzelm
parents:
72600
diff
changeset
|
429 |
|
830222403681
HTML presentation in Isabelle/Scala, based on theory html exports from Isabelle/ML;
wenzelm
parents:
72600
diff
changeset
|
430 |
Library.separate(HTML.break ::: HTML.nl, |
830222403681
HTML presentation in Isabelle/Scala, based on theory html exports from Isabelle/ML;
wenzelm
parents:
72600
diff
changeset
|
431 |
(deps_link :: readme_links ::: document_links). |
830222403681
HTML presentation in Isabelle/Scala, based on theory html exports from Isabelle/ML;
wenzelm
parents:
72600
diff
changeset
|
432 |
map(link => HTML.text("View ") ::: List(link))).flatten |
830222403681
HTML presentation in Isabelle/Scala, based on theory html exports from Isabelle/ML;
wenzelm
parents:
72600
diff
changeset
|
433 |
} |
62971
087e36ce0593
back to exact copy of non-text file (amending dcc8e1d34b18);
wenzelm
parents:
62631
diff
changeset
|
434 |
|
74693 | 435 |
def read_exports(name: Document.Node.Name): List[Entity] = |
74677 | 436 |
{ |
437 |
seen_nodes_cache.get_or_update(name, { |
|
74693 | 438 |
if (Sessions.is_pure(name.theory_base_name)) Nil |
74678 | 439 |
else { |
74677 | 440 |
val session1 = deps(session).theory_qualifier(name) |
441 |
val provider = Export.Provider.database_context(db_context, List(session1), name.theory) |
|
74688 | 442 |
if (Export_Theory.read_theory_parents(provider, name.theory).isDefined) { |
443 |
val theory = Export_Theory.read_theory(provider, session1, name.theory) |
|
74693 | 444 |
theory.entity_iterator.toList |
74688 | 445 |
} |
446 |
else { |
|
447 |
progress.echo_warning("No theory exports for " + name) |
|
74693 | 448 |
Nil |
74677 | 449 |
} |
450 |
} |
|
451 |
}) |
|
452 |
} |
|
453 |
||
454 |
val exports = base.known_theories.map(_._2.name).map(node => node -> read_exports(node)).toMap |
|
74678 | 455 |
val export_ranges = |
456 |
exports.view.mapValues(_.groupBy(entity => |
|
457 |
Text.Range(Position.Offset.get(entity.pos), Position.End_Offset.get(entity.pos)))).toMap |
|
458 |
val export_names = |
|
459 |
exports.map { |
|
460 |
case (node, entities) => node.theory -> entities.map(entity => entity.name -> entity).toMap |
|
461 |
} |
|
74677 | 462 |
|
72989 | 463 |
val theories: List[XML.Body] = |
72962 | 464 |
{ |
465 |
var seen_files = List.empty[(Path, String, Document.Node.Name)] |
|
73056 | 466 |
|
74678 | 467 |
def node_file(node: Document.Node.Name): String = |
74677 | 468 |
if (node.node.endsWith(".thy")) html_name(node) else html_path(Path.explode(node.node)) |
469 |
||
74678 | 470 |
def entity_anchor( |
471 |
context: Entity_Context, range: Symbol.Range, body: XML.Body): Option[XML.Elem] = |
|
74677 | 472 |
{ |
473 |
body match { |
|
474 |
case List(XML.Elem(Markup("span", List("id" -> _)), _)) => None |
|
475 |
case _ => |
|
476 |
Some { |
|
74678 | 477 |
val body1 = |
478 |
if (context.seen_ranges.contains(range)) { |
|
74679 | 479 |
HTML.entity_def(HTML.span(HTML.id(html_context.offset_ref(range)), body)) |
74678 | 480 |
} |
481 |
else HTML.span(body) |
|
74677 | 482 |
export_ranges.get(context.node).flatMap(_.get(range)) match { |
483 |
case Some(entities) => |
|
484 |
entities.map(html_context.export_ref).foldLeft(body1) { |
|
74679 | 485 |
case (elem, id) => |
486 |
HTML.entity_def(HTML.span(HTML.id(id), List(elem))) |
|
74677 | 487 |
} |
488 |
case None => body1 |
|
489 |
} |
|
490 |
} |
|
491 |
} |
|
492 |
} |
|
493 |
||
74678 | 494 |
def entity_ref(theory: String, name: String): Option[String] = |
74677 | 495 |
export_names.get(theory).flatMap(_.get(name)).map(html_context.export_ref) |
496 |
||
74678 | 497 |
def offset_ref(context: Entity_Context, theory: String, props: Properties.T): Option[String] = |
498 |
{ |
|
74677 | 499 |
if (theory == context.node.theory) { |
500 |
for { |
|
501 |
offset <- Position.Def_Offset.unapply(props) |
|
502 |
end_offset <- Position.Def_End_Offset.unapply(props) |
|
503 |
range = Text.Range(offset, end_offset) |
|
504 |
} yield { |
|
505 |
context.seen_ranges += range |
|
506 |
html_context.offset_ref(range) |
|
507 |
} |
|
508 |
} else None |
|
509 |
} |
|
510 |
||
74678 | 511 |
def entity_link( |
512 |
context: Entity_Context, props: Properties.T, body: XML.Body): Option[XML.Elem] = |
|
74677 | 513 |
{ |
514 |
(props, props, props, props) match { |
|
515 |
case (Markup.Kind(Markup.THEORY), Markup.Name(theory), Position.Def_File(thy_file), _) => |
|
73057 | 516 |
val node_name = resources.file_node(Path.explode(thy_file), theory = theory) |
74678 | 517 |
node_relative(deps, session, node_name).map(html_dir => |
518 |
HTML.link(html_dir + html_name(node_name), body)) |
|
74677 | 519 |
case (Markup.Ref(_), Position.Def_File(thy_file), Position.Def_Theory(def_theory), |
520 |
Markup.Name(name)) => |
|
521 |
val theory = if (def_theory.nonEmpty) def_theory else context.node.theory |
|
522 |
val node_name = resources.file_node(Path.explode(thy_file), theory = theory) |
|
523 |
for { |
|
524 |
html_dir <- node_relative(deps, session, node_name) |
|
525 |
html_file = node_file(node_name) |
|
526 |
html_ref <- entity_ref(theory, name).orElse(offset_ref(context, theory, props)) |
|
527 |
} yield { |
|
74679 | 528 |
HTML.entity_ref(HTML.link(html_dir + html_file + "#" + html_ref, body)) |
74677 | 529 |
} |
73056 | 530 |
case _ => None |
531 |
} |
|
74677 | 532 |
} |
73056 | 533 |
|
73215 | 534 |
sealed case class Theory( |
535 |
name: Document.Node.Name, |
|
536 |
command: Command, |
|
537 |
files_html: List[(Path, XML.Tree)], |
|
538 |
html: XML.Tree) |
|
539 |
||
540 |
def read_theory(name: Document.Node.Name): Option[Theory] = |
|
541 |
{ |
|
542 |
progress.expose_interrupt() |
|
73216 | 543 |
if (verbose) progress.echo("Presenting theory " + name) |
73215 | 544 |
|
545 |
for (command <- Build_Job.read_theory(db_context, resources, session, name.theory)) |
|
546 |
yield { |
|
547 |
val snapshot = Document.State.init.snippet(command) |
|
73214 | 548 |
|
73215 | 549 |
val files_html = |
550 |
for { |
|
551 |
(src_path, xml) <- snapshot.xml_markup_blobs(elements = elements.html) |
|
552 |
if xml.nonEmpty |
|
553 |
} |
|
554 |
yield { |
|
555 |
progress.expose_interrupt() |
|
73216 | 556 |
if (verbose) progress.echo("Presenting file " + src_path) |
557 |
||
74677 | 558 |
(src_path, html_context.source( |
559 |
make_html(name, elements, entity_anchor, entity_link, xml))) |
|
73215 | 560 |
} |
561 |
||
562 |
val html = |
|
563 |
html_context.source( |
|
74677 | 564 |
make_html(name, elements, entity_anchor, entity_link, |
565 |
snapshot.xml_markup(elements = elements.html))) |
|
73215 | 566 |
|
567 |
Theory(name, command, files_html, html) |
|
568 |
} |
|
569 |
} |
|
570 |
||
571 |
for (thy <- Par_List.map(read_theory, base.session_theories).flatten) |
|
72669 | 572 |
yield { |
72989 | 573 |
val files = |
73215 | 574 |
for { (src_path, file_html) <- thy.files_html } |
73043 | 575 |
yield { |
74677 | 576 |
val file_name = html_path(src_path) |
72989 | 577 |
|
578 |
seen_files.find(p => p._1 == src_path || p._2 == file_name) match { |
|
73215 | 579 |
case None => seen_files ::= (src_path, file_name, thy.name) |
72989 | 580 |
case Some((_, _, thy_name1)) => |
581 |
error("Incoherent use of file name " + src_path + " as " + quote(file_name) + |
|
73215 | 582 |
" in theory " + thy_name1 + " vs. " + thy.name) |
72989 | 583 |
} |
72669 | 584 |
|
72989 | 585 |
val file_path = session_dir + Path.explode(file_name) |
586 |
html_context.init_fonts(file_path.dir) |
|
587 |
||
588 |
val file_title = "File " + Symbol.cartouche_decoded(src_path.implode_short) |
|
589 |
HTML.write_document(file_path.dir, file_path.file_name, |
|
73215 | 590 |
List(HTML.title(file_title)), List(html_context.head(file_title), file_html)) |
72989 | 591 |
|
592 |
List(HTML.link(file_name, HTML.text(file_title))) |
|
73043 | 593 |
} |
72989 | 594 |
|
73215 | 595 |
val thy_title = "Theory " + thy.name.theory_base_name |
72989 | 596 |
|
73215 | 597 |
HTML.write_document(session_dir, html_name(thy.name), |
598 |
List(HTML.title(thy_title)), List(html_context.head(thy_title), thy.html)) |
|
599 |
||
600 |
List(HTML.link(html_name(thy.name), |
|
601 |
HTML.text(thy.name.theory_base_name) ::: |
|
72989 | 602 |
(if (files.isEmpty) Nil else List(HTML.itemize(files))))) |
72669 | 603 |
} |
72989 | 604 |
} |
61500 | 605 |
|
72622
830222403681
HTML presentation in Isabelle/Scala, based on theory html exports from Isabelle/ML;
wenzelm
parents:
72600
diff
changeset
|
606 |
val title = "Session " + session |
830222403681
HTML presentation in Isabelle/Scala, based on theory html exports from Isabelle/ML;
wenzelm
parents:
72600
diff
changeset
|
607 |
HTML.write_document(session_dir, "index.html", |
73523
2cd23d587db9
further clarification of Isabelle distribution identification -- avoid odd patching of sources;
wenzelm
parents:
73474
diff
changeset
|
608 |
List(HTML.title(title + Isabelle_System.isabelle_heading())), |
73041 | 609 |
html_context.head(title, List(HTML.par(view_links))) :: |
72989 | 610 |
html_context.contents("Theories", theories)) |
61372 | 611 |
} |
50707
5b2bf7611662
maintain session index on Scala side, for more determistic results;
wenzelm
parents:
diff
changeset
|
612 |
} |