SiteTear/Item.pm

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