/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/perl/lib/MathML/itex2MML.pm

  • Committer: Jacques Distler
  • Date: 2019-01-03 19:26:23 UTC
  • Revision ID: distler@golem.ph.utexas.edu-20190103192623-k4p7dta8r62qwyr2
Perl bindings

Show diffs side-by-side

added added

removed removed

 
1
# MathML::itex2MML
 
2
#
 
3
# Copyright, Jacques Distler 2018-19.
 
4
# All Rights Reserved.
 
5
# Licensed under the Perl Artistic License.
 
6
#
 
7
# Version: 1.5.10
 
8
 
 
9
package MathML::itex2MML;
 
10
 
 
11
use strict;
 
12
use warnings;
 
13
 
 
14
use base qw(Exporter);
 
15
use base qw(DynaLoader);
 
16
our $VERSION = '1.5.10';
 
17
 
 
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);
 
22
 
 
23
# ---------- BASE METHODS -------------
 
24
 
 
25
package MathML::itex2MML;
 
26
 
 
27
sub itex_html_filter {
 
28
    my $text = shift(@_);
 
29
    itex2MML_html_filter($text, length($text));
 
30
    return itex2MML_output();
 
31
}
 
32
 
 
33
sub itex_filter {
 
34
    my $text = shift(@_);
 
35
    itex2MML_filter($text, length($text));
 
36
    return itex2MML_output();
 
37
}
 
38
 
 
39
sub itex_inline_filter {
 
40
    my $text = shift(@_);
 
41
    itex2MML_filter('$' . $text . '$', length($text) + 2);
 
42
    return itex2MML_output();
 
43
}
 
44
 
 
45
sub itex_block_filter {
 
46
    my $text = shift(@_);
 
47
    itex2MML_filter('$$' . $text . '$$', length($text) + 4);
 
48
    return itex2MML_output();
 
49
}
 
50
 
 
51
sub TIEHASH {
 
52
    my ($classname,$obj) = @_;
 
53
    return bless $obj, $classname;
 
54
}
 
55
 
 
56
sub CLEAR { }
 
57
 
 
58
sub FIRSTKEY { }
 
59
 
 
60
sub NEXTKEY { }
 
61
 
 
62
sub FETCH {
 
63
    my ($self,$field) = @_;
 
64
    my $member_func = "swig_${field}_get";
 
65
    $self->$member_func();
 
66
}
 
67
 
 
68
sub STORE {
 
69
    my ($self,$field,$newval) = @_;
 
70
    my $member_func = "swig_${field}_set";
 
71
    $self->$member_func($newval);
 
72
}
 
73
 
 
74
sub this {
 
75
    my $ptr = shift;
 
76
    return tied(%$ptr);
 
77
}
 
78
 
 
79
 
 
80
# ------- FUNCTION WRAPPERS --------
 
81
 
 
82
package MathML::itex2MML;
 
83
 
 
84
*itex2MML_filter = *MathML::itex2MMLc::itex2MML_filter;
 
85
*itex2MML_html_filter = *MathML::itex2MMLc::itex2MML_html_filter;
 
86
*itex2MML_output = *MathML::itex2MMLc::itex2MML_output;
 
87
 
 
88
# ------- VARIABLE STUBS --------
 
89
 
 
90
package MathML::itex2MML;
 
91
 
 
92
1;
 
93
__END__
 
94
 
 
95
=head1 NAME
 
96
 
 
97
MathML::itex2MML - Convert itex to MathML
 
98
 
 
99
=head1 SYNOPSIS
 
100
 
 
101
 use MathML::itex2MML;
 
102
 
 
103
 $text    = 'This is an inline equation: $\sin(\pi/2)=1$.';
 
104
 
 
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>&pi;</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>.
 
107
 
 
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>&pi;</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>
 
110
 
 
111
 $text    = '\sin(\pi/2)=1';
 
112
 
 
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>&pi;</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>
 
115
 
 
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>&pi;</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>
 
118
 
 
119
=head1 DESCRIPTION
 
120
 
 
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>.
 
125
 
 
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.
 
129
 
 
130
C<MathML::itex2MML> is based on the commandline converter, itex2MML.
 
131
 
 
132
=head1 FUNCTIONS
 
133
 
 
134
The following functions are exported by default.
 
135
 
 
136
=over 4
 
137
 
 
138
=item * itex2MML_html_filter
 
139
 
 
140
Take a text string, with embedded itex equations, and convert all the equations to MathML, passing through the rest of the text.
 
141
 
 
142
=item * itex2MML_filter
 
143
 
 
144
Take a text string, with embedded itex equations, and convert all the equations to MathML, dropping the rest of the text.
 
145
 
 
146
=item * itex2MML_inline_filter
 
147
 
 
148
Convert a single equation (without the enclosing $'s) to inline MathML.
 
149
 
 
150
=item * itex2MML_block_filter
 
151
 
 
152
Convert a single equation (without the enclosing $$'s) to block-display MathML.
 
153
 
 
154
=back
 
155
 
 
156
=head1 AUTHOR
 
157
 
 
158
Jacques Distler E<lt>distler@golem.ph.utexas.eduE<gt>
 
159
 
 
160
=head1 COPYRIGHT
 
161
 
 
162
Copyright (c) 2018-19 Jacques Distler. All rights reserved.
 
163
 
 
164
This library is free software; you can redistribute it and/or modify
 
165
it under the same terms as Perl itself.
 
166
 
 
167
=head1 SEE ALSO
 
168
 
 
169
L<MathML::Entitities|MathML::Entitities>
 
170
 
 
171
L<https://golem.ph.utexas.edu/~distler/blog/itex2MMLcommands.html>
 
172
 
 
173
L<https://rubygems.org/gems/itextomml>