Only mangle $blosxom::title and body if defined.
[matthijs/upstream/blosxom-plugins.git] / general / interpolate_fancy
index 9810785109f6e1ae628a64f81f333cd38d0a1003..45208d24d77feec491916c5cbce68f8144aee2bf 100644 (file)
@@ -8,6 +8,9 @@
 
 package interpolate_fancy;
 
+use warnings;
+use strict;
+
 # --- Configurable variables -----
 
 # Do you want me to recursively interpolate into the story $title
@@ -106,8 +109,8 @@ sub do_interpolate {
     # 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;
+      $blosxom::title =~ s/<(\@|\??\$)/<#INTERPOLATE_FANCY_DEFANG#$1/gsi if defined $blosxom::title;
+      $blosxom::body =~ s/<(\@|\??\$)/<#INTERPOLATE_FANCY_DEFANG#$1/gsi if defined $blosxom::body;
     }
 
     my $template = shift;
@@ -131,7 +134,7 @@ sub do_interpolate {
     # Variable expansion (unconditional, recursive)
     #
     # e.g. <$var />
-    while( $template =~ s/<\$([a-zA-Z?]\w+(?:::\w+)*)\s+?\/>/"defined \$$1 ? \$$1 : undef"/gsee ) { }
+    while( $template =~ s/<\$([a-zA-Z?]\w+(?:::\w+)*)\s+?\/>/"defined \$$1 ? \$$1 : ''"/gsee ) { }
 
     #
     # Actions 
@@ -152,6 +155,11 @@ sub do_interpolate {
 
 sub _test {
   my($variable, $attr) = @_;
+  # If the variable is not defined, treat it as the empty string in
+  # comparisons below
+  if (!defined $variable) { 
+    $variable = '';
+  }
   my $attributes = interpolate_fancy::_attributes($attr);
 
   defined $attributes->{eq} and return $variable eq $attributes->{eq};
@@ -175,8 +183,16 @@ sub _action {
   $blosxom::plugins{$plugin} > 0
     and $plugin->can($action) 
       and $result = $plugin->$action($attributes, $content);
-
-  return $attributes->{'output'} =~ /yes/i ? $result : undef;
+  # Optionally interpolate and/or output the result, if requested. 
+  if ($attributes->{'output'} =~ /yes/i) {
+    if ($attributes->{'interpolate'} =~ /yes/i) {
+      $result = interpolate_fancy::do_interpolate($result);
+    } 
+    return $result;
+  } else {
+    return undef;
+  }
 }
 
 sub _attributes {
@@ -305,6 +321,10 @@ Specify that results should be sent to the browser using the output="yes" attrib
 
 Otherwise, subroutines will still have their effect, but the results will be tossed out.
 
+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:
+
+  <@plugin.subroutine arg1="a" arg2="bee" output="yes" interpolate="yes"/>
+
 Content wrapped in the action call is sent as another argument to the subroutine:
 
   <@thePlugin.subroutine encoding="Latin1" output="yes">
@@ -343,6 +363,14 @@ sub foreshorten {
   return substr($content, 0, $attributes->{'length'}||$default_length);
 }
 
+# This action includes the template specified in the 'name' attribute
+
+sub includetemplate {
+  my($self, $attributes, $content) = @_;
+  $name = $attributes->{'name'};
+  return &$blosxom::template($blosxom::path_info, $name, $blosxom::flavour);
+}
+
 --
 
 Calling these individually in a Blosxom flavour template looks something like:
@@ -371,6 +399,11 @@ The following bit of text is only 20 characters in length and devoid of HTML:
 </@myplugin.strip_html>
 </@myplugin.foreshorten>
 
+The following includes the 'sidebar' template, while interpolating
+the contents of that file:
+
+<@myplugin.include name="sidebar" output="yes" interpolate="yes"/>
+
 =head1 INSTALLATION
 
 Drop the interpolate_fancy plug-in into your Blosxom plugins folder.