| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Acme::Text::Shorten::ForTwitter; |
|
2
|
|
|
|
|
|
|
# ABSTRACT: Shorten text for use in tweets |
|
3
|
|
|
|
|
|
|
|
|
4
|
1
|
|
|
1
|
|
26378
|
use Moo; |
|
|
1
|
|
|
|
|
14961
|
|
|
|
1
|
|
|
|
|
5
|
|
|
5
|
1
|
|
|
1
|
|
2421
|
use Module::Pluggable require => 1; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
my (%base_rules, %default_rules); |
|
8
|
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
for my $p (__PACKAGE__->plugins()) { |
|
10
|
|
|
|
|
|
|
die "Plugin $p does not implement modify_base_rules method!\n" unless $p->can('modify_base_rules'); |
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
$p->modify_base_rules(\%base_rules); |
|
13
|
|
|
|
|
|
|
} |
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub import { |
|
16
|
|
|
|
|
|
|
my ($pkg, @args) = @_; |
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
%default_rules = (); |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
unshift @args, 'all' unless grep { $_ =~ /(^\+|^all$)/ } @args; |
|
21
|
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
for my $arg (@args) { |
|
23
|
|
|
|
|
|
|
if ($arg eq 'all') { |
|
24
|
|
|
|
|
|
|
%default_rules = %base_rules; |
|
25
|
|
|
|
|
|
|
} elsif ($arg =~ s/^-//) { |
|
26
|
|
|
|
|
|
|
die "Unknown rule $arg\n" unless $base_rules{$arg}; |
|
27
|
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
delete $default_rules{$arg}; |
|
29
|
|
|
|
|
|
|
} elsif ($arg =~ s/^\+//) { |
|
30
|
|
|
|
|
|
|
die "Unknown rule $arg\n" unless $base_rules{$arg}; |
|
31
|
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
$default_rules{$arg} = $base_rules{$arg}; |
|
33
|
|
|
|
|
|
|
} else { |
|
34
|
|
|
|
|
|
|
die "Unknown option $arg\n"; |
|
35
|
|
|
|
|
|
|
} |
|
36
|
|
|
|
|
|
|
} |
|
37
|
|
|
|
|
|
|
} |
|
38
|
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
has rules => ( |
|
40
|
|
|
|
|
|
|
is => 'lazy', |
|
41
|
|
|
|
|
|
|
isa => sub { |
|
42
|
|
|
|
|
|
|
die "$_[0] must be a hashref\n" unless ref $_[0] && ref $_[0] eq 'HASH'; |
|
43
|
|
|
|
|
|
|
}, |
|
44
|
|
|
|
|
|
|
default => sub { |
|
45
|
|
|
|
|
|
|
return { %default_rules } |
|
46
|
|
|
|
|
|
|
} |
|
47
|
|
|
|
|
|
|
); |
|
48
|
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub add_rule { |
|
50
|
|
|
|
|
|
|
my ($self, $name, $rule) = @_; |
|
51
|
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
die "rule $name already exits\n" if $self->rules->{$name}; |
|
53
|
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
unless ($rule) { |
|
55
|
|
|
|
|
|
|
die "No default rule named $name\n" unless $default_rules{$name}; |
|
56
|
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
$self->rules->{$name} = $default_rules{$name}; |
|
58
|
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
return; |
|
60
|
|
|
|
|
|
|
} |
|
61
|
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
die "rule must be a subroutine\n" unless ref $rule && ref $rule eq 'CODE'; |
|
63
|
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
$self->rules->{$name} = $rule; |
|
65
|
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
return; |
|
67
|
|
|
|
|
|
|
} |
|
68
|
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub remove_rule { |
|
70
|
|
|
|
|
|
|
my ($self, $name) = @_; |
|
71
|
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
die "rule $name does not exist\n" unless $self->rules->{$name}; |
|
73
|
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
delete $self->rules->{$name}; |
|
75
|
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
return; |
|
77
|
|
|
|
|
|
|
} |
|
78
|
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub shorten { |
|
80
|
|
|
|
|
|
|
my $self = shift; |
|
81
|
|
|
|
|
|
|
my $text = shift; |
|
82
|
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
for my $rule (values %{ $self->rules }){ |
|
84
|
|
|
|
|
|
|
$rule->(\$text); |
|
85
|
|
|
|
|
|
|
} |
|
86
|
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
return $text; |
|
88
|
|
|
|
|
|
|
} |
|
89
|
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
1; |
|
91
|
|
|
|
|
|
|
__END__ |