| 1 | package App::SD::CLI::Command::Publish; | ||
| 2 | use Any::Moose; | ||
| 3 | extends 'Prophet::CLI::Command::Publish'; | ||
| 4 | use Prophet::Util; | ||
| 5 | use File::Path; | ||
| 6 | use File::Spec; | ||
| 7 | use HTML::TreeBuilder; | ||
| 8 | use URI::file; | ||
| 9 | use Try::Tiny; | ||
| 10 | |||
| 11 | sub export_html { | ||
| 12 | my $self = shift; | ||
| 13 | my $path = $self->arg('path'); | ||
| 14 | |||
| 15 | # if they specify both html and replica, then stick rendered templates | ||
| 16 | # into a subdirectory. if they specify only html, assume they really | ||
| 17 | # want to publish directly into the specified directory | ||
| 18 | if ( $self->has_arg('replica') ) { | ||
| 19 | $path = File::Spec->catdir( $path => 'html' ); | ||
| 20 | mkpath( [$path] ); | ||
| 21 | } | ||
| 22 | |||
| 23 | $self->render_templates_into($path); | ||
| 24 | } | ||
| 25 | |||
| 26 | # helper methods for rendering templates | ||
| 27 | sub render_templates_into { | ||
| 28 | my $self = shift; | ||
| 29 | my $dir = shift; | ||
| 30 | |||
| 31 | require App::SD::Server; | ||
| 32 | my $server = App::SD::Server::Static->new( | ||
| 33 | read_only => 1, static => 1, app_handle => $self->app_handle ); | ||
| 34 | $server->static(1); | ||
| 35 | $server->setup_template_roots(); | ||
| 36 | use CGI; | ||
| 37 | my $file = "/"; | ||
| 38 | { | ||
| 39 | |||
| 40 | local $ENV{'REMOTE_ADDR'} = '127.0.0.1'; | ||
| 41 | local $ENV{'REQUEST_METHOD'} = 'GET'; | ||
| 42 | my $cgi = CGI->new(); | ||
| 43 | |||
| 44 | my @links = ('/'); | ||
| 45 | my $seen = {}; | ||
| 46 | while ( my $file = shift @links ) { | ||
| 47 | next if $seen->{$file}; | ||
| 48 | local $ENV{'REQUEST_URI'} = $file; | ||
| 49 | try { | ||
| 50 | $cgi->path_info($file); | ||
| 51 | my $content = $server->handle_request($cgi); | ||
| 52 | |||
| 53 | if ( defined $content ) { | ||
| 54 | my $page_links = []; | ||
| 55 | ( $content, $page_links ) = $self->work_with_urls( $file, $content ); | ||
| 56 | |||
| 57 | push @links, grep { !$seen->{$_} } @$page_links; | ||
| 58 | |||
| 59 | $self->write_file( $dir, $file, $content ); | ||
| 60 | |||
| 61 | $seen->{$file}++; | ||
| 62 | } | ||
| 63 | } catch { | ||
| 64 | if ( $_ =~ /^REDIRECT (.*)$/ ) { | ||
| 65 | my $new_file = $1; | ||
| 66 | chomp($new_file); | ||
| 67 | $self->handle_redirect( $dir, $file, $new_file ); | ||
| 68 | unshift @links, $new_file; | ||
| 69 | } elsif ($_) { # rethrow | ||
| 70 | die $_; | ||
| 71 | } | ||
| 72 | }; | ||
| 73 | } | ||
| 74 | } | ||
| 75 | } | ||
| 76 | |||
| 77 | sub work_with_urls { | ||
| 78 | my $self = shift; | ||
| 79 | my $current_url = shift; | ||
| 80 | my $content = shift; | ||
| 81 | |||
| 82 | my $current_depth = () = $current_url =~ m{.+?/}g; | ||
| 83 | |||
| 84 | #Extract Links from the file | ||
| 85 | my $h = HTML::TreeBuilder->new; | ||
| 86 | $h->no_space_compacting(1); | ||
| 87 | $h->ignore_ignorable_whitespace(0); | ||
| 88 | $h->parse_content($content); | ||
| 89 | |||
| 90 | my $link_elements = $h->extract_links(qw(img href script style a link )); | ||
| 91 | return ($content, []) unless @$link_elements; | ||
| 92 | |||
| 93 | my $all_links = {}; | ||
| 94 | |||
| 95 | #Grab each img src and re-write them so they are relative URL's | ||
| 96 | foreach my $link_element (@$link_elements) { | ||
| 97 | |||
| 98 | my $link = shift @$link_element; #URL value | ||
| 99 | my $element = shift @$link_element; #HTML::Element Object | ||
| 100 | |||
| 101 | $all_links->{$link}++; | ||
| 102 | |||
| 103 | my $url = $link; | ||
| 104 | |||
| 105 | if ( $url =~ m|/$| ) { | ||
| 106 | $url .= "index.html" | ||
| 107 | } elsif ($url !~ /\.\w{2,4}$/) { | ||
| 108 | $url .= ".html"; | ||
| 109 | } | ||
| 110 | |||
| 111 | # if $url is absolute, let's make it relative | ||
| 112 | if ( $url =~ s{^/}{} && $current_depth ) { | ||
| 113 | $url = ( '../' x $current_depth ) . $url; | ||
| 114 | } | ||
| 115 | |||
| 116 | my ($attr) | ||
| 117 | = grep { defined $element->attr($_) and $link eq $element->attr($_) } | ||
| 118 | @{ $HTML::Tagset::linkElements{ $element->tag } }; | ||
| 119 | |||
| 120 | $element->attr( $attr, $url ); | ||
| 121 | } | ||
| 122 | |||
| 123 | my @links; | ||
| 124 | |||
| 125 | # we nned to turn every link into absolute, here is to find out dir info | ||
| 126 | # e.g. if $current_url is '/foo/bar/baz.html', @dirs will be qw/foo bar/ | ||
| 127 | my @dirs = grep { $_ } split m{/}, $current_url; | ||
| 128 | # pop the page name like history.html | ||
| 129 | pop @dirs; | ||
| 130 | |||
| 131 | for my $link ( keys %$all_links ) { | ||
| 132 | next unless $link; | ||
| 133 | |||
| 134 | # we don't use ./ and file: link in pages, so they are bogus for us | ||
| 135 | # more worse thing is './' will overwride some page with nothing | ||
| 136 | next if $link eq './' || $link =~ /^file:/; | ||
| 137 | |||
| 138 | # generally, if the link is not absolute, we need to find it. | ||
| 139 | if ( $link !~ m{^/} ) { | ||
| 140 | my $depth = $link =~ s{\.\./}{}g; | ||
| 141 | my @tmp_dirs = @dirs; | ||
| 142 | # remove trailing dirs according to $depth | ||
| 143 | if ($depth) { | ||
| 144 | pop @tmp_dirs while $depth--; | ||
| 145 | } | ||
| 146 | $link = '/' . join '/', @tmp_dirs, $link; | ||
| 147 | } | ||
| 148 | |||
| 149 | push @links, $link; | ||
| 150 | } | ||
| 151 | |||
| 152 | return $h->as_HTML, \@links; | ||
| 153 | } | ||
| 154 | |||
| 155 | sub handle_redirect { | ||
| 156 | my $self = shift; | ||
| 157 | my $dir = shift; | ||
| 158 | my $file = shift; | ||
| 159 | my $new_file = shift; | ||
| 160 | my $redirected_from = File::Spec->catfile( $dir => $file ); | ||
| 161 | my $redirected_to = File::Spec->catfile( $dir => $new_file ); | ||
| 162 | { | ||
| 163 | my $parent = Prophet::Util->updir($redirected_from); | ||
| 164 | # mkpath succeeds (but returns nothing) if a directory already exists | ||
| 165 | eval { mkpath( [$parent] ) }; | ||
| 166 | if ( $@ ) { | ||
| 167 | die "Failed to create directory " . $parent . " - for $redirected_to " . $@; | ||
| 168 | } | ||
| 169 | } | ||
| 170 | if ( -d $redirected_from ) { $redirected_from .= "/index.html"; } | ||
| 171 | link( $redirected_to, $redirected_from ); | ||
| 172 | } | ||
| 173 | |||
| 174 | sub write_file { | ||
| 175 | my $self = shift; | ||
| 176 | my $dir = shift; | ||
| 177 | my $file = shift; | ||
| 178 | my $content = shift; | ||
| 179 | |||
| 180 | if ( $file =~ qr|/$| ) { | ||
| 181 | $file .= "index.html" | ||
| 182 | } elsif ($file !~ /\.\w{2,4}$/) { | ||
| 183 | $file .= ".html"; | ||
| 184 | } | ||
| 185 | Prophet::Util->write_file( file => File::Spec->catfile( $dir => $file ), content => $content ); | ||
| 186 | |||
| 187 | } | ||
| 188 | |||
| 189 | __PACKAGE__->meta->make_immutable; | ||
| 190 | no Any::Moose; | ||
| 191 | |||
| 192 | package App::SD::Server::Static; | ||
| 193 | use Any::Moose; | ||
| 194 | extends 'App::SD::Server'; | ||
| 195 | use Params::Validate; | ||
| 196 | use JSON; | ||
| 197 | |||
| 198 | sub log_request { } | ||
| 199 | |||
| 200 | sub send_content { | ||
| 201 | my $self = shift; | ||
| 202 | my %args = validate( @_, { content => 1, content_type => 0, encode_as => 0, static => 0 } ); | ||
| 203 | |||
| 204 | if ( $args{'encode_as'} && $args{'encode_as'} eq 'json' ) { | ||
| 205 | $args{'content'} = to_json( $args{'content'} ); | ||
| 206 | } | ||
| 207 | |||
| 208 | return $args{'content'}; | ||
| 209 | } | ||
| 210 | |||
| 211 | sub _send_redirect { | ||
| 212 | my $self = shift; | ||
| 213 | my %args = validate( @_, { to => 1 } ); | ||
| 214 | die "REDIRECT " . $args{to} . "\n"; | ||
| 215 | } | ||
| 216 | |||
| 217 | sub _send_404 {} | ||
| 218 | |||
| 219 | __PACKAGE__->meta->make_immutable; | ||
| 220 | no Any::Moose; | ||
| 221 | |||
| 222 | 1; | ||
| 223 |