| 1 | package App::Aphra::File; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 7 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 7 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 7 | ▶ | use File::Basename; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 7 | ▶ | use File::Path 'make_path'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 7 | ▶ | use File::Copy; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 7 | ▶ | use Text::FrontMatter::YAML; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 7 | ▶ | use Path::Tiny (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 7 | ▶ | use URI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 7 | has [qw[path name extension ]] => ( | |||||||||||||||||||||||||||
| 13 | isa => 'Str', | ||||||||||||||||||||||||||||
| 14 | is => 'ro', | ||||||||||||||||||||||||||||
| 15 | ); | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 7 | has app => ( | |||||||||||||||||||||||||||
| 18 | isa => 'App::Aphra', | ||||||||||||||||||||||||||||
| 19 | is => 'ro', | ||||||||||||||||||||||||||||
| 20 | ); | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 2 | ▶ | around BUILDARGS => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=8 SCAR=22.1
| |||||||||||||||||||||||||||||
| 23 | 2 | my $orig = shift; | |||||||||||||||||||||||||||
| 24 | 2 | my $class = shift; | |||||||||||||||||||||||||||
| 25 | 2 | ▶ | if (@_ == 1 and not ref $_[0]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@_ == 1 and not ref $_[0]
MC/DC0%0/2
@_ == 1 and not ref $_[0]
@_ == 1
not ref $_[0]
Truth table33%1/3
A and not(B)
A= @_ == 1B= not ref $_[0]
| |||||||||||||||||||||||||||||
| 26 | 0 | croak; | |||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 2 | ▶ | my %args = ref $_[0] ? %{$_[0]} : @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 2 | ▶ | croak "No app attribute\n" unless $args{app}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 2 | ▶ | if ($args{filename}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | 1 | debug("Got $args{filename}"); | |||||||||||||||||||||||||||
| 34 | 1 | my @exts = keys %{ $args{app}->config->{extensions}}; | |||||||||||||||||||||||||||
| 35 | 1 | my ($name, $path, $ext) = fileparse($args{filename}, @exts); | |||||||||||||||||||||||||||
| 36 | 1 | ▶ | chop($path) if $name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 1 | ▶ | chop($name) if $ext; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 1 | @args{qw[path name extension]} = ($path, $name, $ext); | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 2 | return $class->$orig(\%args); | |||||||||||||||||||||||||||
| 42 | 7 | }; | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 2 | ▶ | sub is_template { | ||||||||||||||||||||||||||
|
Subroutine
is_template: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 45 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 4 | return scalar grep { $_ eq $self->extension } | |||||||||||||||||||||||||||
| 48 | 2 | keys %{$self->app->config->{extensions}}; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 2 | ▶ | sub destination_dir { | ||||||||||||||||||||||||||
|
Subroutine
destination_dir: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 52 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 2 | my $dir = $self->path; | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 2 | my $src = $self->app->config->{source}; | |||||||||||||||||||||||||||
| 57 | 2 | my $tgt = $self->app->config->{target}; | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 2 | $dir =~ s/^$src/$tgt/; | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 2 | return $dir; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 4 | ▶ | sub template_name { | ||||||||||||||||||||||||||
|
Subroutine
template_name: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 65 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 4 | my $src = $self->app->config->{source}; | |||||||||||||||||||||||||||
| 68 | 4 | my $template_name = $self->full_name; | |||||||||||||||||||||||||||
| 69 | 4 | $template_name =~ s|^$src/||; | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 4 | return $template_name; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 2 | ▶ | sub output_name { | ||||||||||||||||||||||||||
|
Subroutine
output_name: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 75 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 2 | my $output = $self->template_name; | |||||||||||||||||||||||||||
| 78 | 2 | my $ext = '\.' . $self->extension; | |||||||||||||||||||||||||||
| 79 | 2 | $output =~ s/$ext$//; | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 2 | return $output; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 6 | ▶ | sub full_name { | ||||||||||||||||||||||||||
|
Subroutine
full_name: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 85 | 6 | my $self = shift; | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 6 | my $full_name = $self->path . '/' . $self->name; | |||||||||||||||||||||||||||
| 88 | 6 | ▶ | $full_name .= '.' . $self->extension if $self->extension; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 6 | return $full_name; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 0 | ▶ | sub uri { | ||||||||||||||||||||||||||
|
Subroutine
uri: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 93 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 0 | my $uri = $self->app->uri; | |||||||||||||||||||||||||||
| 96 | 0 | my $base = $self->app->site_vars->{base}; | |||||||||||||||||||||||||||
| 97 | 0 | my $path = $self->output_name; | |||||||||||||||||||||||||||
| 98 | 0 | $path =~ s/^$base//; | |||||||||||||||||||||||||||
| 99 | 0 | $uri .= $path; | |||||||||||||||||||||||||||
| 100 | 0 | $uri =~ s/index\.html$//; | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 0 | return URI->new($uri); | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 0 | ▶ | sub process { | ||||||||||||||||||||||||||
|
Subroutine
process: not called CC=8 SCAR=42.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 106 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 0 | debug('File is: ', $self->full_name); | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 0 | my $dest = $self->destination_dir; | |||||||||||||||||||||||||||
| 111 | 0 | debug("Dest: $dest"); | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 0 | make_path $dest; | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 0 | ▶ | if ($self->is_template) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 0 | debug("It's a template"); | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 0 | my $template = $self->template_name; | |||||||||||||||||||||||||||
| 119 | 0 | my $out = $self->output_name; | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 0 | debug("tt: $template -> $out"); | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | # Check if output file already exists | ||||||||||||||||||||||||||||
| 124 | 0 | my $output_path = Path::Tiny::path($self->app->config->{target}, $out); | |||||||||||||||||||||||||||
| 125 | 0 | ▶ | if (-e $output_path) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 0 | my $source = $self->full_name; | |||||||||||||||||||||||||||
| 127 | 0 | warn "Output file $output_path already exists, skipping processing of $source\n"; | |||||||||||||||||||||||||||
| 128 | 0 | return; | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 0 | my $template_text = Path::Tiny::path($self->full_name)->slurp_utf8; | |||||||||||||||||||||||||||
| 132 | 0 | my $front_matter = Text::FrontMatter::YAML->new( | |||||||||||||||||||||||||||
| 133 | document_string => $template_text, | ||||||||||||||||||||||||||||
| 134 | ); | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 0 | ▶ | my $front_matter_hashref = $front_matter->frontmatter_hashref // {}; | ||||||||||||||||||||||||||
|
Condition0%0/2
$front_matter->frontmatter_hashref // {}
MC/DC0%0/1
$front_matter->frontmatter_hashref // {}
$front_matter->frontmatter_hashref
Truth table0%0/2
A
A= $front_matter->frontmatter_hashref
| |||||||||||||||||||||||||||||
| 137 | 0 | my $orig_layout; | |||||||||||||||||||||||||||
| 138 | 0 | ▶ | if ($front_matter_hashref->{layout}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | 0 | $orig_layout = $self->app->template->{SERVICE}{WRAPPER}; | |||||||||||||||||||||||||||
| 140 | 0 | $self->app->template->{SERVICE}{WRAPPER} = [ $front_matter_hashref->{layout} ]; | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 0 | ▶ | $self->app->template->process($template, { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | page => $front_matter_hashref, | ||||||||||||||||||||||||||||
| 145 | file => $self, | ||||||||||||||||||||||||||||
| 146 | }, $out) | ||||||||||||||||||||||||||||
| 147 | or croak $self->app->template->error; | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 0 | ▶ | $orig_layout and $self->app->template->{SERVICE}{WRAPPER} = $orig_layout; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | } else { | ||||||||||||||||||||||||||||
| 151 | 0 | my $file = $self->full_name; | |||||||||||||||||||||||||||
| 152 | 0 | my $filename = Path::Tiny::path($file)->basename; | |||||||||||||||||||||||||||
| 153 | 0 | my $output_path = Path::Tiny::path($dest, $filename); | |||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | # Check if output file already exists | ||||||||||||||||||||||||||||
| 156 | 0 | ▶ | if (-e $output_path) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | 0 | warn "Output file $output_path already exists, skipping processing of $file\n"; | |||||||||||||||||||||||||||
| 158 | 0 | return; | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 0 | debug("Copy: $file -> ", $self->destination_dir); | |||||||||||||||||||||||||||
| 162 | 0 | copy $file, $self->destination_dir; | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 1 | ▶ | sub debug { | ||||||||||||||||||||||||||
|
Subroutine
debug: called CC=2 SCAR=7.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 167 | 1 | ▶ | warn "@_\n" if $ENV{APHRA_DEBUG}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 7 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 7 | 1; | |||||||||||||||||||||||||||