-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinterpolate_fancy
384 lines (264 loc) · 12.1 KB
/
interpolate_fancy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
# Blosxom Plugin: interpolate_fancy
# Author(s): Rael Dornfest <[email protected]>
# Version: 2003-09-09:12:10
# Documentation: See the bottom of this file or type:
# perldoc interpolate_fancy
package interpolate_fancy;
#use strict;
# --- 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
my $recurse_into_story = 0;
# --------------------------------
sub start {
1;
}
sub interpolate { sub {
package blosxom;
# Halt recursive interpolation of story $body
# by mangling interpolation tags (to be unmangled in a moment)
unless ($recurse_into_story) {
s/<(\@|\??\$)/<#INTERPOLATE_FANCY_DEFANG#$1/gsi
for grep { $_ } $bloxom::title, $bloxsom::body;
}
my $template = shift;
no warnings;
# Backward Compatibility with core Blosxom style interpolation
$template =~ s{(?<!<)(?<!<\?)(?<!<\?!)(\$\w+(?:::)?\w*)}{<$1 />}gs;
# Defined
# e.g. <?$var>display if defined</?>
$template =~ s{<\?(\$\w+(?:::)?\w*)>(.*?)<\/\?>}
{"defined $1 ? \$2 : ''"}gsee;
# Undefined
# e.g. <?!$var>display if not defined</?>
$template =~ s{<\?!(\$\w+(?:::)?\w*)>(.*?)<\/\?>}
{"!defined $1 ? \$2 : ''"}gsee;
# Tests
# eq (eq), ne (ne), lt (<), gt (>), like (=~), unlike (!~)
# e.g. <?$var lt="123">display if $var less than 123</?>
$template =~ s{<\?(\$\w+(?:::)?\w*)\s+?(.+?)>(.*?)<\/\?>}
{"interpolate_fancy::_test(qq{$1}, q{$2}, q{$3})"}gsee;
# Unconditional, Recursive
# e.g. <$var />
while (
$template =~ s{<\$([a-zA-Z?]\w+(?:::)?\w*)\s+?\/>}
{"defined \$$1 ? \$$1 : ''"}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, $content) = @_;
my $result;
my $attributes = interpolate_fancy::_attributes($attr);
no warnings;
defined $attributes->{eq} and return $variable eq $attributes->{eq} ? $content : '';
defined $attributes->{ne} and return $variable ne $attributes->{ne} ? $content : '';
defined $attributes->{gt} and return $variable > $attributes->{gt} ? $content : '';
defined $attributes->{lt} and return $variable < $attributes->{lt} ? $content : '';
defined $attributes->{like} and return $variable =~ /$attributes->{like}/ ? $content : '';
defined $attributes->{unlike} and return $variable !~ /$attributes->{unlike}/ ? $content : '';
return '';
}
sub _action {
my($plugin, $action, $attr, $content) = @_;
$content =~ s#<\@(\w+?)\.(\w+?)\s+?(.+?)?(?:>(.*?)<\/\@\1\.\2>|\s+?\/>)#&interpolate_fancy::_action($1,$2,$3,$4)#gse;
my $attributes = interpolate_fancy::_attributes($attr);
my $result = $blosxom::plugins{$plugin} && $plugin->can($action)
? $plugin->$action($attributes, $content)
: '';
return $attributes->{output} =~ /\byes\b/i ? $result : '';
}
sub _attributes {
my $attr = shift;
my $attributes = {};
while ( $attr =~ /\b(\w+?)\s*?=\s*?(["'])(.*?)\2/g ) {
$attributes->{$1} = $3;
}
return $attributes;
}
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 no.
=head2 Includes
* Unconditionally and recursively
e.g. include a link to the story's path using various template variables.
<a href="<$url /><$path />"><$path /></a>
Limited by the $recurse_into_story configuration directive (see
the CONFIGURATION below).
* Unconditionally and recursively (backward compatibility with Blosxom's standard interpolation)
e.g. include a link to the story's path using various template variables.
<a href="$url$path">$path</a>
Limited by the $recurse_into_story configuration directive (see
the CONFIGURATION below).
* 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><a href="<$url /><$path />"><$path /></a></?>
* The template variable doesn't have a value (i.e. is NOT defined)
e.g. include a hyperlink to home if path is undefined.
<?!$path><a href="<$url />">home</a></?>
* 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::count eq="1">writeback</?>
* 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 /> <?$writeback::count ne="1">writebacks</?>
* The template variable is less than (<) a particular value
e.g. include "no writebacks" if the value of writeback count is < 1
<?$writeback::count lt="1">no writebacks</?>
* The template variable is greater than (>) a particular value
e.g. include "oodles of writebacks" if the value of writeback count is > 50
<?$writeback::count gt="50">oodles of writebacks</?>
* The template variable is like (=~) a particular regular expression
e.g. Greet a visitor properly, depending on their courtesy title
Howdy,
<?$user::courtesy like="^(Mr\.?|Sir)$">Sir</?>
<?$user::courtesy like="^(Mr?s\.?|Miss)$">M'am</?>
* The template variable is unlike (!~) a particular regular expression
e.g. The posting is neither a film nor a book
<?$path unlike="/(Film|Literature)">no review</?>
=head2 Actions
Perform an action (i.e. call a plug-in's subroutine) at any point in your page.
Optionally pass arguments as key/value pairs stated as XML-style attributes.
For example:
<@plugin.subroutine arg1="a" arg2="bee" />
calls &plugin::subroutine( {'arg1'=>'a', 'arg2'=>'bee' } ).
Specify that results should be sent to the browser using the output="yes"
attribute like so:
<@plugin.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:
<@plugin.subroutine encoding="Latin1" output="yes">
pass this content
</@plugin.subroutine>
This calls &plugin::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 <a href="http://www.raelity.org/">me</a>, I plumb
<em>forgot</em> to remove the HTML.
</@myplugin.strip_html>
The following bit of text is only 20 characters in length:
<@myplugin.foreshorten 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.
</@myplugin.foreshorten>
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 output="Yes">
<@myplugin.strip_html output="Yes">
Silly <a href="http://www.raelity.org/">me</a>, I plumb
<em>forgot</em> to remove the HTML.
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.
</@myplugin.strip_html>
</@myplugin.foreshorten>
=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
2003-09-09:12:10
=head1 AUTHOR
Rael Dornfest <[email protected]>, http://www.raelity.org/
=head1 SEE ALSO
Blosxom Home/Docs/Licensing: http://www.raelity.org/apps/blosxom/
Blosxom Plugin Docs: http://www.raelity.org/apps/blosxom/plugin.shtml
=head1 BUGS
Address bug reports and comments to the Blosxom mailing list
[http://www.yahoogroups.com/group/blosxom].
=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.