| 1 | # Blosxom Plugin: datemap
|
|---|
| 2 | # Author: Naoki Okamura (Nyarla) <thotep@nyarla.net>
|
|---|
| 3 | # Version: 2008-03-02
|
|---|
| 4 | # Licesen: public domain
|
|---|
| 5 |
|
|---|
| 6 | package datemap;
|
|---|
| 7 |
|
|---|
| 8 | use strict;
|
|---|
| 9 | use warnings;
|
|---|
| 10 |
|
|---|
| 11 | use FileHandle;
|
|---|
| 12 |
|
|---|
| 13 | # -- configurable variables ---------- #
|
|---|
| 14 |
|
|---|
| 15 | my $file = "$blosxom::plugin_state_dir/datemap.dat";
|
|---|
| 16 |
|
|---|
| 17 | # -- package variables --------------- #
|
|---|
| 18 |
|
|---|
| 19 | my $datemap = { files => {}, others => {} };
|
|---|
| 20 | my $fh = FileHandle->new;
|
|---|
| 21 |
|
|---|
| 22 | # ------------------------------------ #
|
|---|
| 23 |
|
|---|
| 24 | sub start {
|
|---|
| 25 | if ( -f $file && -r _ ) {
|
|---|
| 26 | $datemap = load($file);
|
|---|
| 27 | }
|
|---|
| 28 | return 1;
|
|---|
| 29 | }
|
|---|
| 30 |
|
|---|
| 31 | sub entries {
|
|---|
| 32 | my $sub = $blosxom::entries;
|
|---|
| 33 | return sub {
|
|---|
| 34 | my ( $files, $indexes, $others ) = $sub->();
|
|---|
| 35 |
|
|---|
| 36 | mapping( $files, $datemap->{'files'} );
|
|---|
| 37 | mapping( $others, $datemap->{'others'} );
|
|---|
| 38 |
|
|---|
| 39 | save($file, $datemap);
|
|---|
| 40 |
|
|---|
| 41 | return ( $files, $indexes, $others );
|
|---|
| 42 | }
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | sub mapping {
|
|---|
| 46 | my ( $files, $date ) = @_;
|
|---|
| 47 |
|
|---|
| 48 | for my $path ( keys %{ $files } ) {
|
|---|
| 49 | if ( exists $date->{$path} ) {
|
|---|
| 50 | $files->{$path} = $date->{$path};
|
|---|
| 51 | }
|
|---|
| 52 | else {
|
|---|
| 53 | $date->{$path} = $files->{$path};
|
|---|
| 54 | }
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | for my $path ( keys %{ $date } ) {
|
|---|
| 58 | delete $date->{$path} if ( ! exists $files->{$path} );
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | sub load {
|
|---|
| 64 | my ( $file ) = @_;
|
|---|
| 65 |
|
|---|
| 66 | my $data = { files => {}, others => {} };
|
|---|
| 67 |
|
|---|
| 68 | $fh->open( $file, '<' ) or die "Couldn't open file: $file: $!";
|
|---|
| 69 | while ( my $line = <$fh> ) {
|
|---|
| 70 | chomp $line;
|
|---|
| 71 | my ( $type, $mtime, $path ) = ( $line =~ m{^(\w+)[|](\d+)[|](.+)$} );
|
|---|
| 72 | $data->{$type}->{$path} = $mtime;
|
|---|
| 73 | }
|
|---|
| 74 | $fh->close;
|
|---|
| 75 |
|
|---|
| 76 | return $data;
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | sub save {
|
|---|
| 80 | my ( $file, $data ) = @_;
|
|---|
| 81 | my $output = q{};
|
|---|
| 82 |
|
|---|
| 83 | for my $type ( keys %{ $data } ) {
|
|---|
| 84 | for my $path ( keys %{ $data->{$type} } ) {
|
|---|
| 85 | my $mtime = $data->{$type}->{$path};
|
|---|
| 86 | $output .= "${type}|$mtime|$path\n";
|
|---|
| 87 | }
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | $fh->open( $file, '>' ) or die "Couldn't open file: $file: $!";
|
|---|
| 91 | print $fh $output;
|
|---|
| 92 | $fh->close;
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | 1;
|
|---|
| 96 |
|
|---|
| 97 | __END__
|
|---|
| 98 |
|
|---|
| 99 | =head1 NAME
|
|---|
| 100 |
|
|---|
| 101 | datemap - Fixing dated of entries
|
|---|
| 102 |
|
|---|
| 103 | =head1 AUTHOR
|
|---|
| 104 |
|
|---|
| 105 | Naoki Okamura (Nyarla) E<lt>thotep@nyarla.netE<gt>
|
|---|
| 106 |
|
|---|
| 107 | =head1 LICENSE
|
|---|
| 108 |
|
|---|
| 109 | This code is public domain.
|
|---|
| 110 |
|
|---|
| 111 | =cut |
|---|