]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/utf8_macros/make_table.ml
uses XmlPushParser instead of PXP
[helm.git] / helm / ocaml / utf8_macros / make_table.ml
1 (* Copyright (C) 2004, HELM Team.
2  * 
3  * This file is part of HELM, an Hypertextual, Electronic
4  * Library of Mathematics, developed at the Computer Science
5  * Department, University of Bologna, Italy.
6  * 
7  * HELM is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  * 
12  * HELM is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with HELM; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://helm.cs.unibo.it/
24  *)
25
26 open Printf
27
28 let debug = false
29 let debug_print s = if debug then prerr_endline s
30
31   (* source files for tables xml parsing (if unmarshall=false) *)
32 let xml_tables = [
33 (*
34   `Entities, "/usr/share/gtkmathview/entities-table.xml";
35   `Dictionary, "/usr/share/editex/dictionary-tex.xml"
36 *)
37   `Entities, "data/entities-table.xml";
38   `Dictionary, "data/dictionary-tex.xml";
39   `Entities, "data/extra-entities.xml";
40 ]
41
42 let iter_gen record_tag name_field value_field f fname =
43   let start_element tag attrs =
44     if tag = record_tag then
45       try
46         let name = List.assoc name_field attrs in
47         let value = List.assoc value_field attrs in
48         f name value
49       with Not_found -> ()
50   in
51   let callbacks = {
52     XmlPushParser.default_callbacks with
53       XmlPushParser.start_element = Some start_element
54   } in
55   let xml_parser = XmlPushParser.create_parser callbacks in
56   XmlPushParser.parse xml_parser (`File fname)
57
58 let iter_entities_file    = iter_gen "entity" "name" "value"
59 let iter_dictionary_file  = iter_gen "entry" "name" "val"
60
61 let parse_from_xml () =
62   let (macro2utf8, utf82macro) = (Hashtbl.create 2000, Hashtbl.create 2000) in
63   let add_macro macro utf8 =
64     debug_print (sprintf "Adding macro %s = '%s'" macro utf8);
65     Hashtbl.add macro2utf8 macro utf8;
66 (*     Hashtbl.add utf82macro utf8 macro *)
67   in
68   let fill_table () =
69     List.iter
70       (fun (typ, fname) ->
71         match typ with
72         | `Entities -> iter_entities_file add_macro fname
73         | `Dictionary -> iter_dictionary_file add_macro fname)
74       xml_tables
75   in
76   fill_table ();
77   macro2utf8
78
79 let main () =
80   let oc = open_out Sys.argv.(1) in
81   output_string oc "(* GENERATED by make_table: DO NOT EDIT! *)\n";
82   output_string oc "let macro2utf8 = Hashtbl.create 2000\n";
83   let macro2utf8 = parse_from_xml () in
84   Hashtbl.iter
85     (fun macro utf8 ->
86       fprintf oc "let _ = Hashtbl.add macro2utf8 \"%s\" \"%s\"\n"
87         macro (String.escaped utf8))
88     macro2utf8;
89   close_out oc
90
91 let _ = main ()
92