Skip to content

Commit

Permalink
fix site altering raw file content with base URL
Browse files Browse the repository at this point in the history
The site needs to detect filehandles from Page->render() and pass those
through completely unaltered. This was showing up as "GLOB(0xETCETCETC)"
in the file instead of the content we expected.
  • Loading branch information
preaction committed Dec 22, 2014
1 parent e084118 commit 01b42cd
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 15 deletions.
24 changes: 13 additions & 11 deletions lib/Statocles/Site.pm
Expand Up @@ -206,21 +206,23 @@ sub write {
my $base_path = Mojo::URL->new( $self->base_url )->path;
$base_path =~ s{/$}{};
for my $page ( @pages ) {
my $html = $page->render( %args );

if ( $base_path =~ /\S/ ) {
my $dom = Mojo::DOM->new( $html );
for my $attr ( qw( src href ) ) {
for my $el ( $dom->find( "[$attr]" )->each ) {
my $url = $el->attr( $attr );
next unless $url =~ m{^/};
$el->attr( $attr, join "", $base_path, $url );
my $content = $page->render( %args );

if ( !ref $content ) {
if ( $base_path =~ /\S/ ) {
my $dom = Mojo::DOM->new( $content );
for my $attr ( qw( src href ) ) {
for my $el ( $dom->find( "[$attr]" )->each ) {
my $url = $el->attr( $attr );
next unless $url =~ m{^/};
$el->attr( $attr, join "", $base_path, $url );
}
}
$content = $dom->to_string;
}
$html = $dom->to_string;
}

$store->write_file( $page->path, $html );
$store->write_file( $page->path, $content );
}

# Build the sitemap.xml
Expand Down
60 changes: 56 additions & 4 deletions t/site.t
Expand Up @@ -4,6 +4,7 @@ use Statocles::Site;
use Statocles::Theme;
use Statocles::Store;
use Statocles::App::Blog;
use Statocles::App::Static;
use Mojo::DOM;
use Mojo::URL;
my $SHARE_DIR = path( __DIR__, 'share' );
Expand All @@ -15,19 +16,39 @@ subtest 'site writes application' => sub {
subtest 'build' => sub {
$site->build;

for my $page ( $site->app( 'blog' )->pages ) {
for my $page ( $site->app( 'blog' )->pages, $site->app( 'static' )->pages ) {
ok $tmpdir->child( 'build', $page->path )->exists, $page->path . ' built';
ok !$tmpdir->child( 'deploy', $page->path )->exists, $page->path . ' not deployed yet';
}

subtest 'check static content' => sub {
for my $page ( $site->app( 'static' )->pages ) {
my $fh = $page->render;
my $content = do { local $/; <$fh> };
ok $tmpdir->child( 'build', $page->path )->slurp_raw eq $content,
$page->path . ' content is correct';
}
};

};

subtest 'deploy' => sub {
$site->deploy;

for my $page ( $site->app( 'blog' )->pages ) {
for my $page ( $site->app( 'blog' )->pages, $site->app( 'static' )->pages ) {
ok $tmpdir->child( 'build', $page->path )->exists, $page->path . ' built';
ok $tmpdir->child( 'deploy', $page->path )->exists, $page->path . ' deployed';
}

subtest 'check static content' => sub {
for my $page ( $site->app( 'static' )->pages ) {
my $fh = $page->render;
my $content = do { local $/; <$fh> };
ok $tmpdir->child( 'build', $page->path )->slurp_raw eq $content,
$page->path . ' content is correct';
}
};

};
};

Expand Down Expand Up @@ -95,13 +116,17 @@ subtest 'sitemap.xml and robots.txt' => sub {
'/blog/tag/better/page-2.html' => '2014-06-02',
'/blog/tag/error-message/index.html' => '2014-05-22',
'/blog/tag/even-more-tags/index.html' => '2014-06-02',
'/static.txt' => $today,
'/static.yml' => $today,
);

my @posts = qw(
/blog/2014/04/23/slug.html
/blog/2014/04/30/plug.html
/blog/2014/05/22/(regex)[name].file.html
/blog/2014/06/02/more_tags.html
/static.txt
/static.yml
);

my @lists = qw(
Expand Down Expand Up @@ -210,6 +235,16 @@ subtest 'site urls' => sub {
subtest 'page content: ' . $page->path => test_content( $tmpdir, $site, $page, build => $page->path );
ok !$tmpdir->child( 'deploy', $page->path )->exists, 'not deployed yet';
}

subtest 'check static content' => sub {
for my $page ( $site->app( 'static' )->pages ) {
my $fh = $page->render;
my $content = do { local $/; <$fh> };
is $tmpdir->child( 'build', $page->path )->slurp_raw, $content,
$page->path . ' content is correct';
}
};

};

subtest 'deploy' => sub {
Expand All @@ -218,8 +253,17 @@ subtest 'site urls' => sub {
for my $page ( $site->app( 'blog' )->pages ) {
subtest 'page content: ' . $page->path => test_content( $tmpdir, $site, $page, deploy => $page->path );
}
};

subtest 'check static content' => sub {
for my $page ( $site->app( 'static' )->pages ) {
my $fh = $page->render;
my $content = do { local $/; <$fh> };
is $tmpdir->child( 'deploy', $page->path )->slurp_raw, $content,
$page->path . ' content is correct';
}
};

};
};
};

Expand All @@ -235,12 +279,20 @@ sub test_site {
page_size => 2,
);

my $static = Statocles::App::Static->new(
store => $SHARE_DIR->child( qw( app static ) ),
url_root => '/static',
);

$tmpdir->child( 'build' )->mkpath;
$tmpdir->child( 'deploy' )->mkpath;

my $site = Statocles::Site->new(
title => 'Test Site',
apps => { blog => $blog },
apps => {
blog => $blog,
static => $static,
},
build_store => $tmpdir->child( 'build' ),
deploy_store => $tmpdir->child( 'deploy' ),
base_url => 'http://example.com',
Expand Down

0 comments on commit 01b42cd

Please sign in to comment.