| 1 | package Plagger::Plugin::Widget::HatenaBookmarkUsersCount; | ||
| 2 | use strict; | ||
| 3 | use base qw( Plagger::Plugin Class::Accessor::Fast ); | ||
| 4 | |||
| 5 | sub register { | ||
| 6 | my($self, $context) = @_; | ||
| 7 | |||
| 8 | if ($context->is_loaded('Filter::HatenaBookmarkUsersCount')) { | ||
| 9 | $self->conf->{use_filter} = 1; | ||
| 10 | } | ||
| 11 | |||
| 12 | $context->register_hook( | ||
| 13 | $self, | ||
| 14 | 'publish.entry.fixup' => \&add, | ||
| 15 | ); | ||
| 16 | } | ||
| 17 | |||
| 18 | sub add { | ||
| 19 | my($self, $context, $args) = @_; | ||
| 20 | $args->{entry}->add_widget($self); | ||
| 21 | } | ||
| 22 | |||
| 23 | sub html { | ||
| 24 | my($self, $entry) = @_; | ||
| 25 | |||
| 26 | my $permalink = $entry->permalink; | ||
| 27 | $permalink =~ s/\#/%23/; | ||
| 28 | my $url = HTML::Entities::encode("http://b.hatena.ne.jp/entry/$permalink"); | ||
| 29 | |||
| 30 | if ($self->conf->{use_filter}) { | ||
| 31 | my $user = $entry->meta->{hatenabookmark_users}; | ||
| 32 | my $users = $user >= 2 ? "$user users" : "$user user"; | ||
| 33 | |||
| 34 | return | ||
| 35 | $user >= 10 ? qq(<strong style="background-color: #ffcccc; font-weight: bold; font-style: normal; display: inline;"><a href="$url" style="color: red;">$users</a></strong>) | ||
| 36 | : $user >= 2 ? qq(<em style="background-color: #fff0f0; font-weight: bold; display: inline; font-style: normal;"><a href="$url" style="color: #ff6666;">$users</a></em>) | ||
| 37 | : qq(<a href="$url">$users</a>); | ||
| 38 | } else { | ||
| 39 | my $size = $self->conf->{image_size} || 'normal'; | ||
| 40 | my $img_url = HTML::Entities::encode("http://b.hatena.ne.jp/entry/image/$size/$permalink"); | ||
| 41 | return qq(<a href="$url"><img src="$img_url" style="border:0;vertical-align:middle" /></a>); | ||
| 42 | } | ||
| 43 | } | ||
| 44 | |||
| 45 | 1; | ||
| 46 | |||
| 47 | __END__ |