muse-latex: Make lecture notes and slides work with images, title page, TOC.
[muse-el.git] / contrib / blosxom / metadate_0_0_3
blob638eec893f44eb53e44df52dc27579d663fb01d3
1 # Blosxom Plugin: metadate
2 # Author(s): Mark Ivey <zovirl@zovirl.com>
3 # Version: 0.0.3
4 # Documentation: See the bottom of this file or type: perldoc metadate
6 package metadate;
8 # --- Configurable variables -----
10 # fullpath to the external metadates file (see perldoc for description)
11 $external_file = "$blosxom::datadir/external_metadate";
13 $use_UK_dates = 0;          # Default is mm/dd/yy (US)
14                             # Set to 1 to use dd/mm/yy (UK)
16 # --------------------------------
18 use English;
19 use File::Basename;
20 use Time::Local;
21 use File::Find;
22 use File::stat;
23 use Time::Local;
25 use vars qw( %dirs, %all );
27 my $debug = 0;   # log debug messages or not?
29 sub start 
31     return 1;
34 # FIXME: make the external metadate file optional
35 # FIXME: handle both creation date & modification date...
37 sub filter 
39     _add_from_find();
40     _add_from_file();
41     return 1;
44 sub _add_from_file
46     # read the external metadates file
47     unless (open(FILE, "< $external_file"))
48     {
49         warn "metadate::filter() couldn't open external file $external_file\n"; 
50         return 0;
51     }
52     
53     while (<FILE>) 
54     {
55         next if (/^\s*#/);  # skip comments
56         if (/(.*)=>(.*)/)
57         {
58             my ($file, $time) = ($1, $2);
59             $file =~ s!/*$!!;               # remove any trailing slashes
60             $file =~ s!^/*!!;               # remove any leading slashes
61             $file = "$blosxom::datadir/$file";
62             warn "metadate: $file=>$time\n" if $debug > 0;
63             
64             $time = parsedate($time);
65             
66             _add($file, $time);
67         }
68     }
69     close(FILE);
71     return 1;
74 sub _add_from_find
76     find(
77         sub {
78                 _add($File::Find::name, extract_date($File::Find::name,$files{$File::Find::name}) ||
79                                         $files{$File::Find::name} ||
80                                         stat($File::Find::name)->mtime)
81                 },
82         $blosxom::datadir
83     );
85     return 1;
88 sub _add
90     my $file = shift or return;
91     my $time = shift or return;
93     # check %files, then %others, then check for a directory.  Always add to %all
94     if (exists $blosxom::files{$file})
95     {
96         $blosxom::files{$file} = $time;
97     }
98     elsif (exists $blosxom::others{$file})
99     {
100         $blosxom::others{$file} = $time;
101     }
102     elsif (-d "$file")
103     {
104         $dirs{$file} = $time;
105     }
107     $all{$file} = $time;
111 # extract_date() taken from Fletcher T. Penney's entriescache plugin
112 # <http://www.blosxom.com/plugins/indexing/entries_cache.htm>
114 # FIXME I should make _add_from_file() know about these and then
115 # make them user variables
116 $use_date_tags = 1;
117 $update_meta_date = 0;
118 $meta_timestamp = "meta-creation_timestamp:" unless defined $meta_timestamp;
119 $meta_date = "meta-creation_date:" unless defined $meta_date;
121 sub extract_date {
122         my ($file, $indexed_date) = @_;
123         my $new_story = "";
124         my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst);
125         
126         # This is an attempt for compatibility with Eric Sherman's entries_index_tagged
127         # But it does not handle as many date formats, as there are too many additional
128         # necessary modules that I am not willing to require
129         
130         if ( $use_date_tags != 0) {
131         
132                 open (FILE, $file);
134                 while ($line = <FILE>) {
135                         if ($line =~ /^$meta_timestamp\s*(\d+)/) {
136                                 # If present, this format is used
137                                 close File;
138                                 return $1;
139                         }
140                         
141                         if ($line =~ /^$meta_date\s*(.*)/) {
142                                 close File;
143                                 return parsedate($1);
144                         }
145                         
146                         if ($line =~ /^\s*$/) {
147                                 # Empty Line signifying end of meta-tags
148                                 if ($update_meta_date eq 1) {
149                                         if ($indexed_date eq 0) {
150                                                 $indexed_date = stat($file)->mtime;
151                                         }
152                                         
153                                         ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime($indexed_date);
154                                         $year += 1900;
155                                         $mon += 1;
156                                         $hour = sprintf("%02d",$hour);
157                                         $min = sprintf("%02d",$min);
158                                         $sec = sprintf("%02d",$sec);
159                                 
160                                         if ($use_UK_dates eq 1) {
161                                                 $new_story .= "$meta_date $mday/$mon/$year $hour:$min:$sec\n\n";
162                                         } else {
163                                                 $new_story .= "$meta_date $mon/$mday/$year $hour:$min:$sec\n\n";
164                                         }
165                                         
166                                         while ($line = <FILE>) {
167                                                 $new_story .= $line;
168                                         }
169                                         
170                                         close FILE;
171                                         open (FILE, "> $file") or warn "Unable to update date meta-tag on $file\n";
172                                         print FILE $new_story;
173                                         close FILE;
174                                         return 0;
175                                 } else {
176                                         close File;
177                                         return 0;
178                                 }
179                         }
181                         $new_story .= $line;
182                 }
183         }
184         return 0;       
187 # parsedate() taken from Fletcher T. Penney's entriescache plugin
188 # <http://www.blosxom.com/plugins/indexing/entries_cache.htm>
189 sub parsedate 
191         my ($datestring) = @_;
192         #warn "Parsing $datestring\n";
193         
194         # Possible formatting
195         # Month can be 3 letter abbreviation or full name (in English)
196         # Time must be hh:mm or hh:mm:ss  in 24 hour format
197         # Year must be yyyy
198         # The remaining 1 or 2 digits are treated as date
199         # ie: May 25 2003 18:40 
200         # order is not important as long as pieces are there
201                 
202         # Convert the datestring to a time() format
204         # Find "Shorthand" Date
205         if ( $datestring =~ /\d\d?\/\d\d?\/\d\d\d?\d?/) {
206                 if ( $use_UK_dates eq 0) {
207     # Use US Formatting
208     $datestring =~ s/(\d\d?)\/(\d\d?)\/(\d\d\d?\d?)//;
209     $mon = $1 - 1;
210     $day = $2;
211     $year = $3;
212                 } else {
213     # Use UK Formatting
214     $datestring =~ s/(\d\d?)\/(\d\d?)\/(\d\d\d?\d?)//;
215     $mon = $2 - 1;
216     $day = $1;
217     $year = $3;
218                 }
219                 
220                 # Now, clean up year if 2 digit
221                 # You may change the 70 to whatever cutoff you like
222                 $year += 2000 if ($year < 70 );
223                 $year += 1900 if ($year < 100);
224         }
225         
226         # Find Month
227         $mon = 0 if ($datestring =~ s/(Jan|January)//i);
228         $mon = 1 if ($datestring =~ s/(Feb|February)//i);
229         $mon = 2 if ($datestring =~ s/(Mar|March)//i);
230         $mon = 3 if ($datestring =~ s/(Apr|April)//i);
231         $mon = 4 if ($datestring =~ s/(May)//i);
232         $mon = 5 if ($datestring =~ s/(Jun|June)//i);
233         $mon = 6 if ($datestring =~ s/(Jul|July)//i);
234         $mon = 7 if ($datestring =~ s/(Aug|August)//i);
235         $mon = 8 if ($datestring =~ s/(Sep|September)//i);
236         $mon = 9 if ($datestring =~ s/(Oct|October)//i);
237         $mon = 10 if ($datestring =~ s/(Nov|November)//i);
238         $mon = 11 if ($datestring =~ s/(Dec|December)//i);
240         # Find Time
241         if ($datestring =~ s/(\d\d?):(\d\d)(:\d\d)?//) {
242                 $hour = $1;
243                 $min = $2;
244                 $sec = $3;
245         }
246         
247         if ($datestring =~ s/(\d\d\d\d)//) {
248                 $year = $1;
249         }
250         
251         if ($datestring =~ s/(\d\d?)//) {
252                 $day = $1;
253         }
254         
255         return timelocal($sec,$min,$hour,$day,$mon,$year);
256         
262 __END__
264 =head1 NAME
266 Blosxom Plug-in: metadate
268 =head1 SYNOPSIS
270 Handles meta tags related to dates.
272 =head1 VERSION
274 0.0.3
276 =head1 AUTHOR
278 Mark Ivey <zovirl@zovirl.com>, http://zovirl.com
280 =head1 DESCRIPTION
282 metadate parses metadates in stories.  It also
283 provides a way to save metadates externally from a file.
284 This is most useful for non-story files, such as pictures or categories which
285 can't contain metadates internally.
287 The file $external_file should contain entries in this format:
289  # comment lines start with a "#"
290  /software=>11/17/2003 22:45
291  /software/screenshot.png=>11/18/2003 22:50
292  /software/static_file.patch.asc=>11/18/2003 22:50
294 WARNING: The file format changed between version 0.0.1 and 0.0.2.  It used
295 to contain the full path to the file.  Now it contains the path from
296 $blosxom::datadir, to make it easier to relocate $blosxom::datadir
298 metadates stores dates in %blosxom::files (for story files), %blosxom::others
299 (for non-story files), or %metadate::dirs (for directories).  Metdates
300 also provides %metadates::all, which contains any file/directory metadate knows about.
302 =head1 SEE ALSO
304 Blosxom Home/Docs/Licensing: http://www.raelity.org/apps/blosxom/
306 Blosxom Plugin Docs: http://www.raelity.org/apps/blosxom/plugin.shtml
308 parsedate() and extract_date() taken from Fletcher T. Penney's entriescache plugin:
309 http://www.blosxom.com/plugins/indexing/entries_cache.htm
311 =head1 BUGS
313 Address bug reports and comments to the Blosxom mailing list 
314 [http://www.yahoogroups.com/group/blosxom].
316 =head1 LICENSE
318 metadate Blosxom Plugin Copyright 2003-2004, Mark Ivey
320 Portions Copyright 2003, Fletcher Penney
322 Permission is hereby granted, free of charge, to any person obtaining a
323 copy of this software and associated documentation files (the "Software"),
324 to deal in the Software without restriction, including without limitation
325 the rights to use, copy, modify, merge, publish, distribute, sublicense,
326 and/or sell copies of the Software, and to permit persons to whom the
327 Software is furnished to do so, subject to the following conditions:
329 The above copyright notice and this permission notice shall be included
330 in all copies or substantial portions of the Software.
332 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
333 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
334 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
335 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
336 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
337 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
338 OTHER DEALINGS IN THE SOFTWARE.