]> matita.cs.unibo.it Git - helm.git/blob - helm/DEVEL/mathml_editor/textomml/main.cc
ocaml 3.09 transition
[helm.git] / helm / DEVEL / mathml_editor / textomml / main.cc
1 /* This file is part of EdiTeX, an editor of mathematical
2  * expressions based on TeX syntax.
3  * 
4  * Copyright (C) 2002-2003 Luca Padovani <lpadovan@cs.unibo.it>,
5  *                    2003 Paolo Marinelli <pmarinel@cs.unibo.it>.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library 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 GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  *
21  * For more information, please visit the project's home page
22  * http://helm.cs.unibo.it/editex/
23  * or send an email to <lpadovan@cs.unibo.it>
24  */
25
26 #include <getopt.h>
27 #include <fstream>
28 #include <cassert>
29
30 #include "dom.hh"
31 #include "TPushParser.hh"
32 #include "TPushLexer.hh"
33 #include "TDictionary.hh"
34 #include "CLoggerConsole.hh"
35 #include "CMathMLFactoryXSLT.hh"
36 #include "CMathMLFactoryXSLTDiff.hh"
37 #include "AMathMLConsumer.hh"
38
39 #include "config.dirs"
40
41 enum CommandLineOptionId {
42   OPTION_VERSION = 256,
43   OPTION_HELP,
44   OPTION_VERBOSE,
45   OPTION_DICTIONARY,
46   OPTION_TML_XSLT,
47   OPTION_XSLT
48 };
49
50 static std::string tml_xslt = PKGDATADIR"/tml-mmlp.xsl";
51 static std::string dictionary = PKGDATADIR"/dictionary-tex.xml";
52 static bool xslt = true;
53
54 static bool
55 parseBoolean(const char* s, bool& res)
56 {
57   assert(s != NULL);
58   if (!strcmp(s, "yes")) {
59     res = true;
60     return true;
61   } else if (!strcmp(s, "no")) {
62     res = false;
63     return true;
64   }
65
66   return false;
67 }
68
69 static void
70 printVersion()
71 {
72   std::cout << "TeX to MathML converter " << VERSION << " - Luca Padovani (C) 2003" << std::endl
73             << "This program is covered by the GNU Lesser General Public Licence" << std::endl;
74 }
75
76 static void
77 printHelp()
78 {
79   static char* helpMsg = "\
80 Usage: textomml [options] file\n\n\
81   -V, --version                   Output version information\n\
82   -h, --help                      This small usage guide\n\
83   -v, --verbose[=0-3]             Display messages\n\
84       --dictionary=<path>         Full path of the dictionary\n\
85       --tml-xslt=<path>           Full path of the XSLT stylesheet\n\
86       --xslt[=yes|no]             Enable/disable XSLT transformation (default='yes')\n\
87 ";
88
89   std::cout << helpMsg << std::endl;
90   exit(0);
91 }
92
93 static void
94 parseError(const char* option)
95 {
96   assert(option != NULL);
97   std::cerr << "error while parsing option `" << option << "'" << std::endl << std::endl;
98   printHelp();
99 }
100
101 int
102 main(int argc, char* argv[])
103 {
104   CLoggerConsole logger;
105
106   while (TRUE) {
107     int option_index = 0;
108     static struct option long_options[] =
109     {
110       { "version",       no_argument,       NULL, OPTION_VERSION },
111       { "help",          no_argument,       NULL, OPTION_HELP },
112       { "verbose",       optional_argument, NULL, OPTION_VERBOSE },
113       { "dictionary",    required_argument, NULL, OPTION_DICTIONARY },
114       { "tml-xslt",      required_argument, NULL, OPTION_TML_XSLT },
115       { "xslt",          optional_argument, NULL, OPTION_XSLT },
116
117       { NULL,            no_argument, NULL, 0 }
118     };
119
120     int c = getopt_long(argc, argv, "Vhv::", long_options, &option_index);
121
122     if (c == -1) break;
123
124     switch (c) {
125     case OPTION_VERSION:
126     case 'V':
127       printVersion();
128       break;
129
130     case OPTION_HELP:
131     case 'h':
132       printHelp();
133       break;
134
135     case OPTION_VERBOSE:
136     case 'v':
137       if (optarg == NULL) logger.verbosity(ALogger::Warning);
138       else logger.verbosity(ALogger::Level(*optarg - '0'));
139       break;
140
141     case OPTION_DICTIONARY:
142       dictionary = optarg;
143       break;
144
145     case OPTION_TML_XSLT:
146       tml_xslt = optarg;
147       break;
148
149     case OPTION_XSLT:
150       if (optarg == NULL) printHelp();
151       else if (!parseBoolean(optarg, xslt)) parseError("xslt");
152       break;
153
154     case '?':
155       break;
156
157     default:
158       std::cerr << "*** getopt returned `" << c << "' value" << std::endl;
159       break;
160     }
161   }
162
163   TDictionary dict(logger);
164   logger.info("loading dictionary: `" + dictionary + "'");
165   dict.load("dictionary-test.xml");
166
167   logger.info("loading stylesheet: `" + tml_xslt + "'");
168   DOM::DOMImplementation di;
169   DOM::Document docStyle = di.createDocumentFromURI("./xsl/tml-mmlp.xsl");
170   DOMX::XSLTStylesheet style(docStyle);
171
172   CMathMLFactoryXSLT factory(logger, style);
173   TPushParser parser(logger, factory, dict);
174   TPushLexer lexer(logger, parser);
175
176   if (optind < argc)
177     {
178       std::ifstream file(argv[optind]);
179       if (!file)
180         {
181           std::cerr << "can't open input file `" << argv[optind] << "'" << std::endl;
182           exit(1);
183         }
184       
185       parser.freeze();
186       char ch;
187       while (file.get(ch)) lexer.push(ch);
188       parser.thaw();
189     }
190   else
191     printHelp();
192 }