StaticVolt.pm

filter: Statementstmt 94.8% 111 / 117 Branchbran 58.3% 14 / 24 Conditioncond 50.0% 3 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 21 / 21 Podpod 100% 2 / 2 totaltotal 86.7% 151 / 174 CC 17 SCAR 16.2
CC
17
Cov
87%
compile
12.8
_extract_file_config
4.0
new
3.0
CRAP 5.1
↑ Summary StaticVolt/Convertor.pm »
1 # ABSTRACT: Static website generator
2  
3 package StaticVolt;
4 {
5 4 $StaticVolt::VERSION = '1.00';
6 }
7  
8 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 4 ▶use Cwd qw( getcwd );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use File::Copy qw( copy );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use File::Path qw( mkpath rmtree );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use FindBin;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 ▶use Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶use YAML;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 4 ▶use base qw( StaticVolt::Convertor );
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 4 ▶use StaticVolt::Convertor::Markdown;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 4 ▶use StaticVolt::Convertor::Textile;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 5 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
26 5 my ( $class, %config ) = @_;
27  
28 5 my %config_defaults = (
29 'includes' => '_includes',
30 'layouts' => '_layouts',
31 'source' => '_source',
32 'destination' => '_site',
33 );
34  
35 5 for my $config_key ( keys %config_defaults ) {
36 $config{$config_key} = $config{$config_key}
37 20 ▶ || $config_defaults{$config_key};
Condition66%2/3
$config{$config_key} || $config_defaults{$config_key}
l!l&&r!l&&!r
1640
MC/DC0%0/2
$config{$config_key} || $config_defaults{$config_key}
$config{$config_key} $config_defaults{$config_key}
Truth table66%2/3
A || B
A= $config{$config_key}B= $config_defaults{$config_key}
ABresult
1X1
011
000
38 20 $config{$config_key} = File::Spec->canonpath( $config{$config_key} );
39 }
40  
41 5 return bless \%config, $class;
42 }
43  
44 2 ▶sub _clean_destination {
Subroutine _clean_destination: called CC=1 SCAR=0.0
45 2 my $self = shift;
46  
47 2 my $destination = $self->{'destination'};
48 2 rmtree $destination;
49  
50 2 return;
51 }
52  
53 8 ▶sub _traverse_files {
Subroutine _traverse_files: called CC=1 SCAR=0.0
54 8 my $self = shift;
55  
56 8 push @{ $self->{'files'} }, $File::Find::name;
57  
58 8 return;
59 }
60  
61 2 ▶sub _gather_files {
Subroutine _gather_files: called CC=1 SCAR=0.0
62 2 my $self = shift;
63  
64 2 my $source = $self->{'source'};
65 2 ▶ find sub { _traverse_files $self }, $source;
Subroutine __ANON__: called CC=1 SCAR=0.0
66  
67 2 return;
68 }
69  
70 4 ▶sub _extract_file_config {
Subroutine _extract_file_config: called CC=4 SCAR=14.0
71 4 my ( $self, $fh_source_file ) = @_;
72  
73 4 my $delimiter = qr/^---\n$/;
74 4 ▶ if ( <$fh_source_file> =~ $delimiter ) {
Branch: 1 branch
BranchTrueFalseTotal
if (readline($fh_source_file) =~ /$delimiter/) 4 0 4
75 4 my @yaml_lines;
76 4 ▶ while ( my $line = <$fh_source_file> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh_source_file)) 8 0 8
77 8 ▶ if ( $line =~ $delimiter ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /$delimiter/) 4 4 8
78 4 last;
79 }
80 4 push @yaml_lines, $line;
81 }
82  
83 4 return Load join '', @yaml_lines;
84 }
85 }
86  
87 2 ▶sub compile {
Subroutine compile: called CC=11 SCAR=25.5
Pod documented
88 2 my $self = shift;
89  
90 2 $self->_clean_destination;
91 2 $self->_gather_files;
92  
93 2 my $source = $self->{'source'};
94 2 my $destination = $self->{'destination'};
95 2 for my $source_file ( @{ $self->{'files'} } ) {
96 8 my $destination_file = $source_file;
97 8 $destination_file =~ s/^$source/$destination/;
98 8 ▶ if ( -d $source_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $source_file) 4 4 8
99 4 mkpath $destination_file;
100 4 next;
101 }
102  
103 4 ▶ open my $fh_source_file, '<', $source_file
Branch: 1 branch
BranchTrueFalseTotal
open my $fh_source_file, "<", $source_file or die "Failed to open $source_file for input: $!" 4 0 4
104 or die "Failed to open $source_file for input: $!";
105 4 my $file_config = $self->_extract_file_config($fh_source_file);
106  
107 # For files that do not have a configuration defined, copy them over
108 4 ▶ unless ($file_config) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($file_config) 0 4 4
109 0 copy $source_file, $destination_file;
110 0 next;
111 }
112  
113 4 my ($extension) = $source_file =~ m/\.(.+?)$/;
114  
115 # If file does not have a registered convertor and is not an HTML file,
116 # copy the file over to the destination and skip current loop iteration
117 4 ▶ if ( !$self->has_convertor($extension) && $extension ne 'html' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->has_convertor($extension) and $extension ne "html") 0 4 4
Condition33%1/3
not $self->has_convertor($extension) and $extension ne "html"
!ll&&!rl&&r
400
MC/DC0%0/2
not $self->has_convertor($extension) and $extension ne "html"
not $self->has_convertor($extension) $extension ne "html"
Truth table33%1/3
not(A) and B
A= not $self->has_convertor($extension)B= $extension ne "html"
ABresult
0X0
100
111
118 0 copy $source_file, $destination_file;
119 0 next;
120 }
121  
122 # Only files that have a registered convertor need to be handled
123  
124 4 $destination_file =~ s/\..+?$/.html/; # Change extension to .html
125  
126 4 my $file_layout = $file_config->{'layout'};
127 4 my $includes = $self->{'includes'};
128 4 my $layouts = $self->{'layouts'};
129 4 my $abs_include_path = File::Spec->catfile( getcwd, $includes );
130 4 my $abs_layout_path =
131 File::Spec->catfile( getcwd, $layouts, $file_layout );
132 4 my $template = Template->new(
133 'INCLUDE_PATH' => $abs_include_path,
134 'WRAPPER' => $abs_layout_path,
135 'ABSOLUTE' => 1,
136 );
137  
138 4 my $source_file_content = do { local $/; <$fh_source_file> };
139 4 my $converted_content;
140 4 ▶ if ( $extension eq 'html' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($extension eq 'html') { } 0 4 4
141 0 $converted_content = $source_file_content;
142 }
143 else {
144 4 $converted_content =
145 $self->convert( $source_file_content, $extension );
146 }
147  
148 4 $file_config->{sv_rel_base} = $self->_relative_path ( $destination_file );
149  
150 4 ▶ open my $fh_destination_file, '>', $destination_file
Branch: 1 branch
BranchTrueFalseTotal
open my $fh_destination_file, ">", $destination_file or die "Failed to open $destination_file for output: $!" 4 0 4
151 or die "Failed to open $destination_file for output: $!";
152 4 ▶ if ($file_layout) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file_layout) { } 4 0 4
153 4 ▶ $template->process( \$converted_content, $file_config,
Branch: 1 branch
BranchTrueFalseTotal
$template->process(\$converted_content, $file_config, $fh_destination_file) or die $template->error 4 0 4
154 $fh_destination_file )
155 or die $template->error;
156 }
157 else {
158 0 print $fh_destination_file $converted_content;
159 }
160  
161 4 close $fh_source_file;
162 4 close $fh_destination_file;
163 }
164 }
165  
166  
167 10 ▶sub _relative_path {
Subroutine _relative_path: called CC=2 SCAR=7.0
168  
169 10 my ($self,$dest_file) = @_;
170  
171 10 my ($dummy1,$dest_file_dir,$dummy2) = File::Spec->splitpath( $dest_file );
172  
173 10 my $rel_path = File::Spec->abs2rel ( $self->{'destination'},
174 $dest_file_dir );
175  
176 10 ▶ $rel_path .= "/" if $rel_path;
Branch: 1 branch
BranchTrueFalseTotal
if $rel_path 10 0 10
177  
178 10 return $rel_path;
179  
180 };
181  
182 4 1;
183  
184 __END__
↑ Summary StaticVolt/Convertor.pm »