Skip to content
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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
60 changes: 59 additions & 1 deletion lib/Plerd.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Owner

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.)

use Carp;
use Try::Tiny;

Expand All @@ -30,6 +33,11 @@ has 'template_path' => (
isa => 'Str',
);

has 'asset_path' => (
is => 'ro',
isa => 'Str',
);

has 'publication_path' => (
is => 'ro',
isa => 'Str',
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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);
Copy link
Owner

Choose a reason for hiding this comment

The 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 dircopy method? It seems to me that one call to that would take care of everything.

}

sub publish_recent_page {
my $self = shift;

Expand Down Expand Up @@ -325,6 +349,34 @@ sub _publish_feed {
) || $self->_throw_template_exception( $self->$template_file_method );
}

sub _publish_asset {
Copy link
Owner

Choose a reason for hiding this comment

The 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;

Expand Down Expand Up @@ -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;

Expand Down
3 changes: 2 additions & 1 deletion lib/Plerd/Init.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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: $!";
}
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions t/basic.t
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down