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