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