]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/ocaml/utf8_macros/make_table.ml
new module: expansion from tex like macros to utf8 strings
[helm.git] / helm / ocaml / utf8_macros / make_table.ml
diff --git a/helm/ocaml/utf8_macros/make_table.ml b/helm/ocaml/utf8_macros/make_table.ml
new file mode 100644 (file)
index 0000000..2e37409
--- /dev/null
@@ -0,0 +1,120 @@
+(* Copyright (C) 2004, HELM Team.
+ * 
+ * This file is part of HELM, an Hypertextual, Electronic
+ * Library of Mathematics, developed at the Computer Science
+ * Department, University of Bologna, Italy.
+ * 
+ * HELM is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ * 
+ * HELM is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with HELM; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
+ * MA  02111-1307, USA.
+ * 
+ * For details, see the HELM World-Wide-Web page,
+ * http://helm.cs.unibo.it/
+ *)
+
+open Printf
+open Pxp_types
+open Pxp_ev_parser
+
+let debug = false
+let debug_print s = if debug then prerr_endline s
+
+  (* source files for tables xml parsing (if unmarshall=false) *)
+let xml_tables = [
+(*
+  `Entities, "/usr/share/gtkmathview/entities-table.xml";
+  `Dictionary, "/usr/share/editex/dictionary-tex.xml"
+*)
+  `Entities, "data/entities-table.xml";
+  `Dictionary, "data/dictionary-tex.xml";
+  `Entities, "data/extra-entities.xml";
+]
+
+
+let rec find_first_tag pull_parser =
+  match pull_parser () with
+  | Some (E_start_tag _ as e) -> e
+  | None -> assert false
+  | _ -> find_first_tag pull_parser
+
+let iter_entities_file f pull_parser =
+  ignore (find_first_tag pull_parser); (* <entities-table> *)
+  let rec aux () =
+    match pull_parser () with
+    | Some (E_start_tag ("entity", attrs, _, _)) ->
+       (try
+         let name = List.assoc "name" attrs in
+         let value = List.assoc "value" attrs in
+         f name value
+       with Not_found -> ());
+       aux ()
+    | None -> ()
+    | _ -> aux ()
+  in
+  aux ()
+
+let iter_dictionary_file f pull_parser =
+  ignore (find_first_tag pull_parser); (* <dictionary> *)
+  let rec aux () =
+    match pull_parser () with
+    | Some (E_start_tag ("entry", attrs, _, _)) ->
+       (try
+         let name = List.assoc "name" attrs in
+         let value = List.assoc "val" attrs in
+         f name value
+       with Not_found -> ());
+       aux ()
+    | None -> ()
+    | _ -> aux ()
+  in
+  aux ()
+
+let parse_from_xml () =
+  let (macro2utf8, utf82macro) = (Hashtbl.create 2000, Hashtbl.create 2000) in
+  let add_macro macro utf8 =
+    debug_print (sprintf "Adding macro %s = '%s'" macro utf8);
+    Hashtbl.add macro2utf8 macro utf8;
+(*     Hashtbl.add utf82macro utf8 macro *)
+  in
+  let fill_table () =
+    List.iter
+      (fun (typ, fname) ->
+        let entry = `Entry_document [ `Extend_dtd_fully; `Parse_xml_decl ] in
+        let config = PxpHelmConf.pxp_config in
+        let entity_manager =
+          create_entity_manager ~is_document:true config (from_file fname)
+        in
+        let pull_parser = create_pull_parser config entry entity_manager in
+        match typ with
+        | `Entities -> iter_entities_file add_macro pull_parser
+        | `Dictionary -> iter_dictionary_file add_macro pull_parser)
+      xml_tables
+  in
+  fill_table ();
+  macro2utf8
+
+let main () =
+  let oc = open_out Sys.argv.(1) in
+  output_string oc "(* GENERATED by make_table: DO NOT EDIT! *)\n";
+  output_string oc "let macro2utf8 = Hashtbl.create 2000\n";
+  let macro2utf8 = parse_from_xml () in
+  Hashtbl.iter
+    (fun macro utf8 ->
+      fprintf oc "let _ = Hashtbl.add macro2utf8 \"%s\" \"%s\"\n"
+        macro (String.escaped utf8))
+    macro2utf8;
+  close_out oc
+
+let _ = main ()
+