Add initial microformat plugins.
[matthijs/upstream/blosxom-plugins.git] / gavinc / uf_adr_meta
diff --git a/gavinc/uf_adr_meta b/gavinc/uf_adr_meta
new file mode 100644 (file)
index 0000000..dd9f18d
--- /dev/null
@@ -0,0 +1,218 @@
+# Blosxom Plugin: uf_adr_meta
+# Author(s): Gavin Carr <gavin@openfusion.com.au>
+# Version: 0.001000
+# Documentation: 'perldoc uf_adr_meta'
+
+package uf_adr_meta;
+
+use strict;
+
+# Uncomment next line to enable debug output (don't uncomment debug() lines)
+#use Blosxom::Debug debug_level => 1;
+
+# --- Configuration defaults -----
+
+my %config = (
+
+  # Extra CSS classes to add to the microformat container e.g. to turn display off
+  class => '',
+  #class => 'nodisplay',
+
+  # Whether to automatically add microformat to story bodies. If not set, 
+  # you must explicitly add $uf_adr_meta::adr to a template somewhere.
+  auto_append_to_body => 1,
+
+  # What markup style to use for your adr, if auto-appending. 
+  # 3 styles are currently defined: 
+  # 'div-span' uses a 'div' elt for the container, and 'span' elements for the fields
+  # 'ul' uses a 'ul' list for the container, and 'li' elements for the fields
+  # 'dl' uses a 'dl' list for the container, 'dt' elements for field names, and 
+  #    'dd' elements for the fields themselves
+  #style => 'div-span',
+  #style => 'ul',
+  style => 'dl',
+
+);
+
+# --------------------------------
+
+use vars qw($adr);
+
+# Official adr attributes
+my @attr = qw(post-office-box extended-address street-address
+              locality region postal-code country-name);
+# Attribute aliases
+my %alias = (
+    'post-office-box'       => 'pobox',
+    'street-address'        => 'street',
+    locality                => 'city',
+    region                  => 'state',
+    'postal-code'           => 'postcode',
+    'country-name'          => 'country',
+);
+# Attributes which if set will cause us to skip this plugin (looks like an hcard)
+my @skip_attr = qw(fn name);
+
+$config{style} = 'div-span' unless $config{style} eq 'ul' or $config{style} eq 'dl';
+
+sub start { 1 }
+
+# Return the first existing metadata item key and value given a list of keys
+sub _get_meta {
+    for my $attr ( @_ ) {
+        my $meta_attr = $attr;
+        $meta_attr =~ s/-/_/g;
+        my $value = $blosxom::meta{$meta_attr};
+        $value = eval "\$meta::$attr" unless defined $value;
+        return wantarray ? ( $attr, $value ) : $value if defined $value;
+    }
+    return wantarray ? () : undef;
+}
+
+sub story {
+    my ($pkg, $path, $filename, $story_ref, $title_ref, $body_ref) = @_;
+
+    # Skip if any of the @skip_attr are set
+    for (@skip_attr) {
+        return 1 if $blosxom::meta{$_} || eval "\$meta::$_"; 
+    }
+
+    my $story_style = _get_meta( 'adr_style' ) || $config{style};
+    my $ctag = $story_style eq 'div-span' ? 'div' : $story_style;
+    my $etag = $story_style eq 'div-span' ? 'span' :
+               $story_style eq 'ul' ? 'li' : 'dd';
+
+    $adr = '';
+    for my $attr ( @attr ) {
+        my $meta_attr = $attr;
+        $meta_attr =~ s/-/_/g;
+        my $value = $blosxom::meta{$meta_attr} || eval "\$meta::$attr" 
+                 || $blosxom::meta{ $alias{$attr} } || eval "\$meta::$alias{$attr}";
+        next unless defined $value;
+        $adr .= qq(<dt>$attr</dt>) if $story_style eq 'dl';
+        $adr .= qq(<$etag class="$attr">$value</$etag>\n);
+    }
+    if ($adr) {
+      my $container_classes = 'adr';
+      if (my $meta_class = _get_meta('adr_class')) {
+        $container_classes .= " $meta_class";
+      }
+      else {
+        $container_classes .= " $config{class}" if $config{class};
+      }
+      $adr = qq(<$ctag class="$container_classes">\n$adr</$ctag>\n);
+      # debug(1, "uf_adr_meta: $adr\n");
+    }
+
+    my $autoappend = _get_meta( 'adr_autoappend' );
+    $autoappend = $config{auto_append_to_body} unless defined $autoappend;
+    return 1 unless $autoappend;
+
+    $$body_ref .= "\n\n$adr\n\n";
+
+    return 1;
+}
+
+1;
+
+__END__
+
+=head1 NAME
+
+uf_adr_meta - plugin to create an 'adr' microformat tag from post 
+metadata
+
+=head1 DESCRIPTION
+
+uf_adr_meta is a plugin to create an 'adr' microformat tag from metadata 
+in your post. The microformat tag is created in the $uf_adr_meta::adr 
+variable for use in templates or by other plugins, or if the 
+'auto_append_to_body' config variable is set (it is by default), 
+uf_adr_meta will append the tag to your story body automatically.
+
+=head2 OPTIONAL METADATA ITEMS
+
+=over 4
+
+=item post-office-box (alt: pobox)
+
+=item extended-address 
+
+=item street-address (alt: street)
+
+=item locality (alt: city)
+
+=item region (alt: state)
+
+=item country-name (alt: country)
+
+=back
+
+=head2 Config Elements
+
+uf_adr_meta also supports a couple of config elements that can be used to
+override plugin config data on a per-story basis:
+
+=over 4
+
+=item Adr-Class (metamail) / adr_class (meta)
+
+This class (or list of classes) is appended to the class list applied to the
+top-level adr element in the rendered adr i.e. it overrides the 
+'class' config variable. 
+
+=item Adr-Autoappend (metamail) / adr_autoappend (meta)
+
+This is a flag (0 or 1) indicating whether the rendered adr should be 
+automatically appended to the story body. It overrides the 'auto_append_to_body'
+config variable.
+
+=item Adr-Style (metamail) / adr_style (meta)
+
+One of the following styles: 'div-span', 'ul', 'dl', used to render the adr. 
+It overrides the 'style' config variable.
+
+=back
+
+=head1 USAGE
+
+uf_adr_meta should be loaded after the meta plugins (meta
+itself, or the metaclear/metamail/metadir/metafile family).
+
+=head1 SEE ALSO
+
+Microformats.org: http://www.microformats.org/, http://microformats.org/wiki/address.
+
+Blosxom: http://blosxom.sourceforge.net/
+
+=head1 AUTHOR
+
+Gavin Carr <gavin@openfusion.com.au>, http://www.openfusion.net/
+
+=head1 LICENSE
+
+Copyright 2007, Gavin Carr.
+
+This plugin is licensed under the same terms as blosxom itself i.e.
+
+Permission is hereby granted, free of charge, to any person obtaining a
+copy of this software and associated documentation files (the "Software"),
+to deal in the Software without restriction, including without limitation
+the rights to use, copy, modify, merge, publish, distribute, sublicense,
+and/or sell copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included
+in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+
+=cut
+
+# vim:ft=perl