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

use MT;
use File::Temp qw(tempfile);

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; },
});

sub itexToMML {
    $_=shift;
    $_=~ s/\r//g;
    my ($Reader,$outfile) = tempfile( UNLINK => 1 );
    my ($Writer,$infile) = tempfile( UNLINK => 1 );
    print $Writer "$_";
    system("/usr/local/bin/itex2MML <$infile >$outfile");
    my @out = <$Reader>;
    close $Reader;
    close $Writer;
    eval {unlink ($infile, $outfile);};
    join('',@out);
}

sub itexToMMLpara {
    $_=shift;
    $_=~ s/\r//g;
    $_=splitparas($_);
    my ($Reader,$outfile) = tempfile( UNLINK => 1 );
    my ($Writer,$infile) = tempfile( UNLINK => 1 );
    print $Writer "$_";
    system("/usr/local/bin/itex2MML <$infile >$outfile");
    my @out = <$Reader>;
    close $Reader;
    close $Writer;
    eval {unlink ($infile, $outfile);};
    join('',@out);
}

sub splitparas {
    my $str = shift;
    $str ||= '';
    my @paras = split /\n\n/, $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)@) {
            $p = "<p>$p</p>";
        }
    }
    join "\n\n", @paras;
}

1;