/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
# itex2MML
# version 2.0
# copyright 2003-2019, Jacques Distler
#

package MT::Plugin::itex2MML;

use MT;
use MathML::itex2MML qw( itex_html_filter );

use vars qw( $VERSION );
$VERSION = '2.0';

eval{ require MT::Plugin;};
unless ($@) {
   my $plugin = {
      name => "itex2MML",
      version => $VERSION,
      description => "A Text-Filter, translating embedded itex equations into MathML",
      doc_link => 'http://golem.ph.utexas.edu/~distler/blog/itex2MML.html',
   };
   MT->add_plugin(new MT::Plugin($plugin));
}

MT->add_text_filter(itexToMML => {
	label => 'itex to MathML',
	on_format => sub { &itexToMML; },
});
MT->add_text_filter(itexToMMLpara => {
        label => 'itex to MathML with parbreaks',
        on_format => sub { &itexToMMLpara; },
});

my $itex2mml_number_equations = 1;

sub itexToMML {
    $_=shift;
    $ctx = shift;
    $_=~ s/\r//g;
    $_ = number_equations($_, $itex2mml_number_equations, $ctx);
    my $text = itex_html_filter($_);
    "<div><a href=\"http://golem.ph.utexas.edu/~distler/blog/mathml.html\" onclick=\"window.open\(this.href, \'MathML\', \'width=310,height=150,scrollbars=no,resizable=yes,status=no\'\); return false;\" onkeypress=\"if\(window.event.keyCode == 13\)\{window.open\(this.href, \'MathML\', \'width=310,height=150,scrollbars=no,resizable=yes,status=no\'\); return false;\}\"><img class=\"mathlogo\" src=\"https://golem.ph.utexas.edu/~distler/blog/images/MathML.png\" alt=\"MathML-enabled post \(click for more details\).\" title=\"MathML-enabled post \(click for details\).\" /></a></div>\n" . $text;
}

sub itexToMMLpara {
    $_ = shift;
    $ctx = shift;
    $_=~ s/\r//g;
    $_ = number_equations($_, $itex2mml_number_equations, $ctx);
    $_ = splitparas($_);
    my $text = itex_html_filter($_);
    "<div><a href=\"http://golem.ph.utexas.edu/~distler/blog/mathml.html\" onclick=\"window.open\(this.href, \'MathML\', \'width=310,height=150,scrollbars=no,resizable=yes,status=no\'\); return false;\" onkeypress=\"if\(window.event.keyCode == 13\)\{window.open\(this.href, \'MathML\', \'width=310,height=150,scrollbars=no,resizable=yes,status=no\'\); return false;\}\"><img class=\"mathlogo\" src=\"https://golem.ph.utexas.edu/~distler/blog/images/MathML.png\" alt=\"MathML-enabled post \(click for more details\).\" title=\"MathML-enabled post \(click for details\).\" /></a></div>\n" . $text;
}

sub splitparas {
    my $str = shift;
    $str ||= '';
    my @paras = split /\n{2,}/, $str;
    for my $p (@paras) {
        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)@) {
#        if ($p !~ m@^\s*</?(?:h1|h2|h3|h4|h5|h6|table|ol|dl|ul|menu|dir|p|pre|center|form|fieldset|select|blockquote|address|div|hr)@) {
            $p = "<p>$p</p>";
        }
    }
    join "\n\n", @paras;
}

sub number_equations {
  $_ = shift;
  my $arg_value = shift;
  my $ctx = shift;

  if ($arg_value == 0) {return $_;}
  
  my $prefix = "eq";
  if ((defined $ctx)  && (ref($ctx) eq 'MT::Template::Context')) {
    if ($ctx->stash('comment') ) {
       $prefix = "c" . $ctx->stash('comment')->id;
    } elsif ($ctx->stash('entry') ) {
       $prefix = "e" . $ctx->stash('entry')->id;
    }
  }
  my $cls = "numberedEq";

  my %eqnumber;
  my $eqno=1;

  # add equation numbers to \[...\]
  #  - introduce a wrapper-<div> and a <span> with the equation number
#  while (s/(^\s*)?\\\[(.*?)\\\]/\n\n$1<div class=\"$cls\"><span>\($eqno\)<\/span>\$\$$2\$\$<\/div>\n\n/s) {
  while (s/\\\[(.*?)\\\]/\n\n<div class=\"$cls\"><span>\($eqno\)<\/span>\$\$$1\$\$<\/div>\n\n/s) {
    $eqno++;
  }

  # assemble equation labels into a hash
  # - remove the \label{} command, collapse surrounding whitespace
  # - add an ID to the wrapper-<div>. prefix it to give a fighting chance
  #   for the ID to be unique
  # - hash key is the equation label, value is the equation number
  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) {
    $eqnumber{"$3"} = $1;
  }

  # add cross-references
  # - they can be either (eq:foo) or \eqref{foo}
  s/\(eq:(\w+)\)/\(<a href=\"#$prefix:$1\">$eqnumber{"$1"}<\/a>\)/g;
  s/\\eqref\{(\w+)\}/\(<a href=\'#$prefix:$1\'>$eqnumber{"$1"}<\/a>\)/g;
  
  return $_;
}

1;