File Coverage

blib/lib/App/Aphra/File.pm
Criterion Covered Total %
statement 49 89 55.0
branch 2 16 12.5
condition 0 2 0.0
subroutine 14 16 87.5
pod 0 8 0.0
total 65 131 49.6


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