src/Pure/Thy/bibtex.ML
author wenzelm
Fri, 13 Jan 2023 13:01:19 +0100
changeset 76960 6c623c517a6e
parent 76959 3d491a0af6ef
child 76961 d756f4f78dc7
permissions -rw-r--r--
more "cite" antiquotations;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
67274
4588f714a78a clarified directories;
wenzelm
parents: 67147
diff changeset
     1
(*  Title:      Pure/Thy/bibtex.ML
58544
340f130b3d38 bibtex support in ML: document antiquotation @{cite} with markup;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
340f130b3d38 bibtex support in ML: document antiquotation @{cite} with markup;
wenzelm
parents:
diff changeset
     3
340f130b3d38 bibtex support in ML: document antiquotation @{cite} with markup;
wenzelm
parents:
diff changeset
     4
BibTeX support.
340f130b3d38 bibtex support in ML: document antiquotation @{cite} with markup;
wenzelm
parents:
diff changeset
     5
*)
340f130b3d38 bibtex support in ML: document antiquotation @{cite} with markup;
wenzelm
parents:
diff changeset
     6
340f130b3d38 bibtex support in ML: document antiquotation @{cite} with markup;
wenzelm
parents:
diff changeset
     7
signature BIBTEX =
340f130b3d38 bibtex support in ML: document antiquotation @{cite} with markup;
wenzelm
parents:
diff changeset
     8
sig
67301
e255c76db052 clarified signature;
wenzelm
parents: 67297
diff changeset
     9
  val check_database:
e255c76db052 clarified signature;
wenzelm
parents: 67297
diff changeset
    10
    Position.T -> string -> (string * Position.T) list * (string * Position.T) list
67275
5e427586cb57 check bibtex database on ML side -- for semantic PIDE editing;
wenzelm
parents: 67274
diff changeset
    11
  val check_database_output: Position.T -> string -> unit
58544
340f130b3d38 bibtex support in ML: document antiquotation @{cite} with markup;
wenzelm
parents:
diff changeset
    12
  val cite_macro: string Config.T
76959
3d491a0af6ef clarified signature: more generic operations;
wenzelm
parents: 76958
diff changeset
    13
  val cite_antiquotation: binding -> (Proof.context -> string) -> theory -> theory
58544
340f130b3d38 bibtex support in ML: document antiquotation @{cite} with markup;
wenzelm
parents:
diff changeset
    14
end;
340f130b3d38 bibtex support in ML: document antiquotation @{cite} with markup;
wenzelm
parents:
diff changeset
    15
340f130b3d38 bibtex support in ML: document antiquotation @{cite} with markup;
wenzelm
parents:
diff changeset
    16
structure Bibtex: BIBTEX =
340f130b3d38 bibtex support in ML: document antiquotation @{cite} with markup;
wenzelm
parents:
diff changeset
    17
struct
340f130b3d38 bibtex support in ML: document antiquotation @{cite} with markup;
wenzelm
parents:
diff changeset
    18
67275
5e427586cb57 check bibtex database on ML side -- for semantic PIDE editing;
wenzelm
parents: 67274
diff changeset
    19
(* check database *)
5e427586cb57 check bibtex database on ML side -- for semantic PIDE editing;
wenzelm
parents: 67274
diff changeset
    20
67301
e255c76db052 clarified signature;
wenzelm
parents: 67297
diff changeset
    21
type message = string * Position.T;
67275
5e427586cb57 check bibtex database on ML side -- for semantic PIDE editing;
wenzelm
parents: 67274
diff changeset
    22
5e427586cb57 check bibtex database on ML side -- for semantic PIDE editing;
wenzelm
parents: 67274
diff changeset
    23
fun check_database pos0 database =
72194
eef421b724c0 clarified names;
wenzelm
parents: 71881
diff changeset
    24
  \<^scala>\<open>bibtex_check_database\<close> database
67275
5e427586cb57 check bibtex database on ML side -- for semantic PIDE editing;
wenzelm
parents: 67274
diff changeset
    25
  |> YXML.parse_body
67301
e255c76db052 clarified signature;
wenzelm
parents: 67297
diff changeset
    26
  |> let open XML.Decode in pair (list (pair string properties)) (list (pair string properties)) end
e255c76db052 clarified signature;
wenzelm
parents: 67297
diff changeset
    27
  |> (apply2 o map o apsnd) (fn pos => Position.of_properties (pos @ Position.get_props pos0));
67275
5e427586cb57 check bibtex database on ML side -- for semantic PIDE editing;
wenzelm
parents: 67274
diff changeset
    28
5e427586cb57 check bibtex database on ML side -- for semantic PIDE editing;
wenzelm
parents: 67274
diff changeset
    29
fun check_database_output pos0 database =
67301
e255c76db052 clarified signature;
wenzelm
parents: 67297
diff changeset
    30
  let val (errors, warnings) = check_database pos0 database in
e255c76db052 clarified signature;
wenzelm
parents: 67297
diff changeset
    31
    errors |> List.app (fn (msg, pos) =>
67275
5e427586cb57 check bibtex database on ML side -- for semantic PIDE editing;
wenzelm
parents: 67274
diff changeset
    32
      Output.error_message ("Bibtex error" ^ Position.here pos ^ ":\n  " ^ msg));
67301
e255c76db052 clarified signature;
wenzelm
parents: 67297
diff changeset
    33
    warnings |> List.app (fn (msg, pos) =>
67275
5e427586cb57 check bibtex database on ML side -- for semantic PIDE editing;
wenzelm
parents: 67274
diff changeset
    34
      warning ("Bibtex warning" ^ Position.here pos ^ ":\n  " ^ msg))
5e427586cb57 check bibtex database on ML side -- for semantic PIDE editing;
wenzelm
parents: 67274
diff changeset
    35
  end;
5e427586cb57 check bibtex database on ML side -- for semantic PIDE editing;
wenzelm
parents: 67274
diff changeset
    36
5e427586cb57 check bibtex database on ML side -- for semantic PIDE editing;
wenzelm
parents: 67274
diff changeset
    37
5e427586cb57 check bibtex database on ML side -- for semantic PIDE editing;
wenzelm
parents: 67274
diff changeset
    38
(* document antiquotations *)
5e427586cb57 check bibtex database on ML side -- for semantic PIDE editing;
wenzelm
parents: 67274
diff changeset
    39
67147
dea94b1aabc3 prefer control symbol antiquotations;
wenzelm
parents: 65032
diff changeset
    40
val cite_macro = Attrib.setup_config_string \<^binding>\<open>cite_macro\<close> (K "cite");
58544
340f130b3d38 bibtex support in ML: document antiquotation @{cite} with markup;
wenzelm
parents:
diff changeset
    41
76959
3d491a0af6ef clarified signature: more generic operations;
wenzelm
parents: 76958
diff changeset
    42
fun cite_antiquotation binding get_kind =
3d491a0af6ef clarified signature: more generic operations;
wenzelm
parents: 76958
diff changeset
    43
  Document_Output.antiquotation_raw binding
3d491a0af6ef clarified signature: more generic operations;
wenzelm
parents: 76958
diff changeset
    44
    (Scan.lift (Scan.option Args.cartouche_input -- Parse.and_list1 Args.name_position))
3d491a0af6ef clarified signature: more generic operations;
wenzelm
parents: 76958
diff changeset
    45
    (fn ctxt => fn (opt_loc, citations) =>
3d491a0af6ef clarified signature: more generic operations;
wenzelm
parents: 76958
diff changeset
    46
      let
3d491a0af6ef clarified signature: more generic operations;
wenzelm
parents: 76958
diff changeset
    47
        val loc = the_default Input.empty opt_loc;
3d491a0af6ef clarified signature: more generic operations;
wenzelm
parents: 76958
diff changeset
    48
        val _ =
3d491a0af6ef clarified signature: more generic operations;
wenzelm
parents: 76958
diff changeset
    49
          Context_Position.reports ctxt
3d491a0af6ef clarified signature: more generic operations;
wenzelm
parents: 76958
diff changeset
    50
            (Document_Output.document_reports loc @
3d491a0af6ef clarified signature: more generic operations;
wenzelm
parents: 76958
diff changeset
    51
              map (fn (name, pos) => (pos, Markup.citation name)) citations);
3d491a0af6ef clarified signature: more generic operations;
wenzelm
parents: 76958
diff changeset
    52
3d491a0af6ef clarified signature: more generic operations;
wenzelm
parents: 76958
diff changeset
    53
        val thy_name = Context.theory_long_name (Proof_Context.theory_of ctxt);
3d491a0af6ef clarified signature: more generic operations;
wenzelm
parents: 76958
diff changeset
    54
        val bibtex_entries = Resources.theory_bibtex_entries thy_name;
3d491a0af6ef clarified signature: more generic operations;
wenzelm
parents: 76958
diff changeset
    55
        val _ =
3d491a0af6ef clarified signature: more generic operations;
wenzelm
parents: 76958
diff changeset
    56
          if null bibtex_entries andalso thy_name <> Context.PureN then ()
3d491a0af6ef clarified signature: more generic operations;
wenzelm
parents: 76958
diff changeset
    57
          else
3d491a0af6ef clarified signature: more generic operations;
wenzelm
parents: 76958
diff changeset
    58
            citations |> List.app (fn (name, pos) =>
3d491a0af6ef clarified signature: more generic operations;
wenzelm
parents: 76958
diff changeset
    59
              if member (op =) bibtex_entries name orelse name = "*" then ()
3d491a0af6ef clarified signature: more generic operations;
wenzelm
parents: 76958
diff changeset
    60
              else error ("Unknown Bibtex entry " ^ quote name ^ Position.here pos));
3d491a0af6ef clarified signature: more generic operations;
wenzelm
parents: 76958
diff changeset
    61
3d491a0af6ef clarified signature: more generic operations;
wenzelm
parents: 76958
diff changeset
    62
        val kind = get_kind ctxt;
3d491a0af6ef clarified signature: more generic operations;
wenzelm
parents: 76958
diff changeset
    63
        val location = Document_Output.output_document ctxt {markdown = false} loc;
3d491a0af6ef clarified signature: more generic operations;
wenzelm
parents: 76958
diff changeset
    64
      in Latex.cite {kind = kind, citations = citations, location = location} end);
3d491a0af6ef clarified signature: more generic operations;
wenzelm
parents: 76958
diff changeset
    65
58544
340f130b3d38 bibtex support in ML: document antiquotation @{cite} with markup;
wenzelm
parents:
diff changeset
    66
val _ =
340f130b3d38 bibtex support in ML: document antiquotation @{cite} with markup;
wenzelm
parents:
diff changeset
    67
  Theory.setup
67386
998e01d6f8fd clarified modules;
wenzelm
parents: 67301
diff changeset
    68
   (Document_Antiquotation.setup_option \<^binding>\<open>cite_macro\<close> (Config.put cite_macro) #>
76960
6c623c517a6e more "cite" antiquotations;
wenzelm
parents: 76959
diff changeset
    69
    cite_antiquotation \<^binding>\<open>cite\<close> (fn ctxt => Config.get ctxt cite_macro) #>
6c623c517a6e more "cite" antiquotations;
wenzelm
parents: 76959
diff changeset
    70
    cite_antiquotation \<^binding>\<open>nocite\<close> (K "nocite") #>
6c623c517a6e more "cite" antiquotations;
wenzelm
parents: 76959
diff changeset
    71
    cite_antiquotation \<^binding>\<open>citet\<close> (K "citet") #>
6c623c517a6e more "cite" antiquotations;
wenzelm
parents: 76959
diff changeset
    72
    cite_antiquotation \<^binding>\<open>citep\<close> (K "citep"));
58544
340f130b3d38 bibtex support in ML: document antiquotation @{cite} with markup;
wenzelm
parents:
diff changeset
    73
340f130b3d38 bibtex support in ML: document antiquotation @{cite} with markup;
wenzelm
parents:
diff changeset
    74
end;