Skip to content

Commit

Permalink
fix links to the index app should be the site root
Browse files Browse the repository at this point in the history
Since the Site object is rewriting the location of the index app's index
page, it also needs to rewrite any links to that page.

Fixes #305
  • Loading branch information
preaction committed May 25, 2015
1 parent 03ce696 commit ff5346b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
25 changes: 17 additions & 8 deletions lib/Statocles/Site.pm
Expand Up @@ -342,21 +342,30 @@ sub build {
my $base_path = Mojo::URL->new( $base_url )->path;
$base_path =~ s{/$}{};

my $index_root = $self->index ? $apps->{ $self->index }->url_root : '';
for my $page ( @pages ) {
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 );
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{^/};

# Rewrite links to the index app's index page
if ( $index_root && $url =~ m{^$index_root(?:/index[.]html)?$} ) {
$url = '/';
}

if ( $base_path =~ /\S/ ) {
$url = join "", $base_path, $url;
}

$el->attr( $attr, $url );
}
$content = $dom->to_string;
}
$content = $dom->to_string;
}

$store->write_file( $page->path, $content );
Expand Down
12 changes: 10 additions & 2 deletions t/site/index.t
@@ -1,6 +1,7 @@

use Statocles::Base 'Test';
use Statocles::App::Static;
use Mojo::DOM;
my $SHARE_DIR = path( __DIR__, '..', 'share' );

my ( $site, $build_dir, $deploy_dir ) = build_test_site_apps(
Expand All @@ -13,21 +14,28 @@ my $page = ( $blog->pages )[0];

subtest 'build' => sub {
$site->build;
# XXX: Test the content

ok $build_dir->child( 'index.html' )->exists,
'site index renames app page';
ok !$deploy_dir->child( 'index.html' )->exists, 'not deployed yet';
ok !$build_dir->child( 'blog', 'index.html' )->exists,
'site index renames app page';

my $dom = Mojo::DOM->new( $build_dir->child( '/blog/page/2/index.html' )->slurp_utf8 );
ok !$dom->at( '[href=/blog]' ), 'no link to /blog';
ok !$dom->at( '[href=/blog/index.html]' ), 'no link to /blog/index.html';
};

subtest 'deploy' => sub {
$site->deploy;
# XXX: Test the content
ok $deploy_dir->child( 'index.html' )->exists,
'site index renames app page';
ok !$deploy_dir->child( 'blog', 'index.html' )->exists,
'site index renames app page';

my $dom = Mojo::DOM->new( $deploy_dir->child( '/blog/page/2/index.html' )->slurp_utf8 );
ok !$dom->at( '[href=/blog]' ), 'no link to /blog';
ok !$dom->at( '[href=/blog/index.html]' ), 'no link to /blog/index.html';
};

subtest 'error messages' => sub {
Expand Down

0 comments on commit ff5346b

Please sign in to comment.