3
# copyright 2003-2007, Jacques Distler
6
package MT::Plugin::itex2MML;
9
use File::Temp qw(tempfile);
11
use vars qw( $VERSION );
14
eval{ require MT::Plugin;};
19
description => "A Text-Filter, translating embedded itex equations into MathML",
20
doc_link => 'http://golem.ph.utexas.edu/~distler/blog/itex2MML.html',
22
MT->add_plugin(new MT::Plugin($plugin));
25
MT->add_text_filter(itexToMML => {
26
label => 'itex to MathML',
27
on_format => sub { &itexToMML; },
29
MT->add_text_filter(itexToMMLpara => {
30
label => 'itex to MathML with parbreaks',
31
on_format => sub { &itexToMMLpara; },
34
my $itex2mml_number_equations = 1;
35
my $itex2mml_binary = "/usr/local/bin/itex2MML";
41
$_ = number_equations($_, $itex2mml_number_equations, $ctx);
42
my ($Reader,$outfile) = tempfile( UNLINK => 1 );
43
my ($Writer,$infile) = tempfile( UNLINK => 1 );
45
system("$itex2mml_binary <$infile >$outfile");
49
eval {unlink ($infile, $outfile);};
57
$_ = number_equations($_, $itex2mml_number_equations, $ctx);
59
my ($Reader,$outfile) = tempfile( UNLINK => 1 );
60
my ($Writer,$infile) = tempfile( UNLINK => 1 );
62
system("$itex2mml_binary <$infile >$outfile");
66
eval {unlink ($infile, $outfile);};
73
my @paras = split /\n{2,}/, $str;
75
if ($p !~ m@^</?(?:h1|h2|h3|h4|h5|h6|table|ol|dl|ul|menu|dir|p|pre|center|form|fieldset|select|blockquote|address|div|hr)@) {
82
sub number_equations {
84
my $arg_value = shift;
87
if ($arg_value == 0) {return $_;}
90
if ((defined $ctx) && (ref($ctx) eq 'MT::Template::Context')) {
91
if ($ctx->stash('comment') ) {
92
$prefix = "c" . $ctx->stash('comment')->id;
93
} elsif ($ctx->stash('entry') ) {
94
$prefix = "e" . $ctx->stash('entry')->id;
97
my $cls = "numberedEq";
102
# add equation numbers to \[...\]
103
# - introduce a wrapper-<div> and a <span> with the equation number
104
while (s/\\\[(.*?)\\\]/\n\n<div class=\"$cls\"><span>\($eqno\)<\/span>\$\$$1\$\$<\/div>\n\n/s) {
108
# assemble equation labels into a hash
109
# - remove the \label{} command, collapse surrounding whitespace
110
# - add an ID to the wrapper-<div>. prefix it to give a fighting chance
111
# for the ID to be unique
112
# - hash key is the equation label, value is the equation number
113
while (s/<div class=\"$cls\"><span>\((\d+)\)<\/span>\$\$((?:[^\$]|\\\$)*)\s*\\label{(\w*)}\s*((?:[^\$]|\\\$)*)\$\$<\/div>/<div class=\"$cls\" id=\"$prefix:$3\"><span>\($1\)<\/span>\$\$$2$4\$\$<\/div>/s) {
114
$eqnumber{"$3"} = $1;
117
# add cross-references
118
# - they can be either (eq:foo) or \eqref{foo}
119
s/\(eq:(\w+)\)/\(<a href=\"#$prefix:$1\">$eqnumber{"$1"}<\/a>\)/g;
120
s/\\eqref\{(\w+)\}/\(<a href=\'#$prefix:$1\'>$eqnumber{"$1"}<\/a>\)/g;