diff --git a/src/Pure/General/name_space.ML b/src/Pure/General/name_space.ML --- a/src/Pure/General/name_space.ML +++ b/src/Pure/General/name_space.ML @@ -1,649 +1,649 @@ (* Title: Pure/General/name_space.ML Author: Markus Wenzel, TU Muenchen Generic name spaces with declared and hidden entries; no support for absolute addressing. *) type xstring = string; (*external names*) signature NAME_SPACE = sig type entry = {concealed: bool, group: serial option, theory_long_name: string, pos: Position.T, serial: serial} type T val empty: string -> T val kind_of: T -> string val markup: T -> string -> Markup.T val markup_def: T -> string -> Markup.T val get_names: T -> string list val the_entry: T -> string -> entry val the_entry_theory_name: T -> string -> string val entry_ord: T -> string ord val is_concealed: T -> string -> bool val intern: T -> xstring -> string val names_long: bool Config.T val names_short: bool Config.T val names_unique: bool Config.T val extern: Proof.context -> T -> string -> xstring val extern_ord: Proof.context -> T -> string ord val extern_shortest: Proof.context -> T -> string -> xstring val markup_extern: Proof.context -> T -> string -> Markup.T * xstring val pretty: Proof.context -> T -> string -> Pretty.T val completion: Context.generic -> T -> (string -> bool) -> xstring * Position.T -> Completion.T val merge: T * T -> T type naming val get_scopes: naming -> Binding.scope list val get_scope: naming -> Binding.scope option val new_scope: naming -> Binding.scope * naming val restricted: bool -> Position.T -> naming -> naming val private_scope: Binding.scope -> naming -> naming val private: Position.T -> naming -> naming val qualified_scope: Binding.scope -> naming -> naming val qualified: Position.T -> naming -> naming val concealed: naming -> naming val get_group: naming -> serial option val set_group: serial option -> naming -> naming val set_theory_long_name: string -> naming -> naming val new_group: naming -> naming val reset_group: naming -> naming val add_path: string -> naming -> naming val root_path: naming -> naming val parent_path: naming -> naming val mandatory_path: string -> naming -> naming val qualified_path: bool -> binding -> naming -> naming val global_naming: naming val local_naming: naming val transform_naming: naming -> naming -> naming val transform_binding: naming -> binding -> binding val full_name: naming -> binding -> string val base_name: binding -> string val hide: bool -> string -> T -> T val alias: naming -> binding -> string -> T -> T val naming_of: Context.generic -> naming val map_naming: (naming -> naming) -> Context.generic -> Context.generic val declared: T -> string -> bool val declare: Context.generic -> bool -> binding -> T -> string * T type 'a table val change_base: bool -> 'a table -> 'a table val change_ignore: 'a table -> 'a table val space_of_table: 'a table -> T val check_reports: Context.generic -> 'a table -> xstring * Position.T list -> (string * Position.report list) * 'a val check: Context.generic -> 'a table -> xstring * Position.T -> string * 'a val defined: 'a table -> string -> bool val lookup: 'a table -> string -> 'a option val lookup_key: 'a table -> string -> (string * 'a) option val get: 'a table -> string -> 'a val define: Context.generic -> bool -> binding * 'a -> 'a table -> string * 'a table val alias_table: naming -> binding -> string -> 'a table -> 'a table val hide_table: bool -> string -> 'a table -> 'a table val del_table: string -> 'a table -> 'a table val map_table_entry: string -> ('a -> 'a) -> 'a table -> 'a table val fold_table: (string * 'a -> 'b -> 'b) -> 'a table -> 'b -> 'b val dest_table: 'a table -> (string * 'a) list val empty_table: string -> 'a table val merge_tables: 'a table * 'a table -> 'a table val join_tables: (string -> 'a * 'a -> 'a) (*exception Change_Table.SAME*) -> 'a table * 'a table -> 'a table val extern_entries: bool -> Proof.context -> T -> (string * 'a) list -> ((string * xstring) * 'a) list val markup_entries: bool -> Proof.context -> T -> (string * 'a) list -> ((Markup.T * xstring) * 'a) list val extern_table: bool -> Proof.context -> 'a table -> ((string * xstring) * 'a) list val markup_table: bool -> Proof.context -> 'a table -> ((Markup.T * xstring) * 'a) list end; structure Name_Space: NAME_SPACE = struct (** name spaces **) (* datatype entry *) type entry = {concealed: bool, group: serial option, theory_long_name: string, pos: Position.T, serial: serial}; fun entry_markup def kind (name, {pos, theory_long_name, serial, ...}: entry) = Position.make_entity_markup def serial kind (name, pos) - ||> not (#def def) ? cons (Markup.def_theoryN, theory_long_name); + ||> not (#def def orelse theory_long_name = "") ? cons (Markup.def_theoryN, theory_long_name); fun print_entry_ref kind (name, entry) = quote (Markup.markup (entry_markup {def = false} kind (name, entry)) name); fun err_dup kind entry1 entry2 pos = error ("Duplicate " ^ plain_words kind ^ " declaration " ^ print_entry_ref kind entry1 ^ " vs. " ^ print_entry_ref kind entry2 ^ Position.here pos); (* internal names *) type internals = (string list * string list) Change_Table.T; (*xname -> visible, hidden*) fun map_internals f xname : internals -> internals = Change_Table.map_default (xname, ([], [])) f; val del_name = map_internals o apfst o remove (op =); fun del_name_extra name = map_internals (apfst (fn [] => [] | x :: xs => x :: remove (op =) name xs)); val add_name = map_internals o apfst o update (op =); fun hide_name name = map_internals (apsnd (update (op =) name)) name; (* external accesses *) type accesses = (xstring list * xstring list); (*input / output fragments*) type entries = (accesses * entry) Change_Table.T; (*name -> accesses, entry*) (* datatype T *) datatype T = Name_Space of {kind: string, internals: internals, entries: entries}; fun make_name_space (kind, internals, entries) = Name_Space {kind = kind, internals = internals, entries = entries}; fun map_name_space f (Name_Space {kind = kind, internals = internals, entries = entries}) = make_name_space (f (kind, internals, entries)); fun change_base_space begin = map_name_space (fn (kind, internals, entries) => (kind, Change_Table.change_base begin internals, Change_Table.change_base begin entries)); val change_ignore_space = map_name_space (fn (kind, internals, entries) => (kind, Change_Table.change_ignore internals, Change_Table.change_ignore entries)); fun empty kind = make_name_space (kind, Change_Table.empty, Change_Table.empty); fun kind_of (Name_Space {kind, ...}) = kind; fun gen_markup def (Name_Space {kind, entries, ...}) name = (case Change_Table.lookup entries name of NONE => Markup.intensify | SOME (_, entry) => entry_markup def kind (name, entry)); val markup = gen_markup {def = false}; val markup_def = gen_markup {def = true}; fun undefined (space as Name_Space {kind, entries, ...}) bad = let val (prfx, sfx) = (case Long_Name.dest_hidden bad of SOME name => if Change_Table.defined entries name then ("Inaccessible", Markup.markup (markup space name) (quote name)) else ("Undefined", quote name) | NONE => ("Undefined", quote bad)); in prfx ^ " " ^ plain_words kind ^ ": " ^ sfx end; fun get_names (Name_Space {entries, ...}) = Change_Table.fold (cons o #1) entries []; fun the_entry (space as Name_Space {entries, ...}) name = (case Change_Table.lookup entries name of NONE => error (undefined space name) | SOME (_, entry) => entry); fun the_entry_theory_name space name = Long_Name.base_name (#theory_long_name (the_entry space name)); fun entry_ord space = int_ord o apply2 (#serial o the_entry space); fun is_concealed space name = #concealed (the_entry space name) handle ERROR _ => false; (* intern *) fun intern' (Name_Space {internals, ...}) xname = (case the_default ([], []) (Change_Table.lookup internals xname) of ([name], _) => (name, true) | (name :: _, _) => (name, false) | ([], []) => (Long_Name.hidden xname, true) | ([], name' :: _) => (Long_Name.hidden name', true)); val intern = #1 oo intern'; fun get_accesses (Name_Space {entries, ...}) name = (case Change_Table.lookup entries name of NONE => ([], []) | SOME (accesses, _) => accesses); fun is_valid_access (Name_Space {internals, ...}) name xname = (case Change_Table.lookup internals xname of SOME (name' :: _, _) => name = name' | _ => false); (* extern *) val names_long = Config.declare_option_bool ("names_long", \<^here>); val names_short = Config.declare_option_bool ("names_short", \<^here>); val names_unique = Config.declare_option_bool ("names_unique", \<^here>); fun extern ctxt space name = let val names_long = Config.get ctxt names_long; val names_short = Config.get ctxt names_short; val names_unique = Config.get ctxt names_unique; fun valid require_unique xname = let val (name', is_unique) = intern' space xname in name = name' andalso (not require_unique orelse is_unique) end; fun ext [] = if valid false name then name else Long_Name.hidden name | ext (nm :: nms) = if valid names_unique nm then nm else ext nms; in if names_long then name else if names_short then Long_Name.base_name name else ext (#2 (get_accesses space name)) end; fun extern_ord ctxt space = string_ord o apply2 (extern ctxt space); fun extern_shortest ctxt = extern (ctxt |> Config.put names_long false |> Config.put names_short false |> Config.put names_unique false); fun markup_extern ctxt space name = (markup space name, extern ctxt space name); fun pretty ctxt space name = Pretty.mark_str (markup_extern ctxt space name); (* completion *) fun completion context space pred (xname, pos) = Completion.make (xname, pos) (fn completed => let fun result_ord ((pri1, (xname1, (_, name1))), (pri2, (xname2, (_, name2)))) = (case int_ord (pri2, pri1) of EQUAL => (case bool_ord (apply2 (is_some o Long_Name.dest_local) (name2, name1)) of EQUAL => (case int_ord (apply2 Long_Name.qualification (xname1, xname2)) of EQUAL => string_ord (xname1, xname2) | ord => ord) | ord => ord) | ord => ord); val Name_Space {kind, internals, ...} = space; val ext = extern_shortest (Context.proof_of context) space; val full = Name.clean xname = ""; fun complete xname' name = if (completed xname' orelse exists completed (Long_Name.explode xname')) andalso not (is_concealed space name) andalso pred name then let val xname'' = ext name; val pri = (if xname' = xname'' then 1 else 0) + (if completed xname' then 1 else 0); in if xname' <> xname'' andalso full then I else cons (pri, (xname', (kind, name))) end else I; in Change_Table.fold (fn (xname', (name :: _, _)) => complete xname' name | _ => I) internals [] |> sort_distinct result_ord |> map #2 end); (* merge *) fun merge (Name_Space {kind = kind1, internals = internals1, entries = entries1}, Name_Space {kind = kind2, internals = internals2, entries = entries2}) = let val kind' = if kind1 = kind2 then kind1 else error ("Attempt to merge different kinds of name spaces " ^ quote kind1 ^ " vs. " ^ quote kind2); val internals' = (internals1, internals2) |> Change_Table.join (K (fn ((names1, names1'), (names2, names2')) => if pointer_eq (names1, names2) andalso pointer_eq (names1', names2') then raise Change_Table.SAME else (Library.merge (op =) (names1, names2), Library.merge (op =) (names1', names2')))); val entries' = (entries1, entries2) |> Change_Table.join (fn name => fn ((_, entry1), (_, entry2)) => if #serial entry1 = #serial entry2 then raise Change_Table.SAME else err_dup kind' (name, entry1) (name, entry2) Position.none); in make_name_space (kind', internals', entries') end; (** naming context **) (* datatype naming *) datatype naming = Naming of {scopes: Binding.scope list, restricted: (bool * Binding.scope) option, concealed: bool, group: serial option, theory_long_name: string, path: (string * bool) list}; fun make_naming (scopes, restricted, concealed, group, theory_long_name, path) = Naming {scopes = scopes, restricted = restricted, concealed = concealed, group = group, theory_long_name = theory_long_name, path = path}; fun map_naming f (Naming {scopes, restricted, concealed, group, theory_long_name, path}) = make_naming (f (scopes, restricted, concealed, group, theory_long_name, path)); (* scope and access restriction *) fun get_scopes (Naming {scopes, ...}) = scopes; val get_scope = try hd o get_scopes; fun new_scope naming = let val scope = Binding.new_scope (); val naming' = naming |> map_naming (fn (scopes, restricted, concealed, group, theory_long_name, path) => (scope :: scopes, restricted, concealed, group, theory_long_name, path)); in (scope, naming') end; fun restricted_scope strict scope = map_naming (fn (scopes, _, concealed, group, theory_long_name, path) => (scopes, SOME (strict, scope), concealed, group, theory_long_name, path)); fun restricted strict pos naming = (case get_scope naming of SOME scope => restricted_scope strict scope naming | NONE => error ("Missing local scope -- cannot restrict name space accesses" ^ Position.here pos)); val private_scope = restricted_scope true; val private = restricted true; val qualified_scope = restricted_scope false; val qualified = restricted false; val concealed = map_naming (fn (scopes, restricted, _, group, theory_long_name, path) => (scopes, restricted, true, group, theory_long_name, path)); (* additional structural info *) fun set_theory_long_name theory_long_name = map_naming (fn (scopes, restricted, concealed, group, _, path) => (scopes, restricted, concealed, group, theory_long_name, path)); fun get_group (Naming {group, ...}) = group; fun set_group group = map_naming (fn (scopes, restricted, concealed, _, theory_long_name, path) => (scopes, restricted, concealed, group, theory_long_name, path)); fun new_group naming = set_group (SOME (serial ())) naming; val reset_group = set_group NONE; (* name entry path *) fun get_path (Naming {path, ...}) = path; fun map_path f = map_naming (fn (scopes, restricted, concealed, group, theory_long_name, path) => (scopes, restricted, concealed, group, theory_long_name, f path)); fun add_path elems = map_path (fn path => path @ [(elems, false)]); val root_path = map_path (fn _ => []); val parent_path = map_path (perhaps (try (#1 o split_last))); fun mandatory_path elems = map_path (fn path => path @ [(elems, true)]); fun qualified_path mandatory binding = map_path (fn path => path @ Binding.path_of (Binding.qualify_name mandatory binding "")); val global_naming = make_naming ([], NONE, false, NONE, "", []); val local_naming = global_naming |> add_path Long_Name.localN; (* transform *) fun transform_naming (Naming {restricted = restricted', concealed = concealed', ...}) = (case restricted' of SOME (strict, scope) => restricted_scope strict scope | NONE => I) #> concealed' ? concealed; fun transform_binding (Naming {restricted, concealed, ...}) = Binding.restricted restricted #> concealed ? Binding.concealed; (* full name *) fun name_spec naming binding = Binding.name_spec (get_scopes naming) (get_path naming) (transform_binding naming binding); fun full_name naming = name_spec naming #> #spec #> map #1 #> Long_Name.implode; val base_name = full_name global_naming #> Long_Name.base_name; (* accesses *) fun mandatory xs = map_filter (fn (x, true) => SOME x | _ => NONE) xs; fun mandatory_prefixes xs = mandatory xs :: mandatory_prefixes1 xs and mandatory_prefixes1 [] = [] | mandatory_prefixes1 ((x, true) :: xs) = map (cons x) (mandatory_prefixes1 xs) | mandatory_prefixes1 ((x, false) :: xs) = map (cons x) (mandatory_prefixes xs); fun mandatory_suffixes xs = map rev (mandatory_prefixes (rev xs)); fun make_accesses naming binding = (case name_spec naming binding of {restriction = SOME true, ...} => ([], []) | {restriction, spec, ...} => let val restrict = is_some restriction ? filter (fn [_] => false | _ => true); val sfxs = restrict (mandatory_suffixes spec); val pfxs = restrict (mandatory_prefixes spec); in apply2 (map Long_Name.implode) (sfxs @ pfxs, sfxs) end); (* hide *) fun hide fully name space = space |> map_name_space (fn (kind, internals, entries) => let val _ = the_entry space name; val (accs, accs') = get_accesses space name; val xnames = filter (is_valid_access space name) accs; val internals' = internals |> hide_name name |> fold (del_name name) (if fully then xnames else inter (op =) [Long_Name.base_name name] xnames) |> fold (del_name_extra name) accs'; in (kind, internals', entries) end); (* alias *) fun alias naming binding name space = space |> map_name_space (fn (kind, internals, entries) => let val _ = the_entry space name; val (more_accs, more_accs') = make_accesses naming binding; val internals' = internals |> fold (add_name name) more_accs; val entries' = entries |> Change_Table.map_entry name (apfst (fn (accs, accs') => (fold_rev (update op =) more_accs accs, fold_rev (update op =) more_accs' accs'))) in (kind, internals', entries') end); (** context naming **) structure Data_Args = struct type T = naming; val empty = global_naming; fun init _ = local_naming; val merge = #1; end; structure Global_Naming = Theory_Data(Data_Args); structure Local_Naming = Proof_Data(Data_Args); fun naming_of (Context.Theory thy) = Global_Naming.get thy | naming_of (Context.Proof ctxt) = Local_Naming.get ctxt; fun map_naming f (Context.Theory thy) = Context.Theory (Global_Naming.map f thy) | map_naming f (Context.Proof ctxt) = Context.Proof (Local_Naming.map f ctxt); (** entry definition **) (* declaration *) fun declared (Name_Space {entries, ...}) = Change_Table.defined entries; fun declare context strict binding space = let val naming = naming_of context; val Naming {group, theory_long_name, ...} = naming; val {concealed, spec, ...} = name_spec naming binding; val accesses = make_accesses naming binding; val name = Long_Name.implode (map fst spec); val _ = name = "" andalso error (Binding.bad binding); val (proper_pos, pos) = Position.default (Binding.pos_of binding); val entry = {concealed = concealed, group = group, theory_long_name = theory_long_name, pos = pos, serial = serial ()}; val space' = space |> map_name_space (fn (kind, internals, entries) => let val internals' = internals |> fold (add_name name) (#1 accesses); val entries' = (if strict then Change_Table.update_new else Change_Table.update) (name, (accesses, entry)) entries handle Change_Table.DUP dup => err_dup kind (dup, #2 (the (Change_Table.lookup entries dup))) (name, entry) (#pos entry); in (kind, internals', entries') end); val _ = if proper_pos andalso Context_Position.is_reported_generic context pos then Position.report pos (entry_markup {def = true} (kind_of space) (name, entry)) else (); in (name, space') end; (* definition in symbol table *) datatype 'a table = Table of T * 'a Change_Table.T; fun change_base begin (Table (space, tab)) = Table (change_base_space begin space, Change_Table.change_base begin tab); fun change_ignore (Table (space, tab)) = Table (change_ignore_space space, Change_Table.change_ignore tab); fun space_of_table (Table (space, _)) = space; fun check_reports context (Table (space, tab)) (xname, ps) = let val name = intern space xname in (case Change_Table.lookup tab name of SOME x => let val reports = filter (Context_Position.is_reported_generic context) ps |> map (fn pos => (pos, markup space name)); in ((name, reports), x) end | NONE => error (undefined space name ^ Position.here_list ps ^ Completion.markup_report (map (fn pos => completion context space (K true) (xname, pos)) ps))) end; fun check context table (xname, pos) = let val ((name, reports), x) = check_reports context table (xname, [pos]); val _ = Context_Position.reports_generic context reports; in (name, x) end; fun defined (Table (_, tab)) name = Change_Table.defined tab name; fun lookup (Table (_, tab)) name = Change_Table.lookup tab name; fun lookup_key (Table (_, tab)) name = Change_Table.lookup_key tab name; fun get table name = (case lookup_key table name of SOME (_, x) => x | NONE => error (undefined (space_of_table table) name)); fun define context strict (binding, x) (Table (space, tab)) = let val (name, space') = declare context strict binding space; val tab' = Change_Table.update (name, x) tab; in (name, Table (space', tab')) end; (* derived table operations *) fun alias_table naming binding name (Table (space, tab)) = Table (alias naming binding name space, tab); fun hide_table fully name (Table (space, tab)) = Table (hide fully name space, tab); fun del_table name (Table (space, tab)) = let val space' = hide true name space handle ERROR _ => space; val tab' = Change_Table.delete_safe name tab; in Table (space', tab') end; fun map_table_entry name f (Table (space, tab)) = Table (space, Change_Table.map_entry name f tab); fun fold_table f (Table (_, tab)) = Change_Table.fold f tab; fun dest_table (Table (_, tab)) = Change_Table.dest tab; fun empty_table kind = Table (empty kind, Change_Table.empty); fun merge_tables (Table (space1, tab1), Table (space2, tab2)) = Table (merge (space1, space2), Change_Table.merge (K true) (tab1, tab2)); fun join_tables f (Table (space1, tab1), Table (space2, tab2)) = Table (merge (space1, space2), Change_Table.join f (tab1, tab2)); (* present table content *) fun extern_entries verbose ctxt space entries = fold (fn (name, x) => (verbose orelse not (is_concealed space name)) ? cons ((name, extern ctxt space name), x)) entries [] |> sort_by (#2 o #1); fun markup_entries verbose ctxt space entries = extern_entries verbose ctxt space entries |> map (fn ((name, xname), x) => ((markup space name, xname), x)); fun extern_table verbose ctxt (Table (space, tab)) = extern_entries verbose ctxt space (Change_Table.dest tab); fun markup_table verbose ctxt (Table (space, tab)) = markup_entries verbose ctxt space (Change_Table.dest tab); end; diff --git a/src/Pure/Thy/presentation.scala b/src/Pure/Thy/presentation.scala --- a/src/Pure/Thy/presentation.scala +++ b/src/Pure/Thy/presentation.scala @@ -1,654 +1,652 @@ /* Title: Pure/Thy/presentation.scala Author: Makarius HTML presentation of PIDE document content. */ package isabelle import scala.annotation.tailrec import scala.collection.immutable.SortedMap import scala.collection.mutable object Presentation { /** HTML documents **/ /* HTML context */ sealed case class HTML_Document(title: String, content: String) def html_context(cache: Term.Cache = Term.Cache.make()): HTML_Context = new HTML_Context(cache) final class HTML_Context private[Presentation](val cache: Term.Cache) { private val already_presented = Synchronized(Set.empty[String]) def register_presented(nodes: List[Document.Node.Name]): List[Document.Node.Name] = already_presented.change_result(presented => (nodes.filterNot(name => presented.contains(name.theory)), presented ++ nodes.iterator.map(_.theory))) private val theory_cache = Synchronized(Map.empty[String, Export_Theory.Theory]) def cache_theory(thy_name: String, make_thy: => Export_Theory.Theory): Export_Theory.Theory = { theory_cache.change_result(thys => { thys.get(thy_name) match { case Some(thy) => (thy, thys) case None => val thy = make_thy (thy, thys + (thy_name -> thy)) } }) } def head(title: String, rest: XML.Body = Nil): XML.Tree = HTML.div("head", HTML.chapter(title) :: rest) def source(body: XML.Body): XML.Tree = HTML.pre("source", body) def contents(heading: String, items: List[XML.Body], css_class: String = "contents") : List[XML.Elem] = { if (items.isEmpty) Nil else List(HTML.div(css_class, List(HTML.section(heading), HTML.itemize(items)))) } def html_document(title: String, body: XML.Body, fonts_css: String): HTML_Document = { val content = HTML.output_document( List( HTML.style(fonts_css + "\n\n" + File.read(HTML.isabelle_css)), HTML.title(title)), List(HTML.source(body)), css = "", structural = false) HTML_Document(title, content) } } /* presentation elements */ sealed case class Elements( html: Markup.Elements = Markup.Elements.empty, entity: Markup.Elements = Markup.Elements.empty, language: Markup.Elements = Markup.Elements.empty) val elements1: Elements = Elements( html = Rendering.foreground_elements ++ Rendering.text_color_elements + Markup.NUMERAL + Markup.COMMENT + Markup.ENTITY + Markup.LANGUAGE, entity = Markup.Elements(Markup.THEORY, Markup.TYPE_NAME, Markup.CONSTANT, Markup.FACT, Markup.CLASS, Markup.LOCALE, Markup.FREE)) val elements2: Elements = Elements( html = elements1.html ++ Rendering.markdown_elements, language = Markup.Elements(Markup.Language.DOCUMENT)) /* formal entities */ type Entity = Export_Theory.Entity[Export_Theory.No_Content] object Entity_Context { object Theory_Ref { def unapply(props: Properties.T): Option[Document.Node.Name] = (props, props, props) match { case (Markup.Kind(Markup.THEORY), Markup.Name(theory), Position.Def_File(thy_file)) => Some(Resources.file_node(Path.explode(thy_file), theory = theory)) case _ => None } } object Entity_Ref { - def unapply(props: Properties.T): Option[(Path, String, String, String)] = - (props, props, props, props, props) match { - case (Markup.Ref(_), Position.Def_File(def_file), Position.Def_Theory(def_theory), - Markup.Kind(kind), Markup.Name(name)) => + def unapply(props: Properties.T): Option[(Path, Option[String], String, String)] = + (props, props, props, props) match { + case (Markup.Ref(_), Position.Def_File(def_file), Markup.Kind(kind), Markup.Name(name)) => + val def_theory = Position.Def_Theory.unapply(props) Some((Path.explode(def_file), def_theory, kind, name)) case _ => None } } val empty: Entity_Context = new Entity_Context def make( session: String, deps: Sessions.Deps, node: Document.Node.Name, theory_exports: Map[String, Export_Theory.Theory]): Entity_Context = new Entity_Context { private val seen_ranges: mutable.Set[Symbol.Range] = mutable.Set.empty override def make_def(range: Symbol.Range, body: XML.Body): Option[XML.Elem] = { body match { case List(XML.Elem(Markup("span", List("id" -> _)), _)) => None case _ => Some { val entities = theory_exports.get(node.theory).flatMap(_.entity_by_range.get(range)) .getOrElse(Nil) val body1 = if (seen_ranges.contains(range)) { HTML.entity_def(HTML.span(HTML.id(offset_id(range)), body)) } else HTML.span(body) entities.map(_.kname).foldLeft(body1) { case (elem, id) => HTML.entity_def(HTML.span(HTML.id(id), List(elem))) } } } } private def offset_id(range: Text.Range): String = "offset_" + range.start + ".." + range.stop private def physical_ref(thy_name: String, props: Properties.T): Option[String] = { for { range <- Position.Def_Range.unapply(props) if thy_name == node.theory } yield { seen_ranges += range offset_id(range) } } private def logical_ref(thy_name: String, kind: String, name: String): Option[String] = for { thy <- theory_exports.get(thy_name) entity <- thy.entity_by_kind_name.get((kind, name)) } yield entity.kname override def make_ref(props: Properties.T, body: XML.Body): Option[XML.Elem] = { props match { case Theory_Ref(node_name) => node_relative(deps, session, node_name).map(html_dir => HTML.link(html_dir + html_name(node_name), body)) case Entity_Ref(file_path, def_theory, kind, name) => - val proper_thy_name = - proper_string(def_theory) orElse - (if (File.eq(node.path, file_path)) Some(node.theory) else None) for { - thy_name <- proper_thy_name + thy_name <- + def_theory orElse (if (File.eq(node.path, file_path)) Some(node.theory) else None) node_name = Resources.file_node(file_path, theory = thy_name) html_dir <- node_relative(deps, session, node_name) html_file = node_file(node_name) html_ref <- logical_ref(thy_name, kind, name) orElse physical_ref(thy_name, props) } yield { HTML.entity_ref(HTML.link(html_dir + html_file + "#" + html_ref, body)) } case _ => None } } } } class Entity_Context { def make_def(range: Symbol.Range, body: XML.Body): Option[XML.Elem] = None def make_ref(props: Properties.T, body: XML.Body): Option[XML.Elem] = None } /* HTML output */ private val div_elements = Set(HTML.div.name, HTML.pre.name, HTML.par.name, HTML.list.name, HTML.enum.name, HTML.descr.name) def make_html( entity_context: Entity_Context, elements: Elements, xml: XML.Body): XML.Body = { def html_div(html: XML.Body): Boolean = html exists { case XML.Elem(markup, body) => div_elements.contains(markup.name) || html_div(body) case XML.Text(_) => false } def html_class(c: String, html: XML.Body): XML.Body = if (c == "") html else if (html_div(html)) List(HTML.div(c, html)) else List(HTML.span(c, html)) def html_body(xml_body: XML.Body, end_offset: Symbol.Offset): (XML.Body, Symbol.Offset) = xml_body.foldRight((List.empty[XML.Tree], end_offset)) { case (tree, (res, end_offset1)) => val (res1, offset) = html_body_single(tree, end_offset1) (res1 ++ res, offset) } @tailrec def html_body_single(xml_tree: XML.Tree, end_offset: Symbol.Offset): (XML.Body, Symbol.Offset) = xml_tree match { case XML.Wrapped_Elem(markup, _, body) => html_body_single(XML.Elem(markup, body), end_offset) case XML.Elem(Markup(Markup.ENTITY, props @ Markup.Kind(kind)), body) => val (body1, offset) = html_body(body, end_offset) if (elements.entity(kind)) { entity_context.make_ref(props, body1) match { case Some(link) => (List(link), offset) case None => (body1, offset) } } else (body1, offset) case XML.Elem(Markup(Markup.LANGUAGE, Markup.Name(name)), body) => val (body1, offset) = html_body(body, end_offset) (html_class(if (elements.language(name)) name else "", body1), offset) case XML.Elem(Markup(Markup.MARKDOWN_PARAGRAPH, _), body) => val (body1, offset) = html_body(body, end_offset) (List(HTML.par(body1)), offset) case XML.Elem(Markup(Markup.MARKDOWN_ITEM, _), body) => val (body1, offset) = html_body(body, end_offset) (List(HTML.item(body1)), offset) case XML.Elem(Markup(Markup.Markdown_Bullet.name, _), text) => (Nil, end_offset - XML.symbol_length(text)) case XML.Elem(Markup.Markdown_List(kind), body) => val (body1, offset) = html_body(body, end_offset) if (kind == Markup.ENUMERATE) (List(HTML.enum(body1)), offset) else (List(HTML.list(body1)), offset) case XML.Elem(markup, body) => val name = markup.name val (body1, offset) = html_body(body, end_offset) val html = markup.properties match { case Markup.Kind(kind) if kind == Markup.COMMAND || kind == Markup.KEYWORD => html_class(kind, body1) case _ => body1 } Rendering.foreground.get(name) orElse Rendering.text_color.get(name) match { case Some(c) => (html_class(c.toString, html), offset) case None => (html_class(name, html), offset) } case XML.Text(text) => val offset = end_offset - Symbol.length(text) val body = HTML.text(Symbol.decode(text)) entity_context.make_def(Text.Range(offset, end_offset), body) match { case Some(body1) => (List(body1), offset) case None => (body, offset) } } html_body(xml, XML.symbol_length(xml) + 1)._1 } /* PIDE HTML document */ def html_document( snapshot: Document.Snapshot, html_context: HTML_Context, elements: Elements, plain_text: Boolean = false, fonts_css: String = HTML.fonts_css()): HTML_Document = { require(!snapshot.is_outdated, "document snapshot outdated") val name = snapshot.node_name if (plain_text) { val title = "File " + Symbol.cartouche_decoded(name.path.file_name) val body = HTML.text(snapshot.node.source) html_context.html_document(title, body, fonts_css) } else { Resources.html_document(snapshot) getOrElse { val title = if (name.is_theory) "Theory " + quote(name.theory_base_name) else "File " + Symbol.cartouche_decoded(name.path.file_name) val xml = snapshot.xml_markup(elements = elements.html) val body = make_html(Entity_Context.empty, elements, xml) html_context.html_document(title, body, fonts_css) } } } /** HTML presentation **/ /* presentation context */ object Context { val none: Context = new Context { def enabled: Boolean = false } val standard: Context = new Context { def enabled: Boolean = true } def dir(path: Path): Context = new Context { def enabled: Boolean = true override def dir(store: Sessions.Store): Path = path } def make(s: String): Context = if (s == ":") standard else dir(Path.explode(s)) } abstract class Context private { def enabled: Boolean def enabled(info: Sessions.Info): Boolean = enabled || info.browser_info def dir(store: Sessions.Store): Path = store.presentation_dir def dir(store: Sessions.Store, info: Sessions.Info): Path = dir(store) + Path.explode(info.chapter_session) } /* maintain chapter index */ private val sessions_path = Path.basic(".sessions") private def read_sessions(dir: Path): List[(String, String)] = { val path = dir + sessions_path if (path.is_file) { import XML.Decode._ list(pair(string, string))(Symbol.decode_yxml(File.read(path))) } else Nil } def update_chapter( presentation_dir: Path, chapter: String, new_sessions: List[(String, String)]): Unit = { val dir = Isabelle_System.make_directory(presentation_dir + Path.basic(chapter)) val sessions0 = try { read_sessions(dir) } catch { case _: XML.Error => Nil } val sessions = (SortedMap.empty[String, String] ++ sessions0 ++ new_sessions).toList File.write(dir + sessions_path, { import XML.Encode._ YXML.string_of_body(list(pair(string, string))(sessions)) }) val title = "Isabelle/" + chapter + " sessions" HTML.write_document(dir, "index.html", List(HTML.title(title + Isabelle_System.isabelle_heading())), HTML.chapter(title) :: (if (sessions.isEmpty) Nil else List(HTML.div("sessions", List(HTML.description( sessions.map({ case (name, description) => val descr = Symbol.trim_blank_lines(description) (List(HTML.link(name + "/index.html", HTML.text(name))), if (descr == "") Nil else HTML.break ::: List(HTML.pre(HTML.text(descr)))) })))))), base = Some(presentation_dir)) } def update_root(presentation_dir: Path): Unit = { Isabelle_System.make_directory(presentation_dir) HTML.init_fonts(presentation_dir) Isabelle_System.copy_file(Path.explode("~~/lib/logo/isabelle.gif"), presentation_dir + Path.explode("isabelle.gif")) val title = "The " + XML.text(Isabelle_System.isabelle_name()) + " Library" File.write(presentation_dir + Path.explode("index.html"), HTML.header + """ """ + HTML.head_meta + """ """ + title + """
[Isabelle]
""" + title + """

""" + File.read(Path.explode("~~/lib/html/library_index_content.template")) + """ """ + HTML.footer) } /* present session */ val session_graph_path = Path.explode("session_graph.pdf") val readme_path = Path.explode("README.html") val files_path = Path.explode("files") def html_name(name: Document.Node.Name): String = Path.explode(name.theory_base_name).html.implode def html_path(path: Path): String = (files_path + path.squash.html).implode private def node_file(node: Document.Node.Name): String = if (node.node.endsWith(".thy")) html_name(node) else html_path(Path.explode(node.node)) private def session_relative(deps: Sessions.Deps, session0: String, session1: String): Option[String] = { for { info0 <- deps.sessions_structure.get(session0) info1 <- deps.sessions_structure.get(session1) } yield info0.relative_path(info1) } def node_relative( deps: Sessions.Deps, session0: String, node_name: Document.Node.Name): Option[String] = { val session1 = deps(session0).theory_qualifier(node_name) session_relative(deps, session0, session1) } def theory_link(deps: Sessions.Deps, session0: String, name: Document.Node.Name, body: XML.Body, anchor: Option[String] = None): Option[XML.Tree] = { val session1 = deps(session0).theory_qualifier(name) val info0 = deps.sessions_structure.get(session0) val info1 = deps.sessions_structure.get(session1) val fragment = if (anchor.isDefined) "#" + anchor.get else "" if (info0.isDefined && info1.isDefined) { Some(HTML.link(info0.get.relative_path(info1.get) + html_name(name) + fragment, body)) } else None } def session_html( session: String, deps: Sessions.Deps, db_context: Sessions.Database_Context, progress: Progress = new Progress, verbose: Boolean = false, html_context: HTML_Context, session_elements: Elements, presentation: Context): Unit = { val hierarchy = deps.sessions_structure.hierarchy(session) val info = deps.sessions_structure(session) val options = info.options val base = deps(session) def make_session_dir(name: String): Path = Isabelle_System.make_directory( presentation.dir(db_context.store, deps.sessions_structure(name))) val session_dir = make_session_dir(session) val presentation_dir = presentation.dir(db_context.store) Bytes.write(session_dir + session_graph_path, graphview.Graph_File.make_pdf(options, base.session_graph_display)) val documents = for { doc <- info.document_variants document <- db_context.input_database(session)(Document_Build.read_document(_, _, doc.name)) } yield { val doc_path = (session_dir + doc.path.pdf).expand if (verbose) progress.echo("Presenting document " + session + "/" + doc.name) if (options.bool("document_echo")) progress.echo("Document at " + doc_path) Bytes.write(doc_path, document.pdf) doc } val view_links = { val deps_link = HTML.link(session_graph_path, HTML.text("theory dependencies")) val readme_links = if ((info.dir + readme_path).is_file) { Isabelle_System.copy_file(info.dir + readme_path, session_dir + readme_path) List(HTML.link(readme_path, HTML.text("README"))) } else Nil val document_links = documents.map(doc => HTML.link(doc.path.pdf, HTML.text(doc.name))) Library.separate(HTML.break ::: HTML.nl, (deps_link :: readme_links ::: document_links). map(link => HTML.text("View ") ::: List(link))).flatten } val all_used_theories = hierarchy.reverse.flatMap(a => deps(a).used_theories.map(_._1)) val present_theories = html_context.register_presented(all_used_theories) val theory_exports: Map[String, Export_Theory.Theory] = (for (node <- all_used_theories.iterator) yield { val thy_name = node.theory val theory = if (thy_name == Thy_Header.PURE) Export_Theory.no_theory else { html_context.cache_theory(thy_name, { val provider = Export.Provider.database_context(db_context, hierarchy, thy_name) if (Export_Theory.read_theory_parents(provider, thy_name).isDefined) { Export_Theory.read_theory( provider, session, thy_name, cache = html_context.cache) } else Export_Theory.no_theory }) } thy_name -> theory }).toMap def entity_context(name: Document.Node.Name): Entity_Context = Entity_Context.make(session, deps, name, theory_exports) val theories: List[XML.Body] = { sealed case class Seen_File( src_path: Path, file_name: String, thy_session: String, thy_name: Document.Node.Name) { def check(src_path1: Path, file_name1: String, thy_session1: String): Boolean = (src_path == src_path1 || file_name == file_name1) && thy_session == thy_session1 } var seen_files = List.empty[Seen_File] sealed case class Theory( name: Document.Node.Name, command: Command, files_html: List[(Path, XML.Tree)], html: XML.Tree) def read_theory(name: Document.Node.Name): Option[Theory] = { progress.expose_interrupt() for (command <- Build_Job.read_theory(db_context, hierarchy, name.theory)) yield { if (verbose) progress.echo("Presenting theory " + name) val snapshot = Document.State.init.snippet(command) val thy_elements = session_elements.copy(entity = theory_exports(name.theory).others.keySet.foldLeft(session_elements.entity)(_ + _)) val files_html = for { (src_path, xml) <- snapshot.xml_markup_blobs(elements = thy_elements.html) if xml.nonEmpty } yield { progress.expose_interrupt() if (verbose) progress.echo("Presenting file " + src_path) (src_path, html_context.source( make_html(entity_context(name), thy_elements, xml))) } val html = html_context.source( make_html(entity_context(name), thy_elements, snapshot.xml_markup(elements = thy_elements.html))) Theory(name, command, files_html, html) } } (for (thy <- Par_List.map(read_theory, present_theories).flatten) yield { val thy_session = base.theory_qualifier(thy.name) val thy_dir = make_session_dir(thy_session) val files = for { (src_path, file_html) <- thy.files_html } yield { val file_name = html_path(src_path) seen_files.find(_.check(src_path, file_name, thy_session)) match { case None => seen_files ::= Seen_File(src_path, file_name, thy_session, thy.name) case Some(seen_file) => error("Incoherent use of file name " + src_path + " as " + quote(file_name) + " in theory " + seen_file.thy_name + " vs. " + thy.name) } val file_path = thy_dir + Path.explode(file_name) val file_title = "File " + Symbol.cartouche_decoded(src_path.implode_short) HTML.write_document(file_path.dir, file_path.file_name, List(HTML.title(file_title)), List(html_context.head(file_title), file_html), base = Some(presentation_dir)) List(HTML.link(file_name, HTML.text(file_title))) } val thy_title = "Theory " + thy.name.theory_base_name HTML.write_document(thy_dir, html_name(thy.name), List(HTML.title(thy_title)), List(html_context.head(thy_title), thy.html), base = Some(presentation_dir)) if (thy_session == session) { Some( List(HTML.link(html_name(thy.name), HTML.text(thy.name.theory_base_name) ::: (if (files.isEmpty) Nil else List(HTML.itemize(files)))))) } else None }).flatten } val title = "Session " + session HTML.write_document(session_dir, "index.html", List(HTML.title(title + Isabelle_System.isabelle_heading())), html_context.head(title, List(HTML.par(view_links))) :: html_context.contents("Theories", theories), base = Some(presentation_dir)) } }