]> matita.cs.unibo.it Git - helm.git/blob - helm/DEVEL/mathml_editor/textomml/main.cc
* added show/hide cursro methods
[helm.git] / helm / DEVEL / mathml_editor / textomml / main.cc
1
2 #include <getopt.h>
3
4 #include <fstream>
5
6 #include "dom.hh"
7 #include "TPushParser.hh"
8 #include "TPushLexer.hh"
9 #include "TDictionary.hh"
10 #include "CLoggerConsole.hh"
11 #include "CMathMLFactoryXSLT.hh"
12 #include "CMathMLFactoryXSLTDiff.hh"
13 #include "AMathMLConsumer.hh"
14
15 #include "config.dirs"
16
17 enum CommandLineOptionId {
18   OPTION_VERSION = 256,
19   OPTION_HELP,
20   OPTION_VERBOSE,
21   OPTION_DICTIONARY,
22   OPTION_TML_XSLT,
23   OPTION_XSLT
24 };
25
26 static std::string tml_xslt = PKGDATADIR"/tml-mmlp.xsl";
27 static std::string dictionary = PKGDATADIR"/dictionary-tex.xml";
28 static bool xslt = true;
29
30 static bool
31 parseBoolean(const char* s, bool& res)
32 {
33   assert(s != NULL);
34   if (!strcmp(s, "yes")) {
35     res = true;
36     return true;
37   } else if (!strcmp(s, "no")) {
38     res = false;
39     return true;
40   }
41
42   return false;
43 }
44
45 static void
46 printVersion()
47 {
48   std::cout << "TeX to MathML converter " << VERSION << " - Luca Padovani (C) 2003" << std::endl
49             << "This program is covered by the GNU Lesser General Public Licence" << std::endl;
50 }
51
52 static void
53 printHelp()
54 {
55   static char* helpMsg = "\
56 Usage: textomml [options] file\n\n\
57   -V, --version                   Output version information\n\
58   -h, --help                      This small usage guide\n\
59   -v, --verbose[=0-3]             Display messages\n\
60       --dictionary=<path>         Full path of the dictionary\n\
61       --tml-xslt=<path>           Full path of the XSLT stylesheet\n\
62       --xslt[=yes|no]             Enable/disable XSLT transformation (default='yes')\n\
63 ";
64
65   std::cout << helpMsg << std::endl;
66   exit(0);
67 }
68
69 static void
70 parseError(const char* option)
71 {
72   assert(option != NULL);
73   std::cerr << "error while parsing option `" << option << "'" << std::endl << std::endl;
74   printHelp();
75 }
76
77 void
78 main(int argc, char* argv[])
79 {
80   CLoggerConsole logger;
81
82   while (TRUE) {
83     int option_index = 0;
84     static struct option long_options[] =
85     {
86       { "version",       no_argument,       NULL, OPTION_VERSION },
87       { "help",          no_argument,       NULL, OPTION_HELP },
88       { "verbose",       optional_argument, NULL, OPTION_VERBOSE },
89       { "dictionary",    required_argument, NULL, OPTION_DICTIONARY },
90       { "tml-xslt",      required_argument, NULL, OPTION_TML_XSLT },
91       { "xslt",          optional_argument, NULL, OPTION_XSLT },
92
93       { NULL,            no_argument, NULL, 0 }
94     };
95
96     int c = getopt_long(argc, argv, "Vhv::", long_options, &option_index);
97
98     if (c == -1) break;
99
100     switch (c) {
101     case OPTION_VERSION:
102     case 'V':
103       printVersion();
104       break;
105
106     case OPTION_HELP:
107     case 'h':
108       printHelp();
109       break;
110
111     case OPTION_VERBOSE:
112     case 'v':
113       if (optarg == NULL) logger.verbosity(ALogger::Warning);
114       else logger.verbosity(ALogger::Level(*optarg - '0'));
115       break;
116
117     case OPTION_DICTIONARY:
118       dictionary = optarg;
119       break;
120
121     case OPTION_TML_XSLT:
122       tml_xslt = optarg;
123       break;
124
125     case OPTION_XSLT:
126       if (optarg == NULL) printHelp();
127       else if (!parseBoolean(optarg, xslt)) parseError("xslt");
128       break;
129
130     case '?':
131       break;
132
133     default:
134       std::cerr << "*** getopt returned `" << c << "' value" << std::endl;
135       break;
136     }
137   }
138
139   TDictionary dict(logger);
140   logger.info("loading dictionary: `" + dictionary + "'");
141   dict.load("dictionary-test.xml");
142
143   logger.info("loading stylesheet: `" + tml_xslt + "'");
144   DOM::DOMImplementation di;
145   DOM::Document docStyle = di.createDocumentFromURI("./xsl/tml-mmlp.xsl");
146   DOMX::XSLTStylesheet style(docStyle);
147
148   CMathMLFactoryXSLT factory(logger, style);
149   TPushParser parser(logger, factory, dict);
150   TPushLexer lexer(logger, parser);
151
152   if (optind < argc)
153     {
154       ifstream file(argv[optind]);
155       if (!file)
156         {
157           std::cerr << "can't open input file `" << argv[optind] << "'" << std::endl;
158           exit(1);
159         }
160       
161       parser.freeze();
162       char ch;
163       while (file.get(ch)) lexer.push(ch);
164       parser.thaw();
165     }
166   else
167     printHelp();
168 }