Add a bunch of Rael plugins.
[matthijs/upstream/blosxom-plugins.git] / general / interpolate_conditional
1 # Blosxom Plugin: interpolate_conditional
2 # Author(s): Rael Dornfest <rael@oreilly.com> 
3 # Version: 2003-05-16
4 # Documentation: See the bottom of this file or type: 
5 # perldoc interpolate_conditional
6
7 package interpolate_conditional;
8
9 # --- Configurable variables -----
10
11 # --------------------------------
12
13 sub start {
14   1;
15 }
16
17 sub interpolate {
18   return sub {
19
20     package blosxom;
21
22     my $template = shift;
23
24     # defined
25         $template =~ s#\?\{(\$\w+(?:::)?\w*) (.*?)\}#"defined $1 ? \$2 : ''"#gee;
26
27     # not defined
28         $template =~ s#\?\{!(\$\w+(?:::)?\w*) (.*?)\}#"!defined $1 ? \$2 : ''"#gee;
29
30         # not equal to
31     $template =~ s#\?\{(\$\w+(?:::)?\w*)!=(.*?) (.*?)\}#"defined $1 and $1 ne '$2' ? \$3 : ''"#gee;
32
33         # equal to
34     $template =~ s#\?\{(\$\w+(?:::)?\w*)=(.*?) (.*?)\}#"defined $1 and $1 eq '$2' ? \$3 : ''"#gee;
35
36         # greater than
37         $template =~ s#\?\{(\$\w+(?:::)?\w*)>(.*?) (.*?)\}#"defined $1 and $1 gt '$2' ? \$3 : ''"#gee;
38
39         # less than 
40         $template =~ s#\?\{(\$\w+(?:::)?\w*)<(.*?) (.*?)\}#"defined $1 and $1 lt '$2' ? \$3 : ''"#gee;
41
42         # unconditional (and recursive)
43         while( $template =~ s/(\$[a-zA-Z]\w+(?:::)?\w*)/"defined $1 ? $1 : ''"/gee ) { }
44
45     return $template;
46
47   };  
48 }
49   
50 1;
51
52 __END__
53
54 =head1 NAME
55
56 Blosxom Plug-in: interpolate_conditional
57
58 =head1 SYNOPSIS
59
60 Include bits of text and template variable values in templates based on 
61 one or more of the following conditions:
62
63   * Unconditionally (and recursively), just as with the Blosxom default
64
65     e.g. include a link to the story's path using various template variables.
66
67     <a href="$url$path">$path</a>
68
69   * The template variable has a value (i.e. is defined)
70
71     e.g. include a hyperlink to the story's path if it has a path (i.e.
72     $path is defined).
73
74       ?{$path <a href="$url$path">$path</a>}
75   
76   * The template variable doesn't have a value (i.e. is NOT defined)
77
78     e.g. include a hyperlink to home if path is undefined.
79
80       ?{!$path <a href="$url">/</a>}
81
82   * The template variable is equal (=) to a particular value
83
84     e.g. include "1 writeback" (singular) if the value of writeback count is 1
85
86       ?{$writeback::count=1 $writeback::count writeback}
87
88   * The template variable is not equal (!=) to a particular value
89
90     e.g. include "x writebacks" (plural) if the value of writeback 
91          count (x) is not 1
92
93       ?{$writeback::count!=1 $writeback::count writebacks}
94
95   * The template variable is less than (<) a particular value
96
97     e.g. include "no writebacks" if the value of writeback count is < 1
98
99       ?{$writeback::count<1 no writebacks}
100
101   * The template variable is greater than (>) a particular value
102
103     e.g. include "oodles of writebacks" if the value of writeback count is > 50
104
105       ?{$writeback::count>50 oodles of writebacks}
106
107 Overrides Blosxom's default interpolate() subroutine.
108
109 =head1 INSTALLATION
110
111 Drop the interpolate_conditional plug-in into your Blosxom plugins folder.
112
113 =head1 VERSION
114
115 2003-05-16
116
117 =head1 AUTHOR
118
119 Rael Dornfest  <rael@oreilly.com>, http://www.raelity.org/
120
121 =head1 ACKNOWLEDGEMENTS
122
123 Eric David Sherman, for < and >
124
125 =head1 SEE ALSO
126
127 Blosxom Home/Docs/Licensing: http://www.raelity.org/apps/blosxom/
128
129 Blosxom Plugin Docs: http://www.raelity.org/apps/blosxom/plugin.shtml
130
131 =head1 BUGS
132
133 Address bug reports and comments to the Blosxom mailing list 
134 [http://www.yahoogroups.com/group/blosxom].
135
136 =head1 LICENSE
137
138 Blosxom and this Blosxom Plug-in
139 Copyright 2003, Rael Dornfest 
140
141 Permission is hereby granted, free of charge, to any person obtaining a
142 copy of this software and associated documentation files (the "Software"),
143 to deal in the Software without restriction, including without limitation
144 the rights to use, copy, modify, merge, publish, distribute, sublicense,
145 and/or sell copies of the Software, and to permit persons to whom the
146 Software is furnished to do so, subject to the following conditions:
147
148 The above copyright notice and this permission notice shall be included
149 in all copies or substantial portions of the Software.
150
151 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
152 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
153 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
154 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
155 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
156 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
157 OTHER DEALINGS IN THE SOFTWARE.