Add storydate.
[matthijs/upstream/blosxom-plugins.git] / gavinc / flavourpathinfo
1 # Blosxom Plugin: flavourpathinfo
2 # Author(s): Gavin Carr <gavin@openfusion.com.au>
3 # Version: 0.002001
4 # Documentation: 'perldoc flavourpathinfo'
5 # Follows: extensionless
6
7 package flavourpathinfo;
8
9 use strict;
10
11 # --- Configurable variables -----
12
13 my $debug_level = 0;
14
15 # --------------------------------
16
17 my $package = 'flavourpathinfo';
18 sub debug {
19     my ($level, @msg) = @_;
20
21     if ($debug_level >= $level) {
22         print STDERR "$package debug $level: @msg\n";
23     }
24 }
25
26 sub start { 
27     my $path_info = $blosxom::path_info;
28
29     # Remove any trailing /
30     $path_info =~ s! /$ !!x;
31
32     my $path_file = $blosxom::path_info;
33     $path_file =~ s/\.\w+$/.$blosxom::file_extension/;
34     return 1 if -e "$blosxom::datadir/$path_info" || -f "$blosxom::datadir/$path_file";
35
36     debug(1, "original path_info: $path_info");
37
38     # Check file/flavour variant
39     if ($path_info =~ m! ^ (.*) / ([^/]+) $ !x) {
40         my $flavour = $2;
41         my $path_info_new = "$1.$flavour";
42         my $path_file = "$1.$blosxom::file_extension";
43
44         debug(2, "path_file: $path_file, path_info_new: $path_info_new");
45
46         if (-f "$blosxom::datadir/$path_file") {
47             debug(1, "\$path_info_new exists - updating \$blosxom::path_info");
48             $blosxom::path_info = $path_info_new;
49             $blosxom::flavour = $flavour;
50             $blosxom::path_info_yr = undef;
51             return 1;
52         }
53     }
54
55     # Check dir/flavour variant (implying index.flavour)
56     if ($path_info =~ m! ^ (?: (.*) / )? ([^/]+) $ !x) {
57         my $dir = $1 || '';
58         my $flavour = $2;
59         debug(2, "dir: $dir, flavour: $flavour");
60   
61         # Check there isn't an entry matching this
62         if (-f "$blosxom::datadir$dir/$flavour.$blosxom::file_extension") {
63           debug(2, "entry $blosxom::datadir/$dir/$flavour.$blosxom::file_extension found - skipping");
64           return 1;
65         }
66           
67         # Check $dir is a directory (sanity check - overly aggressive?)
68         if ($dir && ! -d "$blosxom::datadir/$dir") {
69           debug(2, "dir '$dir' is set but not a directory");
70           return 1; 
71         }
72
73         debug(1, "dir '$dir' not set or exists and directory - setting \$blosxom::path_info");
74         $blosxom::path_info = $dir;
75         $blosxom::flavour = $flavour;
76         $blosxom::path_info_yr = undef;
77         return 1;
78     }
79
80     return 1; 
81 }
82
83 1;
84
85 __END__
86
87 =head1 NAME
88
89 flavourpathinfo - allows flavour designation via a trailing path component
90 instead of via a file extension
91
92 =head1 DESCRIPTION
93
94 flavourpathinfo is a syntactic sugar plugin that allows flavour designation 
95 via a trailing path component instead of via a file extension e.g.
96
97     http://blog.example.com/category/post/html
98     http://blog.example.com/category/post/atom
99     http://blog.example.com/category/post/trackback
100
101 instead of the more typical:
102
103     http://blog.example.com/category/post.html
104     http://blog.example.com/category/post.atom
105     http://blog.example.com/category/post.trackback
106
107 With index pages, the 'index' portion can also be omitted, if desired:
108
109     http://blog.example.com/category/html
110     http://blog.example.com/category/atom
111
112 map to:
113
114     http://blog.example.com/category/index.html
115     http://blog.example.com/category/index.atom
116
117 =head1 USAGE
118
119 If used with the 'extensionless' plugin, it should be loaded
120 *after* extensionless.
121
122 But should be loaded early as it manipulates $blosxom::path_info
123 e.g. as 02flavourpathinfo.
124
125 =head1 SEE ALSO
126
127 extensionless
128
129 Blosxom: http://blosxom.sourceforge.net/
130
131 =head1 BUGS
132
133 Please report bugs either directly to the author or to the blosxom 
134 development mailing list: <blosxom-devel@lists.sourceforge.net>.
135
136 =head1 AUTHOR
137
138 Gavin Carr <gavin@openfusion.com.au>, http://www.openfusion.net/
139
140 =head1 LICENSE
141
142 Copyright 2007 Gavin Carr.
143
144 This plugin is licensed under the same terms as blosxom itself i.e.
145
146 Permission is hereby granted, free of charge, to any person obtaining a
147 copy of this software and associated documentation files (the "Software"),
148 to deal in the Software without restriction, including without limitation
149 the rights to use, copy, modify, merge, publish, distribute, sublicense,
150 and/or sell copies of the Software, and to permit persons to whom the
151 Software is furnished to do so, subject to the following conditions:
152
153 The above copyright notice and this permission notice shall be included
154 in all copies or substantial portions of the Software.
155
156 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
157 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
158 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
159 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
160 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
161 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
162 OTHER DEALINGS IN THE SOFTWARE.
163
164 =cut
165
166 # vim:ft=perl
167