| 1 | package Locale::Utils::PlaceholderBabelFish; ## no critic (TidyCode) | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use Carp qw(confess); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use HTML::Entities qw(encode_entities); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 5 | ▶ | use List::Util qw(min); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 5 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 5 | ▶ | use MooX::StrictConstructor; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 5 | ▶ | use MooX::Types::MooseLike::Base qw(Bool CodeRef); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 5 | ▶ | use Scalar::Util qw(looks_like_number); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 5 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 5 | our $VERSION = '0.006'; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 5 | has is_strict => ( | |||||||||||||||||||||||||||
| 17 | is => 'rw', | ||||||||||||||||||||||||||||
| 18 | isa => Bool, | ||||||||||||||||||||||||||||
| 19 | ); | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 2 | ▶ | sub default_modifier_code { | ||||||||||||||||||||||||||
|
Subroutine
default_modifier_code: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 22 | 3 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 23 | 3 | my ( $value, $attributes ) = @_; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 3 | ▶ | if ( $attributes =~ m{ \b html \b }xms ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 1 | $value = encode_entities( $value, q{<>&"} ); | |||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 3 | return $value; | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | 2 | }; | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 5 | has modifier_code => ( | |||||||||||||||||||||||||||
| 34 | is => 'rw', | ||||||||||||||||||||||||||||
| 35 | isa => CodeRef, | ||||||||||||||||||||||||||||
| 36 | clearer => 'clear_modifier_code', | ||||||||||||||||||||||||||||
| 37 | lazy => 1, | ||||||||||||||||||||||||||||
| 38 | default => \&default_modifier_code, | ||||||||||||||||||||||||||||
| 39 | ); | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | has plural_code => ( | ||||||||||||||||||||||||||||
| 42 | is => 'rw', | ||||||||||||||||||||||||||||
| 43 | isa => CodeRef, | ||||||||||||||||||||||||||||
| 44 | lazy => 1, | ||||||||||||||||||||||||||||
| 45 | 2 | ▶ | default => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 46 | 15 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 47 | 15 | my $n = shift; | |||||||||||||||||||||||||||
| 48 | 15 | 0 + ( | |||||||||||||||||||||||||||
| 49 | $n != 1 # en | ||||||||||||||||||||||||||||
| 50 | ); | ||||||||||||||||||||||||||||
| 51 | 2 | }; | |||||||||||||||||||||||||||
| 52 | }, | ||||||||||||||||||||||||||||
| 53 | 5 | clearer => 'clear_plural_code', | |||||||||||||||||||||||||||
| 54 | ); | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 47 | ▶ | sub _mangle_value { | ||||||||||||||||||||||||||
|
Subroutine
_mangle_value: called CC=6 SCAR=18.0
| |||||||||||||||||||||||||||||
| 57 | 47 | my ($self, $placeholder, $value, $attribute) = @_; | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 47 | ▶ | defined $value | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 60 | or return $self->is_strict ? $placeholder : q{}; | ||||||||||||||||||||||||||||
| 61 | 41 | ▶ | defined $attribute | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | or return $value; | ||||||||||||||||||||||||||||
| 63 | 7 | ▶ | $self->modifier_code | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | or return $value; | ||||||||||||||||||||||||||||
| 65 | 7 | $value = $self->modifier_code->($value, $attribute); | |||||||||||||||||||||||||||
| 66 | 7 | ▶ | defined $value | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | or confess 'modifier_code returns nothing or undef'; | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 7 | return $value; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 26 | ▶ | sub expand_babel_fish { | ||||||||||||||||||||||||||
|
Subroutine
expand_babel_fish: called CC=8 SCAR=20.9
Pod
documented
| |||||||||||||||||||||||||||||
| 73 | 26 | my ($self, $text, @args) = @_; | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 26 | ▶ | defined $text | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | or return $text; | ||||||||||||||||||||||||||||
| 77 | 25 | ▶ | my $arg_ref = @args == 1 | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 78 | ? ( ref $args[0] eq 'HASH' ? $args[0] : { count => $args[0] } ) | ||||||||||||||||||||||||||||
| 79 | : { | ||||||||||||||||||||||||||||
| 80 | @args % 2 | ||||||||||||||||||||||||||||
| 81 | ? confess 'Arguments expected pairwise' | ||||||||||||||||||||||||||||
| 82 | : @args | ||||||||||||||||||||||||||||
| 83 | }; | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | # placeholders | ||||||||||||||||||||||||||||
| 86 | 25 | my $regex = join q{|}, map { quotemeta } keys %{$arg_ref}; | |||||||||||||||||||||||||||
| 87 | ## no critic (EscapedMetacharacters) | ||||||||||||||||||||||||||||
| 88 | 25 | $text =~ s{ | |||||||||||||||||||||||||||
| 89 | ( \\ [#] ) # escaped | ||||||||||||||||||||||||||||
| 90 | | | ||||||||||||||||||||||||||||
| 91 | ( | ||||||||||||||||||||||||||||
| 92 | [#] \{ | ||||||||||||||||||||||||||||
| 93 | ( $regex ) # placeholder | ||||||||||||||||||||||||||||
| 94 | (?: [ ]* [:] ( [^\}]+ ) )? # attribute | ||||||||||||||||||||||||||||
| 95 | \} | ||||||||||||||||||||||||||||
| 96 | ) | ||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | { | ||||||||||||||||||||||||||||
| 99 | $1 | ||||||||||||||||||||||||||||
| 100 | ? $1 | ||||||||||||||||||||||||||||
| 101 | 50 | ▶ | : $self->_mangle_value($2, $arg_ref->{$3}, $4) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | }xmsge; | ||||||||||||||||||||||||||||
| 103 | ## use critic (EscapedMetacharacters) | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | # plural | ||||||||||||||||||||||||||||
| 106 | 21 | ▶ | my $replace_code = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=19.6
| |||||||||||||||||||||||||||||
| 107 | 21 | my ( $match, $inner, $count ) = @_; | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 21 | ▶ | looks_like_number($count) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | or return $match; | ||||||||||||||||||||||||||||
| 111 | 16 | $inner =~ s{ \\ [|] }{\0}xmsg; | |||||||||||||||||||||||||||
| 112 | 16 | my @special_plurals; | |||||||||||||||||||||||||||
| 113 | my @plurals | ||||||||||||||||||||||||||||
| 114 | = map { | ||||||||||||||||||||||||||||
| 115 | m{ \A [=] ( \d+ ) \s+ ( .* ) \z }xms | ||||||||||||||||||||||||||||
| 116 | 35 | ▶ | ? do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 1 | push @special_plurals, [$1, $2]; | |||||||||||||||||||||||||||
| 118 | 1 | (); | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | : $_; | ||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | map { ## no critic (ComplexMappings) | ||||||||||||||||||||||||||||
| 123 | 16 | my $item = $_; | |||||||||||||||||||||||||||
| 124 | 35 | $item =~ s{ \0 }{\\|}xmsg; | |||||||||||||||||||||||||||
| 125 | 35 | $item; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | split qr{ [|] }xms, $inner; | ||||||||||||||||||||||||||||
| 128 | 16 | for my $plural ( @special_plurals ) { | |||||||||||||||||||||||||||
| 129 | 1 | ▶ | if ( defined $plural->[0] && $plural->[0] == $count ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $plural->[0] and $plural->[0] == $count
MC/DC0%0/2
defined $plural->[0] and $plural->[0] == $count
defined $plural->[0]
$plural->[0] == $count
Truth table33%1/3
A and B
A= defined $plural->[0]B= $plural->[0] == $count
| |||||||||||||||||||||||||||||
| 130 | 1 | return $plural->[1]; | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | @plurals | ||||||||||||||||||||||||||||
| 134 | 15 | ▶ | or return $match; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | 15 | my $index = $self->plural_code->($count); | |||||||||||||||||||||||||||
| 136 | 15 | $index = min( $index, $#plurals ); | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 15 | return $plurals[$index]; | |||||||||||||||||||||||||||
| 139 | 25 | }; | |||||||||||||||||||||||||||
| 140 | ## no critic (EscapedMetacharacters) | ||||||||||||||||||||||||||||
| 141 | 25 | $text =~ s{ | |||||||||||||||||||||||||||
| 142 | ( \\ \( \( ) # $1: escaped | ||||||||||||||||||||||||||||
| 143 | | | ||||||||||||||||||||||||||||
| 144 | ( # $2: match | ||||||||||||||||||||||||||||
| 145 | \( \( # open | ||||||||||||||||||||||||||||
| 146 | ( .*? ) # $3: inner | ||||||||||||||||||||||||||||
| 147 | \) \) # close | ||||||||||||||||||||||||||||
| 148 | (?: [:] ( \w+ ) )? # $4: count variable name | ||||||||||||||||||||||||||||
| 149 | ) | ||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | { | ||||||||||||||||||||||||||||
| 152 | $1 | ||||||||||||||||||||||||||||
| 153 | ? $1 | ||||||||||||||||||||||||||||
| 154 | : $replace_code->( $2, $3, $4 ? $arg_ref->{$4} : $arg_ref->{count} ) | ||||||||||||||||||||||||||||
| 155 | 21 | ▶ | }xmsge; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 156 | ## use critic (EscapedMetacharacters) | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | # unescape | ||||||||||||||||||||||||||||
| 159 | 25 | $text =~ s{ \\ (.) }{$1}xmsg; | |||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 25 | return $text; | |||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 5 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 5 | 1; | |||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | __END__ | ||||||||||||||||||||||||||||