bzr branch
http://golem.ph.utexas.edu/~distler/code/itexToMML/
|
7
by Jacques Distler
Rename plugin directory to plugins. |
1 |
# itex2MML |
|
81
by Jacques Distler
New version of MovableType plugin |
2 |
# version 2.0 |
3 |
# copyright 2003-2019, Jacques Distler |
|
|
7
by Jacques Distler
Rename plugin directory to plugins. |
4 |
# |
5 |
||
6 |
package MT::Plugin::itex2MML; |
|
7 |
||
8 |
use MT; |
|
|
81
by Jacques Distler
New version of MovableType plugin |
9 |
use MathML::itex2MML qw( itex_html_filter ); |
|
7
by Jacques Distler
Rename plugin directory to plugins. |
10 |
|
11 |
use vars qw( $VERSION ); |
|
|
81
by Jacques Distler
New version of MovableType plugin |
12 |
$VERSION = '2.0'; |
|
7
by Jacques Distler
Rename plugin directory to plugins. |
13 |
|
14 |
eval{ require MT::Plugin;};
|
|
15 |
unless ($@) {
|
|
16 |
my $plugin = {
|
|
17 |
name => "itex2MML", |
|
18 |
version => $VERSION, |
|
19 |
description => "A Text-Filter, translating embedded itex equations into MathML", |
|
20 |
doc_link => 'http://golem.ph.utexas.edu/~distler/blog/itex2MML.html', |
|
21 |
}; |
|
22 |
MT->add_plugin(new MT::Plugin($plugin)); |
|
23 |
} |
|
24 |
||
25 |
MT->add_text_filter(itexToMML => {
|
|
26 |
label => 'itex to MathML', |
|
27 |
on_format => sub { &itexToMML; },
|
|
28 |
}); |
|
29 |
MT->add_text_filter(itexToMMLpara => {
|
|
30 |
label => 'itex to MathML with parbreaks', |
|
31 |
on_format => sub { &itexToMMLpara; },
|
|
32 |
}); |
|
33 |
||
34 |
my $itex2mml_number_equations = 1; |
|
35 |
||
36 |
sub itexToMML {
|
|
37 |
$_=shift; |
|
38 |
$ctx = shift; |
|
39 |
$_=~ s/\r//g; |
|
40 |
$_ = number_equations($_, $itex2mml_number_equations, $ctx); |
|
|
83
by Jacques Distler
Ack. Post vanilla version of the MT plugin |
41 |
itex_html_filter($_); |
|
7
by Jacques Distler
Rename plugin directory to plugins. |
42 |
} |
43 |
||
44 |
sub itexToMMLpara {
|
|
45 |
$_ = shift; |
|
46 |
$ctx = shift; |
|
47 |
$_=~ s/\r//g; |
|
48 |
$_ = number_equations($_, $itex2mml_number_equations, $ctx); |
|
49 |
$_ = splitparas($_); |
|
|
83
by Jacques Distler
Ack. Post vanilla version of the MT plugin |
50 |
itex_html_filter($_); |
|
7
by Jacques Distler
Rename plugin directory to plugins. |
51 |
} |
52 |
||
53 |
sub splitparas {
|
|
54 |
my $str = shift; |
|
55 |
$str ||= ''; |
|
56 |
my @paras = split /\n{2,}/, $str;
|
|
57 |
for my $p (@paras) {
|
|
58 |
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)@) {
|
|
|
81
by Jacques Distler
New version of MovableType plugin |
59 |
# 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)@) {
|
|
7
by Jacques Distler
Rename plugin directory to plugins. |
60 |
$p = "<p>$p</p>"; |
61 |
} |
|
62 |
} |
|
63 |
join "\n\n", @paras; |
|
64 |
} |
|
65 |
||
66 |
sub number_equations {
|
|
67 |
$_ = shift; |
|
68 |
my $arg_value = shift; |
|
69 |
my $ctx = shift; |
|
70 |
||
71 |
if ($arg_value == 0) {return $_;}
|
|
72 |
||
73 |
my $prefix = "eq"; |
|
74 |
if ((defined $ctx) && (ref($ctx) eq 'MT::Template::Context')) {
|
|
75 |
if ($ctx->stash('comment') ) {
|
|
76 |
$prefix = "c" . $ctx->stash('comment')->id;
|
|
77 |
} elsif ($ctx->stash('entry') ) {
|
|
78 |
$prefix = "e" . $ctx->stash('entry')->id;
|
|
79 |
} |
|
80 |
} |
|
81 |
my $cls = "numberedEq"; |
|
82 |
||
83 |
my %eqnumber; |
|
84 |
my $eqno=1; |
|
85 |
||
86 |
# add equation numbers to \[...\] |
|
87 |
# - introduce a wrapper-<div> and a <span> with the equation number |
|
|
81
by Jacques Distler
New version of MovableType plugin |
88 |
# while (s/(^\s*)?\\\[(.*?)\\\]/\n\n$1<div class=\"$cls\"><span>\($eqno\)<\/span>\$\$$2\$\$<\/div>\n\n/s) {
|
|
7
by Jacques Distler
Rename plugin directory to plugins. |
89 |
while (s/\\\[(.*?)\\\]/\n\n<div class=\"$cls\"><span>\($eqno\)<\/span>\$\$$1\$\$<\/div>\n\n/s) {
|
90 |
$eqno++; |
|
91 |
} |
|
92 |
||
93 |
# assemble equation labels into a hash |
|
94 |
# - remove the \label{} command, collapse surrounding whitespace
|
|
95 |
# - add an ID to the wrapper-<div>. prefix it to give a fighting chance |
|
96 |
# for the ID to be unique |
|
97 |
# - hash key is the equation label, value is the equation number |
|
98 |
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) {
|
|
99 |
$eqnumber{"$3"} = $1;
|
|
100 |
} |
|
101 |
||
102 |
# add cross-references |
|
103 |
# - they can be either (eq:foo) or \eqref{foo}
|
|
104 |
s/\(eq:(\w+)\)/\(<a href=\"#$prefix:$1\">$eqnumber{"$1"}<\/a>\)/g;
|
|
105 |
s/\\eqref\{(\w+)\}/\(<a href=\'#$prefix:$1\'>$eqnumber{"$1"}<\/a>\)/g;
|
|
106 |
||
107 |
return $_; |
|
108 |
} |
|
109 |
||
110 |
1; |