src/Pure/Thy/document_marker.ML
author wenzelm
Sun, 17 Mar 2019 20:03:55 +0100
changeset 69916 3235ecdcd884
parent 69891 def3ec9cdb7e
child 69962 82e945d472d5
permissions -rw-r--r--
more meta data from "dcterms" (superset of "dc");
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
69887
b9985133805d added semantic document markers;
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/Thy/document_marker.ML
b9985133805d added semantic document markers;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
b9985133805d added semantic document markers;
wenzelm
parents:
diff changeset
     3
b9985133805d added semantic document markers;
wenzelm
parents:
diff changeset
     4
Document markers: declarations on the presentation context.
b9985133805d added semantic document markers;
wenzelm
parents:
diff changeset
     5
*)
b9985133805d added semantic document markers;
wenzelm
parents:
diff changeset
     6
b9985133805d added semantic document markers;
wenzelm
parents:
diff changeset
     7
signature DOCUMENT_MARKER =
b9985133805d added semantic document markers;
wenzelm
parents:
diff changeset
     8
sig
b9985133805d added semantic document markers;
wenzelm
parents:
diff changeset
     9
  type marker = Proof.context -> Proof.context
b9985133805d added semantic document markers;
wenzelm
parents:
diff changeset
    10
  val setup: binding -> 'a context_parser -> ('a -> marker) -> theory -> theory
69889
be04e9a053a7 markup and document markers for some meta data from "Dublin Core Metadata Element Set";
wenzelm
parents: 69887
diff changeset
    11
  val setup0: binding -> 'a parser -> ('a -> marker) -> theory -> theory
69887
b9985133805d added semantic document markers;
wenzelm
parents:
diff changeset
    12
  val evaluate: Input.source -> marker
69891
def3ec9cdb7e document markers are formal comments, and may thus occur anywhere in the command-span;
wenzelm
parents: 69890
diff changeset
    13
  val legacy_tag: string -> Input.source
69887
b9985133805d added semantic document markers;
wenzelm
parents:
diff changeset
    14
end;
b9985133805d added semantic document markers;
wenzelm
parents:
diff changeset
    15
b9985133805d added semantic document markers;
wenzelm
parents:
diff changeset
    16
structure Document_Marker: DOCUMENT_MARKER =
b9985133805d added semantic document markers;
wenzelm
parents:
diff changeset
    17
struct
b9985133805d added semantic document markers;
wenzelm
parents:
diff changeset
    18
b9985133805d added semantic document markers;
wenzelm
parents:
diff changeset
    19
(* theory data *)
b9985133805d added semantic document markers;
wenzelm
parents:
diff changeset
    20
b9985133805d added semantic document markers;
wenzelm
parents:
diff changeset
    21
type marker = Proof.context -> Proof.context;
b9985133805d added semantic document markers;
wenzelm
parents:
diff changeset
    22
b9985133805d added semantic document markers;
wenzelm
parents:
diff changeset
    23
structure Markers = Theory_Data
b9985133805d added semantic document markers;
wenzelm
parents:
diff changeset
    24
(
b9985133805d added semantic document markers;
wenzelm
parents:
diff changeset
    25
  type T = (Token.src -> Proof.context -> Proof.context) Name_Space.table;
b9985133805d added semantic document markers;
wenzelm
parents:
diff changeset
    26
  val empty : T = Name_Space.empty_table "document_marker";
b9985133805d added semantic document markers;
wenzelm
parents:
diff changeset
    27
  val extend = I;
b9985133805d added semantic document markers;
wenzelm
parents:
diff changeset
    28
  val merge = Name_Space.merge_tables;
b9985133805d added semantic document markers;
wenzelm
parents:
diff changeset
    29
);
b9985133805d added semantic document markers;
wenzelm
parents:
diff changeset
    30
b9985133805d added semantic document markers;
wenzelm
parents:
diff changeset
    31
val get_markers = Markers.get o Proof_Context.theory_of;
b9985133805d added semantic document markers;
wenzelm
parents:
diff changeset
    32
b9985133805d added semantic document markers;
wenzelm
parents:
diff changeset
    33
fun check ctxt = Name_Space.check (Context.Proof ctxt) (get_markers ctxt);
b9985133805d added semantic document markers;
wenzelm
parents:
diff changeset
    34
b9985133805d added semantic document markers;
wenzelm
parents:
diff changeset
    35
fun setup name scan body thy =
b9985133805d added semantic document markers;
wenzelm
parents:
diff changeset
    36
  let
b9985133805d added semantic document markers;
wenzelm
parents:
diff changeset
    37
    fun marker src ctxt =
b9985133805d added semantic document markers;
wenzelm
parents:
diff changeset
    38
      let val (x, ctxt') = Token.syntax scan src ctxt
b9985133805d added semantic document markers;
wenzelm
parents:
diff changeset
    39
      in body x ctxt' end;
b9985133805d added semantic document markers;
wenzelm
parents:
diff changeset
    40
  in thy |> Markers.map (Name_Space.define (Context.Theory thy) true (name, marker) #> #2) end;
b9985133805d added semantic document markers;
wenzelm
parents:
diff changeset
    41
69889
be04e9a053a7 markup and document markers for some meta data from "Dublin Core Metadata Element Set";
wenzelm
parents: 69887
diff changeset
    42
fun setup0 name scan = setup name (Scan.lift scan);
be04e9a053a7 markup and document markers for some meta data from "Dublin Core Metadata Element Set";
wenzelm
parents: 69887
diff changeset
    43
69887
b9985133805d added semantic document markers;
wenzelm
parents:
diff changeset
    44
b9985133805d added semantic document markers;
wenzelm
parents:
diff changeset
    45
(* evaluate inner syntax *)
b9985133805d added semantic document markers;
wenzelm
parents:
diff changeset
    46
b9985133805d added semantic document markers;
wenzelm
parents:
diff changeset
    47
val parse_marker = Parse.token Parse.liberal_name ::: Parse.!!! Parse.args;
b9985133805d added semantic document markers;
wenzelm
parents:
diff changeset
    48
b9985133805d added semantic document markers;
wenzelm
parents:
diff changeset
    49
fun evaluate input ctxt =
b9985133805d added semantic document markers;
wenzelm
parents:
diff changeset
    50
  let
69891
def3ec9cdb7e document markers are formal comments, and may thus occur anywhere in the command-span;
wenzelm
parents: 69890
diff changeset
    51
    val body =
def3ec9cdb7e document markers are formal comments, and may thus occur anywhere in the command-span;
wenzelm
parents: 69890
diff changeset
    52
      Input.source_explode input
def3ec9cdb7e document markers are formal comments, and may thus occur anywhere in the command-span;
wenzelm
parents: 69890
diff changeset
    53
      |> drop_prefix (fn (s, _) => s = Symbol.marker)
def3ec9cdb7e document markers are formal comments, and may thus occur anywhere in the command-span;
wenzelm
parents: 69890
diff changeset
    54
      |> Symbol_Pos.cartouche_content;
69887
b9985133805d added semantic document markers;
wenzelm
parents:
diff changeset
    55
    val markers =
69891
def3ec9cdb7e document markers are formal comments, and may thus occur anywhere in the command-span;
wenzelm
parents: 69890
diff changeset
    56
      (case Token.read_body (Thy_Header.get_keywords' ctxt) (Parse.list parse_marker) body of
69887
b9985133805d added semantic document markers;
wenzelm
parents:
diff changeset
    57
        SOME res => res
69891
def3ec9cdb7e document markers are formal comments, and may thus occur anywhere in the command-span;
wenzelm
parents: 69890
diff changeset
    58
      | NONE => error ("Bad input" ^ Position.here (Input.pos_of input)));
69887
b9985133805d added semantic document markers;
wenzelm
parents:
diff changeset
    59
  in fold (fn src => #2 (check ctxt (Token.name_of_src src)) src) markers ctxt end;
b9985133805d added semantic document markers;
wenzelm
parents:
diff changeset
    60
b9985133805d added semantic document markers;
wenzelm
parents:
diff changeset
    61
b9985133805d added semantic document markers;
wenzelm
parents:
diff changeset
    62
(* concrete markers *)
b9985133805d added semantic document markers;
wenzelm
parents:
diff changeset
    63
69889
be04e9a053a7 markup and document markers for some meta data from "Dublin Core Metadata Element Set";
wenzelm
parents: 69887
diff changeset
    64
fun meta_data markup arg ctxt =
be04e9a053a7 markup and document markers for some meta data from "Dublin Core Metadata Element Set";
wenzelm
parents: 69887
diff changeset
    65
  (Context_Position.report_text ctxt (Position.thread_data ()) markup arg; ctxt);
be04e9a053a7 markup and document markers for some meta data from "Dublin Core Metadata Element Set";
wenzelm
parents: 69887
diff changeset
    66
69887
b9985133805d added semantic document markers;
wenzelm
parents:
diff changeset
    67
val _ =
b9985133805d added semantic document markers;
wenzelm
parents:
diff changeset
    68
  Theory.setup
69889
be04e9a053a7 markup and document markers for some meta data from "Dublin Core Metadata Element Set";
wenzelm
parents: 69887
diff changeset
    69
    (setup0 (Binding.make ("tag", \<^here>)) Parse.name Document_Source.update_tags #>
be04e9a053a7 markup and document markers for some meta data from "Dublin Core Metadata Element Set";
wenzelm
parents: 69887
diff changeset
    70
     setup0 (Binding.make ("title", \<^here>)) Parse.embedded (meta_data Markup.meta_title) #>
be04e9a053a7 markup and document markers for some meta data from "Dublin Core Metadata Element Set";
wenzelm
parents: 69887
diff changeset
    71
     setup0 (Binding.make ("creator", \<^here>)) Parse.embedded (meta_data Markup.meta_creator) #>
be04e9a053a7 markup and document markers for some meta data from "Dublin Core Metadata Element Set";
wenzelm
parents: 69887
diff changeset
    72
     setup0 (Binding.make ("contributor", \<^here>)) Parse.embedded (meta_data Markup.meta_contributor) #>
be04e9a053a7 markup and document markers for some meta data from "Dublin Core Metadata Element Set";
wenzelm
parents: 69887
diff changeset
    73
     setup0 (Binding.make ("date", \<^here>)) Parse.embedded (meta_data Markup.meta_date) #>
69890
cb643a1a5313 PIDE markup for spell-checking;
wenzelm
parents: 69889
diff changeset
    74
     setup0 (Binding.make ("description", \<^here>)) (Parse.input Parse.embedded)
cb643a1a5313 PIDE markup for spell-checking;
wenzelm
parents: 69889
diff changeset
    75
      (fn source => fn ctxt =>
cb643a1a5313 PIDE markup for spell-checking;
wenzelm
parents: 69889
diff changeset
    76
        let
cb643a1a5313 PIDE markup for spell-checking;
wenzelm
parents: 69889
diff changeset
    77
          val (arg, pos) = Input.source_content source;
cb643a1a5313 PIDE markup for spell-checking;
wenzelm
parents: 69889
diff changeset
    78
          val _ = Context_Position.report ctxt pos Markup.words;
69916
3235ecdcd884 more meta data from "dcterms" (superset of "dc");
wenzelm
parents: 69891
diff changeset
    79
        in meta_data Markup.meta_description arg ctxt end) #>
3235ecdcd884 more meta data from "dcterms" (superset of "dc");
wenzelm
parents: 69891
diff changeset
    80
     setup0 (Binding.make ("license", \<^here>)) Parse.embedded (meta_data Markup.meta_license));
69887
b9985133805d added semantic document markers;
wenzelm
parents:
diff changeset
    81
69891
def3ec9cdb7e document markers are formal comments, and may thus occur anywhere in the command-span;
wenzelm
parents: 69890
diff changeset
    82
fun legacy_tag name =
def3ec9cdb7e document markers are formal comments, and may thus occur anywhere in the command-span;
wenzelm
parents: 69890
diff changeset
    83
  Input.string (cartouche ("tag " ^ Symbol_Pos.quote_string_qq name));
def3ec9cdb7e document markers are formal comments, and may thus occur anywhere in the command-span;
wenzelm
parents: 69890
diff changeset
    84
69887
b9985133805d added semantic document markers;
wenzelm
parents:
diff changeset
    85
end;