]> matita.cs.unibo.it Git - helm.git/blob - components/syntax_extensions/make_table.ml
matita 0.5.1 tagged
[helm.git] / components / syntax_extensions / 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 (* $Id$ *)
27
28 open Printf
29
30 let debug = false
31 let debug_print s = if debug then prerr_endline (Lazy.force s)
32
33   (* source files for tables xml parsing (if unmarshall=false) *)
34 let xml_tables = [
35 (*
36   `Entities, "/usr/share/gtkmathview/entities-table.xml";
37   `Dictionary, "/usr/share/editex/dictionary-tex.xml"
38 *)
39   `Entities, "data/entities-table.xml";
40   `Dictionary, "data/dictionary-tex.xml";
41   `Entities, "data/extra-entities.xml";
42   (** extra-entities.xml should be the last one since it is used to override
43    * previous mappings. Add there overrides as needed. *)
44 ]
45
46 let iter_gen record_tag name_field value_field f fname =
47   let start_element tag attrs =
48     if tag = record_tag then
49       try
50         let name = List.assoc name_field attrs in
51         let value = List.assoc value_field attrs in
52         f name value
53       with Not_found -> ()
54   in
55   let callbacks = {
56     XmlPushParser.default_callbacks with
57       XmlPushParser.start_element = Some start_element
58   } in
59   let xml_parser = XmlPushParser.create_parser callbacks in
60   XmlPushParser.parse xml_parser (`File fname)
61
62 let iter_entities_file    = iter_gen "entity" "name" "value"
63 let iter_dictionary_file  = iter_gen "entry" "name" "val"
64
65 let parse_from_xml () =
66   let macro2utf8 = Hashtbl.create 2000 in
67   let add_macro macro utf8 =
68     debug_print (lazy (sprintf "Adding macro %s = '%s'" macro utf8));
69     Hashtbl.replace macro2utf8 macro utf8
70   in
71   let fill_table () =
72     List.iter
73       (fun (typ, fname) ->
74         match typ with
75         | `Entities -> iter_entities_file add_macro fname
76         | `Dictionary -> iter_dictionary_file add_macro fname)
77       xml_tables
78   in
79   fill_table ();
80   macro2utf8
81
82 let main () =
83   let oc = open_out Sys.argv.(1) in
84   let oc_doc = open_out Sys.argv.(2) in
85   output_string oc "(* GENERATED by make_table: DO NOT EDIT! *)\n";
86   output_string oc_doc "(* GENERATED by make_table: DO NOT EDIT! *)\n";
87   output_string oc "let macro2utf8 = Hashtbl.create 2000\n";
88   output_string oc "let utf82macro = Hashtbl.create 2000\n";
89   output_string oc "let data = [\n";
90   let macro2utf8 = parse_from_xml () in
91   Hashtbl.iter
92     (fun macro utf8 ->
93             fprintf oc "  \"%s\", \"%s\";\n" macro (String.escaped utf8);
94             fprintf oc_doc "\\%s %s\n" macro utf8)
95     macro2utf8;
96   output_string oc "  ];;\n";
97   output_string oc "let _ =\n";
98   output_string oc "  List.iter\n";
99   output_string oc "    (fun (macro, utf8) ->\n";
100   output_string oc "      Hashtbl.replace macro2utf8 macro utf8;\n";
101   output_string oc "      Hashtbl.replace utf82macro utf8 macro)\n";
102   output_string oc "    data;;\n";
103   close_out oc;
104   close_out oc_doc
105
106 let _ = main ()
107