otsune's SnakeOil RSSフィード

python -c "print''.join([chr(x) for x in 111&1101,110+~-~11,10^11*11,110+~-~11,-~101,-~11^11*11,~-110,111&11*11,11+11+10,11|~-0110,1-~11^11*11,10^11*11,-~11^11*11,110,101])"

2007-06-20

Filter::AddAuthorToTitleを書いた

| Filter::AddAuthorToTitleを書いた - otsune's SnakeOil  を含むブックマーク はてなブックマーク - Filter::AddAuthorToTitleを書いた - otsune's SnakeOil  Filter::AddAuthorToTitleを書いた - otsune's SnakeOil  のブックマークコメント

主にCustomFeed::Mixiでマイミク最新日記にauthor名を追加するため。

なんかexpression hackで出来んじゃネーの。という気もするが。

追記:

hookの「update.feed.fixup」で

for my $entry ($args->{feed}->entries) {
    #...
}

するのと

「update.entry.fixup」で

my $entry = $args->{entry};
#...

するのはどっちでも良いんだろうか?

lib/Plagger/Plugin/Filter/AddAuthorToTitle.pm

package Plagger::Plugin::Filter::AddAuthorToTitle;
use strict;
use base qw( Plagger::Plugin );

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

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

    my $entry  = $args->{entry};
    my $add_to = $self->conf->{add_to} || 'left';

    my $author = $entry->author;
    my $title  = $entry->title;
    if ($author) {
        $title = "$title - $author" if $add_to eq 'right';
        $title = "$author - $title" if $add_to eq 'left';

        $entry->title($title);
        $self->log(debug => "Add author to entry title: $title");
    }
}

1;

__END__

=head1 NAME

Plagger::Plugin::Filter::AddAuthorToTitle - Add author to entry titles

=head1 SYNOPSIS

  - module: Filter::AddAuthorToTitle

=head1 DESCRIPTION

This plugin uses entry author to be appended to title. 

=head1 CONFIG

=over 4

=item add_to

Specify 'left' or 'right' of the title. Defaults to 'left'.

=back

=head1 AUTHOR

Masafumi Otsune

=head1 SEE ALSO

L<Plagger>

=cut

t/samples/add-author-title.xml

<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>foo</title>
<item>
<title>Plagger rocks</title>
<link>http://plagger.org/</link>
<author>miyagawa</author>
</item>
<item>
<title>Boofy rocks</title>
<link>http://mixxi.jp/</link>
</item>
<item>
<title>Otsune</title>
<link>http://www.otsune.com/</link>
<author>otsune</author>
</item>
</channel>
</rss>

t/plugins/Filter-AddAuthorToTitle/filter.t

use strict;
use t::TestPlagger;

plan tests => 8;
run_eval_expected;

__END__
=== Test without AddAuthorTitle
--- input config
plugins:
  - module: Subscription::Config
    config:
      feed:
        - file://$t::TestPlagger::BaseDirURI/t/samples/add-author-title.xml
--- expected
is $context->update->feeds->[0]->entries->[0]->title, 'Plagger rocks';

=== Test with AddAuthorTitle (default)
--- input config
plugins:
  - module: Subscription::Config
    config:
      feed:
        - file://$t::TestPlagger::BaseDirURI/t/samples/add-author-title.xml
  - module: Filter::AddAuthorToTitle
--- expected
is $context->update->feeds->[0]->entries->[0]->title, 'miyagawa - Plagger rocks';

=== Test with AddAuthorToTitle (add to explicitly left)
--- input config
plugins:
  - module: Subscription::Config
    config:
      feed:
        - file://$t::TestPlagger::BaseDirURI/t/samples/add-author-title.xml
  - module: Filter::AddAuthorToTitle
    config:
      add_to: left
--- expected
is $context->update->feeds->[0]->entries->[0]->title, 'miyagawa - Plagger rocks';

=== Test with AddAuthorToTitle (add to right)
--- input config
plugins:
  - module: Subscription::Config
    config:
      feed:
        - file://$t::TestPlagger::BaseDirURI/t/samples/add-author-title.xml
  - module: Filter::AddAuthorToTitle
    config:
      add_to: right
--- expected
is $context->update->feeds->[0]->entries->[0]->title, 'Plagger rocks - miyagawa';
is $context->update->feeds->[0]->entries->[1]->title, 'Boofy rocks';
is $context->update->feeds->[0]->entries->[2]->title, 'Otsune - otsune';

=== Test with AddAuthorTitle (without author)
--- input config
plugins:
  - module: Subscription::Config
    config:
      feed:
        - file://$t::TestPlagger::BaseDirURI/t/samples/add-author-title.xml
  - module: Filter::AddAuthorToTitle
--- expected
is $context->update->feeds->[0]->entries->[1]->title, 'Boofy rocks';

=== Test with AddAuthorTitle (third entry)
--- input config
plugins:
  - module: Subscription::Config
    config:
      feed:
        - file://$t::TestPlagger::BaseDirURI/t/samples/add-author-title.xml
  - module: Filter::AddAuthorToTitle
--- expected
is $context->update->feeds->[0]->entries->[2]->title, 'otsune - Otsune';
トラックバック - http://subtech.g.hatena.ne.jp/otsune/20070620