src/Pure/Build/browser_info.scala
changeset 79502 c7a98469c0e7
parent 78379 f6ec57648894
child 81555 4eba973e8a7b
equal deleted inserted replaced
79501:bce98b5dfec6 79502:c7a98469c0e7
       
     1 /*  Title:      Pure/Build/browser_info.scala
       
     2     Author:     Makarius
       
     3 
       
     4 HTML/PDF presentation of PIDE document information.
       
     5 */
       
     6 
       
     7 package isabelle
       
     8 
       
     9 
       
    10 import scala.annotation.tailrec
       
    11 import scala.collection.immutable.SortedMap
       
    12 import scala.collection.mutable
       
    13 
       
    14 
       
    15 object Browser_Info {
       
    16   /* browser_info store configuration */
       
    17 
       
    18   object Config {
       
    19     val none: Config = new Config { def enabled: Boolean = false }
       
    20     val standard: Config = new Config { def enabled: Boolean = true }
       
    21 
       
    22     def dir(path: Path): Config =
       
    23       new Config {
       
    24         def enabled: Boolean = true
       
    25         override def presentation_dir(store: Store): Path = path
       
    26       }
       
    27 
       
    28     def make(s: String): Config =
       
    29       if (s == ":") standard else dir(Path.explode(s))
       
    30   }
       
    31 
       
    32   abstract class Config private {
       
    33     def enabled: Boolean
       
    34     def enabled(info: Sessions.Info): Boolean = enabled || info.browser_info
       
    35     def presentation_dir(store: Store): Path = store.presentation_dir
       
    36   }
       
    37 
       
    38 
       
    39   /* meta info within the file-system */
       
    40 
       
    41   object Meta_Info {
       
    42     /* directory */
       
    43 
       
    44     val PATH: Path = Path.explode(".browser_info")
       
    45 
       
    46     def check_directory(dir: Path): Unit = {
       
    47       if (dir.is_dir && !(dir + PATH).is_dir && File.read_dir(dir).nonEmpty) {
       
    48         error("Existing content in " + dir.expand + " lacks " + PATH + " meta info.\n" +
       
    49           "To avoid potential disaster, it has not been changed automatically.\n" +
       
    50           "If this is the intended directory, please move/remove/empty it manually.")
       
    51       }
       
    52     }
       
    53 
       
    54     def init_directory(dir: Path): Path = {
       
    55       check_directory(dir)
       
    56       Isabelle_System.make_directory(dir + PATH)
       
    57       dir
       
    58     }
       
    59 
       
    60     def clean_directory(dir: Path): Path = {
       
    61       check_directory(dir)
       
    62       Isabelle_System.rm_tree(dir)  // guarded by check_directory!
       
    63       Isabelle_System.new_directory(dir + PATH)
       
    64     }
       
    65 
       
    66 
       
    67     /* content */
       
    68 
       
    69     def make_path(dir: Path, name: String): Path =
       
    70       dir + PATH + Path.basic(name)
       
    71 
       
    72     def value(dir: Path, name: String): String = {
       
    73       val path = make_path(dir, name)
       
    74       if (path.is_file) File.read(path) else ""
       
    75     }
       
    76 
       
    77     def change(dir: Path, name: String)(f: String => String): Unit = {
       
    78       val path = make_path(dir, name)
       
    79       val x = value(dir, name)
       
    80       val y =
       
    81         try { f(x) }
       
    82         catch { case ERROR(msg) => error("Failed to change " + path.expand + ":\n" + msg)}
       
    83       if (x != y) File.write(path, y)
       
    84     }
       
    85 
       
    86 
       
    87     /* build_uuid */
       
    88 
       
    89     val BUILD_UUID = "build_uuid"
       
    90 
       
    91     def check_build_uuid(dir: Path, uuid: String): Boolean = {
       
    92       val uuid0 = value(dir, BUILD_UUID)
       
    93       uuid0.nonEmpty && uuid.nonEmpty && uuid0 == uuid
       
    94     }
       
    95 
       
    96     def set_build_uuid(dir: Path, uuid: String): Unit =
       
    97       change(dir, BUILD_UUID)(_ => uuid)
       
    98 
       
    99 
       
   100     /* index */
       
   101 
       
   102     val INDEX = "index.json"
       
   103 
       
   104     object Item {
       
   105       def parse(json: JSON.T): Item = {
       
   106         def err(): Nothing =
       
   107           error("Bad JSON object for item:\n" + JSON.Format.pretty_print(json))
       
   108         val obj = JSON.Object.unapply(json) getOrElse err()
       
   109 
       
   110         val name = JSON.string(obj, "name") getOrElse err()
       
   111         val description = JSON.string(obj, "description") getOrElse ""
       
   112         Item(name, description = Symbol.trim_blank_lines(description))
       
   113       }
       
   114     }
       
   115 
       
   116     sealed case class Item(name: String, description: String = "") {
       
   117       override def toString: String = name
       
   118 
       
   119       def json: JSON.T = JSON.Object("name" -> name, "description" -> description)
       
   120     }
       
   121 
       
   122     object Index {
       
   123       def parse(s: JSON.S, kind: String): Index = {
       
   124         if (s.isEmpty) Index(kind, Nil)
       
   125         else {
       
   126           def err(): Nothing = error("Bad JSON object " + kind + " index:\n" + s)
       
   127 
       
   128           val json = JSON.parse(s)
       
   129           val obj = JSON.Object.unapply(json) getOrElse err()
       
   130 
       
   131           val kind1 = JSON.string(obj, "kind") getOrElse err()
       
   132           val items = JSON.list(obj, "items", x => Some(Item.parse(x))) getOrElse err()
       
   133           if (kind == kind1) Index(kind, items)
       
   134           else error("Expected index kind " + quote(kind) + " but found " + quote(kind1))
       
   135         }
       
   136       }
       
   137     }
       
   138 
       
   139     sealed case class Index(kind: String, items: List[Item]) {
       
   140       def is_empty: Boolean = items.isEmpty
       
   141 
       
   142       def + (item: Item): Index =
       
   143         Index(kind, (item :: items.filterNot(_.name == item.name)).sortBy(_.name))
       
   144 
       
   145       def json: JSON.T = JSON.Object("kind" -> kind, "items" -> items.map(_.json))
       
   146       def print_json: JSON.S = JSON.Format.pretty_print(json)
       
   147     }
       
   148   }
       
   149 
       
   150 
       
   151   /* presentation elements */
       
   152 
       
   153   sealed case class Elements(
       
   154     html: Markup.Elements = Markup.Elements.empty,
       
   155     entity: Markup.Elements = Markup.Elements.empty,
       
   156     language: Markup.Elements = Markup.Elements.empty)
       
   157 
       
   158   val default_elements: Elements =
       
   159     Elements(
       
   160       html = Rendering.foreground_elements ++ Rendering.text_color_elements +
       
   161         Markup.NUMERAL + Markup.COMMENT + Markup.ENTITY + Markup.LANGUAGE +
       
   162         Markup.PATH + Markup.URL,
       
   163       entity = Markup.Elements(Markup.THEORY, Markup.TYPE_NAME, Markup.CONSTANT, Markup.FACT,
       
   164         Markup.CLASS, Markup.LOCALE, Markup.FREE))
       
   165 
       
   166   val extra_elements: Elements =
       
   167     Elements(
       
   168       html = default_elements.html ++ Rendering.markdown_elements,
       
   169       language = Markup.Elements(Markup.Language.DOCUMENT))
       
   170 
       
   171 
       
   172 
       
   173   /** HTML/PDF presentation context **/
       
   174 
       
   175   def context(
       
   176     sessions_structure: Sessions.Structure,
       
   177     elements: Elements = default_elements,
       
   178     root_dir: Path = Path.current,
       
   179     document_info: Document_Info = Document_Info.empty
       
   180   ): Context = new Context(sessions_structure, elements, root_dir, document_info)
       
   181 
       
   182   class Context private[Browser_Info](
       
   183     sessions_structure: Sessions.Structure,
       
   184     val elements: Elements,
       
   185     val root_dir: Path,
       
   186     val document_info: Document_Info
       
   187   ) {
       
   188     /* directory structure and resources */
       
   189 
       
   190     def theory_by_name(session: String, theory: String): Option[Document_Info.Theory] =
       
   191       document_info.theory_by_name(session, theory)
       
   192 
       
   193     def theory_by_file(session: String, file: String): Option[Document_Info.Theory] =
       
   194       document_info.theory_by_file(session, file)
       
   195 
       
   196     def session_chapter(session: String): String =
       
   197       sessions_structure(session).chapter
       
   198 
       
   199     def chapter_dir(session: String): Path =
       
   200       root_dir + Path.basic(session_chapter(session))
       
   201 
       
   202     def session_dir(session: String): Path =
       
   203       chapter_dir(session) + Path.basic(session)
       
   204 
       
   205     def theory_dir(theory: Document_Info.Theory): Path =
       
   206       session_dir(theory.dynamic_session)
       
   207 
       
   208     def theory_html(theory: Document_Info.Theory): Path =
       
   209     {
       
   210       def check(name: String): Option[Path] = {
       
   211         val path = Path.basic(name).html
       
   212         if (Path.eq_case_insensitive(path, Path.index_html)) None
       
   213         else Some(path)
       
   214       }
       
   215       check(theory.print_short) orElse check(theory.name) getOrElse
       
   216         error("Illegal global theory name " + quote(theory.name) +
       
   217           " (conflict with " + Path.index_html + ")")
       
   218     }
       
   219 
       
   220     def file_html(file: String): Path =
       
   221       Path.explode(file).squash.html
       
   222 
       
   223     def smart_html(theory: Document_Info.Theory, file: String): Path =
       
   224       if (File.is_thy(file)) theory_html(theory) else file_html(file)
       
   225 
       
   226 
       
   227     /* HTML content */
       
   228 
       
   229     def head(title: String, rest: XML.Body = Nil): XML.Tree =
       
   230       HTML.div("head", HTML.chapter(title) :: rest)
       
   231 
       
   232     def source(body: XML.Body): XML.Tree = HTML.pre("source", body)
       
   233 
       
   234     def contents(
       
   235       heading: String,
       
   236       items: List[XML.Body],
       
   237       css_class: String = "contents"
       
   238     ) : List[XML.Elem] = {
       
   239       if (items.isEmpty) Nil
       
   240       else List(HTML.div(css_class, List(HTML.section(heading), HTML.itemize(items))))
       
   241     }
       
   242 
       
   243 
       
   244     /* preview PIDE document */
       
   245 
       
   246     lazy val isabelle_css: String = File.read(HTML.isabelle_css)
       
   247 
       
   248     def html_document(title: String, body: XML.Body, fonts_css: String): HTML_Document = {
       
   249       val content =
       
   250         HTML.output_document(
       
   251           List(
       
   252             HTML.style(fonts_css + "\n\n" + isabelle_css),
       
   253             HTML.title(title)),
       
   254           List(HTML.source(body)), css = "", structural = false)
       
   255       HTML_Document(title, content)
       
   256     }
       
   257 
       
   258     def preview_document(
       
   259       snapshot: Document.Snapshot,
       
   260       plain_text: Boolean = false,
       
   261       fonts_css: String = HTML.fonts_css()
       
   262     ): HTML_Document = {
       
   263       require(!snapshot.is_outdated, "document snapshot outdated")
       
   264 
       
   265       val name = snapshot.node_name
       
   266       if (plain_text) {
       
   267         val title = "File " + Symbol.cartouche_decoded(name.file_name)
       
   268         val body = HTML.text(snapshot.node.source)
       
   269         html_document(title, body, fonts_css)
       
   270       }
       
   271       else {
       
   272         Resources.html_document(snapshot) getOrElse {
       
   273           val title =
       
   274             if (name.is_theory) "Theory " + quote(name.theory_base_name)
       
   275             else "File " + Symbol.cartouche_decoded(name.file_name)
       
   276           val xml = snapshot.xml_markup(elements = elements.html)
       
   277           val body = Node_Context.empty.make_html(elements, xml)
       
   278           html_document(title, body, fonts_css)
       
   279         }
       
   280       }
       
   281     }
       
   282 
       
   283 
       
   284     /* maintain presentation structure */
       
   285 
       
   286     def update_chapter(session_name: String, session_description: String): Unit = synchronized {
       
   287       val dir = Meta_Info.init_directory(chapter_dir(session_name))
       
   288       Meta_Info.change(dir, Meta_Info.INDEX) { text =>
       
   289         val index0 = Meta_Info.Index.parse(text, "chapter")
       
   290         val item = Meta_Info.Item(session_name, description = session_description)
       
   291         val index = index0 + item
       
   292 
       
   293         if (index != index0) {
       
   294           val title = "Isabelle/" + session_chapter(session_name) + " sessions"
       
   295           HTML.write_document(dir, "index.html",
       
   296             List(HTML.title(title + Isabelle_System.isabelle_heading())),
       
   297             HTML.chapter(title) ::
       
   298               (if (index.is_empty) Nil
       
   299               else
       
   300                 List(HTML.div("sessions",
       
   301                   List(HTML.description(
       
   302                     index.items.map(item =>
       
   303                       (List(HTML.link(item.name + "/index.html", HTML.text(item.name))),
       
   304                         if (item.description.isEmpty) Nil
       
   305                         else HTML.break ::: List(HTML.pre(HTML.text(item.description)))))))))),
       
   306             root = Some(root_dir))
       
   307         }
       
   308 
       
   309         index.print_json
       
   310       }
       
   311     }
       
   312 
       
   313     def update_root(): Unit = synchronized {
       
   314       Meta_Info.init_directory(root_dir)
       
   315       HTML.init_fonts(root_dir)
       
   316       Isabelle_System.copy_file(Path.explode("~~/lib/logo/isabelle.gif"),
       
   317         root_dir + Path.explode("isabelle.gif"))
       
   318 
       
   319       Meta_Info.change(root_dir, Meta_Info.INDEX) { text =>
       
   320         val index0 = Meta_Info.Index.parse(text, "root")
       
   321         val index = {
       
   322           val items1 =
       
   323             sessions_structure.known_chapters
       
   324               .map(ch => Meta_Info.Item(ch.name, description = ch.description))
       
   325           val items2 = index0.items.filterNot(item => items1.exists(_.name == item.name))
       
   326           index0.copy(items = items1 ::: items2)
       
   327         }
       
   328 
       
   329         if (index != index0) {
       
   330           val title = "The " + XML.text(Isabelle_System.isabelle_name()) + " Library"
       
   331           HTML.write_document(root_dir, "index.html",
       
   332             List(HTML.title(title + Isabelle_System.isabelle_heading())),
       
   333             HTML.chapter(title) ::
       
   334               (if (index.is_empty) Nil
       
   335               else
       
   336                 List(HTML.div("sessions",
       
   337                   List(HTML.description(
       
   338                     index.items.map(item =>
       
   339                       (List(HTML.link(item.name + "/index.html", HTML.text(item.name))),
       
   340                         if (item.description.isEmpty) Nil
       
   341                         else HTML.break ::: List(HTML.pre(HTML.text(item.description)))))))))),
       
   342             root = Some(root_dir))
       
   343         }
       
   344 
       
   345         index.print_json
       
   346       }
       
   347     }
       
   348   }
       
   349 
       
   350   sealed case class HTML_Document(title: String, content: String)
       
   351 
       
   352 
       
   353   /* formal entities */
       
   354 
       
   355   object Theory_Ref {
       
   356     def unapply(props: Properties.T): Option[String] =
       
   357       (props, props) match {
       
   358         case (Markup.Kind(Markup.THEORY), Markup.Name(theory)) => Some(theory)
       
   359         case _ => None
       
   360       }
       
   361   }
       
   362 
       
   363   object Entity_Ref {
       
   364     def unapply(props: Properties.T): Option[(String, String, String)] =
       
   365       (props, props, props, props) match {
       
   366         case (Markup.Entity.Ref.Prop(_), Position.Def_File(file), Markup.Kind(kind), Markup.Name(name))
       
   367         if Path.is_wellformed(file) => Some((file, kind, name))
       
   368         case _ => None
       
   369       }
       
   370   }
       
   371 
       
   372   object Node_Context {
       
   373     val empty: Node_Context = new Node_Context
       
   374 
       
   375     def make(
       
   376       context: Context,
       
   377       session_name: String,
       
   378       theory_name: String,
       
   379       file_name: String,
       
   380       node_dir: Path,
       
   381     ): Node_Context =
       
   382       new Node_Context {
       
   383         private val seen_ranges: mutable.Set[Symbol.Range] = mutable.Set.empty
       
   384 
       
   385         override def make_def(range: Symbol.Range, body: XML.Body): Option[XML.Elem] =
       
   386           body match {
       
   387             case List(XML.Elem(Markup("span", List("id" -> _)), _)) => None
       
   388             case _ =>
       
   389               for (theory <- context.theory_by_name(session_name, theory_name))
       
   390               yield {
       
   391                 val body1 =
       
   392                   if (seen_ranges.contains(range)) {
       
   393                     HTML.entity_def(HTML.span(HTML.id(offset_id(range)), body))
       
   394                   }
       
   395                   else HTML.span(body)
       
   396                 theory.get_defs(file_name, range).foldLeft(body1) {
       
   397                   case (elem, entity) =>
       
   398                     HTML.entity_def(HTML.span(HTML.id(entity.kname), List(elem)))
       
   399                 }
       
   400               }
       
   401           }
       
   402 
       
   403         private def offset_id(range: Text.Range): String =
       
   404           "offset_" + range.start + ".." + range.stop
       
   405 
       
   406         override def make_file_ref(file: String, body: XML.Body): Option[XML.Elem] = {
       
   407           for (theory <- context.theory_by_file(session_name, file))
       
   408           yield {
       
   409             val html_path = context.theory_dir(theory) + context.smart_html(theory, file)
       
   410             val html_link = HTML.relative_href(html_path, base = Some(node_dir))
       
   411             HTML.link(html_link, body)
       
   412           }
       
   413         }
       
   414 
       
   415         override def make_ref(props: Properties.T, body: XML.Body): Option[XML.Elem] = {
       
   416           props match {
       
   417             case Theory_Ref(thy_name) =>
       
   418               for (theory <- context.theory_by_name(session_name, thy_name))
       
   419               yield {
       
   420                 val html_path = context.theory_dir(theory) + context.theory_html(theory)
       
   421                 val html_link = HTML.relative_href(html_path, base = Some(node_dir))
       
   422                 HTML.link(html_link, body)
       
   423               }
       
   424             case Entity_Ref(def_file, kind, name) =>
       
   425               def logical_ref(theory: Document_Info.Theory): Option[String] =
       
   426                 theory.get_def(def_file, kind, name).map(_.kname)
       
   427 
       
   428               def physical_ref(theory: Document_Info.Theory): Option[String] =
       
   429                 props match {
       
   430                   case Position.Def_Range(range) if theory.name == theory_name =>
       
   431                     seen_ranges += range
       
   432                     Some(offset_id(range))
       
   433                   case _ => None
       
   434                 }
       
   435 
       
   436               for {
       
   437                 theory <- context.theory_by_file(session_name, def_file)
       
   438                 html_ref <- logical_ref(theory) orElse physical_ref(theory)
       
   439               }
       
   440               yield {
       
   441                 val html_path = context.theory_dir(theory) + context.smart_html(theory, def_file)
       
   442                 val html_link = HTML.relative_href(html_path, base = Some(node_dir))
       
   443                 HTML.entity_ref(HTML.link(html_link + "#" + html_ref, body))
       
   444               }
       
   445             case _ => None
       
   446           }
       
   447         }
       
   448       }
       
   449   }
       
   450 
       
   451   class Node_Context {
       
   452     def make_def(range: Symbol.Range, body: XML.Body): Option[XML.Elem] = None
       
   453     def make_ref(props: Properties.T, body: XML.Body): Option[XML.Elem] = None
       
   454     def make_file_ref(file: String, body: XML.Body): Option[XML.Elem] = None
       
   455 
       
   456     val div_elements: Set[String] =
       
   457       Set(HTML.div.name, HTML.pre.name, HTML.par.name, HTML.list.name, HTML.`enum`.name,
       
   458         HTML.descr.name)
       
   459 
       
   460     def make_html(elements: Elements, xml: XML.Body): XML.Body = {
       
   461       def html_div(html: XML.Body): Boolean =
       
   462         html exists {
       
   463           case XML.Elem(markup, body) => div_elements.contains(markup.name) || html_div(body)
       
   464           case XML.Text(_) => false
       
   465         }
       
   466 
       
   467       def html_class(c: String, html: XML.Body): XML.Body =
       
   468         if (c == "") html
       
   469         else if (html_div(html)) List(HTML.div(c, html))
       
   470         else List(HTML.span(c, html))
       
   471 
       
   472       def html_body(xml_body: XML.Body, end_offset: Symbol.Offset): (XML.Body, Symbol.Offset) =
       
   473         xml_body.foldRight((List.empty[XML.Tree], end_offset)) { case (tree, (res, end_offset1)) =>
       
   474           val (res1, offset) = html_body_single(tree, end_offset1)
       
   475           (res1 ++ res, offset)
       
   476         }
       
   477 
       
   478       @tailrec
       
   479       def html_body_single(xml_tree: XML.Tree, end_offset: Symbol.Offset): (XML.Body, Symbol.Offset) =
       
   480         xml_tree match {
       
   481           case XML.Wrapped_Elem(markup, _, body) => html_body_single(XML.Elem(markup, body), end_offset)
       
   482           case XML.Elem(Markup(Markup.ENTITY, props @ Markup.Kind(kind)), body) =>
       
   483             val (body1, offset) = html_body(body, end_offset)
       
   484             if (elements.entity(kind)) {
       
   485               make_ref(props, body1) match {
       
   486                 case Some(link) => (List(link), offset)
       
   487                 case None => (body1, offset)
       
   488               }
       
   489             }
       
   490             else (body1, offset)
       
   491           case XML.Elem(Markup.Path(file), body) =>
       
   492             val (body1, offset) = html_body(body, end_offset)
       
   493             make_file_ref(file, body1) match {
       
   494               case Some(link) => (List(link), offset)
       
   495               case None => (body1, offset)
       
   496             }
       
   497           case XML.Elem(Markup.Url(href), body) =>
       
   498             val (body1, offset) = html_body(body, end_offset)
       
   499             (List(HTML.link(href, body1)), offset)
       
   500           case XML.Elem(Markup(Markup.LANGUAGE, Markup.Name(name)), body) =>
       
   501             val (body1, offset) = html_body(body, end_offset)
       
   502             (html_class(if (elements.language(name)) name else "", body1), offset)
       
   503           case XML.Elem(Markup(Markup.MARKDOWN_PARAGRAPH, _), body) =>
       
   504             val (body1, offset) = html_body(body, end_offset)
       
   505             (List(HTML.par(body1)), offset)
       
   506           case XML.Elem(Markup(Markup.MARKDOWN_ITEM, _), body) =>
       
   507             val (body1, offset) = html_body(body, end_offset)
       
   508             (List(HTML.item(body1)), offset)
       
   509           case XML.Elem(Markup(Markup.Markdown_Bullet.name, _), text) =>
       
   510             (Nil, end_offset - XML.symbol_length(text))
       
   511           case XML.Elem(Markup.Markdown_List(kind), body) =>
       
   512             val (body1, offset) = html_body(body, end_offset)
       
   513             if (kind == Markup.ENUMERATE) (List(HTML.`enum`(body1)), offset)
       
   514             else (List(HTML.list(body1)), offset)
       
   515           case XML.Elem(markup, body) =>
       
   516             val name = markup.name
       
   517             val (body1, offset) = html_body(body, end_offset)
       
   518             val html =
       
   519               markup.properties match {
       
   520                 case Markup.Kind(kind) if kind == Markup.COMMAND || kind == Markup.KEYWORD =>
       
   521                   html_class(kind, body1)
       
   522                 case _ =>
       
   523                   body1
       
   524               }
       
   525             Rendering.foreground.get(name) orElse Rendering.text_color.get(name) match {
       
   526               case Some(c) => (html_class(c.toString, html), offset)
       
   527               case None => (html_class(name, html), offset)
       
   528             }
       
   529           case XML.Text(text) =>
       
   530             val offset = end_offset - Symbol.length(text)
       
   531             val body = HTML.text(Symbol.decode(text))
       
   532             make_def(Text.Range(offset, end_offset), body) match {
       
   533               case Some(body1) => (List(body1), offset)
       
   534               case None => (body, offset)
       
   535             }
       
   536         }
       
   537 
       
   538       html_body(xml, XML.symbol_length(xml) + 1)._1
       
   539     }
       
   540   }
       
   541 
       
   542 
       
   543 
       
   544   /** build presentation **/
       
   545 
       
   546   val session_graph_path: Path = Path.explode("session_graph.pdf")
       
   547 
       
   548   def build_session(
       
   549     context: Context,
       
   550     session_context: Export.Session_Context,
       
   551     progress: Progress = new Progress,
       
   552   ): Unit = {
       
   553     progress.expose_interrupt()
       
   554 
       
   555     val session_name = session_context.session_name
       
   556     val session_info = session_context.sessions_structure(session_name)
       
   557 
       
   558     val session_dir = context.session_dir(session_name).expand
       
   559     progress.echo("Presenting " + session_name + " in " + session_dir + " ...")
       
   560 
       
   561     Meta_Info.init_directory(context.chapter_dir(session_name))
       
   562     Meta_Info.clean_directory(session_dir)
       
   563 
       
   564     val session = context.document_info.the_session(session_name)
       
   565 
       
   566     Bytes.write(session_dir + session_graph_path,
       
   567       graphview.Graph_File.make_pdf(session_info.options,
       
   568         session_context.session_base.session_graph_display))
       
   569 
       
   570     val document_variants =
       
   571       for {
       
   572         doc <- session_info.document_variants
       
   573         db <- session_context.session_db()
       
   574         document <- Document_Build.read_document(db, session_name, doc.name)
       
   575       }
       
   576       yield {
       
   577         val doc_path = session_dir + doc.path.pdf
       
   578         if (Path.eq_case_insensitive(doc.path.pdf, session_graph_path)) {
       
   579           error("Illegal document variant " + quote(doc.name) +
       
   580             " (conflict with " + session_graph_path + ")")
       
   581         }
       
   582         progress.echo("Presenting document " + session_name + "/" + doc.name, verbose = true)
       
   583         if (session_info.document_echo) progress.echo("Document at " + doc_path)
       
   584         Bytes.write(doc_path, document.pdf)
       
   585         doc
       
   586       }
       
   587 
       
   588     val document_links = {
       
   589       val link1 = HTML.link(session_graph_path, HTML.text("theory dependencies"))
       
   590       val links2 = document_variants.map(doc => HTML.link(doc.path.pdf, HTML.text(doc.name)))
       
   591       Library.separate(HTML.break ::: HTML.nl,
       
   592         (link1 :: links2).map(link => HTML.text("View ") ::: List(link))).flatten
       
   593     }
       
   594 
       
   595     def present_theory(theory_name: String): XML.Body = {
       
   596       progress.expose_interrupt()
       
   597 
       
   598       def err(): Nothing =
       
   599         error("Missing document information for theory: " + quote(theory_name))
       
   600 
       
   601       val snapshot = Build.read_theory(session_context.theory(theory_name)) getOrElse err()
       
   602       val theory = context.theory_by_name(session_name, theory_name) getOrElse err()
       
   603 
       
   604       progress.echo("Presenting theory " + quote(theory_name), verbose = true)
       
   605 
       
   606       val thy_elements = theory.elements(context.elements)
       
   607 
       
   608       def node_context(file_name: String, node_dir: Path): Node_Context =
       
   609         Node_Context.make(context, session_name, theory_name, file_name, node_dir)
       
   610 
       
   611       val thy_html =
       
   612         context.source(
       
   613           node_context(theory.thy_file, session_dir).
       
   614             make_html(thy_elements, snapshot.xml_markup(elements = thy_elements.html)))
       
   615 
       
   616       val master_dir = Path.explode(snapshot.node_name.master_dir)
       
   617 
       
   618       val files =
       
   619         for {
       
   620           blob_name <- snapshot.node_files.tail
       
   621           xml = snapshot.switch(blob_name).xml_markup(elements = thy_elements.html)
       
   622           if xml.nonEmpty
       
   623         }
       
   624         yield {
       
   625           progress.expose_interrupt()
       
   626 
       
   627           val file = blob_name.node
       
   628           progress.echo("Presenting file " + quote(file), verbose = true)
       
   629 
       
   630           val file_html = session_dir + context.file_html(file)
       
   631           val file_dir = file_html.dir
       
   632           val html_link = HTML.relative_href(file_html, base = Some(session_dir))
       
   633           val html = context.source(node_context(file, file_dir).make_html(thy_elements, xml))
       
   634 
       
   635           val path = Path.explode(file)
       
   636           val src_path = File.relative_path(master_dir, path).getOrElse(path)
       
   637 
       
   638           val file_title = "File " + Symbol.cartouche_decoded(src_path.implode_short)
       
   639           HTML.write_document(file_dir, file_html.file_name,
       
   640             List(HTML.title(file_title)), List(context.head(file_title), html),
       
   641             root = Some(context.root_dir))
       
   642           List(HTML.link(html_link, HTML.text(file_title)))
       
   643         }
       
   644 
       
   645       val thy_title = "Theory " + theory.print_short
       
   646       HTML.write_document(session_dir, context.theory_html(theory).implode,
       
   647         List(HTML.title(thy_title)), List(context.head(thy_title), thy_html),
       
   648         root = Some(context.root_dir))
       
   649 
       
   650       List(HTML.link(context.theory_html(theory),
       
   651         HTML.text(theory.print_short) :::
       
   652         (if (files.isEmpty) Nil else List(HTML.itemize(files)))))
       
   653     }
       
   654 
       
   655     val theories = session.used_theories.map(present_theory)
       
   656 
       
   657     val title = "Session " + session_name
       
   658       HTML.write_document(session_dir, "index.html",
       
   659         List(HTML.title(title + Isabelle_System.isabelle_heading())),
       
   660         context.head(title, List(HTML.par(document_links))) ::
       
   661           context.contents("Theories", theories),
       
   662         root = Some(context.root_dir))
       
   663 
       
   664     Meta_Info.set_build_uuid(session_dir, session.build_uuid)
       
   665 
       
   666     context.update_chapter(session_name, session_info.description)
       
   667   }
       
   668 
       
   669   def build(
       
   670     browser_info: Config,
       
   671     store: Store,
       
   672     deps: Sessions.Deps,
       
   673     sessions: List[String],
       
   674     progress: Progress = new Progress,
       
   675     server: SSH.Server = SSH.no_server
       
   676   ): Unit = {
       
   677     val root_dir = browser_info.presentation_dir(store).absolute
       
   678     progress.echo("Presentation in " + root_dir)
       
   679 
       
   680     using(Export.open_database_context(store, server = server)) { database_context =>
       
   681       val context0 = context(deps.sessions_structure, root_dir = root_dir)
       
   682 
       
   683       val sessions1 =
       
   684         deps.sessions_structure.build_requirements(sessions).filter { session_name =>
       
   685           using(database_context.open_database(session_name)) { session_database =>
       
   686             database_context.store.read_build(session_database.db, session_name) match {
       
   687               case None => false
       
   688               case Some(build) =>
       
   689                 val session_dir = context0.session_dir(session_name)
       
   690                 !Meta_Info.check_build_uuid(session_dir, build.uuid)
       
   691             }
       
   692           }
       
   693         }
       
   694 
       
   695       val context1 =
       
   696         context(deps.sessions_structure, root_dir = root_dir,
       
   697           document_info = Document_Info.read(database_context, deps, sessions1))
       
   698 
       
   699       context1.update_root()
       
   700 
       
   701       Par_List.map({ (session: String) =>
       
   702         using(database_context.open_session(deps.background(session))) { session_context =>
       
   703           build_session(context1, session_context, progress = progress)
       
   704         }
       
   705       }, sessions1)
       
   706     }
       
   707   }
       
   708 }