| 1 | package Mojolicious::Plugin::MoreUtilHelpers; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 7 | ▶ | use Mojo::Base 'Mojolicious::Plugin'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 7 | ▶ | use Mojo::Collection; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 7 | ▶ | use Mojo::DOM; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 7 | ▶ | use Mojo::Util; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 7 | ▶ | use Lingua::EN::Inflect; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 7 | our $VERSION = '0.06'; | |||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | 7 | ▶ | sub register { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
register: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 7 | my ($self, $app, $defaults) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | 5 | ▶ | $app->helper(count => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.6
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 5 | my ($c, $item, $type) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 17 | 5 | my $count = $item; | |||||||||||||||||||||||||||||||||||||||||
| 18 | 5 | ▶ | return unless defined $item; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | 5 | ▶ | my $tr = sub { lc( (split /::/, ref(shift))[-1] ) }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | 5 | ▶ | if(ref($item) eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | 5 | $count = @$item; | |||||||||||||||||||||||||||||||||||||||||
| 24 | 5 | ▶ | $type = $tr->($item->[0]) unless $type; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | 5 | ▶ | $type ||= $tr->($item); | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$type ||= &$tr($item)
MC/DC0%0/2
$type ||= &$tr($item)
$type
&$tr($item)
Truth table33%1/3
A ||= B
A= $typeB= &$tr($item)
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | 5 | return "$count " . Lingua::EN::Inflect::PL($type, $count); | |||||||||||||||||||||||||||||||||||||||||
| 29 | 7 | }); | |||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | 5 | ▶ | $app->helper(paragraphs => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 5 | my ($c, $text) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 33 | 5 | ▶ | return unless $text; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | 5 | my $html = join '', map $c->tag('p', $_), split /^\s*\015?\012/m, $text; | |||||||||||||||||||||||||||||||||||||||||
| 36 | 5 | return $c->b($html); | |||||||||||||||||||||||||||||||||||||||||
| 37 | 7 | }); | |||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | 7 | my $maxwords = $defaults->{maxwords}; | |||||||||||||||||||||||||||||||||||||||||
| 41 | 7 | ▶ | $app->helper(maxwords => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | 7 | my $c = shift; | |||||||||||||||||||||||||||||||||||||||||
| 43 | 7 | my $text = shift; | |||||||||||||||||||||||||||||||||||||||||
| 44 | 7 | ▶ | my $n = shift // $maxwords->{max}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
shift() // $maxwords->{"max"}
MC/DC0%0/2
shift() // $maxwords->{"max"}
shift()
$maxwords->{"max"}
Truth table66%2/3
A // B
A= shift()B= $maxwords->{"max"}
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | 7 | ▶ | return $text unless $text and $n and $n > 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$text and $n and $n > 0
$text and $n
MC/DC66%2/3
$text and $n and $n > 0
$text
$n
$n > 0
Truth table75%3/4
A and B and C
A= $textB= $nC= $n > 0
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | 5 | ▶ | my $omited = shift // $maxwords->{omit} // '...'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%5/5
(shift() // $maxwords->{"omit"}) // "..."
shift() // $maxwords->{"omit"}
MC/DC100%2/2
(shift() // $maxwords->{"omit"}) // "..."
shift()
$maxwords->{"omit"}
Truth table100%3/3
A // B
A= shift()B= $maxwords->{"omit"}
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 5 | my @words = split /\s+/, $text; | |||||||||||||||||||||||||||||||||||||||||
| 50 | 5 | ▶ | return $text unless @words > $n; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | 4 | $text = join ' ', @words[0..$n-1]; | |||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | 4 | ▶ | if(@words > $n) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 4 | $text =~ s/[[:punct:]]$//; | |||||||||||||||||||||||||||||||||||||||||
| 56 | 4 | $text .= $omited; | |||||||||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | 4 | return $text; | |||||||||||||||||||||||||||||||||||||||||
| 60 | 7 | }); | |||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | 7 | my $sanitize = $defaults->{sanitize}; | |||||||||||||||||||||||||||||||||||||||||
| 63 | 5 | ▶ | $app->helper(sanitize => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=13 SCAR=25.7
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | 5 | my $c = shift; | |||||||||||||||||||||||||||||||||||||||||
| 65 | 5 | my $html = shift; | |||||||||||||||||||||||||||||||||||||||||
| 66 | 5 | ▶ | return unless $html; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | 5 | my %options = @_; | |||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | 5 | my (%tags, %attr); | |||||||||||||||||||||||||||||||||||||||||
| 71 | 5 | ▶ | my $names = $options{tags} // $sanitize->{tags}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$options{"tags"} // $sanitize->{"tags"}
MC/DC100%2/2
$options{"tags"} // $sanitize->{"tags"}
$options{"tags"}
$sanitize->{"tags"}
Truth table100%3/3
A // B
A= $options{"tags"}B= $sanitize->{"tags"}
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | 5 | ▶ | @tags{@$names} = (1) x @$names if ref $names eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | 5 | ▶ | $names = $options{attr} // $sanitize->{attr}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$options{"attr"} // $sanitize->{"attr"}
MC/DC100%2/2
$options{"attr"} // $sanitize->{"attr"}
$options{"attr"}
$sanitize->{"attr"}
Truth table100%3/3
A // B
A= $options{"attr"}B= $sanitize->{"attr"}
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | 5 | ▶ | @attr{@$names} = (1) x @$names if ref $names eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | 5 | my $doc = Mojo::DOM->new($html); | |||||||||||||||||||||||||||||||||||||||||
| 78 | 5 | ▶ | if (! %tags) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | 1 | my $txt = $doc->all_text; | |||||||||||||||||||||||||||||||||||||||||
| 80 | 1 | $txt =~ s/\s+/ /g; | |||||||||||||||||||||||||||||||||||||||||
| 81 | 1 | $txt =~ s/^ //; | |||||||||||||||||||||||||||||||||||||||||
| 82 | 1 | $txt =~ s/ $//; | |||||||||||||||||||||||||||||||||||||||||
| 83 | 1 | return $txt; | |||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | 4 | for my $node (@{$doc->descendant_nodes}) { | |||||||||||||||||||||||||||||||||||||||||
| 87 | 44 | ▶ | if($node->tag && !$tags{ $node->tag }) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$node->tag and not $tags{$node->tag}
MC/DC100%2/2
$node->tag and not $tags{$node->tag}
$node->tag
not $tags{$node->tag}
Truth table100%3/3
A and not(B)
A= $node->tagB= not $tags{$node->tag}
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | 10 | $node->strip; | |||||||||||||||||||||||||||||||||||||||||
| 89 | 10 | next; | |||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | 34 | ▶ | if(%attr) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | 21 | for my $name (keys %{$node->attr}) { | |||||||||||||||||||||||||||||||||||||||||
| 94 | 4 | ▶ | delete $node->attr->{$name} unless $attr{$name}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | 4 | return $c->b($doc->to_string); | |||||||||||||||||||||||||||||||||||||||||
| 100 | 7 | }); | |||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | 7 | ▶ | $app->helper(trim_param => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 103 | 7 | my $c = shift; | |||||||||||||||||||||||||||||||||||||||||
| 104 | 7 | ▶ | return unless @_; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | 10 | ▶ | my $trim = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | 10 | my $name = shift; | |||||||||||||||||||||||||||||||||||||||||
| 108 | 10 | my $vals = $c->every_param($name); | |||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | 10 | ▶ | $c->param($name => @$vals == 1 ? | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | Mojo::Util::trim($vals->[0]) : | ||||||||||||||||||||||||||||||||||||||||||
| 112 | [ map Mojo::Util::trim($_), @$vals ]); | ||||||||||||||||||||||||||||||||||||||||||
| 113 | 7 | }; | |||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | 7 | my %params; | |||||||||||||||||||||||||||||||||||||||||
| 116 | 7 | my $names = $c->req->params->names; | |||||||||||||||||||||||||||||||||||||||||
| 117 | 7 | @params{ @$names } = (1) x @$names; | |||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | 7 | for my $name (@_) { | |||||||||||||||||||||||||||||||||||||||||
| 120 | 10 | ▶ | if(ref($name) ne 'Regexp') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 121 | 6 | $trim->($name); | |||||||||||||||||||||||||||||||||||||||||
| 122 | 6 | next; | |||||||||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | 4 | for(keys %params) { | |||||||||||||||||||||||||||||||||||||||||
| 126 | 7 | ▶ | next unless $_ =~ $name; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | 4 | $trim->($_); | |||||||||||||||||||||||||||||||||||||||||
| 129 | 4 | delete $params{$_}; | |||||||||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||||||||||||
| 132 | 7 | }); | |||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||
| 134 | 3 | ▶ | $app->helper(collection => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||
| 135 | 3 | my $c = shift; | |||||||||||||||||||||||||||||||||||||||||
| 136 | 3 | ▶ | my @data = ( @_ == 1 && ref($_[0]) eq 'ARRAY' ? @{$_[0]} : @_ ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@_ == 1 and ref $_[0] eq "ARRAY"
MC/DC100%2/2
@_ == 1 and ref $_[0] eq "ARRAY"
@_ == 1
ref $_[0] eq "ARRAY"
Truth table100%3/3
A and B
A= @_ == 1B= ref $_[0] eq "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||
| 137 | 3 | ▶ | return Mojo::Collection->new(@data == 1 && !defined $data[0] ? () : @data ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@data == 1 and not defined $data[0]
MC/DC50%1/2
@data == 1 and not defined $data[0]
@data == 1
not defined $data[0]
Truth table66%2/3
A and not(B)
A= @data == 1B= not defined $data[0]
| |||||||||||||||||||||||||||||||||||||||||||
| 138 | 7 | }); | |||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||
| 140 | 7 | ▶ | if($defaults->{collection}->{patch}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 141 | 0 | ▶ | $app->helper(c => sub { shift->collection(@_) }); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||
| 146 | 7 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||
| 148 | __END__ | ||||||||||||||||||||||||||||||||||||||||||