1 # Blosxom Plugin: interpolate_fancy
2 # Author: Rael Dornfest <rael@oreilly.com>,
3 # Modified by: Matthijs Kooijman <m.kooijman@student.utwente.nl>
4 # and Barijaona Ramaholimihaso
6 # Documentation: See the bottom of this file or type:
7 # perldoc interpolate_fancy
9 package interpolate_fancy;
14 # --- Configurable variables -----
16 # Do you want me to recursively interpolate into the story $title
17 # and $body? Consider and reconsider carefully before turning this
18 # on as if anyone other than you has the ability to post stories,
19 # there's a chance of some tomfoolery, exposing variables and calling
20 # actions/subroutines you might not want called.
21 # 0 = No (default), 1 = Yes
22 our $recurse_into_story = 0;
24 # --------------------------------
26 my $parsing_story = 0 ;
34 # (Recursively) resolve conditinal includes <?...>...</?>.
35 # Expects two arguments: The (part of the) template we are expanding and
36 # wether or not this part will be displayed. When this last argument is false,
37 # this part of the template will not be displayed, but is still parsed to
38 # properly resolve nested conditions.
40 # This will resolve and substitute all conditionals on the "current level".
41 # This effectively means, the routine will search for and resolve all tags it
42 # finds (with proper nesting), until it finds a closing tag (</?>) for which
43 # it has not seen the opening tag, or there are no more opening tags in the
46 # This function will return the template with all conditionals on the current
47 # level resolved. This will guarantee that if there are any opening or closing
48 # tags left in the template, the first one will be a closing tag (which can
49 # then be resolved in the upper level).
55 if ($template !~ /(.*?)<\?(\!?\$\w+(?:::\w+)*)(?:\s+?(.+?))?>(.*)/s) {
56 return $template; # No open tags, normal text
64 if ($head =~ /<\/\?>/s) {
65 return $template; # There is a closing tag before the first open tag,
69 my $displayitem = $display;
70 if ($displayitem) { # Don't care about these tests if we're not displayed anyway.
71 my $negated = ($var =~ s/^\!//); # Remove negation mark
72 if ($negated || !$attr) {
74 warn("<?!$var $attr>: Negated expression can't have attributes, ignoring them.");
76 $displayitem = eval("defined $var");
77 if ($negated) { $displayitem = !$displayitem; }
79 $displayitem = _test(eval($var), $attr);
84 $tail = _resolve_nested($tail, $displayitem);
86 if ($tail !~ /<\/\?>/s) { # Is there no closing tag?
87 warn("Invalid nesting: <?$var $attr> missing closing tag.");
91 $tail =~ s/<\/\?>//s; # Remove the closing tag
93 $tail =~ s/.*?<\/\?>//s; # Remove up to the closing tag
95 $template = $head . $tail;
102 return \&interpolate_fancy::do_interpolate;
109 # Halt recursive interpolation of story $body
110 # by mangling interpolation tags (to be unmangled in a moment)
111 unless ($recurse_into_story) {
112 $blosxom::title =~ s/<(\@|\??\$)/<#INTERPOLATE_FANCY_DEFANG#$1/gsi if defined $blosxom::title;
113 $blosxom::body =~ s/<(\@|\??\$)/<#INTERPOLATE_FANCY_DEFANG#$1/gsi if defined $blosxom::body;
116 my $template = shift;
118 # Backward Compatibility with core Blosxom style interpolation
119 unless ($parsing_story)
120 {$template =~ s#(?<!<)(?<!<\?)(?<!<\?!)(\$\w+(?:::\w+)*)#<$1 />#gs; };
123 # Conditional inclusion
125 # e.g. <?$var>display if defined</?>
127 # e.g. <?!$var>display if not defined</?>
129 # eq (eq), ne (ne), lt (<), gt (>), like (=~), unlike (!~)
130 # e.g. <?$var lt="123">display if $var less than 123</?>
131 $template = interpolate_fancy::_resolve_nested($template, 1);
134 # Variable expansion (unconditional, recursive)
137 while( $template =~ s/<\$([a-zA-Z?]\w+(?:::\w+)*)\s+?\/>/"defined \$$1 ? \$$1 : ''"/gsee ) { }
142 # e.g. <@plugin.subroutine arg1="a" output="no" />
143 # e.g. <@plugin.subroutine encoding="Latin1" output="yes">pass content</@>
144 $template =~ s#<\@((?:\w|::)+?)\.(\w+?)\s+?(.+?)?(?:>(.*?)<\/\@\1\.\2>|\s+?\/>)#&interpolate_fancy::_action($1,$2,$3,$4)#gse;
146 # Unmangle mangled interpolation tags in story $title and $body
147 # (by now in the template itself)
148 unless ($recurse_into_story) {
149 $template =~ s/<#INTERPOLATE_FANCY_DEFANG#(\@|\??\$)/<$1/gsi;
157 my($variable, $attr) = @_;
158 # If the variable is not defined, treat it as the empty string in
160 if (!defined $variable) {
163 my $attributes = interpolate_fancy::_attributes($attr);
165 defined $attributes->{eq} and return $variable eq $attributes->{eq};
166 defined $attributes->{ne} and return $variable ne $attributes->{ne};
167 defined $attributes->{gt} and return $variable > $attributes->{gt};
168 defined $attributes->{lt} and return $variable < $attributes->{lt};
169 defined $attributes->{like} and return $variable =~ /$attributes->{like}/;
170 defined $attributes->{unlike} and return $variable !~ /$attributes->{unlike}/;
176 my($plugin, $action, $attr, $content) = @_;
179 $content =~ s#<\@((?:\w|::)+?)\.(\w+?)\s+?(.+?)?(?:>(.*?)<\/\@\1\.\2>|\s+?\/>)#&interpolate_fancy::_action($1,$2,$3,$4)#gse;
181 my $attributes = interpolate_fancy::_attributes($attr);
183 $blosxom::plugins{$plugin} > 0
184 and $plugin->can($action)
185 and $result = $plugin->$action($attributes, $content);
187 # Optionally interpolate and/or output the result, if requested.
188 if ($attributes->{'output'} =~ /yes/i) {
189 if ($attributes->{'interpolate'} =~ /yes/i) {
190 $result = interpolate_fancy::do_interpolate($result);
202 while ( $attr =~ /\b(\w+?)\s*?=\s*?(["'])(.*?)\2/g ) {
203 $attributes->{$1} = $3;
210 my($pkg, $path, $filename, $story_ref, $title_ref, $body_ref) = @_;
212 if ($recurse_into_story) {
214 $$title_ref =&$blosxom::interpolate($$title_ref);
215 $$body_ref =&$blosxom::interpolate($$body_ref);
227 Blosxom Plug-in: interpolate_fancy
231 Overrides Blosxom's far simpler to use, but more limited, default interpolate() subroutine.
233 * Include bits of text and template variable values in templates, either conditionally or unconditionally;
235 * 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.
237 =head2 Inclusions of bits of text and variables
239 This syntax will expand to the value of the referenced variable and can be used to include dynamic content in your pages.
243 =item Unconditionally and recursively
245 e.g. include a link to the story's path using various template variables.
247 <a href="<$url /><$path />"><$path /></a>
249 Limited by the $recurse_into_story configuration directive (see the CONFIGURATION below).
251 =item Unconditionally (backward compatibility with Blosxom's standard interpolation)
253 e.g. include a link to the story's path using various template variables (non-recursive, and only inside templates).
255 <a href="$url$path">$path</a>
257 =item If the template variable has a value (i.e. is defined)
259 e.g. include a hyperlink to the story's path if it has a path (i.e. $path is defined).
261 <?$path><a href="<$url /><$path />"><$path /></a></?>
263 =item If the template variable doesn't have a value (i.e. is NOT defined)
265 e.g. include a hyperlink to home if path is undefined.
267 <?!$path><a href="<$url />">home</a></?>
269 =item If the template variable is equal (=) to a particular value
271 e.g. include "1 writeback" (singular) if the value of writeback count is 1
273 <$writeback::count /> <?$writeback::count eq="1">writeback</?>
275 =item If the template variable is not equal (!=) to a particular value
277 e.g. include "x writebacks" (plural) if the value of Writeback count (x) is not 1
279 <$writeback::count /> <?$writeback::count ne="1">writebacks</?>
281 =item If the template variable is less than (<) a particular value
283 e.g. include "no writebacks" if the value of writeback count is < 1
285 <?$writeback::count lt="1">no writebacks</?>
287 =item If the template variable is greater than (>) a particular value
289 e.g. include "oodles of writebacks" if the value of writeback count is > 50
291 <?$writeback::count gt="50">oodles of writebacks</?>
293 =item If the template variable is like (=~) a particular regular expression
295 e.g. Greet a visitor properly, depending on their courtesy title
298 <?$user::courtesy like="^(Mr\.?|Sir)$">Sir</?>
299 <?$user::courtesy like="^(Mr?s\.?|Miss)$">M'am</?>
301 =item If the template variable is unlike (!~) a particular regular expression
303 e.g. The posting is neither a film nor a book
305 <?$path unlike="/(Film|Literature)">no review</?>
311 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.
315 <@thePlugin.subroutine arg1="a" arg2="bee" />
317 calls &Blosxom::Plugin::ThePlugin::subroutine( {'arg1'=>'a', 'arg2'=>'bee' } ).
319 Specify that results should be sent to the browser using the output="yes" attribute like so:
320 <@thePlugin.subroutine arg1="a" arg2="bee" output="yes" />
322 Otherwise, subroutines will still have their effect, but the results will be tossed out.
324 Normally, the result from the subroutine is sent as-is, but you can set the interpolate="yes" attribute to let interpolate_fancy interpolate the results, as follows:
326 <@plugin.subroutine arg1="a" arg2="bee" output="yes" interpolate="yes"/>
328 Content wrapped in the action call is sent as another argument to the subroutine:
330 <@thePlugin.subroutine encoding="Latin1" output="yes">
332 </@thePlugin.subroutine>
334 This calls &thePlugin::subroutine( {'encoding'=>'Latin1', 'output'=>'yes' }, "pass this content" ).
336 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.
338 Recursion is limited by the $recurse_into_story configuration directive (see the CONFIGURATION below).
342 For those of you interested in writing plugin actions or using some of the
343 more advanced features in your Blosxom blog templates, here are a couple of
348 # For the sake of this example, assume these actions live in a "myplugin" plugin
350 # This action strips HTML from its content
353 my($self, $attributes, $content) = @_;
354 $content =~ s!</?.+?>!!g;
358 # This action foreshortens its content to a length specified in the call to action's length attribute
361 my($self, $attributes, $content) = @_;
362 my $default_length = 144;
363 return substr($content, 0, $attributes->{'length'}||$default_length);
366 # This action includes the template specified in the 'name' attribute
368 sub includetemplate {
369 my($self, $attributes, $content) = @_;
370 $name = $attributes->{'name'};
371 return &$blosxom::template($blosxom::path_info, $name, $blosxom::flavour);
376 Calling these individually in a Blosxom flavour template looks something like:
378 The following bit of text is devoid of HTML:
380 <@myplugin.strip_html output="Yes">
381 Silly <a href="http://www.raelity.org/">me</a>, I plumb
382 <em>forgot</em> to remove the HTML.
383 </@myplugin.strip_html>
385 The following bit of text is only 20 characters in length:
387 <@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
389 </@myplugin.foreshorten>
391 And in combination, stripping the HTML _before_ foreshortening (notice
392 the strip_html action is embedded inside the foreshorten action and
395 The following bit of text is only 20 characters in length and devoid of HTML:
397 <@myplugin.foreshorten length="20" output="Yes"><@myplugin.strip_html output="Yes">Silly <a href="http://www.raelity.org/">me</a>, I plumb
398 <em>forgot</em> to remove the HTML.
399 </@myplugin.strip_html>
400 </@myplugin.foreshorten>
402 The following includes the 'sidebar' template, while interpolating
403 the contents of that file:
405 <@myplugin.include name="sidebar" output="yes" interpolate="yes"/>
409 Drop the interpolate_fancy plug-in into your Blosxom plugins folder.
413 None necessary; interpolate_fancy will run out of the box with no need
414 of additional configuration or fiddling on your part (caveat: see
415 BACKWARD COMPATILITY below).
417 The interpolate_fancy plugin does sport one configuration directive
418 which you should very much consider leaving alone.
420 # 0 = No (default), 1 = Yes
421 my $recurse_into_story = 0;
423 $recurse_into_story decides whether or not the interpolation engine
424 should respect and interpolate (swap for the associated value)
425 variables and actions embedded in story $title and $body themselves.
427 Off by default, you should consider and reconsider carefully before
428 turning this on as if anyone other than you has the ability to post
429 stories to your blog, there's a chance of some tomfoolery, exposing
430 variables and calling actions/subroutines you might not want called.
432 =head1 BACKWARD COMPATIBILITY
434 If you've been using core Blosxom's interpolation style
435 (e.g. $title), this plugin will provide backward compatibility,
436 requiring no template rewriting on your part.
438 If you've been using the interpolate_conditional plugin,
439 the conditional bits won't be respected by default. You should
440 run your templates through the interpolate2fancy utility
441 [http://www.blosxom.com/downloads/utilities/interpolate2fancy.py].
447 =head1 VERSION HISTORY
449 2007-09-27 : enables more than one :: in variable names
451 2007-09-13 : corrects the $recurse_into_story feature in XML flavours
453 v20061114 : fixes from Matthijs Kooijman (including properly support for nested
458 Rael Dornfest <rael@oreilly.com>, http://www.raelity.org/
459 Modified by Matthijs Kooijman <m.kooijman@student.utwente.nl>, http://katherina.student.utwente.nl/~matthijs/blog
461 This plugin is now maintained by the Blosxom Sourceforge Team,
462 <blosxom-devel@lists.sourceforge.net>.
466 Blosxom Home/Docs/Licensing: http://blosxom.sourceforge.net/
468 Blosxom Plugin Docs: http://blosxom.sourceforge.net/documentation/users/plugins.html
472 None known; please send bug reports and feedback to the Blosxom
473 development mailing list <blosxom-devel@lists.sourceforge.net>.
477 Blosxom and this Blosxom Plug-in
478 Copyright 2003, Rael Dornfest
480 Permission is hereby granted, free of charge, to any person obtaining a
481 copy of this software and associated documentation files (the "Software"),
482 to deal in the Software without restriction, including without limitation
483 the rights to use, copy, modify, merge, publish, distribute, sublicense,
484 and/or sell copies of the Software, and to permit persons to whom the
485 Software is furnished to do so, subject to the following conditions:
487 The above copyright notice and this permission notice shall be included
488 in all copies or substantial portions of the Software.
490 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
491 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
492 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
493 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
494 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
495 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
496 OTHER DEALINGS IN THE SOFTWARE.