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