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