App-SD

 view release on metacpan or  search on metacpan

lib/App/SD/CLI/Command/Publish.pm  view on Meta::CPAN

package App::SD::CLI::Command::Publish;
use Any::Moose;
extends 'Prophet::CLI::Command::Publish';
use Prophet::Util;
use File::Path;
use File::Spec;
use HTML::TreeBuilder;
use URI::file;
use Try::Tiny;

sub export_html {
    my $self = shift;
    my $path = $self->arg('path');

    # if they specify both html and replica, then stick rendered templates
    # into a subdirectory. if they specify only html, assume they really
    # want to publish directly into the specified directory
    if ( $self->has_arg('replica') ) {
        $path = File::Spec->catdir( $path => 'html' );
        mkpath( [$path] );
    }

    $self->render_templates_into($path);
}

# helper methods for rendering templates
sub render_templates_into {
    my $self = shift;
    my $dir  = shift;

    require App::SD::Server;
    my $server = App::SD::Server::Static->new(
        read_only => 1, static => 1, app_handle => $self->app_handle );
    $server->static(1);
    $server->setup_template_roots();
    use CGI;
    my $file = "/";
    {

        local $ENV{'REMOTE_ADDR'}    = '127.0.0.1';
        local $ENV{'REQUEST_METHOD'} = 'GET';
        my $cgi = CGI->new();

        my @links = ('/');
        my $seen  = {};
        while ( my $file = shift @links ) {
            next if $seen->{$file};
	    local $ENV{'REQUEST_URI'} = $file;
            try {
                $cgi->path_info($file);
                my $content = $server->handle_request($cgi);

		if ( defined $content ) {
		    my $page_links = [];
		    ( $content, $page_links ) = $self->work_with_urls( $file, $content );

		    push @links, grep { !$seen->{$_} } @$page_links;

		    $self->write_file( $dir, $file, $content );

		    $seen->{$file}++;
		}
            } catch {
		if ( $_ =~ /^REDIRECT (.*)$/ ) {
		    my $new_file = $1;
		    chomp($new_file);
		    $self->handle_redirect( $dir, $file, $new_file );
		    unshift @links, $new_file;
		} elsif ($_) { # rethrow



( run in 1.040 second using v1.01-cache-2.11-cpan-ceb78f64989 )