Add __END_CONFIG__ token to remaining gavinc plugins.
[matthijs/upstream/blosxom-plugins.git] / gavinc / uf_adr_meta
1 # Blosxom Plugin: uf_adr_meta
2 # Author(s): Gavin Carr <gavin@openfusion.com.au>
3 # Version: 0.001000
4 # Documentation: 'perldoc uf_adr_meta'
5
6 package uf_adr_meta;
7
8 use strict;
9
10 # Uncomment next line to enable debug output (don't uncomment debug() lines)
11 #use Blosxom::Debug debug_level => 1;
12
13 # --- Configuration defaults -----
14
15 my %config = (
16
17   # Extra CSS classes to add to the microformat container e.g. to turn display off
18   class => '',
19   #class => 'nodisplay',
20
21   # Whether to automatically add microformat to story bodies. If not set, 
22   # you must explicitly add $uf_adr_meta::adr to a template somewhere.
23   auto_append_to_body => 1,
24
25   # What markup style to use for your adr, if auto-appending. 
26   # 3 styles are currently defined: 
27   # 'div-span' uses a 'div' elt for the container, and 'span' elements for the fields
28   # 'ul' uses a 'ul' list for the container, and 'li' elements for the fields
29   # 'dl' uses a 'dl' list for the container, 'dt' elements for field names, and 
30   #    'dd' elements for the fields themselves
31   #style => 'div-span',
32   #style => 'ul',
33   style => 'dl',
34
35 );
36
37 # --------------------------------
38 # __END_CONFIG__
39
40 use vars qw($adr);
41
42 # Official adr attributes
43 my @attr = qw(post-office-box extended-address street-address
44               locality region postal-code country-name);
45 # Attribute aliases
46 my %alias = (
47     'post-office-box'       => 'pobox',
48     'street-address'        => 'street',
49     locality                => 'city',
50     region                  => 'state',
51     'postal-code'           => 'postcode',
52     'country-name'          => 'country',
53 );
54 # Attributes which if set will cause us to skip this plugin (looks like an hcard)
55 my @skip_attr = qw(fn name);
56
57 $config{style} = 'div-span' unless $config{style} eq 'ul' or $config{style} eq 'dl';
58
59 sub start { 1 }
60
61 # Return the first existing metadata item key and value given a list of keys
62 sub _get_meta {
63     for my $attr ( @_ ) {
64         my $meta_attr = $attr;
65         $meta_attr =~ s/-/_/g;
66         my $value = $blosxom::meta{$meta_attr};
67         $value = eval "\$meta::$attr" unless defined $value;
68         return wantarray ? ( $attr, $value ) : $value if defined $value;
69     }
70     return wantarray ? () : undef;
71 }
72
73 sub story {
74     my ($pkg, $path, $filename, $story_ref, $title_ref, $body_ref) = @_;
75
76     # Skip if any of the @skip_attr are set
77     for (@skip_attr) {
78         return 1 if $blosxom::meta{$_} || eval "\$meta::$_"; 
79     }
80
81     my $story_style = _get_meta( 'adr_style' ) || $config{style};
82     my $ctag = $story_style eq 'div-span' ? 'div' : $story_style;
83     my $etag = $story_style eq 'div-span' ? 'span' :
84                $story_style eq 'ul' ? 'li' : 'dd';
85
86     $adr = '';
87     for my $attr ( @attr ) {
88         my $meta_attr = $attr;
89         $meta_attr =~ s/-/_/g;
90         my $value = $blosxom::meta{$meta_attr} || eval "\$meta::$attr" 
91                  || $blosxom::meta{ $alias{$attr} } || eval "\$meta::$alias{$attr}";
92         next unless defined $value;
93         $adr .= qq(<dt>$attr</dt>) if $story_style eq 'dl';
94         $adr .= qq(<$etag class="$attr">$value</$etag>\n);
95     }
96     if ($adr) {
97       my $container_classes = 'adr';
98       if (my $meta_class = _get_meta('adr_class')) {
99         $container_classes .= " $meta_class";
100       }
101       else {
102         $container_classes .= " $config{class}" if $config{class};
103       }
104       $adr = qq(<$ctag class="$container_classes">\n$adr</$ctag>\n);
105       # debug(1, "uf_adr_meta: $adr\n");
106     }
107
108     my $autoappend = _get_meta( 'adr_autoappend' );
109     $autoappend = $config{auto_append_to_body} unless defined $autoappend;
110     return 1 unless $autoappend;
111
112     $$body_ref .= "\n\n$adr\n\n";
113
114     return 1;
115 }
116
117 1;
118
119 __END__
120
121 =head1 NAME
122
123 uf_adr_meta - plugin to create an 'adr' microformat tag from post 
124 metadata
125
126 =head1 DESCRIPTION
127
128 uf_adr_meta is a plugin to create an 'adr' microformat tag from metadata 
129 in your post. The microformat tag is created in the $uf_adr_meta::adr 
130 variable for use in templates or by other plugins, or if the 
131 'auto_append_to_body' config variable is set (it is by default), 
132 uf_adr_meta will append the tag to your story body automatically.
133
134 =head2 OPTIONAL METADATA ITEMS
135
136 =over 4
137
138 =item post-office-box (alt: pobox)
139
140 =item extended-address 
141
142 =item street-address (alt: street)
143
144 =item locality (alt: city)
145
146 =item region (alt: state)
147
148 =item country-name (alt: country)
149
150 =back
151
152 =head2 Config Elements
153
154 uf_adr_meta also supports a couple of config elements that can be used to
155 override plugin config data on a per-story basis:
156
157 =over 4
158
159 =item Adr-Class (metamail) / adr_class (meta)
160
161 This class (or list of classes) is appended to the class list applied to the
162 top-level adr element in the rendered adr i.e. it overrides the 
163 'class' config variable. 
164
165 =item Adr-Autoappend (metamail) / adr_autoappend (meta)
166
167 This is a flag (0 or 1) indicating whether the rendered adr should be 
168 automatically appended to the story body. It overrides the 'auto_append_to_body'
169 config variable.
170
171 =item Adr-Style (metamail) / adr_style (meta)
172
173 One of the following styles: 'div-span', 'ul', 'dl', used to render the adr. 
174 It overrides the 'style' config variable.
175
176 =back
177
178 =head1 USAGE
179
180 uf_adr_meta should be loaded after the meta plugins (meta
181 itself, or the metaclear/metamail/metadir/metafile family).
182
183 =head1 SEE ALSO
184
185 Microformats.org: http://www.microformats.org/, http://microformats.org/wiki/address.
186
187 Blosxom: http://blosxom.sourceforge.net/
188
189 =head1 AUTHOR
190
191 Gavin Carr <gavin@openfusion.com.au>, http://www.openfusion.net/
192
193 =head1 LICENSE
194
195 Copyright 2007, Gavin Carr.
196
197 This plugin is licensed under the same terms as blosxom itself i.e.
198
199 Permission is hereby granted, free of charge, to any person obtaining a
200 copy of this software and associated documentation files (the "Software"),
201 to deal in the Software without restriction, including without limitation
202 the rights to use, copy, modify, merge, publish, distribute, sublicense,
203 and/or sell copies of the Software, and to permit persons to whom the
204 Software is furnished to do so, subject to the following conditions:
205
206 The above copyright notice and this permission notice shall be included
207 in all copies or substantial portions of the Software.
208
209 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
210 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
211 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
212 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
213 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
214 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
215 OTHER DEALINGS IN THE SOFTWARE.
216
217 =cut
218
219 # vim:ft=perl