diff --git a/lib/Plerd.pm b/lib/Plerd.pm index 80b1f0d..f093a44 100644 --- a/lib/Plerd.pm +++ b/lib/Plerd.pm @@ -6,9 +6,12 @@ use Moose; use MooseX::Types::URI qw(Uri); use Template; use Path::Class::Dir; +use File::Path; use DateTime; use DateTime::Format::W3CDTF; use URI; +use File::Find; +use File::Copy; use Carp; use Try::Tiny; @@ -30,6 +33,11 @@ has 'template_path' => ( isa => 'Str', ); +has 'asset_path' => ( + is => 'ro', + isa => 'Str', +); + has 'publication_path' => ( is => 'ro', isa => 'Str', @@ -107,6 +115,12 @@ has 'source_directory' => ( lazy_build => 1, ); +has 'asset_directory' => ( + is => 'ro', + isa => 'Path::Class::Dir', + lazy_build => 1, +); + has 'template_directory' => ( is => 'ro', isa => 'Path::Class::Dir', @@ -223,7 +237,7 @@ sub BUILD { my $self = shift; unless ( $self->path ) { - for my $subdir_type ( qw( source template publication database ) ) { + for my $subdir_type ( qw( source template publication database asset ) ) { try { my $method = "${subdir_type}_directory"; my $dir = $self->$method; @@ -245,6 +259,10 @@ sub publish_all { $post->publish; } + if (-d $self->asset_directory) { + $self->publish_assets; + } + $self->publish_archive_page; $self->publish_recent_page; @@ -257,6 +275,12 @@ sub publish_all { $self->clear_post_url_index_hash; } +sub publish_assets { + my $self = shift; + + find(sub { $self->_publish_asset($File::Find::name) if -f }, $self->asset_directory); +} + sub publish_recent_page { my $self = shift; @@ -325,6 +349,34 @@ sub _publish_feed { ) || $self->_throw_template_exception( $self->$template_file_method ); } +sub _publish_asset { + my $self = shift; + + my ($asset) = @_; + + my $pub_dir = Path::Class::File->new( + $self->publication_directory + ); + + my $asset_dir = Path::Class::File->new($self->asset_directory); + + my $new_asset = Path::Class::File->new( + $self->publication_directory, + substr $asset, length $asset_dir + ); + + + # Create directories if they dont exist + my @new_asset_split = grep { $_ ne '' } (split "/", substr $new_asset, length $pub_dir); + pop @new_asset_split; + if (@new_asset_split > 0) { + my $missing_directory = Path::Class::File->new($self->publication_directory, join "/", @new_asset_split); + File::Path::make_path($missing_directory); + } + + copy($asset, $new_asset); +} + sub publish_archive_page { my $self = shift; @@ -396,6 +448,12 @@ sub _build_template_directory { return $self->_build_subdirectory( 'template_path', 'templates' ); } +sub _build_asset_directory { + my $self = shift; + + return $self->_build_subdirectory( 'asset_path', 'assets' ); +} + sub _build_template { my $self = shift; diff --git a/lib/Plerd/Init.pm b/lib/Plerd/Init.pm index e96f556..62d8139 100644 --- a/lib/Plerd/Init.pm +++ b/lib/Plerd/Init.pm @@ -55,7 +55,7 @@ sub populate_directory ( $$ ) { my %file_content = file_content( $dir ); try { - foreach ( qw( docroot source templates log run db conf ) ) { + foreach ( qw( docroot source templates log run db conf assets ) ) { my $subdir = Path::Class::Dir->new( $dir, $_ ); mkdir $subdir or die "Can't create subdir $subdir: $!"; } @@ -489,6 +489,7 @@ image_alt: "My Cool Blog's logo -- a photograph of Fido, the author's gray tabby # database_path: /opt/plerd/db # run_path: /tmp/plerd/run # log_path: /var/log/plerd/ +# asset_path: /home/me/Dropbox/plerd/assets EOF ); return %file_content; diff --git a/t/basic.t b/t/basic.t index cb27d0f..c931ed2 100644 --- a/t/basic.t +++ b/t/basic.t @@ -220,6 +220,7 @@ my $alt_config_plerd = Plerd->new( source_path => "$FindBin::Bin/source", publication_path => "$FindBin::Bin/docroot", template_path => "$FindBin::Bin/templates", + asset_path => "$FindBin::Bin/assets", database_path => "$FindBin::Bin/db", title => 'Test Blog', author_name => 'Nobody',