# Blosxom Plugin: interpolate_fancy # Author(s): Victor Ganata # Version: 0.2 # Documentation: See the bottom of this file or type: # perldoc interpolate_pseudoxml package interpolate_pseudoxml; # --- Configurable variables ----- # -------------------------------- sub start { 1; } sub interpolate { return sub { package blosxom; my $template = shift; # Defined # e.g. display if defined $template =~ s#(.*?)<\/blosxom:if-exists>#"defined $1 ? \$2 : undef"#sgee; # Undefined # e.g. display if not defined $template =~ s#(.*?)<\/blosxom:if-not-exists>#"!defined $1 ? \$2 : undef"#sgee; # Tests # eq (eq), ne (ne), lt (<), gt (>), like (=~), unlike (!~) # e.g. display if $var less than 123 $template =~ s#(.*?)<\/blosxom:test>#"interpolate_pseudoxml::_test(qq{$1}, q{$2}, q{$3})"#sgee; # Actions # e.g. # e.g. pass content $template =~ s#(.*?)<\/\blosxom:action>|\s+?\/>)#&interpolate_pseudoxml::_action($1, $2, $3, $4)#ge; # Unconditional, Recursive # e.g. {$var} while( $template =~ s/\{\$([a-zA-Z?]\w+(?:::)?\w*)\}/"defined \$$1 ? \$$1 : undef"/gee ) { } return $template; }; } sub _test { my($variable, $attr, $content) = @_; my $result; my $attributes = interpolate_pseudoxml::_attributes($attr); $attributes->{eq} and return $variable eq $attributes->{eq} ? $content : undef; $attributes->{ne} and return $variable ne $attributes->{ne} ? $content : undef; $attributes->{gt} and return $variable > $attributes->{gt} ? $content : undef; $attributes->{lt} and return $variable < $attributes->{lt} ? $content : undef; $attributes->{like} and return $variable =~ /$attributes->{like}/ ? $content : undef; $attributes->{unlike} and return $variable !~ /$attributes->{unlike}/ ? $content : undef; return undef; } sub _action { my($plugin, $action, $attr, $content) = @_; my $result; my $attributes = interpolate_pseudoxml::_attributes($attr); $blosxom::plugins{$plugin} and $plugin->can($action) and $result = $plugin->$action($attributes, $content); return $attributes->{'output'} =~ /yes/i ? $result : undef; } sub _attributes { my $attr = shift; my $attributes = {}; while ( $attr =~ /\b(\w+?)\s*?=\s*?(["'])(.*?)\2/g ) { $attributes->{$1} = $3; # warn "found $1=$3\n"; } return $attributes; } 1; __END__ =head1 NAME Blosxom Plug-in: interpolate_pseudoxml =head1 SYNOPSIS Overrides Blosxom's far simpler to use, but more limited, default interpolate() subroutine. Perform actions (i.e. call plug-in subroutines) at any point in your page, whether to act on current content and return results or no. Include bits of text and template variable values in templates, either conditionally or unconditionally: =head2 Actions Perform an action (i.e. call a plug-in's subroutine) at any point in your page. Optionally pass arguments as key/value pairs stated as XML-style attributes. For example: calls &plugin::subroutine( {'arg1'=>'a', 'arg2'=>'bee' } ). Specify that results should be sent to the browser using the output="yes" attribute like so: Otherwise, subroutines will still have their effect, but the results will be tossed out. Content wrapped in the action call is sent as another argument to the subroutine: pass this content This calls &plugin::subroutine( {'encoding'=>'Latin1', 'output'=>'yes' }, "pass this content" ). For those of you interested in writing plugin actions, here's a sample subroutine to (for no real reason) substitute a particular character with another: sub silly_substitute { my($self, $attributes, $content) = @_; $content =~ s/$attributes->{'from'}/$attributes->{'to'}/g; return $content; } =head2 Includes * Unconditionally and recursively e.g. include a link to the story's path using various template variables. {$path} * The template variable has a value (i.e. is defined) e.g. include a hyperlink to the story's path if it has a path (i.e. $path is defined). {$path} * The template variable doesn't have a value (i.e. is NOT defined) e.g. include a hyperlink to home if path is undefined. home * The template variable is equal (=) to a particular value e.g. include "1 writeback" (singular) if the value of writeback count is 1 {$writeback::count} writeback * The template variable is not equal (!=) to a particular value e.g. include "x writebacks" (plural) if the value of writeback count (x) is not 1 {$writeback::count} writebacks * The template variable is less than (<) a particular value e.g. include "no writebacks" if the value of writeback count is < 1 no writebacks * The template variable is greater than (>) a particular value e.g. include "oodles of writebacks" if the value of writeback count is > 50 oodles of writebacks * The template variable is like (=~) a particular regular expression e.g. Greet a visitor properly, depending on their courtesy title Howdy, Sir M'am * The template variable is unlike (!~) a particular regular expression e.g. The posting is neither a film nor a book no review =head1 INSTALLATION Drop the interpolate_pseudoxml plug-in into your Blosxom plugins folder. =head1 VERSION 0.2 =head1 HISTORY 2003-09-16 Changed action tags 2003-08-23 Initial version based on interpolate_fancy =head1 AUTHOR Victor Ganata , http://www.fatoprofugus.net/ =head1 SEE ALSO Blosxom Home/Docs/Licensing: http://www.raelity.org/apps/blosxom/ Blosxom Plugin Docs: http://www.raelity.org/apps/blosxom/plugin.shtml =head1 BUGS Address bug reports and comments to the Blosxom mailing list [http://www.yahoogroups.com/group/blosxom]. =head1 LICENSE Blosxom: Copyright 2003, Rael Dornfest this Blosxom plug-in: Copyright 2003, Victor Ganata Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.