/itexToMML

To download this project, use:
bzr branch http://golem.ph.utexas.edu/~distler/code/itexToMML/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*             itex2MML 1.3.1
 *   itex2MML.cc last modified 10/24/2007
 */

#include <cstdio>

#include <string>

#include "itex2MML.h"

int main (int argc, char ** argv)
{
	bool bPrintItex = false;
	bool bRawFilter = false;
	bool bInline    = false;
	bool bDisplay   = false;

	bool bStop = false;
	bool bForbidMarkup = false;

	for (int arg = 1; arg < argc; arg++)
		{
			std::string args = argv[arg];

			if (args == "--help")
				{
					fputs ("usage: itex2MML [OPTIONS]\n"
						   "\n"
						   "itex2MML filters an input text stream (e.g., an XHTML web page) converting itex expressions\n"
						   "to MathML. Inline itex expressions are delimited either side by single dollar symbols ($):\n"
						   "\n"
						   "\t<p>The parameters $\\alpha$ and $\\beta$ in the function $f(x)$ are defined below.</p>\n"
						   "\n"
						   "For normal display of equations, etc., itex expressions can be delimited with double dollar\n"
						   "symbols ($$) either side or by \\[ to the left and \\] to the right:\n"
						   "\n"
						   "\t<p class=\"equation\">\\[\n"
						   "\t\tf(x) = \\alpha x + \\frac{\\beta}{1+|x|}\n"
						   "\t\\]</p>\n"
						   "\n"
						   "itex2MML Options:\n"
						   "\n"
						   "  --raw-filter    filter input stream, converting equations as found to MathML [stops on error]\n"
						   "  --inline        converts a single itex equation, without any $ symbols, to inline MathML\n"
						   "  --display       converts a single itex equation, without any $ symbols, to display-mode MathML\n"
						   "  --forbid-markup forbid markup (more precisely, the '<' and '>' characters) in itex equations\n"
						   "  --print-itex    used in conjuction with --inline or --display: prints the itex string\n"
						   "\n"
						   "For further information, see http://golem.ph.utexas.edu/~distler/blog/itex2MML.html\n", stdout);

					bStop = true;
					break;
				}
			if (args == "--print-itex")
				{
					bPrintItex = true;
					bRawFilter = false;
					continue;
				}
			if (args == "--forbid-markup")
				{
					bRawFilter = false;
					bForbidMarkup = true;
					continue;
				}
			if (args == "--inline")
				{
					bRawFilter = false;
					bInline    = true;
					bDisplay   = false;
					continue;
				}
			if (args == "--display")
				{
					bRawFilter = false;
					bInline    = false;
					bDisplay   = true;
					continue;
				}
			if (args == "--raw-filter")
				{
					bRawFilter = true;
					bPrintItex = false;
					bInline    = false;
					bDisplay   = false;
					continue;
				}
		}
	if (bStop) return 0;

	std::string itex;

	if (bInline)  itex += "$";
	if (bDisplay) itex += "$$";

#define BUFSIZE 1024
	char buffer[BUFSIZE];
	while (fgets (buffer, BUFSIZE, stdin)) itex += buffer;

	if (bInline)  itex += "$";
	if (bDisplay) itex += "$$";

	if (bPrintItex)
		{
			fputs (itex.c_str (), stdout);
			fputs ("\n", stdout);
			fflush (stdout);
		}

	if (!bInline && !bDisplay)
		{
			if (bRawFilter)
				itex2MML_filter (itex.c_str(), itex.size());
			else
				if (bForbidMarkup)
					itex2MML_strict_html_filter (itex.c_str(), itex.size());
				else
					itex2MML_html_filter (itex.c_str(), itex.size());
			return 0;
		}

	char * mathml = itex2MML_parse (itex.c_str(), itex.size());

	if (mathml)
		{
			fputs (mathml, stdout);
			fputs ("\n", stdout);

			itex2MML_free_string (mathml);
			mathml = 0;
		}
	else
		{
			fputs ("itex2MML: itex parser failed to generate MathML from itex!\n", stderr);
		}
	return 0;
}