# Blosxom Plugin: (yet another) section # Original Author: cho45 # Author: KITAMURA Akatsuki # Version: 2006-09-25 # Blosxom Home/Docs/Licensing: http://www.blosxom.com/ # This script is encoded in UTF-8. package section; use strict; use CGI qw( path_info ); # --- Configurable variables ----------- # headテンプレート内の $section::title を # 一時的に置き換えるための文字列 my $title_placeholder = '{{{title}}}'; # --- Plug-in package variables -------- # テンプレート用変数 # (blosxom.cgiの書き方に合わせて our でなく use vars) use vars qw( $se $title ); # プラグイン内部変数 # (プラグイン外から参照されないようmyで宣言) my (%section_table, $file_specified); # -------------------------------------- sub start { # 静的生成の場合は動作させない return 0 if ($blosxom::static_or_dynamic eq 'static'); 1; } sub filter { # 引数を取得 my $files_ref = $_[1]; # path_info() から 年・月・日・記事番号・拡張子を取得 # (マッチしない場合、各変数は undef に設定される) my ($path_yr, $path_mo_num, $path_da, $path_se, $path_flavour) = ( path_info() =~ m{^/(\d+)/(\d{2})/(\d{2})/(\d+)(?:\.(\w+))?$} ); # $path_se が値を持ち、かつ0以上なら日付($path_day)を設定 my $path_day = ($path_se && 0 + $path_se) ? "$path_yr-$path_mo_num-$path_da" : ''; # path_infoに拡張子がついていたらそれをフレーバーとして設定 if ($path_flavour) { $blosxom::flavour = $path_flavour; $blosxom::path_info = ''; } # %$files_ref のキー(ファイル名)を更新時刻の昇順に揃えておく # 更新時刻が同じ場合はファイル名順 my @files_date_order = sort { $files_ref->{$a} <=> $files_ref->{$b} || $a cmp $b } keys %$files_ref; # %day2files = ( $day1 => [ $file1, $file2, ... ], ... ) # というデータ構造を作成 my %day2files; foreach my $file (@files_date_order) { # Unix timeから日付(yyyy-mm-dd)を出す my $day = &unixtime2day( $files_ref->{$file} ); # ファイルを追加 push @{ $day2files{$day} }, $file; } # %day2files から # %section_table = ( $file1 => $se1, $file2 => $se2, ... ) # を作成 %section_table = (); foreach my $files_in_day_ref (values %day2files) { my $index = 1; foreach my $file (@$files_in_day_ref) { $section_table{$file} = $index++; # 代入後に+1 } } # $path_day の値が無ければここで復帰 return 1 if (!$path_day); # $path_day と $path_se で特定されるファイルのみを %$files_ref に残す $file_specified = $day2files{$path_day}[$path_se - 1]; %$files_ref = ($file_specified) ? ( $file_specified => $files_ref->{$file_specified} ) : (); 1; } sub head { # $section::title に置き換える文字列 $title = ($file_specified) ? $title_placeholder : ''; 1; } sub story { # 引数を取得 my ($path, $fn, $title_ref) = @_[1, 2, 4]; # 引数より記事ファイル名を特定 my $file_path = "$blosxom::datadir$path/$fn.$blosxom::file_extension"; # ファイル名からsection番号を取得 $se = $section_table{$file_path}; # $file_specified が設定されていれば出力にtitleを追加 # (headテンプレートに記事タイトルを追加するための処理) if ($file_specified) { $blosxom::output =~ s/$title_placeholder/$$title_ref/; } 1; } # Unix timeから日付(yyyy-mm-dd)を出す sub unixtime2day { my $unixtime = $_[0]; # blosxom.cgiのnice_dateサブルーチンを利用 my ($mo_num, $da, $yr) = ( blosxom::nice_date($unixtime) )[2, 3, 5]; return "$yr-$mo_num-$da"; } 1; __END__ =encoding utf8 =head1 NAME Blosxom Plugin: (yet another) section =head1 SYNOPSIS blosxom.cgi/yyyy/mm/dd/s.flav のようなパスで 個別記事表示を行うためのプラグインです。 yyyy: 西暦年 mm: 月(01 .. 12) dd: 日(01 .. 31) s: 1日の内の記事の順番を示す番号(1, 2, ...) .flav: フレーバー拡張子 拡張子(.flav)はオプションです。 付けなかった場合は $blosxom::default_flavour が使われます。 =head1 DESCRIPTION 以下のテンプレート用変数が使えます。 =over =item $section::se 1日の内の該当記事の番号を表します。storyテンプレートで使用。 =item $section::title /yyyy/mm/dd/s.flav のようなパスで個別記事表示する場合に、 headテンプレートに記事タイトルを入れるために使用します。 =back =head1 VERSION 2006-09-25 =head1 AUTHOR Original section plugin author: cho45 , http://lowreal.net/ This plugin author: 北村曉 (KITAMURA Akatsuki) , http://www.akatsukinishisu.net/ =head1 SEE ALSO blosxom section plugin http://lowreal.net/logs/2006/09/10/1 =head1 LICENSE section Blosxom plugin Copyright 2006 cho45, KITAMURA Akatsuki This plugin is licensed under Creative-Commons by 2.1 jp. See the below URL: http://creativecommons.org/licenses/by/2.1/jp/