| 1 | # Blosxom Plugin: datesection
|
|---|
| 2 | # Author: Naoki Okamura (Nyarla) <thotep@nyarla.net>
|
|---|
| 3 | # Version: 2008-03-02
|
|---|
| 4 | # License: public domain
|
|---|
| 5 |
|
|---|
| 6 | package datesection;
|
|---|
| 7 |
|
|---|
| 8 | use strict;
|
|---|
| 9 | use warnings;
|
|---|
| 10 |
|
|---|
| 11 | use CGI qw( path_info );
|
|---|
| 12 | use FileHandle;
|
|---|
| 13 |
|
|---|
| 14 | # -- package variables --------------- #
|
|---|
| 15 |
|
|---|
| 16 | our %section = ();
|
|---|
| 17 |
|
|---|
| 18 | my @index2section = ();
|
|---|
| 19 | my %section2index = ();
|
|---|
| 20 | my %section2path = ();
|
|---|
| 21 |
|
|---|
| 22 | my $datekey = q{};
|
|---|
| 23 | my $fh = FileHandle->new;
|
|---|
| 24 |
|
|---|
| 25 | our (
|
|---|
| 26 | $number, $section, $filename,
|
|---|
| 27 | $title, $body,
|
|---|
| 28 | $is_permalink,
|
|---|
| 29 | $next_path, $prev_path, $next_title, $prev_title
|
|---|
| 30 | );
|
|---|
| 31 |
|
|---|
| 32 | # ------------------------------------ #
|
|---|
| 33 |
|
|---|
| 34 | sub start {
|
|---|
| 35 | if ( $blosxom::path_info_da ne q{} ) {
|
|---|
| 36 | my $path = path_info();
|
|---|
| 37 | my $datepath = join q{/}, ( $blosxom::path_info_yr, $blosxom::path_info_mo, $blosxom::path_info_da );
|
|---|
| 38 | if ( $path =~ m{$datepath/(\d+)(?:\.(.+?))?$} ) {
|
|---|
| 39 | $section = $1;
|
|---|
| 40 | $datekey = join q{-}, ( $blosxom::path_info_yr, $blosxom::path_info_mo_num, $blosxom::path_info_da );
|
|---|
| 41 | $is_permalink = 1;
|
|---|
| 42 | $blosxom::flavour = $2 if ( $2 ne q{} );
|
|---|
| 43 | $blosxom::path_info =~ s{$section\.(.+?)$}{};
|
|---|
| 44 | }
|
|---|
| 45 | }
|
|---|
| 46 | return 1;
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | sub filter {
|
|---|
| 50 | my ( $class, $files, $others ) = @_;
|
|---|
| 51 |
|
|---|
| 52 | my $entries = {};
|
|---|
| 53 | for my $path ( sort { $files->{$a} <=> $files->{$b} } keys %{ $files } ) {
|
|---|
| 54 | my $key = join q{-}, (blosxom::nice_date( $files->{$path} ))[5,2,3];
|
|---|
| 55 | $entries->{$key} = [] if ( ref $entries->{$key} ne 'ARRAY' );
|
|---|
| 56 | push @{ $entries->{$key} }, $path;
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | for my $date ( keys %{ $entries } ) {
|
|---|
| 60 | my $stories = $entries->{$date};
|
|---|
| 61 | for ( my $i = 0; $i < @{ $stories }; $i++ ) {
|
|---|
| 62 | my $path = $stories->[$i];
|
|---|
| 63 | $section{$path} = {
|
|---|
| 64 | date => $date,
|
|---|
| 65 | section => $i + 1,
|
|---|
| 66 | };
|
|---|
| 67 | my $sectionkey = "${date}-" . ($i + 1);
|
|---|
| 68 | push @index2section, $sectionkey;
|
|---|
| 69 | $section2path{$sectionkey} = $path;
|
|---|
| 70 | }
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | @index2section = sort { $b cmp $a } @index2section;
|
|---|
| 74 | %section2index = map { $index2section[$_] => $_ } 0 .. $#index2section;
|
|---|
| 75 |
|
|---|
| 76 | if ( $section ) {
|
|---|
| 77 | ( $filename ) = grep {
|
|---|
| 78 | my $data = $section{$_};
|
|---|
| 79 | $data->{'date'} eq $datekey && $data->{'section'} eq $section
|
|---|
| 80 | } keys %section;
|
|---|
| 81 | my $mtime = $files->{$filename};
|
|---|
| 82 | %{ $files } = ( $filename => $mtime );
|
|---|
| 83 | ( $title, $body ) = get_data( $filename );
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 | return 1;
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | sub head {
|
|---|
| 91 | my ( $class, $path, $head_ref ) = @_;
|
|---|
| 92 |
|
|---|
| 93 | if ( $is_permalink ) {
|
|---|
| 94 | my $key = "${datekey}-${section}";
|
|---|
| 95 | my $index = $section2index{$key};
|
|---|
| 96 |
|
|---|
| 97 | if ( $index < $#index2section ) {
|
|---|
| 98 | $prev_path = $index2section[ $index + 1 ];
|
|---|
| 99 | $prev_path =~ s{[-]}{/}g;
|
|---|
| 100 | $prev_title = ( get_data( $section2path{ $index2section[ $index + 1 ] } ) )[0];
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | if ( $index > 0 ) {
|
|---|
| 104 | $next_path = $index2section[ $index - 1 ];
|
|---|
| 105 | $next_path =~ s{[-]}{/}g;
|
|---|
| 106 | $next_title = ( get_data( $section2path{ $index2section[ $index - 1 ] } ) )[0]
|
|---|
| 107 | }
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | sub story {
|
|---|
| 113 | my ( $class, $path, $fn, $story_ref, $title_ref, $body_ref ) = @_;
|
|---|
| 114 | my $key = "$blosxom::datadir$path/$fn.$blosxom::file_extension";
|
|---|
| 115 | $number = $section{$key}->{'section'};
|
|---|
| 116 | return 1;
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | sub get_data {
|
|---|
| 120 | my ( $file ) = @_;
|
|---|
| 121 |
|
|---|
| 122 | my ( $title, $body );
|
|---|
| 123 |
|
|---|
| 124 | if ( -f $file && $fh->open( $file, '<' ) ) {
|
|---|
| 125 | chomp( $title = <$fh> );
|
|---|
| 126 | $body = do { local $/; <$fh> };
|
|---|
| 127 | $fh->close;
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | return ( $title, $body );
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | 1;
|
|---|