# ModBlosxom Plugin: Tagging # Author: Taro FUNAKI # Version: 2005-11-04 # Blosxom Home/Docs/Licensing: http://www.blosxom.com/ package ModBlosxom::plugin::Tagging; use strict; use warnings; use Cache::FileCache; sub new { bless {}, shift; } sub start { my($self, $blosxom) = @_; my($currentdir, $state_dir) = $blosxom->settings([ qw(currentdir plugin_state_dir) ]); my $cache = Cache::FileCache->new({ namespace => 'Tagging', cache_root => $state_dir, }); if ($currentdir eq 'json') { if (! ($self->{tags} = $cache->get('tags'))) { $currentdir = ''; $self->{cache} = $cache; } $blosxom->settings( currentdir => $currentdir, num_entries => 65535, flavour => 'json', ); $blosxom->{templates}->{'json'} = { 'content_type' => 'text/plain', 'head' => 'var tags = ', 'date' => ' ', 'story' => ' ', 'foot' => '$tagging::json' }; } elsif ($currentdir =~ /^tag\/(.+)$/) { my $tags_to_files = $cache->get('tags_to_files'); $self->{files} = $tags_to_files->{$1} || []; $blosxom->settings( currentdir => '', ); } 1; } sub filter { my($self, $blosxom, $files_ref, $others_ref) = @_; if ($self->{files}) { my $files_ref_tmp = {}; foreach (@{$self->{files}}) { if($files_ref->{$_}){ $files_ref_tmp->{$_} = $files_ref->{$_}; } } %$files_ref = %$files_ref_tmp; } 1; } sub story { my($self, $blosxom, $path, $fn, $story_ref, $title_ref, $body_ref) = @_; my($datadir, $ext, $url) = $blosxom->settings([ qw(datadir file_extension url) ]); if ($ModBlosxom::plugin::Meta::tags) { my @tags = split(/\s+/, $ModBlosxom::plugin::Meta::tags); ## asign a file to tags map { push @{$self->{tags_to_files}->{$_}}, "$datadir$path/$fn.$ext" } @tags; ## count up tags in the list map { $self->{tags}->{$_}++; } @tags; my $tags_in_this = ''; foreach (sort @tags) { $tags_in_this .= "$_ "; } $blosxom->param( 'tagging::tags_in_this' => $tags_in_this, ); } 1; } sub foot { my($self, $blosxom, $path, $foot_ref) = @_; my $tags = $self->{tags}; my $json = '{'; $json .= join(',', map { "\"$_\":" . $tags->{$_} } sort(keys(%$tags))); $json .= '}'; $blosxom->param( 'tagging::json' => $json, ); 1; } sub end { my($self, $blosxom) = @_; if ($self->{cache}) { my $expires_in = $blosxom->settings('tags_expires_in') || 3600; $self->{cache}->set('tags', $self->{tags}, $expires_in); $self->{cache}->set('tags_to_files', $self->{tags_to_files}); } 1; } 1; __END__ =head1 NAME Blosxom Plug-in: Tagging