src/Pure/Thy/bibtex.ML
author wenzelm
Thu, 12 Jan 2023 19:48:47 +0100
changeset 76956 e47fb5cfef41
parent 76955 3f25c28c4257
child 76958 d9727629cb67
permissions -rw-r--r--
clarified Latex markup: optional cite "location" consists of nested document text;
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
340f130b3d38 bibtex support in ML: document antiquotation @{cite} with markup;
wenzelm
parents:
diff changeset
    13
end;
340f130b3d38 bibtex support in ML: document antiquotation @{cite} with markup;
wenzelm
parents:
diff changeset
    14
340f130b3d38 bibtex support in ML: document antiquotation @{cite} with markup;
wenzelm
parents:
diff changeset
    15
structure Bibtex: BIBTEX =
340f130b3d38 bibtex support in ML: document antiquotation @{cite} with markup;
wenzelm
parents:
diff changeset
    16
struct
340f130b3d38 bibtex support in ML: document antiquotation @{cite} with markup;
wenzelm
parents:
diff changeset
    17
67275
5e427586cb57 check bibtex database on ML side -- for semantic PIDE editing;
wenzelm
parents: 67274
diff changeset
    18
(* check database *)
5e427586cb57 check bibtex database on ML side -- for semantic PIDE editing;
wenzelm
parents: 67274
diff changeset
    19
67301
e255c76db052 clarified signature;
wenzelm
parents: 67297
diff changeset
    20
type message = string * Position.T;
67275
5e427586cb57 check bibtex database on ML side -- for semantic PIDE editing;
wenzelm
parents: 67274
diff changeset
    21
5e427586cb57 check bibtex database on ML side -- for semantic PIDE editing;
wenzelm
parents: 67274
diff changeset
    22
fun check_database pos0 database =
72194
eef421b724c0 clarified names;
wenzelm
parents: 71881
diff changeset
    23
  \<^scala>\<open>bibtex_check_database\<close> database
67275
5e427586cb57 check bibtex database on ML side -- for semantic PIDE editing;
wenzelm
parents: 67274
diff changeset
    24
  |> YXML.parse_body
67301
e255c76db052 clarified signature;
wenzelm
parents: 67297
diff changeset
    25
  |> let open XML.Decode in pair (list (pair string properties)) (list (pair string properties)) end
e255c76db052 clarified signature;
wenzelm
parents: 67297
diff changeset
    26
  |> (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
    27
5e427586cb57 check bibtex database on ML side -- for semantic PIDE editing;
wenzelm
parents: 67274
diff changeset
    28
fun check_database_output pos0 database =
67301
e255c76db052 clarified signature;
wenzelm
parents: 67297
diff changeset
    29
  let val (errors, warnings) = check_database pos0 database in
e255c76db052 clarified signature;
wenzelm
parents: 67297
diff changeset
    30
    errors |> List.app (fn (msg, pos) =>
67275
5e427586cb57 check bibtex database on ML side -- for semantic PIDE editing;
wenzelm
parents: 67274
diff changeset
    31
      Output.error_message ("Bibtex error" ^ Position.here pos ^ ":\n  " ^ msg));
67301
e255c76db052 clarified signature;
wenzelm
parents: 67297
diff changeset
    32
    warnings |> List.app (fn (msg, pos) =>
67275
5e427586cb57 check bibtex database on ML side -- for semantic PIDE editing;
wenzelm
parents: 67274
diff changeset
    33
      warning ("Bibtex warning" ^ Position.here pos ^ ":\n  " ^ msg))
5e427586cb57 check bibtex database on ML side -- for semantic PIDE editing;
wenzelm
parents: 67274
diff changeset
    34
  end;
5e427586cb57 check bibtex database on ML side -- for semantic PIDE editing;
wenzelm
parents: 67274
diff changeset
    35
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
(* document antiquotations *)
5e427586cb57 check bibtex database on ML side -- for semantic PIDE editing;
wenzelm
parents: 67274
diff changeset
    38
67147
dea94b1aabc3 prefer control symbol antiquotations;
wenzelm
parents: 65032
diff changeset
    39
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
    40
340f130b3d38 bibtex support in ML: document antiquotation @{cite} with markup;
wenzelm
parents:
diff changeset
    41
val _ =
340f130b3d38 bibtex support in ML: document antiquotation @{cite} with markup;
wenzelm
parents:
diff changeset
    42
  Theory.setup
67386
998e01d6f8fd clarified modules;
wenzelm
parents: 67301
diff changeset
    43
   (Document_Antiquotation.setup_option \<^binding>\<open>cite_macro\<close> (Config.put cite_macro) #>
73761
ef1a18e20ace clarified modules;
wenzelm
parents: 73148
diff changeset
    44
    Document_Output.antiquotation_raw \<^binding>\<open>cite\<close>
76956
e47fb5cfef41 clarified Latex markup: optional cite "location" consists of nested document text;
wenzelm
parents: 76955
diff changeset
    45
      (Scan.lift (Scan.option Args.cartouche_input -- Parse.and_list1 Args.name_position))
e47fb5cfef41 clarified Latex markup: optional cite "location" consists of nested document text;
wenzelm
parents: 76955
diff changeset
    46
      (fn ctxt => fn (opt_loc, citations) =>
58544
340f130b3d38 bibtex support in ML: document antiquotation @{cite} with markup;
wenzelm
parents:
diff changeset
    47
        let
76956
e47fb5cfef41 clarified Latex markup: optional cite "location" consists of nested document text;
wenzelm
parents: 76955
diff changeset
    48
          val loc = the_default Input.empty opt_loc;
73148
5f49f1149c1c clarified reports before errors: support completion of bibtex entries in Isabelle/Scala (amending d01ea9e3bd2d);
wenzelm
parents: 73147
diff changeset
    49
          val _ =
5f49f1149c1c clarified reports before errors: support completion of bibtex entries in Isabelle/Scala (amending d01ea9e3bd2d);
wenzelm
parents: 73147
diff changeset
    50
            Context_Position.reports ctxt
76956
e47fb5cfef41 clarified Latex markup: optional cite "location" consists of nested document text;
wenzelm
parents: 76955
diff changeset
    51
              (Document_Output.document_reports loc @
e47fb5cfef41 clarified Latex markup: optional cite "location" consists of nested document text;
wenzelm
parents: 76955
diff changeset
    52
                map (fn (name, pos) => (pos, Markup.citation name)) citations);
73148
5f49f1149c1c clarified reports before errors: support completion of bibtex entries in Isabelle/Scala (amending d01ea9e3bd2d);
wenzelm
parents: 73147
diff changeset
    53
73147
wenzelm
parents: 73146
diff changeset
    54
          val thy_name = Context.theory_long_name (Proof_Context.theory_of ctxt);
wenzelm
parents: 73146
diff changeset
    55
          val bibtex_entries = Resources.theory_bibtex_entries thy_name;
67297
86a099f896fc formal check of @{cite} bibtex entries -- only in batch-mode session builds;
wenzelm
parents: 67280
diff changeset
    56
          val _ =
73147
wenzelm
parents: 73146
diff changeset
    57
            if null bibtex_entries andalso thy_name <> Context.PureN then ()
67297
86a099f896fc formal check of @{cite} bibtex entries -- only in batch-mode session builds;
wenzelm
parents: 67280
diff changeset
    58
            else
86a099f896fc formal check of @{cite} bibtex entries -- only in batch-mode session builds;
wenzelm
parents: 67280
diff changeset
    59
              citations |> List.app (fn (name, pos) =>
86a099f896fc formal check of @{cite} bibtex entries -- only in batch-mode session builds;
wenzelm
parents: 67280
diff changeset
    60
                if member (op =) bibtex_entries name then ()
86a099f896fc formal check of @{cite} bibtex entries -- only in batch-mode session builds;
wenzelm
parents: 67280
diff changeset
    61
                else error ("Unknown Bibtex entry " ^ quote name ^ Position.here pos));
73148
5f49f1149c1c clarified reports before errors: support completion of bibtex entries in Isabelle/Scala (amending d01ea9e3bd2d);
wenzelm
parents: 73147
diff changeset
    62
76955
3f25c28c4257 more explicit latex markup;
wenzelm
parents: 74887
diff changeset
    63
          val kind = Config.get ctxt cite_macro;
76956
e47fb5cfef41 clarified Latex markup: optional cite "location" consists of nested document text;
wenzelm
parents: 76955
diff changeset
    64
          val location = Document_Output.output_document ctxt {markdown = false} loc;
e47fb5cfef41 clarified Latex markup: optional cite "location" consists of nested document text;
wenzelm
parents: 76955
diff changeset
    65
        in Latex.cite {kind = kind, citations = citations, location = location} end));
58544
340f130b3d38 bibtex support in ML: document antiquotation @{cite} with markup;
wenzelm
parents:
diff changeset
    66
340f130b3d38 bibtex support in ML: document antiquotation @{cite} with markup;
wenzelm
parents:
diff changeset
    67
end;