1 # Blosxom Plugin: mason_blocks
2 # Author(s): Gavin Carr <gavin@openfusion.com.au>
9 # --- Configurable variables -----
14 # --------------------------------
19 my($pkg, $currentdir, $head_ref) = @_;
20 munge_blocks($head_ref);
25 my ($pkg, $path, $filename, $story_ref, $title_ref, $body_ref) = @_;
26 munge_blocks($story_ref);
31 my($pkg, $currentdir, $foot_ref) = @_;
32 munge_blocks($foot_ref);
37 my ($flavour_ref) = @_;
40 my ($doc, @if, @else, @if_blocks, @else_blocks);
41 @if = @else = @if_blocks = @else_blocks = ();
42 for (split /\n/, $$flavour_ref) {
43 # Ignore lines beginning with '%#'
46 # Ignore if in doc block <%doc> .... </%doc> (at beginning of lines)
47 if (m!^</%doc>! && $doc) {
52 } elsif (m!^<%doc>!) {
57 # Minimalist version - if, } else {, } only, nesting supported
60 $if =~ s/^%\s*if\s*//;
62 # New block, record condition, and add new entries to other arrays
66 push @else_blocks, [];
69 elsif (m/^%\s*\}\s*else\s*\{/) {
70 # Else at same level - set current else flag to true
75 # End of block - pull latest entries from all arrays
78 my $if_block = pop @if_blocks;
79 my $else_block = pop @else_blocks;
80 warn "end_block: if '$if', if block " .
81 scalar(@$if_block) . " lines, else block " .
82 scalar(@$else_block) . " lines\n"
84 # Check condition, and replace current line with appropriate flattened block
86 $_ = join "\n", @$if_block;
88 $_ = join "\n", @$else_block;
92 # Regular line - add to @else_blocks, @if_blocks, or @flavour
93 if (@else && $else[$#else]) {
94 push @{$else_blocks[$#else_blocks]}, $_;
97 push @{$if_blocks[$#if_blocks]}, $_;
104 $$flavour_ref = join "\n", @flavour;
113 mason_blocks - blosxom plugin to support simple mason-style if-blocks
114 in blosxom flavours/templates
118 # In a flavour or template file ...
120 # Mason-style conditionals
121 % if ($pagetype::pagetype ne 'story') {
122 <a href="$permalink::story#comments">Comments ($feedback::count) »</a>
124 <a href="$permalink::story#leave_comment">Leave a comment »</a>
127 # Mason-style comments
128 %# Only show a comments section if there are comments
129 % if ($feedback::count > 0) {
133 # Mason-style block comments
136 multi-line, extremely important
143 mason_blocks is a blosxom plugin implementing simple conditional and
144 commment blocks using mason-style syntax (as in the HTML::Mason perl
145 templating system), for use in blosxom flavour and template files.
149 mason_blocks supports simple if and if-else blocks using lines beginning
150 with the '%' character (which must be the first character on the line)
153 % if ($pagetype::pagetype eq 'story') {
154 <a href="$permalink::story#leave_comment">Leave a comment »</a>
159 % if ($pagetype::pagetype ne 'story') {
160 <a href="$permalink::story#comments">Comments ($feedback::count) »</a>
162 <a href="$permalink::story#leave_comment">Leave a comment »</a>
165 Whitespace is not significant, but braces are required and should match,
166 just as in perl. The if-conditions can comprise any valid perl condition.
170 Two comment styles are also supported. Single line comments must begin with
171 a '%' character, followed by optional whitespace, follwed by a '#' character,
172 and continue only to the end of the line e.g.
174 %# This is a completely profound and illuminating comment
177 Block comments must begin with a <%doc> token (at the beginning of a line)
178 and end with a </%doc> token (also at the beginning of a line). All text
179 between these two tokens is treated as a comment and not included in output.
183 More enlightening profundities from your template author
184 Explaining why this stuff is as ugly as it is ...
187 Block comments cannot be nested.
189 =head2 VS. INTERPOLATE_FANCY
191 mason_blocks was initially born out of my frustration with older versions
192 of interpolate_fancy not supporting nested constructs properly (though I'd
193 also been frustrated with the syntax and the limits on the conditionals
196 I thought for an experiment I'd see how hard simple non-nested
197 conditionals using a mason-style syntax would be, and it turned out to be
198 not very difficult at all. I no longer use interpolate_fancy at all - my
199 limited use cases seem better met using mason_blocks.
201 As I see it, mason_blocks has the following advantages over
206 =item Nesting support
208 Earlier versions of interpolate_fancy had problems with nested
209 constructs. I believe that this has been fixed in the later versions
210 updated by Matthijs Kooijman (>= version 20060111).
212 mason_blocks fully supports nested conditionals.
214 =item If-Else Constructs
216 mason_blocks supports simple if-else blocks, instead of making you
217 use 'if x eq 1; if x ne 1' pairs. It does not currently support
220 =item Full perl conditions
222 mason_blocks allows you to use full perl conditions (including composite
223 conditions) instead of being limited to simple conditions using only the
224 most common operators. For instance, all of the following require hoop
225 jumping with interpolate_fancy:
231 =item if (substr($foo, 3, 1) eq 'X')
233 =item if ($pagetype::pagetype eq 'story' && $feedback::comments > 0)
239 mason_blocks does not provide any of interpolate_fancy's interpolation
240 or action functionality, however.
245 mason_blocks should probably be loaded relatively late, since you'll
246 often want to use various plugin package variables in your conditionals.
248 It uses the 'head', 'story', and 'footer' hooks, rather than 'interpolate',
249 so it should be able to be used alongside any of the interpolate plugins
255 HTML::Mason, for the block syntax (the module is not used or required by
256 this plugin, however).
258 Blosxom: http://blosxom.sourceforge.net/
263 Please report bugs either directly to the author or to the blosxom
264 development mailing list: <blosxom-devel@lists.sourceforge.net>.
274 Gavin Carr <gavin@openfusion.com.au>, http://www.openfusion.net/
278 Copyright 2007, Gavin Carr.
280 This plugin is licensed under the same terms as blosxom itself i.e.
282 Permission is hereby granted, free of charge, to any person obtaining a
283 copy of this software and associated documentation files (the "Software"),
284 to deal in the Software without restriction, including without limitation
285 the rights to use, copy, modify, merge, publish, distribute, sublicense,
286 and/or sell copies of the Software, and to permit persons to whom the
287 Software is furnished to do so, subject to the following conditions:
289 The above copyright notice and this permission notice shall be included
290 in all copies or substantial portions of the Software.
292 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
293 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
294 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
295 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
296 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
297 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
298 OTHER DEALINGS IN THE SOFTWARE.