SiteTear/Item.pm

filter: Statementstmt 17.2% 29 / 168 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 32.0% 8 / 25 Podpod 88.2% 15 / 17 totaltotal 18.9% 52 / 274 CC 32 SCAR 44.5
CC
32
Cov
13%
change_path
272.0
copy_to_linkpath
30.0
copy_linked_files
30.0
CRAP 85.9
« SiteTear/PageFilter.pm ↑ Summary SiteTear.pm »
1 package HTML::SiteTear::Item;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use File::Basename qw(basename dirname);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use File::Copy qw(copy);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use File::Path qw(mkpath);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use URI::file;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 #use Data::Dumper;
12  
13 1 ▶use base qw(Class::Accessor);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 __PACKAGE__->mk_accessors(qw(linkpath
15 link_uri
16 source_path
17 source_uri
18 base_uri
19 target_path
20 target_uri
21 kind
22 parent
23 source_root));
24  
25 1 require HTML::SiteTear::Page;
26 1 require HTML::SiteTear::CSS;
27  
28  
29 1 our $VERSION = '1.45';
30  
31 =head1 NAME
32  
33 HTML::SiteTear::Item - treat javascript files, image files and so on.
34  
35 =head1 SYMPOSIS
36  
37 use HTML::SiteTear::Item;
38  
39 $item = HTML::SiteTear::Item->new($parent, $source_path, $kind);
40 $item->linkpath($path); # usually called from the mothod "change_path"
41 # of the parent object.
42 $item->copy_to_linkpath;
43 $item->copy_liked_files;
44  
45 =head1 DESCRIPTION
46  
47 This module is to treat general files liked from web pages. It's also a super class of L<HTML::SiteTear::Page>, L<HTML::SiteTear::CSS>. Internal use only.
48  
49 =head1 METHODS
50  
51 =head2 new
52  
53 $item = HTML::SiteTear::Item->new('parent' => $parent,
54 'source_path' => $source_path,
55 'kind' => $kind);
56  
57 Make an instance of this moduel. $parent must be an instance of HTML::SiteTear::Root or HTML::SiteTear::Page. This method is called from $parent.
58  
59 =cut
60  
61 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
62 0 my $class = shift @_;
63 0 my %args = @_;
64 0 my $self = $class->SUPER::new(\%args);
65 0 $self->source_root($self->parent->source_root);
66 0 ▶ if (exists $args{'source_path'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"source_path"}) 0 0 0
67 0 $self->source_path($self->source_path);
68 }
69
70 0 return $self;
71 }
72  
73 =head2 copy_to_linkpath
74  
75 $item->copy_to_linkpath;
76  
77 Copy $source_path into new linked path from $parent.
78  
79 =cut
80  
81 0 ▶sub copy_to_linkpath {
Subroutine copy_to_linkpath: not called CC=5 SCAR=34.0
Pod documented
82 0 my ($self) = @_;
83 0 my $source_path = $self->source_path;
84 0 ▶ unless ($self->exists_in_copied_files($source_path)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->exists_in_copied_files($source_path)) 0 0 0
85 0 ▶ unless (-e $source_path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $source_path) 0 0 0
86 0 die("The file \"$source_path\" does not exists.\n");
87 0 return;
88 }
89  
90 0 my $target_path = do {
91 0 ▶ if (my $target_uri = $self->item_in_filemap($source_path)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $target_uri = $self->item_in_filemap($source_path)) { } 0 0 0
92 0 $target_uri->file;
93 } else {
94 0 $self->link_uri->file;
95 }};
96
97 0 print "\nCopying asset...\n";
98 0 print "from : $source_path\n";
99 0 print "to : $target_path\n";
100 0 ▶ ($source_path eq $target_path) and die "source and target is same file.\n";
Branch: 1 branch
BranchTrueFalseTotal
$source_path eq $target_path and die "source and target is same file.\n" 0 0 0
101 0 mkpath(dirname($target_path));
102 0 copy($source_path, $target_path);
103 0 $self->add_to_copyied_files($source_path);
104 0 $self->target_path($target_path);
105 }
106 }
107  
108 =head2 add_to_linked_files
109  
110 $item->add_to_linked_files($linked_obj)
111  
112 Add $linked_obj into the internal list. in $linked_obj is an instance of HTML::SiteTear::Item or subclass of HTML::SiteTear::Item for linked files from $source_path.
113  
114 =cut
115  
116 0 ▶sub add_to_linked_files {
Subroutine add_to_linked_files: not called CC=1 SCAR=6.9
Pod documented
117 0 my ($self, $linked_obj) = @_;
118 0 push (@{$self->{'linkedFiles'}}, $linked_obj);
119 }
120  
121 =head2 change_path
122  
123 $new_linkpath = $item->change_path($linkpath, $folder_name, $kind)
124  
125 make a new link path from a link path($linkpath) in $source_path. $folder_name is folder name to store, if $linkpath is not under $source_path.
126  
127 =cut
128  
129 0 ▶sub change_path {
Subroutine change_path: not called CC=16 SCAR=56.1
Pod documented
130 # print STDERR "start change_path\n";
131 0 my ($self, $linkpath, $folder_name, $kind) = @_;
132 0 my $result_path;
133
134 0 my $uri = URI->new($linkpath);
135 0 ▶ if (($uri->scheme) and ($uri->scheme ne 'file')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri->scheme and $uri->scheme ne "file") 0 0 0
Condition0%0/3
$uri->scheme and $uri->scheme ne "file"
!ll&&!rl&&r
000
MC/DC0%0/2
$uri->scheme and $uri->scheme ne "file"
$uri->scheme $uri->scheme ne "file"
Truth table0%0/3
A and B
A= $uri->schemeB= $uri->scheme ne "file"
ABresult
0X0
100
111
136 0 return $linkpath;
137 }
138
139 0 ▶ unless (defined($kind)){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $kind) 0 0 0
140 0 $kind = $folder_name;
141 }
142 0 my $fragment = $uri->fragment;
143 0 $uri = $uri->abs($self->base_uri);
144 0 my $abs_path = $uri->file;
145 0 ▶ unless (-e $abs_path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $abs_path) 0 0 0
146 0 warn("\n$abs_path is not found.\nThe link to this path is not changed.\n");
147 0 return $linkpath;
148 }
149
150 #$abs_path = Cwd::abs_path($abs_path);
151 #print "abs_path in change_path:".$abs_path."\n";
152 0 ▶ if ($self->exists_in_filemap($abs_path) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->exists_in_filemap($abs_path)) { } 0 0 0
153 0 $result_path
154 = $self->rel_for_mappedfile($abs_path, $self->target_uri);
155 0 $result_path->fragment($fragment);
156 } else {
157  
158 0 my $new_linked_obj;
159 0 my %args = ('parent' => $self,
160 'source_path' => $abs_path,
161 'kind' => $kind);
162 0 ▶ if ($kind eq 'page') {
Branch: 2 branches
BranchTrueFalseTotal
if ($kind eq 'page') { } 0 0 0
elsif ($kind eq 'css') { } 0 0 0
163 0 $new_linked_obj = HTML::SiteTear::Page->new(%args);
164  
165 } elsif ($kind eq 'css') {
166 0 $new_linked_obj = HTML::SiteTear::CSS->new(%args);
167  
168 } else {
169 0 $new_linked_obj = HTML::SiteTear::Item->new(%args);
170 }
171  
172 ## obtain relative path from source_root
173 ## to judge whether $abs_path is under sourceRoot or not.
174 0 my $rel_from_root = File::Spec->abs2rel($abs_path, dirname($self->source_root_path));
175 0 my $updir_str = File::Spec->updir();
176
177 0 my $new_link_uri;
178 0 my $should_copy = 1;
179 0 ▶ if ($rel_from_root =~ /^\Q$updir_str\E/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rel_from_root =~ /^\Q$updir_str\E/) { } 0 0 0
180 ## not under sourceRoot
181 0 ▶ if ($self->source_root->only_subitems) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->source_root->only_subitems) { } 0 0 0
182 0 $new_link_uri = $uri->rel($self->target_uri);
183 0 $should_copy = 0;
184 } else {
185 0 my $filename = basename($abs_path);
186 0 $new_link_uri = URI->new("$folder_name/$filename");
187 }
188
189 } else { # when under sourceRoot, linpath is not changed.
190 0 $new_link_uri = URI->new($linkpath);
191 }
192 0 my $target_uri = $new_link_uri->abs($self->target_uri);
193 0 ▶ if ($should_copy and $self->exists_in_target_files($target_uri)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($should_copy and $self->exists_in_target_files($target_uri)) 0 0 0
Condition0%0/3
$should_copy and $self->exists_in_target_files($target_uri)
!ll&&!rl&&r
000
MC/DC0%0/2
$should_copy and $self->exists_in_target_files($target_uri)
$should_copy $self->exists_in_target_files($target_uri)
Truth table0%0/3
A and B
A= $should_copyB= $self->exists_in_target_files($target_uri)
ABresult
0X0
100
111
194 #target file is already created. should change file name.
195 0 my @path_segments = $target_uri->path_segments;
196 0 my $filename = pop @path_segments;
197 0 my $suffix = '';
198 0 my $basename = do {
199 0 ▶ if ($filename =~ /(.+)(\.[^.]+$)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename =~ /(.+)(\.[^.]+$)/) { } 0 0 0
200 0 $suffix = $2;
201 0 $1;
202 } else {
203 0 $filename
204 }};
205 0 my $n = 1;
206 0 ▶ do {$target_uri->path_segments(@path_segments, $basename.($n++).$suffix);
Branch: 1 branch
BranchTrueFalseTotal
$self->exists_in_target_files($target_uri) and do { do { $target_uri->path_segments(@path_segments, $basename . $n++ . $suffix) } } 0 0 0
207 } while ($self->exists_in_target_files($target_uri));
208 0 $new_link_uri = $target_uri->rel($self->target_uri);
209 }
210 0 $new_linked_obj->link_uri($target_uri);
211 0 ▶ $self->add_to_linked_files($new_linked_obj) if $should_copy;
Branch: 1 branch
BranchTrueFalseTotal
if $should_copy 0 0 0
212 0 $self->add_to_filemap($abs_path, $target_uri);
213 0 ▶ if ($fragment) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fragment) 0 0 0
214 0 $new_link_uri->fragment($fragment);
215 }
216 0 $result_path = $new_link_uri->as_string;
217 0 $new_linked_obj->linkpath($result_path);
218 }
219 #print "end of change_path\n";
220 0 return $result_path
221 }
222  
223 =head2 copy_linked_files
224  
225 $item->copy_linked_files();
226  
227 Call method "copy_to_linkpath()" of every object added by "addToLikedFiles($linked_obj)".
228  
229 =cut
230  
231 0 ▶sub copy_linked_files {
Subroutine copy_linked_files: not called CC=5 SCAR=34.0
Pod documented
232 0 my ($self) = @_;
233 0 my @page_list = ();
234  
235 0 foreach my $linked_file (@{$self->{'linkedFiles'}}) {
236 0 ▶ if ($linked_file->kind eq 'page') {
Branch: 1 branch
BranchTrueFalseTotal
if ($linked_file->kind eq 'page') { } 0 0 0
237 0 push @page_list, $linked_file;
238 }
239 else {
240 0 $linked_file->copy_to_linkpath();
241 }
242 }
243
244 #HTML file must be copied after other assets.
245 0 ▶ unless (@page_list) {return};
Branch: 1 branch
BranchTrueFalseTotal
unless (@page_list) 0 0 0
246 0 foreach my $linked_file (@page_list) {
247 0 $linked_file->copy_to_linkpath();
248 }
249 }
250  
251  
252 ##== methods to access root object
253  
254 =head2 add_to_copyied_files
255  
256 $item->add_to_copyied_files($source_path);
257  
258 Add a file path already copied to the copiedFiles table of the root object of the parent chain.
259  
260 =cut
261  
262 0 ▶sub add_to_copyied_files {
Subroutine add_to_copyied_files: not called CC=1 SCAR=6.9
Pod documented
263 0 my ($self, $path) = @_;
264 0 $self->parent->add_to_copyied_files($path);
265 }
266  
267 =head2 exists_in_copied_files
268  
269 $item->exists_in_copied_files($source_path);
270  
271 Check existance of $source_path in the copiedFiles entry.
272  
273 =cut
274  
275 0 ▶sub exists_in_copied_files {
Subroutine exists_in_copied_files: not called CC=1 SCAR=6.9
Pod documented
276 0 my ($self, $path) = @_;
277 0 return $self->parent->exists_in_copied_files($path);
278 }
279  
280 =head2 add_to_filemap
281  
282 $item->add_to_filemap($source_path, $target_path);
283  
284 Add a relation between $source_path and $target_path to the internal table of the root object of the parent chain.
285  
286 =cut
287  
288 0 ▶sub add_to_filemap {
Subroutine add_to_filemap: not called CC=1 SCAR=6.9
Pod documented
289 0 my ($self, $source_path, $target_path) = @_;
290 0 $self->parent->add_to_filemap($source_path, $target_path);
291 }
292  
293 =head2 exists_in_filemap
294  
295 $bool = $item->exists_in_filemap($source_path);
296  
297 Check existance of $source_path in the internal table the root object of parent chain.
298  
299 =cut
300  
301 0 ▶sub exists_in_filemap{
Subroutine exists_in_filemap: not called CC=1 SCAR=6.9
Pod documented
302 0 my ($self, $path) = @_;
303 #return $self->parent->exists_in_filemap($path);
304 0 return $self->source_root->exists_in_filemap($path);
305 }
306  
307  
308 0 ▶sub item_in_filemap {
Subroutine item_in_filemap: not called CC=1 SCAR=6.9
Pod undocumented
309 0 my ($self, $path) = @_;
310 #return $self->parent->item_in_filemap($path);
311 0 return $self->source_root->item_in_filemap($path);
312 }
313  
314 0 ▶sub exists_in_target_files {
Subroutine exists_in_target_files: not called CC=1 SCAR=6.9
Pod undocumented
315 0 my ($self, $path) = @_;
316 0 return $self->source_root->exists_in_target_files($path);
317 }
318  
319 =head2 source_root_path
320  
321 $source_root_path = $item->source_root_path;
322  
323 Get the root source path which is an argument of HTML::SiteTear::CopyTo.
324  
325 =cut
326  
327 0 ▶sub source_root_path {
Subroutine source_root_path: not called CC=1 SCAR=6.9
Pod documented
328 0 my ($self) = @_;
329 0 return $self->source_root->source_path;
330 }
331  
332 =head2 rel_for_mappedfile
333  
334 $relativePath = $item->rel_for_mappedfile($source_path, $base_uri);
335  
336 Get a relative link of the target path corresponding to $source_path based from $base_uri.
337  
338 =cut
339  
340 0 ▶sub rel_for_mappedfile {
Subroutine rel_for_mappedfile: not called CC=1 SCAR=6.9
Pod documented
341 0 my ($self, $source_path, $base_uri) = @_;
342 0 return $self->parent->rel_for_mappedfile($source_path, $base_uri);
343 }
344  
345 ##== accessors
346  
347 =head2 source_path
348  
349 $item->source_path;
350 $item->source_path($path);
351  
352 Get and set the source path of this objcet.
353  
354 =cut
355  
356 0 ▶sub source_path {
Subroutine source_path: not called CC=3 SCAR=24.8
Pod documented
357 0 my $self = shift @_;
358
359 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
360 #my $path = Cwd::abs_path($_[0]);
361 0 my $path = shift @_;
362 0 $self->{'source_path'} = $path;
363 0 my $uri = URI::file->new_abs($path);
364 0 $self->source_uri($uri);
365 0 $self->base_uri($uri);
366 }
367 0 ▶ if ($self->source_uri) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->source_uri) { } 0 0 0
368 0 return $self->source_uri->file;
369 } else {
370 0 return $self->{'source_path'};
371 }
372 }
373  
374 =head2 target_path
375  
376 $item->taget_path;
377 $item->target_path($path);
378  
379 Get and set the target path which is the copy destination of $source_path. This method is called from "copy_to_linkpath()". Before calling this method, $path must be exists.
380  
381 =cut
382  
383 0 ▶sub target_path {
Subroutine target_path: not called CC=2 SCAR=17.9
Pod documented
384 0 my $self = shift @_;
385
386 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
387 0 my $path = $_[0];
388 0 $self->{'target_path'} = $path;
389 0 my $uri = URI::file->new($path);
390 0 $self->target_uri($uri);
391 }
392
393 0 return $self->{'target_path'};
394 }
395  
396 =head2 linkpath
397  
398 $item->linkpath;
399 $item->linkpath($path);
400  
401 Get and set the new link path from $parent. Usually called from the method "change_path" of the parent object.
402  
403 =head2 page_folder_name
404  
405 $item->page_folder_name;
406 $item->page_folder_name('pages');
407  
408 Get and set name of a folder to store HTML files linked from $source_path. If $item does not have the name, $parent give the name.
409  
410 =cut
411  
412 0 ▶sub page_folder_name {
Subroutine page_folder_name: not called CC=3 SCAR=24.8
Pod documented
413 0 my $self = shift @_;
414
415 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
416 0 return $self->{'page_folder_name'} = shift @_;
417 }
418
419 0 ▶ if (exists $self->{'page_folder_name'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'page_folder_name'}) { } 0 0 0
420 0 return $self->{'page_folder_name'};
421 }
422 else {
423 0 return $self->parent->page_folder_name;
424 }
425 }
426  
427 =head2 resource_folder_name
428  
429 $item->resource_folder_name;
430 $item->resource_folder_name('assets');
431  
432 Get and set name of a folder to store not HTML files(javascript, image, CSS) linked from $source_path. If $item does not have the name, $parent gives the name.
433  
434 =cut
435  
436 0 ▶sub resource_folder_name {
Subroutine resource_folder_name: not called CC=3 SCAR=24.8
Pod documented
437 0 my $self = shift @_;
438
439 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
440 0 return $self->{'resource_folder_name'} = shift @_;
441 }
442
443 0 ▶ if (exists $self->{'resource_folder_name'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'resource_folder_name'}) { } 0 0 0
444 0 return $self->{'resource_folder_name'};
445 }
446 else {
447 0 return $self->parent->resource_folder_name;
448 }
449 }
450  
451 =head1 SEE ALSO
452  
453 L<HTML::SiteTear>, L<HTML::SiteTear::Page>, L<HTML::SiteTear::CSS>, L<HTML::SiteTear::Root>
454  
455 =head1 AUTHOR
456  
457 Tetsuro KURITA <tkurita@mac.com>
458  
459 =cut
460  
461 1 1;
« SiteTear/PageFilter.pm ↑ Summary SiteTear.pm »