Latex / LaTeX.pl

# MT LaTeX Filter
#
# Nick O'Neill - nick@sixapart.com - http://www.raquo.net/fine-structure/
# 

package MT::Plugin::LaTeX;

use strict;

use base qw( MT::Plugin );
use LaTeXRender;
use HTML::Entities;
use CGI qw(escapeHTML);
use Data::Dumper;

our $VERSION = '1.1';

my $plugin = MT::Plugin::LaTeX->new({
   id          => 'LaTeX',
   key         => 'latex',
   name        => 'LaTeX',
   description => "Render mathematical formulas on you blog using the LaTeX Rendering Engine and this handy text filter.",
   version     => $VERSION,
   author_name => "Nick O'Neill",
   author_link => "http://www.raquo.net/fine-structure/",
   plugin_link => "#",
   system_config_template => 'system_config.tmpl',
   settings    => new MT::PluginSettings([
       [ 'tools_location', { Default =>  '/usr/bin', Scope => 'system' } ],
   ]),
});

MT->add_plugin($plugin);
sub init_registry {
    my $plugin = shift;
    $plugin->registry({
        tags => {
            modifier => {
                'latex' => \&_fltr_latex,
            },
        },
    });
}

sub instance { $plugin }

sub _fltr_latex {
    my ($str, $val, $ctx) = @_;
    my $plugin = MT::Plugin::LaTeX->instance;
    
    my $blog = $ctx->stash('blog');
    
    my $latexrender_path = $blog->{'column_values'}{'site_path'};
    my $latexrender_path_http = $blog->{'column_values'}{'site_url'};
    
    my $tools_location = $plugin->get_config_value( 'tools_location', 'system' );
    
    my $latex = new LaTeXRender( "$latexrender_path/latex",
                                 $latexrender_path_http."latex",
                                 "$latexrender_path/tmp",
                                 $tools_location, );

    while ($str =~ m/\$latex\s(.*?)\$/si ) {
        
        my ( $left, $latex_formula, $right ) = ( $`, $1, $' );
       
        $latex_formula = decode_entities($latex_formula);

        my $url = $latex->getFormulaURL($latex_formula);

        my $alt_latex_formula = escapeHTML($latex_formula);
        $alt_latex_formula =~ s/\\r/
/g;
        $alt_latex_formula =~ s/\\n/
/g;

        if ($url) {
            $str =
                "$left <img src=\"$url\" title=\"$alt_latex_formula\" alt=\"$alt_latex_formula\" align=\"absmiddle\" /> $right";
        } else {
            $str =
                "$left [Unparseable or potentially dangerous latex formula. Error $_errorcode $_errorextra] $right"; 		
        } 
    }
		return $str;
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.