tagging: Allow using titles in for related stories.
[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 This plugin is now maintained by the Blosxom Sourceforge Team,
122 <blosxom-devel@lists.sourceforge.net>.
123
124 =head1 ACKNOWLEDGEMENTS
125
126 Eric David Sherman, for < and >
127
128 =head1 SEE ALSO
129
130 Blosxom Home/Docs/Licensing: http://blosxom.sourceforge.net/
131
132 Blosxom Plugin Docs: http://blosxom.sourceforge.net/documentation/users/plugins.html
133
134 =head1 BUGS
135
136 None known; please send bug reports and feedback to the Blosxom
137 development mailing list <blosxom-devel@lists.sourceforge.net>.
138
139 =head1 LICENSE
140
141 Blosxom and this Blosxom Plug-in
142 Copyright 2003, Rael Dornfest 
143
144 Permission is hereby granted, free of charge, to any person obtaining a
145 copy of this software and associated documentation files (the "Software"),
146 to deal in the Software without restriction, including without limitation
147 the rights to use, copy, modify, merge, publish, distribute, sublicense,
148 and/or sell copies of the Software, and to permit persons to whom the
149 Software is furnished to do so, subject to the following conditions:
150
151 The above copyright notice and this permission notice shall be included
152 in all copies or substantial portions of the Software.
153
154 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
155 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
156 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
157 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
158 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
159 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
160 OTHER DEALINGS IN THE SOFTWARE.