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