src/Pure/Thy/present.scala
author wenzelm
Fri, 09 Oct 2015 16:58:24 +0200
changeset 61374 b3c665940d62
parent 61372 cf40b6b1de54
child 61484 dcc8e1d34b18
permissions -rw-r--r--
server-side fonts;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50707
5b2bf7611662 maintain session index on Scala side, for more determistic results;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/Thy/present.scala
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
5b2bf7611662 maintain session index on Scala side, for more determistic results;
wenzelm
parents:
diff changeset
     4
Theory presentation: HTML.
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
61372
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
    10
import java.io.{File => JFile}
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
    11
51402
b05cd411d3d3 include session description in chapter index;
wenzelm
parents: 51399
diff changeset
    12
import scala.collection.immutable.SortedMap
b05cd411d3d3 include session description in chapter index;
wenzelm
parents: 51399
diff changeset
    13
b05cd411d3d3 include session description in chapter index;
wenzelm
parents: 51399
diff changeset
    14
50707
5b2bf7611662 maintain session index on Scala side, for more determistic results;
wenzelm
parents:
diff changeset
    15
object Present
5b2bf7611662 maintain session index on Scala side, for more determistic results;
wenzelm
parents:
diff changeset
    16
{
60077
55cb9462e602 tuned signature, clarified modules;
wenzelm
parents: 51987
diff changeset
    17
  /* session graph */
55cb9462e602 tuned signature, clarified modules;
wenzelm
parents: 51987
diff changeset
    18
55cb9462e602 tuned signature, clarified modules;
wenzelm
parents: 51987
diff changeset
    19
  def session_graph(
55cb9462e602 tuned signature, clarified modules;
wenzelm
parents: 51987
diff changeset
    20
    parent_session: String,
55cb9462e602 tuned signature, clarified modules;
wenzelm
parents: 51987
diff changeset
    21
    parent_loaded: String => Boolean,
55cb9462e602 tuned signature, clarified modules;
wenzelm
parents: 51987
diff changeset
    22
    deps: List[Thy_Info.Dep]): Graph_Display.Graph =
55cb9462e602 tuned signature, clarified modules;
wenzelm
parents: 51987
diff changeset
    23
  {
55cb9462e602 tuned signature, clarified modules;
wenzelm
parents: 51987
diff changeset
    24
    val parent_session_node =
55cb9462e602 tuned signature, clarified modules;
wenzelm
parents: 51987
diff changeset
    25
      Graph_Display.Node("[" + parent_session + "]", "session." + parent_session)
55cb9462e602 tuned signature, clarified modules;
wenzelm
parents: 51987
diff changeset
    26
55cb9462e602 tuned signature, clarified modules;
wenzelm
parents: 51987
diff changeset
    27
    def node(name: Document.Node.Name): Graph_Display.Node =
55cb9462e602 tuned signature, clarified modules;
wenzelm
parents: 51987
diff changeset
    28
      if (parent_loaded(name.theory)) parent_session_node
55cb9462e602 tuned signature, clarified modules;
wenzelm
parents: 51987
diff changeset
    29
      else Graph_Display.Node(name.theory, "theory." + name.theory)
55cb9462e602 tuned signature, clarified modules;
wenzelm
parents: 51987
diff changeset
    30
55cb9462e602 tuned signature, clarified modules;
wenzelm
parents: 51987
diff changeset
    31
    (Graph_Display.empty_graph /: deps) {
55cb9462e602 tuned signature, clarified modules;
wenzelm
parents: 51987
diff changeset
    32
      case (g, dep) =>
55cb9462e602 tuned signature, clarified modules;
wenzelm
parents: 51987
diff changeset
    33
        if (parent_loaded(dep.name.theory)) g
55cb9462e602 tuned signature, clarified modules;
wenzelm
parents: 51987
diff changeset
    34
        else {
55cb9462e602 tuned signature, clarified modules;
wenzelm
parents: 51987
diff changeset
    35
          val a = node(dep.name)
55cb9462e602 tuned signature, clarified modules;
wenzelm
parents: 51987
diff changeset
    36
          val bs = dep.header.imports.map({ case (name, _) => node(name) })
55cb9462e602 tuned signature, clarified modules;
wenzelm
parents: 51987
diff changeset
    37
          ((g /: (a :: bs))(_.default_node(_, Nil)) /: bs)(_.add_edge(_, a))
55cb9462e602 tuned signature, clarified modules;
wenzelm
parents: 51987
diff changeset
    38
        }
55cb9462e602 tuned signature, clarified modules;
wenzelm
parents: 51987
diff changeset
    39
    }
55cb9462e602 tuned signature, clarified modules;
wenzelm
parents: 51987
diff changeset
    40
  }
55cb9462e602 tuned signature, clarified modules;
wenzelm
parents: 51987
diff changeset
    41
55cb9462e602 tuned signature, clarified modules;
wenzelm
parents: 51987
diff changeset
    42
51399
6ac3c29a300e discontinued "isabelle usedir" option -r (reset session path);
wenzelm
parents: 50707
diff changeset
    43
  /* maintain chapter index -- NOT thread-safe */
50707
5b2bf7611662 maintain session index on Scala side, for more determistic results;
wenzelm
parents:
diff changeset
    44
5b2bf7611662 maintain session index on Scala side, for more determistic results;
wenzelm
parents:
diff changeset
    45
  private val index_path = Path.basic("index.html")
51399
6ac3c29a300e discontinued "isabelle usedir" option -r (reset session path);
wenzelm
parents: 50707
diff changeset
    46
  private val sessions_path = Path.basic(".sessions")
50707
5b2bf7611662 maintain session index on Scala side, for more determistic results;
wenzelm
parents:
diff changeset
    47
51402
b05cd411d3d3 include session description in chapter index;
wenzelm
parents: 51399
diff changeset
    48
  private def read_sessions(dir: Path): List[(String, String)] =
b05cd411d3d3 include session description in chapter index;
wenzelm
parents: 51399
diff changeset
    49
  {
51416
e2505a192a7c do not absorb I/O errors;
wenzelm
parents: 51406
diff changeset
    50
    val path = dir + sessions_path
e2505a192a7c do not absorb I/O errors;
wenzelm
parents: 51406
diff changeset
    51
    if (path.is_file) {
e2505a192a7c do not absorb I/O errors;
wenzelm
parents: 51406
diff changeset
    52
      import XML.Decode._
e2505a192a7c do not absorb I/O errors;
wenzelm
parents: 51406
diff changeset
    53
      list(pair(string, string))(YXML.parse_body(File.read(path)))
e2505a192a7c do not absorb I/O errors;
wenzelm
parents: 51406
diff changeset
    54
    }
e2505a192a7c do not absorb I/O errors;
wenzelm
parents: 51406
diff changeset
    55
    else Nil
51402
b05cd411d3d3 include session description in chapter index;
wenzelm
parents: 51399
diff changeset
    56
  }
50707
5b2bf7611662 maintain session index on Scala side, for more determistic results;
wenzelm
parents:
diff changeset
    57
51402
b05cd411d3d3 include session description in chapter index;
wenzelm
parents: 51399
diff changeset
    58
  private def write_sessions(dir: Path, sessions: List[(String, String)])
b05cd411d3d3 include session description in chapter index;
wenzelm
parents: 51399
diff changeset
    59
  {
b05cd411d3d3 include session description in chapter index;
wenzelm
parents: 51399
diff changeset
    60
    import XML.Encode._
b05cd411d3d3 include session description in chapter index;
wenzelm
parents: 51399
diff changeset
    61
    File.write(dir + sessions_path, YXML.string_of_body(list(pair(string, string))(sessions)))
b05cd411d3d3 include session description in chapter index;
wenzelm
parents: 51399
diff changeset
    62
  }
50707
5b2bf7611662 maintain session index on Scala side, for more determistic results;
wenzelm
parents:
diff changeset
    63
61372
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
    64
  def update_chapter_index(browser_info: Path, chapter: String, new_sessions: List[(String, String)])
51399
6ac3c29a300e discontinued "isabelle usedir" option -r (reset session path);
wenzelm
parents: 50707
diff changeset
    65
  {
61372
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
    66
    val dir = browser_info + Path.basic(chapter)
51399
6ac3c29a300e discontinued "isabelle usedir" option -r (reset session path);
wenzelm
parents: 50707
diff changeset
    67
    Isabelle_System.mkdirs(dir)
50707
5b2bf7611662 maintain session index on Scala side, for more determistic results;
wenzelm
parents:
diff changeset
    68
51399
6ac3c29a300e discontinued "isabelle usedir" option -r (reset session path);
wenzelm
parents: 50707
diff changeset
    69
    val sessions0 =
51406
950b897f95bb proper path -- I/O was hidden due to permissiveness;
wenzelm
parents: 51402
diff changeset
    70
      try { read_sessions(dir) }
51987
7d8e0e3c553b tuned signature;
wenzelm
parents: 51416
diff changeset
    71
      catch { case _: XML.Error => Nil }
51399
6ac3c29a300e discontinued "isabelle usedir" option -r (reset session path);
wenzelm
parents: 50707
diff changeset
    72
51402
b05cd411d3d3 include session description in chapter index;
wenzelm
parents: 51399
diff changeset
    73
    val sessions = (SortedMap.empty[String, String] ++ sessions0 ++ new_sessions).toList
b05cd411d3d3 include session description in chapter index;
wenzelm
parents: 51399
diff changeset
    74
51399
6ac3c29a300e discontinued "isabelle usedir" option -r (reset session path);
wenzelm
parents: 50707
diff changeset
    75
    write_sessions(dir, sessions)
6ac3c29a300e discontinued "isabelle usedir" option -r (reset session path);
wenzelm
parents: 50707
diff changeset
    76
    File.write(dir + index_path, HTML.chapter_index(chapter, sessions))
6ac3c29a300e discontinued "isabelle usedir" option -r (reset session path);
wenzelm
parents: 50707
diff changeset
    77
  }
61372
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
    78
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
    79
  def make_global_index(browser_info: Path)
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
    80
  {
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
    81
    if (!(browser_info + Path.explode("index.html")).is_file) {
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
    82
      Isabelle_System.mkdirs(browser_info)
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
    83
      File.copy(Path.explode("~~/lib/logo/isabelle.gif"),
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
    84
        browser_info + Path.explode("isabelle.gif"))
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
    85
      File.write(browser_info + Path.explode("index.html"),
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
    86
        File.read(Path.explode("~~/lib/html/library_index_header.template")) +
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
    87
        File.read(Path.explode("~~/lib/html/library_index_content.template")) +
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
    88
        File.read(Path.explode("~~/lib/html/library_index_footer.template")))
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
    89
    }
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
    90
  }
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
    91
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
    92
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
    93
  /* finish session */
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
    94
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
    95
  def finish(
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
    96
    progress: Progress,
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
    97
    browser_info: Path,
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
    98
    graph_file: JFile,
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
    99
    info: Build.Session_Info,
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
   100
    name: String)
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
   101
  {
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
   102
    val session_prefix = browser_info + Path.basic(info.chapter) + Path.basic(name)
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
   103
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
   104
    if (info.options.bool("browser_info")) {
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
   105
      Isabelle_System.mkdirs(session_prefix)
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
   106
      File.copy(graph_file, (session_prefix + Path.basic("session_graph.pdf")).file)
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
   107
      File.copy(Path.explode("~~/etc/isabelle.css"), session_prefix)
61374
b3c665940d62 server-side fonts;
wenzelm
parents: 61372
diff changeset
   108
      File.copy(Path.explode("~~/lib/fonts/IsabelleText.ttf"), session_prefix)
b3c665940d62 server-side fonts;
wenzelm
parents: 61372
diff changeset
   109
      File.copy(Path.explode("~~/lib/fonts/IsabelleTextBold.ttf"), session_prefix)
61372
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
   110
    }
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
   111
  }
50707
5b2bf7611662 maintain session index on Scala side, for more determistic results;
wenzelm
parents:
diff changeset
   112
}