line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::MoreUtilHelpers; |
2
|
|
|
|
|
|
|
|
3
|
7
|
|
|
7
|
|
4739
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
7
|
|
|
|
|
10
|
|
|
7
|
|
|
|
|
42
|
|
4
|
7
|
|
|
7
|
|
1184
|
use Mojo::Collection; |
|
7
|
|
|
|
|
11
|
|
|
7
|
|
|
|
|
254
|
|
5
|
7
|
|
|
7
|
|
35
|
use Mojo::DOM; |
|
7
|
|
|
|
|
12
|
|
|
7
|
|
|
|
|
147
|
|
6
|
7
|
|
|
7
|
|
25
|
use Mojo::Util; |
|
7
|
|
|
|
|
8
|
|
|
7
|
|
|
|
|
212
|
|
7
|
|
|
|
|
|
|
|
8
|
7
|
|
|
7
|
|
4693
|
use Lingua::EN::Inflect; |
|
7
|
|
|
|
|
99113
|
|
|
7
|
|
|
|
|
6565
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $VERSION = '0.04'; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub register { |
13
|
7
|
|
|
7
|
1
|
327
|
my ($self, $app, $defaults) = @_; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
$app->helper(count => sub { |
16
|
5
|
|
|
5
|
|
61788
|
my ($c, $item, $type) = @_; |
17
|
5
|
|
|
|
|
16
|
my $count = $item; |
18
|
5
|
50
|
|
|
|
25
|
return unless defined $item; |
19
|
|
|
|
|
|
|
|
20
|
5
|
|
|
|
|
36
|
my $tr = sub { lc( (split /::/, ref(shift))[-1] ) }; |
|
2
|
|
|
|
|
18
|
|
21
|
|
|
|
|
|
|
|
22
|
5
|
50
|
|
|
|
35
|
if(ref($item) eq 'ARRAY') { |
23
|
5
|
|
|
|
|
12
|
$count = @$item; |
24
|
5
|
100
|
|
|
|
23
|
$type = $tr->($item->[0]) unless $type; |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
5
|
|
33
|
|
|
18
|
$type ||= $tr->($item); |
28
|
5
|
|
|
|
|
40
|
return "$count " . Lingua::EN::Inflect::PL($type, $count); |
29
|
7
|
|
|
|
|
73
|
}); |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
$app->helper(paragraphs => sub { |
32
|
5
|
|
|
5
|
|
26075
|
my ($c, $text) = @_; |
33
|
5
|
50
|
|
|
|
11
|
return unless $text; |
34
|
|
|
|
|
|
|
|
35
|
5
|
|
|
|
|
37
|
my $html = join '', map $c->tag('p', $_), split /^\s*\015?\012/m, $text; |
36
|
5
|
|
|
|
|
577
|
return $c->b($html); |
37
|
7
|
|
|
|
|
258
|
}); |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
|
40
|
7
|
|
|
|
|
142
|
my $maxwords = $defaults->{maxwords}; |
41
|
|
|
|
|
|
|
$app->helper(maxwords => sub { |
42
|
7
|
|
|
7
|
|
44574
|
my $c = shift; |
43
|
7
|
|
|
|
|
17
|
my $text = shift; |
44
|
7
|
|
66
|
|
|
23
|
my $n = shift // $maxwords->{max}; |
45
|
|
|
|
|
|
|
|
46
|
7
|
100
|
66
|
|
|
64
|
return $text unless $text and $n and $n > 0; |
|
|
|
100
|
|
|
|
|
47
|
|
|
|
|
|
|
|
48
|
5
|
|
100
|
|
|
24
|
my $omited = shift // $maxwords->{omit} // '...'; |
|
|
|
100
|
|
|
|
|
49
|
5
|
|
|
|
|
15
|
my @words = split /\s+/, $text; |
50
|
5
|
100
|
|
|
|
16
|
return $text unless @words > $n; |
51
|
|
|
|
|
|
|
|
52
|
4
|
|
|
|
|
15
|
$text = join ' ', @words[0..$n-1]; |
53
|
|
|
|
|
|
|
|
54
|
4
|
50
|
|
|
|
9
|
if(@words > $n) { |
55
|
4
|
|
|
|
|
14
|
$text =~ s/[[:punct:]]$//; |
56
|
4
|
|
|
|
|
5
|
$text .= $omited; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
4
|
|
|
|
|
21
|
return $text; |
60
|
7
|
|
|
|
|
27
|
}); |
61
|
|
|
|
|
|
|
|
62
|
7
|
|
|
|
|
85
|
my $sanitize = $defaults->{sanitize}; |
63
|
|
|
|
|
|
|
$app->helper(sanitize => sub { |
64
|
5
|
|
|
5
|
|
31548
|
my $c = shift; |
65
|
5
|
|
|
|
|
16
|
my $html = shift; |
66
|
5
|
50
|
|
|
|
17
|
return unless $html; |
67
|
|
|
|
|
|
|
|
68
|
5
|
|
|
|
|
12
|
my %options = @_; |
69
|
|
|
|
|
|
|
|
70
|
5
|
|
|
|
|
5
|
my (%tags, %attr); |
71
|
5
|
|
100
|
|
|
22
|
my $names = $options{tags} // $sanitize->{tags}; |
72
|
5
|
100
|
|
|
|
28
|
@tags{@$names} = (1) x @$names if ref $names eq 'ARRAY'; |
73
|
|
|
|
|
|
|
|
74
|
5
|
|
100
|
|
|
20
|
$names = $options{attr} // $sanitize->{attr}; |
75
|
5
|
100
|
|
|
|
16
|
@attr{@$names} = (1) x @$names if ref $names eq 'ARRAY'; |
76
|
|
|
|
|
|
|
|
77
|
5
|
|
|
|
|
27
|
my $doc = Mojo::DOM->new($html); |
78
|
5
|
100
|
|
|
|
2180
|
return $doc->all_text unless %tags; |
79
|
|
|
|
|
|
|
|
80
|
4
|
|
|
|
|
5
|
for my $node (@{$doc->descendant_nodes}) { |
|
4
|
|
|
|
|
14
|
|
81
|
44
|
100
|
100
|
|
|
1171
|
if($node->tag && !$tags{ $node->tag }) { |
82
|
10
|
|
|
|
|
183
|
$node->strip; |
83
|
10
|
|
|
|
|
854
|
next; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
34
|
100
|
|
|
|
354
|
if(%attr) { |
87
|
21
|
|
|
|
|
16
|
for my $name (keys %{$node->attr}) { |
|
21
|
|
|
|
|
32
|
|
88
|
4
|
100
|
|
|
|
68
|
delete $node->attr->{$name} unless $attr{$name}; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
4
|
|
|
|
|
76
|
return $c->b($doc->to_string); |
94
|
7
|
|
|
|
|
26
|
}); |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
$app->helper(trim_param => sub { |
97
|
7
|
|
|
7
|
|
48455
|
my $c = shift; |
98
|
7
|
50
|
|
|
|
29
|
return unless @_; |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
my $trim = sub { |
101
|
10
|
|
|
|
|
16
|
my $name = shift; |
102
|
10
|
|
|
|
|
33
|
my $vals = $c->every_param($name); |
103
|
|
|
|
|
|
|
|
104
|
10
|
100
|
|
|
|
665
|
$c->param($name => @$vals == 1 ? |
105
|
|
|
|
|
|
|
Mojo::Util::trim($vals->[0]) : |
106
|
|
|
|
|
|
|
[ map Mojo::Util::trim($_), @$vals ]); |
107
|
7
|
|
|
|
|
38
|
}; |
108
|
|
|
|
|
|
|
|
109
|
7
|
|
|
|
|
11
|
my %params; |
110
|
7
|
|
|
|
|
24
|
my $names = $c->req->params->names; |
111
|
7
|
|
|
|
|
1451
|
@params{ @$names } = (1) x @$names; |
112
|
|
|
|
|
|
|
|
113
|
7
|
|
|
|
|
20
|
for my $name (@_) { |
114
|
10
|
100
|
|
|
|
34
|
if(ref($name) ne 'Regexp') { |
115
|
6
|
|
|
|
|
12
|
$trim->($name); |
116
|
6
|
|
|
|
|
164
|
next; |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
4
|
|
|
|
|
9
|
for(keys %params) { |
120
|
7
|
100
|
|
|
|
45
|
next unless $_ =~ $name; |
121
|
|
|
|
|
|
|
|
122
|
4
|
|
|
|
|
11
|
$trim->($_); |
123
|
4
|
|
|
|
|
117
|
delete $params{$_}; |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
} |
126
|
7
|
|
|
|
|
101
|
}); |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
$app->helper(collection => sub { |
129
|
3
|
|
|
3
|
|
18549
|
my $c = shift; |
130
|
3
|
100
|
100
|
|
|
22
|
my @data = ( @_ == 1 && ref($_[0]) eq 'ARRAY' ? @{$_[0]} : @_ ); |
|
1
|
|
|
|
|
3
|
|
131
|
3
|
100
|
66
|
|
|
21
|
return Mojo::Collection->new(@data == 1 && !defined $data[0] ? () : @data ); |
132
|
7
|
|
|
|
|
98
|
}); |
133
|
|
|
|
|
|
|
|
134
|
7
|
100
|
|
|
|
112
|
if($defaults->{collection}->{patch}) { |
135
|
1
|
|
|
0
|
|
2
|
$app->helper(c => sub { shift->collection(@_) }); |
|
0
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
1; |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
__END__ |