root/lang/perl/blosxom/plugins/datemap

Revision 7370, 2.2 kB (checked in by nyarla, 6 months ago)

lang/perl/blosxom/plugins/datemap: 微調整

Line 
1# Blosxom Plugin: datemap
2# Author: Naoki Okamura (Nyarla) <thotep@nyarla.net>
3# Version: 2008-03-02
4# Licesen: public domain
5
6package datemap;
7
8use strict;
9use warnings;
10
11use FileHandle;
12
13# -- configurable variables ---------- #
14
15my $file = "$blosxom::plugin_state_dir/datemap.dat";
16
17# -- package variables --------------- #
18
19my $datemap = { files => {}, others => {} };
20my $fh      = FileHandle->new;
21
22# ------------------------------------ #
23
24sub start {
25    if ( -f $file && -r _ ) {
26        $datemap = load($file);
27    }
28    return 1;
29}
30
31sub 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
45sub 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
63sub 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
79sub 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
951;
96
97__END__
98
99=head1 NAME
100
101datemap - Fixing dated of entries
102
103=head1 AUTHOR
104
105Naoki Okamura (Nyarla) E<lt>thotep@nyarla.netE<gt>
106
107=head1 LICENSE
108
109This code is public domain.
110
111=cut
Note: See TracBrowser for help on using the browser.