Mojolicious-Plugin-Export

 view release on metacpan or  search on metacpan

t/plugin.t  view on Meta::CPAN

use Mojolicious::Command::export;
use Mojolicious;

my $app = Mojolicious->new;
$app->plugin( Export => );

unshift @{$app->renderer->classes}, 'main';
unshift @{$app->static->classes}, 'main';
$app->static->warmup;

$app->routes->get( '/' )->name( 'index' );
$app->routes->get( '/docs' )->name( 'docs' );
$app->routes->get( '/docs/more' )->name( 'docs/more' )->to( cb => sub {
    my ( $c ) = @_;
    $c->res->headers->content_type( 'text/html;charset=UTF-8' );
    $c->render( inline => "<h1>\x{2603}</h1>" );
});
$app->routes->get( '/about' )->name( 'about' );
$app->routes->get( '/redirect' )->to( cb => sub {
    shift->redirect_to( 'http://example.com' )
});

my $dom;
my $home = path;
my $tmp = tempdir;

$app->export->on( before_write => sub {
    my ( $export, $pages ) = @_;
    for my $path ( keys %$pages ) {
        my $content = $pages->{ $path };
        if ( ref $content eq 'Mojo::DOM' ) {
            $content->append_content( '<footer>Added footer</footer>' );
        }
    }
    $pages->{ '/added/index.html' } = '<h1>Added</h1>';
} );

$app->export->export({
    to => $tmp,
});

ok -e $tmp->child( 'index.html' ), 'root exists';
$dom = Mojo::DOM->new( $tmp->child( 'index.html' )->slurp );
is $dom->at( 'h1' ), '<h1>Export</h1>', 'root content is correct';
is $dom->at( 'footer' ), '<footer>Added footer</footer>', 'added footer from event';

ok -e $tmp->child( 'added/index.html' ), 'added page exists';
$dom = Mojo::DOM->new( $tmp->child( 'added/index.html' )->slurp );
is $dom->at( 'h1' ), '<h1>Added</h1>', 'added content is correct';

done_testing;

__DATA__
@@ index.html.ep
<head>
    <link rel="stylesheet" href="//cdnjs.org/bootstrap.css">
</head>
<h1>Export</h1>

@@ logo-white-2x.png (base64)
iVBORw0KGgoAAAANSUhEUgAAAUIAAABMCAYAAAAY0L5YAAABG2lUWHRYTUw6Y29tLmFkb2JlLnhtcAAAAAAAPD94cGFja2V0IGJlZ2luPSLvu78iIGlkPSJXNU0wTXBDZWhpSHpyZVN6TlRjemtjOWQiPz4KPHg6eG1wbWV0YSB4bWxuczp4PSJhZG9iZTpuczptZXRhLyIgeDp4bXB0az0iWE1QIENvcmUgNS41LjAiPgogPHJkZjpSRE...



( run in 1.248 second using v1.01-cache-2.11-cpan-df04353d9ac )