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