Add initial microformat plugins.
[matthijs/upstream/blosxom-plugins.git] / gavinc / uf_geo_meta
1 # Blosxom Plugin: uf_geo_meta
2 # Author(s): Gavin Carr <gavin@openfusion.com.au>
3 # Version: 0.001000
4 # Documentation: 'perldoc uf_geo_meta'
5
6 package uf_geo_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 # --- Configurable variables -----
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
39 use vars qw($geo);
40
41 # Attributes which if set will cause us to skip this plugin (looks like an adr)
42 my @skip_attr = qw(locality region postal-code country-name city state postcode country);
43
44 $config{style} = 'div-span' unless $config{style} eq 'ul' or $config{style} eq 'dl';
45
46 sub start { 1 }
47
48 # Return the first existing metadata item key and value given a list of keys
49 sub _get_meta {
50     for my $attr ( @_ ) {
51         my $meta_attr = $attr;
52         $meta_attr =~ s/-/_/g;
53         my $value = $blosxom::meta{$meta_attr};
54         $value = eval "\$meta::$attr" unless defined $value;
55         return wantarray ? ( $attr, $value ) : $value if defined $value;
56     }
57     return wantarray ? () : undef;
58 }
59
60 sub story {
61     my ($pkg, $path, $filename, $story_ref, $title_ref, $body_ref) = @_;
62
63     # Skip if any of the @skip_attr are set
64     for (@skip_attr) {
65         return 1 if $blosxom::meta{$_} || eval "\$meta::$_"; 
66     }
67
68     my $story_style = _get_meta( 'geo_style' ) || $config{style};
69     my $ctag = $story_style eq 'div-span' ? 'div' : $story_style;
70     my $etag = $story_style eq 'div-span' ? 'span' :
71                $story_style eq 'ul' ? 'li' : 'dd';
72     my $sep = ', ' if $story_style eq 'div-span';
73
74     my $latitude = _get_meta('latitude');
75     my $longitude = _get_meta('longitude');
76     return 1 unless defined $latitude && defined $longitude;
77
78     $geo = '';
79     my $container_classes = 'geo';
80     if (my $meta_class = _get_meta('geo_class')) {
81       $container_classes .= " $meta_class";
82     }
83     else {
84       $container_classes .= " $config{class}" if $config{class};
85     }
86
87     $geo .= qq(<$ctag class="$container_classes">);
88     $geo .= qq(<dt>Latitude</dt>) if $story_style eq 'dl';
89     $geo .= qq(<$etag class="latitude">$latitude</$etag>);
90     $geo .= $sep if $story_style = 'div-span';
91     $geo .= qq(<dt>Longitude</dt>) if $story_style eq 'dl';
92     $geo .= qq(<$etag class="longitude">$longitude</$etag>);
93     $geo .= qq(</$ctag>);
94     # debug(1, "uf_geo_meta: $geo\n");
95
96     my $autoappend = _get_meta( 'geo_autoappend' );
97     $autoappend = $config{auto_append_to_body} unless defined $autoappend;
98     return 1 unless $autoappend;
99
100     $$body_ref .= "\n\n$geo\n\n";
101
102     return 1;
103 }
104
105 1;
106
107 __END__
108
109 =head1 NAME
110
111 uf_geo_meta - plugin to create a 'geo' microformat tag from post metadata
112
113 =head1 DESCRIPTION
114
115 uf_geo_meta is a plugin to create a 'geo' microformat tag from metadata
116 in your post. The microformat tag is created in the $uf_geo_meta::geo
117 variable for use in templates or by other plugins, or if the
118 'auto_append_to_body' config variable is set (it is by default), uf_geo_meta 
119 will append the tag to your story body automatically.
120
121 =head2 REQUIRED METADATA ITEMS
122
123 =over 4
124
125 =item latitude
126
127 A decimal between -90.0 (South Pole) and +90.0 (North Pole), indicating 
128 degrees of latitude.
129
130 =item longitude
131
132 A decimal between -180.0 (western hemisphere) and +180.0 (eastern hemisphere), 
133 indicating degrees of longitude.
134
135 =back
136
137 If any required metadata is missing the plugin just skips the story.
138
139 =head2 Config Elements
140
141 uf_geo_meta also supports a couple of config elements that can be used to
142 override plugin config data on a per-story basis:
143
144 =over 4
145
146 =item Geo-Class (metamail) / geo_class (meta)
147
148 This class (or list of classes) is appended to the class list applied to the
149 top-level geo element in the rendered geo i.e. it overrides the 
150 'class' config variable. 
151
152 =item Geo-Autoappend (metamail) / geo_autoappend (meta)
153
154 This is a flag (0 or 1) indicating whether the rendered geo should be 
155 automatically appended to the story body. It overrides the 'auto_append_to_body'
156 config variable.
157
158 =item Geo-Style (metamail) / geo_style (meta)
159
160 One of the following styles: 'div-span', 'ul', 'dl', used to render the geo. 
161 It overrides the 'style' config variable.
162
163 =back
164
165 =head1 EXAMPLE
166
167 Adding a geo microformat to  your post then becomes as simple as:
168
169   Testing uf_geo_meta
170   Latitude: -33.717770
171   Longitude: 151.115886
172
173 if using metamail, or:
174
175   Random blog post
176   meta-latitude: -33.717770
177   meta-longitude: 151.115886
178
179 =head1 USAGE
180
181 uf_geo_meta should be loaded after the meta plugins (meta
182 itself, or the metaclear/metamail/metadir/metafile family).
183
184 =head1 SEE ALSO
185
186 Microformats.org: http://www.microformats.org/, 
187 http://microformats.org/wiki/geo.
188
189 Blosxom: http://blosxom.sourceforge.net/
190
191 =head1 AUTHOR
192
193 Gavin Carr <gavin@openfusion.com.au>, http://www.openfusion.net/
194
195 =head1 LICENSE
196
197 Copyright 2007, Gavin Carr.
198
199 This plugin is licensed under the same terms as blosxom itself i.e.
200
201 Permission is hereby granted, free of charge, to any person obtaining a
202 copy of this software and associated documentation files (the "Software"),
203 to deal in the Software without restriction, including without limitation
204 the rights to use, copy, modify, merge, publish, distribute, sublicense,
205 and/or sell copies of the Software, and to permit persons to whom the
206 Software is furnished to do so, subject to the following conditions:
207
208 The above copyright notice and this permission notice shall be included
209 in all copies or substantial portions of the Software.
210
211 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
212 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
213 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
214 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
215 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
216 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
217 OTHER DEALINGS IN THE SOFTWARE.
218
219 =cut
220
221 # vim:ft=perl