| 1 | package Mojolicious::Plugin::NoIndex; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # ABSTRACT: add meta tag to HTML output to define a policy for robots | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use Mojo::Base 'Mojolicious::Plugin'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 3 | our $VERSION = '0.02'; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 3 | my %routes; | |||||||||||||||||||||||||||
| 12 | my %routes_special; | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 19 | ▶ | sub register { | ||||||||||||||||||||||||||
|
Subroutine
register: called CC=15 SCAR=27.4
Pod
documented
| |||||||||||||||||||||||||||||
| 16 | 19 | my ($self, $app, $config) = @_; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 19 | ▶ | if ( !$config || !%{$config} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$config and %{$config;}
MC/DC50%1/2
$config and %{$config;}
$config
%{$config;}
Truth table66%2/3
A and B
A= $configB= %{$config;}
| |||||||||||||||||||||||||||||
| 19 | 2 | $config = { all_routes => 1 }; | |||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 19 | ▶ | my $default_value = $config->{default} // 'noindex'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$config->{"default"} // "noindex"
MC/DC100%1/1
$config->{"default"} // "noindex"
$config->{"default"}
Truth table100%2/2
A
A= $config->{"default"}
| |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 19 | %routes = (); | |||||||||||||||||||||||||||
| 25 | 19 | %routes_special = (); | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | ROUTENAME: | ||||||||||||||||||||||||||||
| 28 | 19 | ▶ | for my $route_name ( keys %{ $config->{routes} || {} } ) { | ||||||||||||||||||||||||||
|
Condition100%2/2
$config->{'routes'} || {}
MC/DC100%1/1
$config->{'routes'} || {}
$config->{'routes'}
Truth table100%2/2
A
A= $config->{'routes'}
| |||||||||||||||||||||||||||||
| 29 | 14 | my $value = $config->{routes}->{$route_name}; | |||||||||||||||||||||||||||
| 30 | 14 | ▶ | $value = $default_value if $value eq '1'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 14 | ▶ | if ( $route_name =~ m{\A(.*?)\#\#\#(.*)}xms ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | 4 | ▶ | my %conditions = map{ my ($param, $regex) = split /=/, $_; $param => qr/\A(?:$regex)\z/ } split /\&/, $2 // ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$2 // ""
MC/DC0%0/1
$2 // ""
$2
Truth table50%1/2
A
A= $2
| |||||||||||||||||||||||||||||
| 34 | 4 | push @{ $routes_special{$1}->{conditions} }, \%conditions; | |||||||||||||||||||||||||||
| 35 | 4 | $routes_special{$1}->{value} = $value; | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 4 | next ROUTENAME; | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 10 | $routes{$route_name} = $value; | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 19 | ▶ | for my $value ( keys %{ $config->{by_value} || {} } ) { | ||||||||||||||||||||||||||
|
Condition100%2/2
$config->{'by_value'} || {}
MC/DC100%1/1
$config->{'by_value'} || {}
$config->{'by_value'}
Truth table100%2/2
A
A= $config->{'by_value'}
| |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | ROUTE: | ||||||||||||||||||||||||||||
| 46 | 6 | ▶ | for my $route_name ( @{ $config->{by_value}->{$value} || [] } ) { | ||||||||||||||||||||||||||
|
Condition50%1/2
$config->{'by_value'}{$value} || []
MC/DC0%0/1
$config->{'by_value'}{$value} || []
$config->{'by_value'}{$value}
Truth table50%1/2
A
A= $config->{'by_value'}{$value}
| |||||||||||||||||||||||||||||
| 47 | 7 | ▶ | if ( $route_name =~ m{\A(.*?)\#\#\#(.*)}xms ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 2 | ▶ | my %conditions = map{ my ($param, $regex) = split /=/, $_; $param => qr/\A(?:$regex)\z/ } split /\&/, $2 // ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$2 // ""
MC/DC0%0/1
$2 // ""
$2
Truth table50%1/2
A
A= $2
| |||||||||||||||||||||||||||||
| 49 | 2 | push @{ $routes_special{$1}->{conditions} }, \%conditions; | |||||||||||||||||||||||||||
| 50 | 2 | $routes_special{$1}->{value} = $value; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 2 | next ROUTE; | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 5 | $routes{$route_name} = $value; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | $app->hook( | ||||||||||||||||||||||||||||
| 60 | 418 | ▶ | after_render => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=15 SCAR=28.0
| |||||||||||||||||||||||||||||
| 61 | 418 | my ($c, $content, $format) = @_; | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 418 | ▶ | return if !$format; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 418 | ▶ | return if $format ne 'html'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 418 | my $route = $c->current_route; | |||||||||||||||||||||||||||
| 67 | 418 | my $value; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 418 | ▶ | if ( $routes{$route} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 142 | ▶ | $value = $routes{$route} // $default_value; | ||||||||||||||||||||||||||
|
Condition33%1/3
$routes{$route} // $default_value
MC/DC0%0/2
$routes{$route} // $default_value
$routes{$route}
$default_value
Truth table33%1/3
A // B
A= $routes{$route}B= $default_value
| |||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 418 | ▶ | if ( $routes_special{$route} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 94 | my $matched; | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | CONDITION: | ||||||||||||||||||||||||||||
| 77 | 94 | ▶ | for my $conditions ( @{ $routes_special{$route}->{conditions} || [] } ) { | ||||||||||||||||||||||||||
|
Condition50%1/2
$routes_special{$route}{'conditions'} || []
MC/DC0%0/1
$routes_special{$route}{'conditions'} || []
$routes_special{$route}{'conditions'}
Truth table50%1/2
A
A= $routes_special{$route}{'conditions'}
| |||||||||||||||||||||||||||||
| 78 | 94 | ▶ | for my $param ( keys %{ $conditions || {} } ) { | ||||||||||||||||||||||||||
|
Condition50%1/2
$conditions || {}
MC/DC0%0/1
$conditions || {}
$conditions
Truth table50%1/2
A
A= $conditions
| |||||||||||||||||||||||||||||
| 79 | 100 | ▶ | if ( $c->param( $param ) !~ $conditions->{$param} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 37 | next CONDITION; | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 57 | ▶ | $value = $routes_special{$route}->{value} // $default_value; | ||||||||||||||||||||||||||
|
Condition33%1/3
$routes_special{$route}{"value"} // $default_value
MC/DC0%0/2
$routes_special{$route}{"value"} // $default_value
$routes_special{$route}{"value"}
$default_value
Truth table33%1/3
A // B
A= $routes_special{$route}{"value"}B= $default_value
| |||||||||||||||||||||||||||||
| 85 | 57 | last CONDITION; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 418 | ▶ | if ( $config->{all_routes} && !$value ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$config->{"all_routes"} and not $value
MC/DC50%1/2
$config->{"all_routes"} and not $value
$config->{"all_routes"}
not $value
Truth table66%2/3
A and not(B)
A= $config->{"all_routes"}B= not $value
| |||||||||||||||||||||||||||||
| 90 | 11 | $value = $default_value; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 418 | ▶ | return if !$value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 210 | $$content =~ s{<meta [^>]+ name="robots" [^>]+ >}{}x; | |||||||||||||||||||||||||||
| 96 | 210 | $$content =~ s{<head>\K}{<meta name="robots" content="$value">}; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | 19 | ); | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 3 | 1; | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | __END__ | ||||||||||||||||||||||||||||