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