Plagger/Plugin/Widget/Simple.pm untested

filter: Statementstmt 0.0% 0 / 66 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 110 CC 10 SCAR 25.6
CC
10
Cov
0%
html
30.0
value
20.0
initialize
12.0
CRAP 13.0
1 package Plagger::Plugin::Widget::Simple;
2 use strict;
3 use base qw( Plagger::Plugin );
4  
5 use Encode;
6 use HTML::Entities;
7 use URI;
8  
9 sub register {
10 my($self, $context) = @_;
11 $context->register_hook(
12 $self,
13 'publish.entry.fixup' => \&add,
14 'plugin.init' => \&initialize,
15 );
16 }
17  
18 sub initialize {
19 my($self, $context, $args) = @_;
20  
21 if (my $name = $self->conf->{widget}) {
22 my $found;
23 $self->load_assets(
24 "$name.yaml",
25 sub {
26 my $data = YAML::LoadFile(shift);
27 $self->{conf} = { %{$self->{conf}}, %$data };
28 $found++;
29 },
30 );
31  
32 unless ($found) {
33 $context->log(error => "Can't find widget for $name");
34 }
35 }
36 }
37  
38 sub add {
39 my($self, $context, $args) = @_;
40  
41 my $widget = Plagger::Plugin::Widget::Simple::Widget->new;
42 $widget->{feed} = $args->{entry}->source || $args->{feed};
43 $widget->{plugin} = $self;
44  
45 $args->{entry}->add_widget($widget);
46 }
47  
48 package Plagger::Plugin::Widget::Simple::Widget;
49  
50 sub new { bless {}, shift }
51  
52 sub feed { shift->{feed} }
53 sub plugin { shift->{plugin} }
54  
55 sub value {
56 my($self, $string, $args) = @_;
57  
58 if ($string =~ /\$/) { # DWIM
59 $string = eval $string;
60 Plagger->context->log(error => $@) if $@;
61  
62 $string = "$string"; # stringify ::Content
63 utf8::encode($string) if utf8::is_utf8($string);
64 }
65  
66 $string;
67 }
68  
69 sub html {
70 my($self, $entry) = @_;
71 my $uri = URI->new($self->plugin->conf->{link});
72  
73 my $args = { entry => $entry, feed => $self->{feed} };
74  
75 if (my $append = $self->plugin->conf->{append}) {
76 $uri->path( $uri->path . $self->value($append, $args) );
77 }
78  
79 if (my $query = $self->plugin->conf->{query}) {
80 if (ref $query) {
81 my @query = map {
82 ($_ => $self->value($query->{$_}, $args));
83 } sort keys %$query;
84 $uri->query_form(@query);
85 } else {
86 $query = $self->value($query, $args);
87 $uri->query($query);
88 }
89 }
90  
91 my $url = HTML::Entities::encode($uri->as_string);
92  
93 my $content;
94 if (my $template = $self->plugin->conf->{content_dynamic}) {
95 $content = $self->plugin->templatize(\$template, $args);
96 } else {
97 $content = $self->plugin->conf->{content};
98 }
99  
100 return qq(<a href="$url">$content</a>);
101 }
102  
103 1;
104 __END__