Main > Diary > Development
« mtcolorer test | Main | Genes, Genes the Musical Fruit »March 10, 2006
mtcolorer-0.2 plugin source code cleanup
Here is a patch for mtcolorer-0.2 to clean up the source a bit. I was initially having trouble getting the plugin to work and I made some mostly cosmetic improvements to the source in order to facilitate debugging. Happy coding!
Here's the diff (link), using mtcolorer:
--- mtcolorer.pl.orig 2006-03-10 18:48:32.000000000 -0600 +++ mtcolorer.pl 2006-03-10 18:46:19.000000000 -0600 @@ -1,5 +1,5 @@ # --------------------------------------------------------------------------- -# MT-Colorer +# MT-Colorer # A plugin for Movable Type # # Release 1.0.0 @@ -11,66 +11,70 @@ # You may use it for commercial or personal use. # If you distribute it, please keep this notice intact. # -# Copyright (c) 2006 -# --------------------------------------------------------------------------- +# Copyright (c) 2006 +# --------------------------------------------------------------------------- +# +# Additional Changes: +# +# 10-Mar-2006 - minor fixes/clean-up work Jordan Husney <jordan@husney.com> +# package MT::Plugin::Colorer; use Syntax::Highlight::Universal; use strict; use MT; -use MT::Entry; -use MT::Plugin; +use MT::Plugin; use MT::Template::Context; -use MT::WeblogPublisher; -my $highlighter = Syntax::Highlight::Universal->new(); -my $plugin = new MT::Plugin(); -$plugin->name("MT Colorer"); -$plugin->description('Source code hightlighter based on Colorer (http://colorer.sf.net)'); -$plugin->doc_link('http://klim.doslash.org/projects/mtcolorer'); -$plugin->author_name('Vladimir Klimontovich'); +my $highlighter = Syntax::Highlight::Universal->new(); +my $plugin = new MT::Plugin(); +$plugin->name("MT Colorer"); +$plugin->description('Source code hightlighter based on Colorer (http://colorer.sf.net)'); +$plugin->doc_link('http://klim.doslash.org/projects/mtcolorer'); +$plugin->author_name('Vladimir Klimontovich'); $plugin->author_link('http://klim.doslash.org/eng_index.shtml'); $plugin->plugin_link('http://klim.doslash.org/projects/mtcolorer'); -$plugin->version("0.2"); +$plugin->version("0.2-jrh"); +MT->add_plugin($plugin); MT->add_callback("BuildPage", 1, $plugin, \&hightlight_text); -MT->add_plugin($plugin); - -sub trim -{ - my($string)=@_; - for ($string) - { - s/^\s+//; - s/\s+$//; - } - return $string; +sub trim +{ + my($string)=@_; + for ($string) + { + s/^\s+//; + s/\s+$//; + } + return $string; } sub hightlight_block -{ - my ($lang, $source) = @_; - my $htext = $highlighter->highlight($lang, $source); +{ + my ($lang, $source) = @_; + my $htext = $highlighter->highlight($lang, $source); $htext = trim($htext); return "<pre class=\"mtc_block\">".$htext."</pre>"; -} +} sub hightlight_inline -{ - my ($lang, $source) = @_; - my $htext = $highlighter->highlight($lang, $source); +{ + my ($lang, $source) = @_; + my $htext = $highlighter->highlight($lang, $source); $htext = trim($htext); return "<span class=\"mtc_inline\">".$htext."</span>"; -} - -sub hightlight_text -{ +} + +sub hightlight_text +{ my ($cb, %args) = @_; - use Data::Dumper; my $html = ${$args{'Content'}}; + $html =~ s/<mtc:block([^>]*?)lang="([^"]+?)"([^>]*?)>(.*?)<\/mtc:block>/hightlight_block($2,$4)/ges; $html =~ s/<mtc:inline([^>]*?)lang="([^"]+?)"([^>]*?)>(.*?)<\/mtc:inline>/hightlight_inline($2,$4)/ges; - ${$args{'Content'}} = $html; - return 1; + ${$args{'Content'}} = $html; + + return 1; } +1;
Posted by jordanh at March 10, 2006 6:49 PM
Trackback Pings
TrackBack URL for this entry:
http://jordan.husney.com/mt/mt-tb.cgi/237.

Posting comment...