3
# Copyright, Jacques Distler 2018-19.
5
# Licensed under the Perl Artistic License.
9
package MathML::itex2MML;
14
use base qw(Exporter);
15
use base qw(DynaLoader);
16
our $VERSION = '1.5.10';
18
package MathML::itex2MMLc;
19
bootstrap MathML::itex2MML;
20
package MathML::itex2MML;
21
our @EXPORT = qw(itex_html_filter itex_filter itex_inline_filter itex_block_filter);
23
# ---------- BASE METHODS -------------
25
package MathML::itex2MML;
27
sub itex_html_filter {
29
itex2MML_html_filter($text, length($text));
30
return itex2MML_output();
35
itex2MML_filter($text, length($text));
36
return itex2MML_output();
39
sub itex_inline_filter {
41
itex2MML_filter('$' . $text . '$', length($text) + 2);
42
return itex2MML_output();
45
sub itex_block_filter {
47
itex2MML_filter('$$' . $text . '$$', length($text) + 4);
48
return itex2MML_output();
52
my ($classname,$obj) = @_;
53
return bless $obj, $classname;
63
my ($self,$field) = @_;
64
my $member_func = "swig_${field}_get";
65
$self->$member_func();
69
my ($self,$field,$newval) = @_;
70
my $member_func = "swig_${field}_set";
71
$self->$member_func($newval);
80
# ------- FUNCTION WRAPPERS --------
82
package MathML::itex2MML;
84
*itex2MML_filter = *MathML::itex2MMLc::itex2MML_filter;
85
*itex2MML_html_filter = *MathML::itex2MMLc::itex2MML_html_filter;
86
*itex2MML_output = *MathML::itex2MMLc::itex2MML_output;
88
# ------- VARIABLE STUBS --------
90
package MathML::itex2MML;
97
MathML::itex2MML - Convert itex to MathML
101
use MathML::itex2MML;
103
$text = 'This is an inline equation: $\sin(\pi/2)=1$.';
105
# convert embedded itex equations to MathML:
106
$converted = itex_html_filter($text); # This is an inline equation: <math xmlns='http://www.w3.org/1998/Math/MathML' display='inline'><semantics><mrow><mi>sin</mi><mo stretchy="false">(</mo><mi>π</mi><mo stretchy="false">/</mo><mn>2</mn><mo stretchy="false">)</mo><mo>=</mo><mn>1</mn></mrow><annotation encoding='application/x-tex'>\sin(\pi/2)=1</annotation></semantics></math>.
108
# just the equations:
109
$converted = itex_filter($text); # <math xmlns='http://www.w3.org/1998/Math/MathML' display='inline'><semantics><mrow><mi>sin</mi><mo stretchy="false">(</mo><mi>π</mi><mo stretchy="false">/</mo><mn>2</mn><mo stretchy="false">)</mo><mo>=</mo><mn>1</mn></mrow><annotation encoding='application/x-tex'>\sin(\pi/2)=1</annotation></semantics></math>
111
$text = '\sin(\pi/2)=1';
113
# inline equation (without the $'s)
114
$converted = itex_inline_filter($text); # <math xmlns='http://www.w3.org/1998/Math/MathML' display='inline'><semantics><mrow><mi>sin</mi><mo stretchy="false">(</mo><mi>π</mi><mo stretchy="false">/</mo><mn>2</mn><mo stretchy="false">)</mo><mo>=</mo><mn>1</mn></mrow><annotation encoding='application/x-tex'>\sin(\pi/2)=1</annotation></semantics></math>
116
# block equation (without the $$'s)
117
$converted = itex_block_filter($text); # <math xmlns='http://www.w3.org/1998/Math/MathML' display='block'><semantics><mrow><mi>sin</mi><mo stretchy="false">(</mo><mi>π</mi><mo stretchy="false">/</mo><mn>2</mn><mo stretchy="false">)</mo><mo>=</mo><mn>1</mn></mrow><annotation encoding='application/x-tex'>\sin(\pi/2)=1</annotation></semantics></math>
121
C<MathML::itex2MML> converts itex (a dialect of LaTeX) equations into
122
MathML. Inline equations are demarcated by C<$..$> or C<\(...\)>. Display
123
equations are demarcated by C<$$...$$> or C<\[...\]>. The syntax supported
124
is described L<here|https://golem.ph.utexas.edu/~distler/blog/itex2MMLcommands.html>.
126
It is strongly suggested that you run the output through C<MathML::Entities>,
127
to convert named entities into either numeric character references or UTF-8 characters,
128
if you intend putting the result on the Web.
130
C<MathML::itex2MML> is based on the commandline converter, itex2MML.
134
The following functions are exported by default.
138
=item * itex2MML_html_filter
140
Take a text string, with embedded itex equations, and convert all the equations to MathML, passing through the rest of the text.
142
=item * itex2MML_filter
144
Take a text string, with embedded itex equations, and convert all the equations to MathML, dropping the rest of the text.
146
=item * itex2MML_inline_filter
148
Convert a single equation (without the enclosing $'s) to inline MathML.
150
=item * itex2MML_block_filter
152
Convert a single equation (without the enclosing $$'s) to block-display MathML.
158
Jacques Distler E<lt>distler@golem.ph.utexas.eduE<gt>
162
Copyright (c) 2018-19 Jacques Distler. All rights reserved.
164
This library is free software; you can redistribute it and/or modify
165
it under the same terms as Perl itself.
169
L<MathML::Entitities|MathML::Entitities>
171
L<https://golem.ph.utexas.edu/~distler/blog/itex2MMLcommands.html>
173
L<https://rubygems.org/gems/itextomml>