/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 plugin/itex2MML/itex2MML.pl

  • Committer: Jacques Distler
  • Date: 2007-01-18 15:08:31 UTC
  • Revision ID: distler@golem.ph.utexas.edu-20070118150831-72tmyxmgpcov01dn
Rename plugin directory to plugins.

Show diffs side-by-side

added added

removed removed

1
 
# itex2MML
2
 
# version 1.0
3
 
# copyright 2003-2007, Jacques Distler
4
 
#
5
 
 
6
 
package MT::Plugin::itex2MML;
7
 
 
8
 
use MT;
9
 
use File::Temp qw(tempfile);
10
 
 
11
 
use vars qw( $VERSION );
12
 
$VERSION = '1.0';
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
 
my $itex2mml_binary = "/usr/local/bin/itex2MML";
36
 
 
37
 
sub itexToMML {
38
 
    $_=shift;
39
 
    $ctx = shift;
40
 
    $_=~ s/\r//g;
41
 
    $_ = number_equations($_, $itex2mml_number_equations, $ctx);
42
 
    my ($Reader,$outfile) = tempfile( UNLINK => 1 );
43
 
    my ($Writer,$infile) = tempfile( UNLINK => 1 );
44
 
    print $Writer "$_";
45
 
    system("$itex2mml_binary <$infile >$outfile");
46
 
    my @out = <$Reader>;
47
 
    close $Reader;
48
 
    close $Writer;
49
 
    eval {unlink ($infile, $outfile);};
50
 
    join('',@out);
51
 
}
52
 
 
53
 
sub itexToMMLpara {
54
 
    $_ = shift;
55
 
    $ctx = shift;
56
 
    $_=~ s/\r//g;
57
 
    $_ = number_equations($_, $itex2mml_number_equations, $ctx);
58
 
    $_ = splitparas($_);
59
 
    my ($Reader,$outfile) = tempfile( UNLINK => 1 );
60
 
    my ($Writer,$infile) = tempfile( UNLINK => 1 );
61
 
    print $Writer "$_";
62
 
    system("$itex2mml_binary <$infile >$outfile");
63
 
    my @out = <$Reader>;
64
 
    close $Reader;
65
 
    close $Writer;
66
 
    eval {unlink ($infile, $outfile);};
67
 
    join('',@out);
68
 
}
69
 
 
70
 
sub splitparas {
71
 
    my $str = shift;
72
 
    $str ||= '';
73
 
    my @paras = split /\n{2,}/, $str;
74
 
    for my $p (@paras) {
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)@) {
76
 
            $p = "<p>$p</p>";
77
 
        }
78
 
    }
79
 
    join "\n\n", @paras;
80
 
}
81
 
 
82
 
sub number_equations {
83
 
  $_ = shift;
84
 
  my $arg_value = shift;
85
 
  my $ctx = shift;
86
 
 
87
 
  if ($arg_value == 0) {return $_;}
88
 
  
89
 
  my $prefix = "eq";
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;
95
 
    }
96
 
  }
97
 
  my $cls = "numberedEq";
98
 
 
99
 
  my %eqnumber;
100
 
  my $eqno=1;
101
 
 
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) {
105
 
    $eqno++;
106
 
  }
107
 
 
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;
115
 
  }
116
 
 
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;
121
 
  
122
 
  return $_;
123
 
}
124
 
 
125
 
1;