From 01b42cdd82def3865c25e78d605b24b089dcf93c Mon Sep 17 00:00:00 2001 From: Doug Bell Date: Mon, 22 Dec 2014 04:36:23 -0600 Subject: [PATCH] fix site altering raw file content with base URL 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. --- lib/Statocles/Site.pm | 24 +++++++++-------- t/site.t | 60 ++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 69 insertions(+), 15 deletions(-) diff --git a/lib/Statocles/Site.pm b/lib/Statocles/Site.pm index 8699c184..1c98ee7d 100644 --- a/lib/Statocles/Site.pm +++ b/lib/Statocles/Site.pm @@ -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 diff --git a/t/site.t b/t/site.t index cf14c098..0cc7c9be 100644 --- a/t/site.t +++ b/t/site.t @@ -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' ); @@ -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'; + } + }; + }; }; @@ -95,6 +116,8 @@ 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( @@ -102,6 +125,8 @@ subtest 'sitemap.xml and robots.txt' => sub { /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( @@ -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 { @@ -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'; + } + }; + + }; }; }; @@ -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',