本を読む

読書やコンピュータなどに関するメモ

P::P::Publish::TagCloud

Plagger::Plugin::Publish::TagCloudというアイデアを見たので野良試作。HTML::TagCloudにデータを渡してるだけだけど。

MYCOMジャーナルをFilter::Summarize::Japaneseでタグづけして、Publish::TagCloudにかけるとこんな感じ。

Publish::TagCloudサンプル


設定はこんな感じ。

mycomjournaltag.yaml
global:
  log:
    level: debug

plugins:
  - module: Subscription::Config
    config:
      feed:
        - http://journal.mycom.co.jp/haishin/rss/index.rdf

  - module: Filter::EntryFullText

  - module: Filter::Regexp
    config:
      regexp: s/\[IMAGE\]//g

  - module: Filter::Summarize::Japanese

  - module: Publish::TagCloud
    config:
      dir: /tmp/mycomjournal
      filename: tagcloud.html
      maxtags: 30


Publish::TagCloudはとりあえずこんな感じ
(2006.11.12追記:HTML::TagCloudのアップデートに併せて更新)。

Publish::TagCloud
package Plagger::Plugin::Publish::TagCloud;
use strict;
use warnings;
use base qw ( Plagger::Plugin );

our $VERSION = 0.01;

use Encode;
use HTML::TagCloud;
use File::Spec;

sub register {
    my ($self, $context) = @_;
    $context->register_hook(
        $self,
        'publish.feed' => \&feed,
    );
}

sub feed {
    my($self, $context, $args) = @_;

    my $conf = $self->conf;
    my $f = $args->{feed};

    my %tagcount;
    for my $e ($f->entries) {
        for my $t (@{$e->tags}){
            $tagcount{$t}++;
        }
    }

    my $cloud = HTML::TagCloud->new;
    while ((my $k, my $c) =  each %tagcount) {
        $cloud->add($k, '', $c);
    }

    my $file = Plagger::Util::filename_for($f, $self->conf->{filename} || "%i.html");
    my $filepath = File::Spec->catfile($self->conf->{dir}, $file);
    $context->log(info => "save TagCloud for " . $f->link . " to $filepath");

    open my $output, ">:utf8", $filepath or $context->error("$filepath: $!");
    print $output cloud_html_and_css($cloud, $self->conf->{maxtags} || undef);
    close $output;
}

sub cloud_html_and_css{
    (my $cloud, my $limit) = @_;

    my $html = $cloud->html_and_css($limit);

    $html =~ /line-height: (\d+)px/;
    my $size = $1;

    while($html =~ /class="tagcloud(\d+)"/g){
        my $sz = $1 + 12;
        $size = $sz if $size < $sz;
    }

    $html =~ s/line-height: (\d+)px/line-height: ${size}px/;

    $html;
}

1;

__END__

=head1

Plagger::Plugin::Publish::TagCloud - Publish feeds as TagCloud

=head1 SYNOPSIS

  - module: Publish::TagCloud
    config:
      dir: /tmp/tag
      filename: output.html
      maxtags: 10

=head1 CONFIG

=head2 dir

Directory to save csv files in.

=head2 filename

Filename to be used to create csv files. It defaults to C<%i.html>. It
supports the following format like printf():

=head2 maxtags

Numbers of tags to output.

=head1 AUTHOR

Masakazu Takahashi

=head1 SEE ALSO

L<Plagger>, L<HTML::TagCloud>

=cut

コメント

コメントの投稿

管理者にだけ表示を許可する

トラックバック

http://emasaka.blog65.fc2.com/tb.php/149-18184b10

 | HOME | 

Categories

Recent Entries

Recent Comments

Recent Trackbacks

Appendix

emasaka

emasaka

フリーター。
連絡先はこのへん

Monthly


FC2Ad