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

  • Committer: Jacques Distler
  • Date: 2007-01-11 07:17:18 UTC
  • Revision ID: distler@golem.ph.utexas.edu-20070111071718-3azxrr5b29d00h26
Initial commit.

Show diffs side-by-side

added added

removed removed

 
1
# itex2MML
 
2
# version 0.12
 
3
# copyright 2003-2005, Jacques Distler
 
4
#
 
5
 
 
6
use MT;
 
7
use File::Temp qw(tempfile);
 
8
 
 
9
MT->add_text_filter(itexToMML => {
 
10
        label => 'itex to MathML',
 
11
        on_format => sub { &itexToMML; },
 
12
});
 
13
MT->add_text_filter(itexToMMLpara => {
 
14
        label => 'itex to MathML with parbreaks',
 
15
        on_format => sub { &itexToMMLpara; },
 
16
});
 
17
 
 
18
sub itexToMML {
 
19
    $_=shift;
 
20
    $_=~ s/\r//g;
 
21
    my ($Reader,$outfile) = tempfile( UNLINK => 1 );
 
22
    my ($Writer,$infile) = tempfile( UNLINK => 1 );
 
23
    print $Writer "$_";
 
24
    system("/usr/local/bin/itex2MML <$infile >$outfile");
 
25
    my @out = <$Reader>;
 
26
    close $Reader;
 
27
    close $Writer;
 
28
    eval {unlink ($infile, $outfile);};
 
29
    join('',@out);
 
30
}
 
31
 
 
32
sub itexToMMLpara {
 
33
    $_=shift;
 
34
    $_=~ s/\r//g;
 
35
    $_=splitparas($_);
 
36
    my ($Reader,$outfile) = tempfile( UNLINK => 1 );
 
37
    my ($Writer,$infile) = tempfile( UNLINK => 1 );
 
38
    print $Writer "$_";
 
39
    system("/usr/local/bin/itex2MML <$infile >$outfile");
 
40
    my @out = <$Reader>;
 
41
    close $Reader;
 
42
    close $Writer;
 
43
    eval {unlink ($infile, $outfile);};
 
44
    join('',@out);
 
45
}
 
46
 
 
47
sub splitparas {
 
48
    my $str = shift;
 
49
    $str ||= '';
 
50
    my @paras = split /\n\n/, $str;
 
51
    for my $p (@paras) {
 
52
        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)@) {
 
53
            $p = "<p>$p</p>";
 
54
        }
 
55
    }
 
56
    join "\n\n", @paras;
 
57
}
 
58
 
 
59
1;