Convert remaining gavinc plugins to %config versions.
[matthijs/upstream/blosxom-plugins.git] / gavinc / storydate
index 0341ea0d2dc0dd3a4a87f197481fa60802142ee0..f8c724689f5d847fe7983d4d82002a1e9cceb5bf 100644 (file)
@@ -2,13 +2,14 @@
 # Author(s): Gavin Carr <gavin@openfusion.com.au>
 # (based on work by Frank Hecker <hecker@hecker.org>
 #  and Bob Schumaker <cobblers@pobox.com>)
-# Version: 0.001000
+# Version: 0.003000
 # Documentation: See the bottom of this file or type: perldoc storydate
 
 package storydate;
 
 use strict;
 
+use vars qw(%config);
 use File::stat;
 
 # Uncomment next line to enable debug output (don't uncomment debug() lines)
@@ -16,10 +17,13 @@ use File::stat;
 
 # --- Configuration variables -----
 
+%config = ();
+
 # Perl modules to use for strftime, in search order
-my @strftime_modules = qw(Time::Piece Date::Format POSIX);
+$config{strftime_modules} = [ qw(Time::Piece Date::Format POSIX) ];
 
 # ---------------------------------
+# __END_CONFIG__
 
 # Package variables
 use vars qw(
@@ -29,14 +33,16 @@ use vars qw(
   $story_mtime_iso8601
   $now_rfc822
   $now_iso8601
+  $latest_rfc822
+  $latest_iso8601
   %strftime_formats
 );
 
 %strftime_formats = (
-    rfc822      => "%a, %d %b %Y  %T %z",
+    rfc822      => "%a, %d %b %Y %T %z",
     # ISO 8601 format (localtime, including time zone offset)
     # Format is YYYY-MM-DDThh:mm:ssTZD per http://www.w3.org/TR/NOTE-datetime
-    iso8601     => "%Y-%m-%dT%T %z",
+    iso8601     => "%Y-%m-%dT%T%z",
 );
 
 sub start { 
@@ -48,6 +54,22 @@ sub start {
     1;
 }
 
+sub filter {
+    my ($pkg, $files, $others) = @_;
+
+    # Find the latest publish date in our stories
+    my $latest_ts = 0;
+    for (values %$files) {
+         $latest_ts = $_ if $_ > $latest_ts;
+    }
+
+    # Format the 'latest' variables
+    $latest_rfc822  = format_date('rfc822', $latest_ts);
+    $latest_iso8601 = format_date('iso8601', $latest_ts);
+
+    return 1;
+}
+
 sub story {
     my ($pkg, $path, $filename, $story_ref, $title_ref, $body_ref) = @_;
 
@@ -93,15 +115,23 @@ sub get_story_dates {
 sub format_date {
     my ($format, $time, $use_gmtime) = @_;
     return unless $format;
+
+    my $date = '';
     if ($format =~ m/%/) {
-        return strftime($format, $time, $use_gmtime);
+        $date = strftime($format, $time, $use_gmtime);
     }
     elsif ($strftime_formats{ $format }) {
-        return strftime($strftime_formats{ $format }, $time, $use_gmtime);
+        $date = strftime($strftime_formats{ $format }, $time, $use_gmtime);
+    }
+    # Hack to handle the fact that ISO8601 dates require a : in the timezone, which strftime doesn't support
+    if ($format eq 'iso8601') {
+        $date =~ s/(\d)(\d{2})$/$1:$2/;
     }
+
+    return $date;
 }
 
-# strftime wrapper, using the first strftime() it finds in @strftime_modules
+# strftime wrapper, using the first strftime() it finds in 'strftime_modules'
 my %cache = ();
 my $strftime_module = '';
 sub strftime {
@@ -118,7 +148,7 @@ sub strftime {
 
     # Search for a strftime module, and load
     if (! $strftime_module) {
-        for my $module (@strftime_modules) {
+        for my $module (@{$config{strftime_modules}}) {
             if (eval "require $module") {
                 $strftime_module = $module;
                 # debug(2, "strftime_module: $strftime_module");
@@ -198,6 +228,14 @@ The current time in RFC822 format.
 
 The current time in ISO8601 format.
 
+=item $latest_rfc822
+
+The latest story publication date, in RFC822 format.
+
+=item $latest_iso8601
+
+The latest story publication date, in ISO8601 format.
+
 =back
 
 In addition, storydate defines a few subroutines that might be useful