/itexToMML

To download this project, use:
bzr branch http://golem.ph.utexas.edu/~distler/code/itexToMML/

« back to all changes in this revision

Viewing changes to itex-src/itex2MML.cc

  • Committer: Jacques Distler
  • Date: 2007-01-11 07:17:18 UTC
  • Revision ID: distler@golem.ph.utexas.edu-20070111071718-3azxrr5b29d00h26
Initial commit.

Show diffs side-by-side

added added

removed removed

 
1
/*             itex2MML 1.1.8
 
2
 *   itex2MML.cc last modified 9/15/2005
 
3
 */
 
4
 
 
5
#include <cstdio>
 
6
 
 
7
#include <string>
 
8
 
 
9
#include "itex2MML.h"
 
10
 
 
11
int main (int argc, char ** argv)
 
12
{
 
13
        bool bPrintItex = false;
 
14
        bool bRawFilter = false;
 
15
        bool bInline    = false;
 
16
        bool bDisplay   = false;
 
17
 
 
18
        bool bStop = false;
 
19
 
 
20
        for (int arg = 1; arg < argc; arg++)
 
21
                {
 
22
                        std::string args = argv[arg];
 
23
 
 
24
                        if (args == "--help")
 
25
                                {
 
26
                                        fputs ("usage: itex2MML [OPTIONS]\n"
 
27
                                                   "\n"
 
28
                                                   "itex2MML filters an input text stream (e.g., an XHTML web page) converting itex expressions\n"
 
29
                                                   "to MathML. Inline itex expressions are delimited either side by single dollar symbols ($):\n"
 
30
                                                   "\n"
 
31
                                                   "\t<p>The parameters $\\alpha$ and $\\beta$ in the function $f(x)$ are defined below.</p>\n"
 
32
                                                   "\n"
 
33
                                                   "For normal display of equations, etc., itex expressions can be delimited with double dollar\n"
 
34
                                                   "symbols ($$) either side or by \\[ to the left and \\] to the right:\n"
 
35
                                                   "\n"
 
36
                                                   "\t<p class=\"equation\">\\[\n"
 
37
                                                   "\t\tf(x) = \\alpha x + \\frac{\\beta}{1+|x|}\n"
 
38
                                                   "\t\\]</p>\n"
 
39
                                                   "\n"
 
40
                                                   "itex2MML Options:\n"
 
41
                                                   "\n"
 
42
                                                   "  --raw-filter  filter input stream, converting equations as found to MathML [stops on error]\n"
 
43
                                                   "  --inline      converts a single itex equation, without any $ symbols, to inline MathML\n"
 
44
                                                   "  --display     converts a single itex equation, without any $ symbols, to display-mode MathML\n"
 
45
                                                   "  --print-itex  used in conjuction with --inline or --display: prints the itex string\n"
 
46
                                                   "\n"
 
47
                                                   "For further information, see http://golem.ph.utexas.edu/~distler/blog/itex2MML.html\n", stdout);
 
48
 
 
49
                                        bStop = true;
 
50
                                        break;
 
51
                                }
 
52
                        if (args == "--print-itex")
 
53
                                {
 
54
                                        bPrintItex = true;
 
55
                                        bRawFilter = false;
 
56
                                        continue;
 
57
                                }
 
58
                        if (args == "--inline")
 
59
                                {
 
60
                                        bRawFilter = false;
 
61
                                        bInline    = true;
 
62
                                        bDisplay   = false;
 
63
                                        continue;
 
64
                                }
 
65
                        if (args == "--display")
 
66
                                {
 
67
                                        bRawFilter = false;
 
68
                                        bInline    = false;
 
69
                                        bDisplay   = true;
 
70
                                        continue;
 
71
                                }
 
72
                        if (args == "--raw-filter")
 
73
                                {
 
74
                                        bRawFilter = true;
 
75
                                        bPrintItex = false;
 
76
                                        bInline    = false;
 
77
                                        bDisplay   = false;
 
78
                                        continue;
 
79
                                }
 
80
                }
 
81
        if (bStop) return 0;
 
82
 
 
83
        std::string itex;
 
84
 
 
85
        if (bInline)  itex += "$";
 
86
        if (bDisplay) itex += "$$";
 
87
 
 
88
#define BUFSIZE 1024
 
89
        char buffer[BUFSIZE];
 
90
        while (fgets (buffer, BUFSIZE, stdin)) itex += buffer;
 
91
 
 
92
        if (bInline)  itex += "$";
 
93
        if (bDisplay) itex += "$$";
 
94
 
 
95
        if (bPrintItex)
 
96
                {
 
97
                        fputs (itex.c_str (), stdout);
 
98
                        fputs ("\n", stdout);
 
99
                        fflush (stdout);
 
100
                }
 
101
 
 
102
        if (!bInline && !bDisplay)
 
103
                {
 
104
                        if (bRawFilter)
 
105
                                itex2MML_filter (itex.c_str(), itex.size());
 
106
                        else
 
107
                                itex2MML_html_filter (itex.c_str(), itex.size());
 
108
                        return 0;
 
109
                }
 
110
 
 
111
        char * mathml = itex2MML_parse (itex.c_str(), itex.size());
 
112
 
 
113
        if (mathml)
 
114
                {
 
115
                        fputs (mathml, stdout);
 
116
                        fputs ("\n", stdout);
 
117
 
 
118
                        itex2MML_free_string (mathml);
 
119
                        mathml = 0;
 
120
                }
 
121
        else
 
122
                {
 
123
                        fputs ("itex2MML: itex parser failed to generate MathML from itex!\n", stderr);
 
124
                }
 
125
        return 0;
 
126
}