| HTML-SiteTear documentation | Contained in the HTML-SiteTear distribution. |
HTML::SiteTear::Item - treat javascript files, image files and so on.
use HTML::SiteTear::Item;
$item = HTML::SiteTear::Item->new($parent, $source_path, $kind);
$item->linkpath($path); # usually called from the mothod "change_path"
# of the parent object.
$item->copy_to_linkpath;
$item->copy_liked_files;
This module is to treat general files liked from web pages. It's also a super class of HTML::SiteTear::Page, HTML::SiteTear::CSS. Internal use only.
$item = HTML::SiteTear::Item->new('parent' => $parent,
'source_path' => $source_path,
'kind' => $kind);
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.
$item->copy_to_linkpath;
Copy $source_path into new linked path from $parent.
$item->add_to_linked_files($linked_obj)
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.
$new_linkpath = $item->change_path($linkpath, $folder_name, $kind)
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.
$item->copy_linked_files();
Call method "copy_to_linkpath()" of every object added by "addToLikedFiles($linked_obj)".
$item->add_to_copyied_files($source_path);
Add a file path already copied to the copiedFiles table of the root object of the parent chain.
$item->exists_in_copied_files($source_path);
Check existance of $source_path in the copiedFiles entry.
$item->add_to_filemap($source_path, $target_path);
Add a relation between $source_path and $target_path to the internal table of the root object of the parent chain.
$bool = $item->exists_in_filemap($source_path);
Check existance of $source_path in the internal table the root object of parent chain.
$source_root_path = $item->source_root_path;
Get the root source path which is an argument of HTML::SiteTear::CopyTo.
$relativePath = $item->rel_for_mappedfile($source_path, $base_uri);
Get a relative link of the target path corresponding to $source_path based from $base_uri.
$item->source_path;
$item->source_path($path);
Get and set the source path of this objcet.
$item->taget_path;
$item->target_path($path);
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.
$item->linkpath;
$item->linkpath($path);
Get and set the new link path from $parent. Usually called from the method "change_path" of the parent object.
$item->page_folder_name;
$item->page_folder_name('pages');
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.
$item->resource_folder_name;
$item->resource_folder_name('assets');
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.
Tetsuro KURITA <tkurita@mac.com>
| HTML-SiteTear documentation | Contained in the HTML-SiteTear distribution. |
package HTML::SiteTear::Item; use strict; use warnings; use File::Spec; use File::Basename; use File::Copy; use File::Path; #use Cwd; use URI::file; use Data::Dumper; use base qw(Class::Accessor); __PACKAGE__->mk_accessors(qw(linkpath link_uri source_path source_uri base_uri target_path target_uri kind parent source_root)); require HTML::SiteTear::Page; require HTML::SiteTear::CSS; our $VERSION = '1.43';
sub new { my $class = shift @_; my %args = @_; my $self = $class->SUPER::new(\%args); $self->source_root($self->parent->source_root); if (exists $args{'source_path'}) { $self->source_path($self->source_path); } return $self; }
sub copy_to_linkpath { my ($self) = @_; my $source_path = $self->source_path; unless ($self->exists_in_copied_files($source_path)) { unless (-e $source_path) { die("The file \"$source_path\" does not exists.\n"); return; } my $target_path; if (my $target_uri = $self->item_in_filemap($source_path)) { $target_path = $target_uri->file; } else { $target_path = $self->link_uri->file; } print "\nCopying asset...\n"; print "from : $source_path\n"; print "to : $target_path\n"; ($source_path eq $target_path) and die "source and target is same file.\n"; mkpath(dirname($target_path)); copy($source_path, $target_path); $self->add_to_copyied_files($source_path); #$self->target_path(Cwd::abs_path($target_path)); $self->target_path($target_path); } }
sub add_to_linked_files { my ($self, $linked_obj) = @_; push (@{$self->{'linkedFiles'}}, $linked_obj); }
sub change_path { #print STDERR "start change_path\n"; my ($self, $linkpath, $folder_name, $kind) = @_; my $result_path; my $uri = URI->new($linkpath); if (($uri->scheme) and ($uri->scheme ne 'file')) { return $linkpath; } unless (defined($kind)){ $kind = $folder_name; } my $fragment = $uri->fragment; $uri = $uri->abs($self->base_uri); my $abs_path = $uri->file; unless (-e $abs_path) { warn("\n$abs_path is not found.\nThe link to this path is not changed.\n"); return $linkpath; } #$abs_path = Cwd::abs_path($abs_path); #print "abs_path in change_path:".$abs_path."\n"; if ($self->exists_in_filemap($abs_path) ) { $result_path = $self->rel_for_mappedfile($abs_path, $self->target_uri); $result_path->fragment($fragment); } else { my $new_linked_obj; my %args = ('parent' => $self, 'source_path' => $abs_path, 'kind' => $kind); if ($kind eq 'page') { $new_linked_obj = HTML::SiteTear::Page->new(%args); } elsif ($kind eq 'css') { $new_linked_obj = HTML::SiteTear::CSS->new(%args); } else { $new_linked_obj = HTML::SiteTear::Item->new(%args); } ## obtain relative path from source_root ## to judge whether $abs_path is under sourceRoot or not. my $rel_from_root = File::Spec->abs2rel($abs_path, dirname($self->source_root_path)); my $updir_str = File::Spec->updir(); my $new_link_uri; my $should_copy = 1; if ($rel_from_root =~ /^\Q$updir_str\E/) { ## not under sourceRoot if ($self->source_root->only_subitems) { $new_link_uri = $uri->rel($self->target_uri); $should_copy = 0; } else { my $file_name = basename($abs_path); $new_link_uri = URI->new("$folder_name/$file_name"); } } else { # when under sourceRoot, linpath is not changed. $new_link_uri = URI->new($linkpath); } $new_linked_obj->linkpath($result_path); my $target_uri = $new_link_uri->abs($self->target_uri); $new_linked_obj->link_uri($target_uri); $self->add_to_linked_files($new_linked_obj) if $should_copy; $self->add_to_filemap($abs_path, $target_uri); if ($fragment) { $new_link_uri->fragment($fragment); } $result_path = $new_link_uri->as_string; } #print "end of change_path\n"; return $result_path }
sub copy_linked_files { my ($self) = @_; my @page_list = (); foreach my $linked_file (@{$self->{'linkedFiles'}}) { if ($linked_file->kind eq 'page') { push @page_list, $linked_file; } else { $linked_file->copy_to_linkpath(); } } #HTML file must be copied after other assets. unless (@page_list) {return}; foreach my $linked_file (@page_list) { $linked_file->copy_to_linkpath(); } } ##== methods to access root object
sub add_to_copyied_files { my ($self, $path) = @_; $self->parent->add_to_copyied_files($path); }
sub exists_in_copied_files { my ($self, $path) = @_; return $self->parent->exists_in_copied_files($path); }
sub add_to_filemap { my ($self, $source_path, $target_path) = @_; $self->parent->add_to_filemap($source_path, $target_path); }
sub exists_in_filemap{ my ($self, $path) = @_; #return $self->parent->exists_in_filemap($path); return $self->source_root->exists_in_filemap($path); } sub item_in_filemap { my ($self, $path) = @_; #return $self->parent->item_in_filemap($path); return $self->source_root->item_in_filemap($path); }
sub source_root_path { my ($self) = @_; return $self->source_root->source_path; }
sub rel_for_mappedfile { my ($self, $source_path, $base_uri) = @_; return $self->parent->rel_for_mappedfile($source_path, $base_uri); } ##== accessors
sub source_path { my $self = shift @_; if (@_) { #my $path = Cwd::abs_path($_[0]); my $path = shift @_; $self->{'source_path'} = $path; my $uri = URI::file->new_abs($path); $self->source_uri($uri); $self->base_uri($uri); } if ($self->source_uri) { return $self->source_uri->file; } else { return $self->{'source_path'}; } }
sub target_path { my $self = shift @_; if (@_) { my $path = $_[0]; $self->{'target_path'} = $path; my $uri = URI::file->new($path); $self->target_uri($uri); } return $self->{'target_path'}; }
sub page_folder_name { my $self = shift @_; if (@_) { return $self->{'page_folder_name'} = shift @_; } if (exists $self->{'page_folder_name'}) { return $self->{'page_folder_name'}; } else { return $self->parent->page_folder_name; } }
sub resource_folder_name { my $self = shift @_; if (@_) { return $self->{'resource_folder_name'} = shift @_; } if (exists $self->{'resource_folder_name'}) { return $self->{'resource_folder_name'}; } else { return $self->parent->resource_folder_name; } }
1;