-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ability to have static assets #30
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My sense is that this is more complicated that it needs to be. If I understand your intention right, what should happen here is that every file inside the asset directory (if present) should get copied into the docroot. Correct? If so, then rather than this call to a hand-rolled file-copying method (_publish_asset), how about making use of File::Copy::Recursive's |
||
} | ||
|
||
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If my hunch above is correct, then this method becomes unnecessary. |
||
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; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is on the right track, but I suspect that File::Copy::Recursive might be more appropriate. (See below.)
(This would also obviate the requirements of File::Path and File::Find.)