# Blosxom Plugin: interpolate_fancy # Author: Rael Dornfest , # Modified by: Matthijs Kooijman # and Barijaona Ramaholimihaso # Version: 2007-09-27 # Documentation: See the bottom of this file or type: # perldoc interpolate_fancy package interpolate_fancy; # --- Configurable variables ----- # Do you want me to recursively interpolate into the story $title # and $body? Consider and reconsider carefully before turning this # on as if anyone other than you has the ability to post stories, # there's a chance of some tomfoolery, exposing variables and calling # actions/subroutines you might not want called. # 0 = No (default), 1 = Yes our $recurse_into_story = 0; # -------------------------------- my $parsing_story = 0 ; sub start { 1; } # (Recursively) resolve conditinal includes .... # Expects two arguments: The (part of the) template we are expanding and # wether or not this part will be displayed. When this last argument is false, # this part of the template will not be displayed, but is still parsed to # properly resolve nested conditions. # # This will resolve and substitute all conditionals on the "current level". # This effectively means, the routine will search for and resolve all tags it # finds (with proper nesting), until it finds a closing tag () for which # it has not seen the opening tag, or there are no more opening tags in the # file. # # This function will return the template with all conditionals on the current # level resolved. This will guarantee that if there are any opening or closing # tags left in the template, the first one will be a closing tag (which can # then be resolved in the upper level). sub _resolve_nested { my $template = shift; my $display = shift; while (1) { if ($template !~ /(.*?)<\?(\!?\$\w+(?:::\w+)*)(?:\s+?(.+?))?>(.*)/s) { return $template; # No open tags, normal text } my $head = $1; my $var = $2; my $attr = $3; my $tail = $4; if ($head =~ /<\/\?>/s) { return $template; # There is a closing tag before the first open tag, # we're done. } my $displayitem = $display; if ($displayitem) { # Don't care about these tests if we're not displayed anyway. my $negated = ($var =~ s/^\!//); # Remove negation mark if ($negated || !$attr) { if ($attr) { warn(": Negated expression can't have attributes, ignoring them."); } $displayitem = eval("defined $var"); if ($negated) { $displayitem = !$displayitem; } } else { $displayitem = _test(eval($var), $attr); } } $tail = _resolve_nested($tail, $displayitem); if ($tail !~ /<\/\?>/s) { # Is there no closing tag? warn("Invalid nesting: missing closing tag."); } if ($displayitem) { $tail =~ s/<\/\?>//s; # Remove the closing tag } else { $tail =~ s/.*?<\/\?>//s; # Remove up to the closing tag } $template = $head . $tail; } return $template; } sub interpolate { return sub { package blosxom; # Halt recursive interpolation of story $body # by mangling interpolation tags (to be unmangled in a moment) unless ($recurse_into_story) { $blosxom::title =~ s/<(\@|\??\$)/<#INTERPOLATE_FANCY_DEFANG#$1/gsi; $blosxom::body =~ s/<(\@|\??\$)/<#INTERPOLATE_FANCY_DEFANG#$1/gsi; } my $template = shift; # Backward Compatibility with core Blosxom style interpolation unless ($parsing_story) {$template =~ s#(?#gs; }; # # Conditional inclusion # Defined # e.g. display if defined # Undefined # e.g. display if not defined # Tests # eq (eq), ne (ne), lt (<), gt (>), like (=~), unlike (!~) # e.g. display if $var less than 123 $template = interpolate_fancy::_resolve_nested($template, 1); # # Variable expansion (unconditional, recursive) # # e.g. <$var /> while( $template =~ s/<\$([a-zA-Z?]\w+(?:::\w+)*)\s+?\/>/"defined \$$1 ? \$$1 : undef"/gsee ) { } # # Actions # # e.g. <@plugin.subroutine arg1="a" output="no" /> # e.g. <@plugin.subroutine encoding="Latin1" output="yes">pass content $template =~ s#<\@((?:\w|::)+?)\.(\w+?)\s+?(.+?)?(?:>(.*?)<\/\@\1\.\2>|\s+?\/>)#&interpolate_fancy::_action($1,$2,$3,$4)#gse; # Unmangle mangled interpolation tags in story $title and $body # (by now in the template itself) unless ($recurse_into_story) { $template =~ s/<#INTERPOLATE_FANCY_DEFANG#(\@|\??\$)/<$1/gsi; } return $template; }; } sub _test { my($variable, $attr) = @_; my $attributes = interpolate_fancy::_attributes($attr); defined $attributes->{eq} and return $variable eq $attributes->{eq}; defined $attributes->{ne} and return $variable ne $attributes->{ne}; defined $attributes->{gt} and return $variable > $attributes->{gt}; defined $attributes->{lt} and return $variable < $attributes->{lt}; defined $attributes->{like} and return $variable =~ /$attributes->{like}/; defined $attributes->{unlike} and return $variable !~ /$attributes->{unlike}/; return 0; } sub _action { my($plugin, $action, $attr, $content) = @_; my $result; $content =~ s#<\@((?:\w|::)+?)\.(\w+?)\s+?(.+?)?(?:>(.*?)<\/\@\1\.\2>|\s+?\/>)#&interpolate_fancy::_action($1,$2,$3,$4)#gse; my $attributes = interpolate_fancy::_attributes($attr); $blosxom::plugins{$plugin} > 0 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; } return $attributes; } sub story { my($pkg, $path, $filename, $story_ref, $title_ref, $body_ref) = @_; if ($recurse_into_story) { $parsing_story = 1; $$title_ref =&$blosxom::interpolate($$title_ref); $$body_ref =&$blosxom::interpolate($$body_ref); $parsing_story = 0; } return 1; } 1; __END__ =head1 NAME Blosxom Plug-in: interpolate_fancy =head1 SYNOPSIS Overrides Blosxom's far simpler to use, but more limited, default interpolate() subroutine. * Include bits of text and template variable values in templates, either conditionally or unconditionally; * Perform actions (i.e. call plug-in subroutines) at any point in your page, whether to act on current content and return results or not. =head2 Inclusions of bits of text and variables This syntax will expand to the value of the referenced variable and can be used to include dynamic content in your pages. =over 2 =item Unconditionally and recursively e.g. include a link to the story's path using various template variables. <$path /> Limited by the $recurse_into_story configuration directive (see the CONFIGURATION below). =item Unconditionally (backward compatibility with Blosxom's standard interpolation) e.g. include a link to the story's path using various template variables (non-recursive, and only inside templates). $path =item If 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 /> =item If 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 =item If 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 =item If 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 =item If the template variable is less than (<) a particular value e.g. include "no writebacks" if the value of writeback count is < 1 no writebacks =item If 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 =item If the template variable is like (=~) a particular regular expression e.g. Greet a visitor properly, depending on their courtesy title Howdy, Sir M'am =item If the template variable is unlike (!~) a particular regular expression e.g. The posting is neither a film nor a book no review =back =head2 Actions Performs an action (i.e. calls a plug-in's subroutine) at any point in your page. Optionally passes arguments as key/value pairs stated as XML-style attributes. For example: <@thePlugin.subroutine arg1="a" arg2="bee" /> calls &Blosxom::Plugin::ThePlugin::subroutine( {'arg1'=>'a', 'arg2'=>'bee' } ). Specify that results should be sent to the browser using the output="yes" attribute like so: <@thePlugin.subroutine arg1="a" arg2="bee" output="yes" /> 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: <@thePlugin.subroutine encoding="Latin1" output="yes"> pass this content This calls &thePlugin::subroutine( {'encoding'=>'Latin1', 'output'=>'yes' }, "pass this content" ). Actions are recursive, meaning that you can embed an action inside another, and so on and so on and so on. Actions are unfolded from the inside out, with the most deeply embedded first, second deepest second, and so forth until the outermost action is performed last. Recursion is limited by the $recurse_into_story configuration directive (see the CONFIGURATION below). =head3 An Example For those of you interested in writing plugin actions or using some of the more advanced features in your Blosxom blog templates, here are a couple of sample actions: -- # For the sake of this example, assume these actions live in a "myplugin" plugin # This action strips HTML from its content sub strip_html { my($self, $attributes, $content) = @_; $content =~ s!!!g; return $content; } # This action foreshortens its content to a length specified in the call to action's length attribute sub foreshorten { my($self, $attributes, $content) = @_; my $default_length = 144; return substr($content, 0, $attributes->{'length'}||$default_length); } -- Calling these individually in a Blosxom flavour template looks something like: The following bit of text is devoid of HTML: <@myplugin.strip_html output="Yes"> Silly me, I plumb forgot to remove the HTML. The following bit of text is only 20 characters in length: <@myplugin.foreshorten length="20" output="Yes">This text is far longer than 20 characters on the page, but will only appear as "This text is far lon" in the resulting page. And in combination, stripping the HTML _before_ foreshortening (notice the strip_html action is embedded inside the foreshorten action and thus is run first). The following bit of text is only 20 characters in length and devoid of HTML: <@myplugin.foreshorten length="20" output="Yes"><@myplugin.strip_html output="Yes">Silly me, I plumb forgot to remove the HTML. =head1 INSTALLATION Drop the interpolate_fancy plug-in into your Blosxom plugins folder. =head1 CONFIGURATION None necessary; interpolate_fancy will run out of the box with no need of additional configuration or fiddling on your part (caveat: see BACKWARD COMPATILITY below). The interpolate_fancy plugin does sport one configuration directive which you should very much consider leaving alone. # 0 = No (default), 1 = Yes my $recurse_into_story = 0; $recurse_into_story decides whether or not the interpolation engine should respect and interpolate (swap for the associated value) variables and actions embedded in story $title and $body themselves. Off by default, you should consider and reconsider carefully before turning this on as if anyone other than you has the ability to post stories to your blog, there's a chance of some tomfoolery, exposing variables and calling actions/subroutines you might not want called. =head1 BACKWARD COMPATIBILITY If you've been using core Blosxom's interpolation style (e.g. $title), this plugin will provide backward compatibility, requiring no template rewriting on your part. If you've been using the interpolate_conditional plugin, the conditional bits won't be respected by default. You should run your templates through the interpolate2fancy utility [http://www.blosxom.com/downloads/utilities/interpolate2fancy.py]. =head1 VERSION 2007-09-27 =head1 VERSION HISTORY 2007-09-27 : enables more than one :: in variable names 2007-09-13 : corrects the $recurse_into_story feature in XML flavours v20061114 : fixes from Matthijs Kooijman (including properly support for nested conditions) =head1 AUTHOR Rael Dornfest , http://www.raelity.org/ Modified by Matthijs Kooijman , http://katherina.student.utwente.nl/~matthijs/blog This plugin is now maintained by the Blosxom Sourceforge Team, . =head1 SEE ALSO Blosxom Home/Docs/Licensing: http://blosxom.sourceforge.net/ Blosxom Plugin Docs: http://blosxom.sourceforge.net/documentation/users/plugins.html =head1 BUGS None known; please send bug reports and feedback to the Blosxom development mailing list . =head1 LICENSE Blosxom and this Blosxom Plug-in Copyright 2003, Rael Dornfest 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.