1 |
package Frey::Designer; |
package Frey::Designer; |
|
|
|
2 |
use Moose; |
use Moose; |
|
use Moose::Util::TypeConstraints; |
|
3 |
|
|
4 |
use URI (); |
=head1 NAME |
5 |
|
|
6 |
|
Frey::Designer - modify html (sometime in future) |
7 |
|
|
8 |
|
=cut |
9 |
|
|
10 |
|
use Frey::Types; |
11 |
|
#use MooseX::Types::URI qw(Uri FileUri DataUri); |
12 |
|
|
13 |
subtype 'Uri' |
extends 'Frey'; |
14 |
=> as 'Object' |
with 'Frey::Web'; |
|
=> where { $_->isa('URI') }; |
|
|
|
|
|
coerce 'Uri' |
|
|
=> from 'Object' |
|
|
=> via { $_->isa('URI') |
|
|
? $_ |
|
|
: Params::Coerce::coerce( 'URI', $_ ) } |
|
|
=> from 'Str' |
|
|
=> via { URI->new( $_ ) }; |
|
15 |
|
|
16 |
has 'uri' => ( |
has 'uri' => ( |
17 |
is => 'rw', |
is => 'rw', |
18 |
isa => 'Uri', |
isa => 'Uri', coerce => 1, |
|
coerce => 1, |
|
19 |
required => 1, |
required => 1, |
20 |
); |
); |
21 |
|
|
22 |
|
has 'mirror' => ( |
23 |
|
is => 'rw', |
24 |
|
isa => 'Bool', |
25 |
|
default => 1, |
26 |
|
); |
27 |
|
|
28 |
|
has 'resolve_links' => ( |
29 |
|
is => 'rw', |
30 |
|
isa => 'Bool', |
31 |
|
default => 1, |
32 |
|
); |
33 |
|
|
34 |
#use String::TT qw/strip tt/; |
#use String::TT qw/strip tt/; |
35 |
|
|
36 |
use pQuery; |
#use pQuery; |
37 |
|
use HTML::Query; |
38 |
use File::Slurp; |
use File::Slurp; |
39 |
use LWP::Simple; |
use LWP::Simple qw/get/; |
40 |
use File::Path; |
use File::Path; |
41 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
42 |
|
use HTML::ResolveLink; |
43 |
|
|
44 |
sub template_path { |
sub path { |
45 |
return 'templates/www.carnet.hr/' . shift; |
my $self = shift; |
46 |
|
my $path = 'templates/www.carnet.hr/' . $self->uri->path; |
47 |
|
$path .= '.html' if $path !~ m/\.\w+$/; |
48 |
|
return $path; |
49 |
} |
} |
50 |
|
|
51 |
sub mirror_design { |
sub get_page { |
52 |
my ( $url, $path ) = @_; |
my ( $self ) = @_; |
|
return if -e $path; |
|
53 |
|
|
54 |
my $base_path = $path; |
my $path = $self->path; |
55 |
$base_path =~ s{/[^/]+$}{}; |
my $body; |
|
mkpath $base_path if ! -e $base_path; |
|
56 |
|
|
57 |
warn ">> mirror $url -> $path\n"; |
if ( ! -e $path && $self->mirror ) { |
58 |
|
|
59 |
mirror( $url, $path ) or die "can't mirror $url: $!"; |
my $base_path = $path; |
60 |
} |
$base_path =~ s{/[^/]+$}{}; |
61 |
|
mkpath $base_path if ! -e $base_path; |
62 |
|
|
63 |
sub handler { |
my $url = $self->uri; |
64 |
my ( $self, $c ) = @_; |
warn ">> mirror $url -> $path\n"; |
65 |
|
|
66 |
my $req_dump = dump( $c->req ); |
$body = get( $url ) or die "can't mirror $url: $!"; |
|
my $raw = $c->req->raw_body; |
|
|
my $body; |
|
67 |
|
|
68 |
my $path = template_path( $c->req->path ); |
if ( $self->resolve_links ) { |
69 |
|
my $resolver = HTML::ResolveLink->new( base => $url ); |
70 |
|
$body = $resolver->resolve( $body ); |
71 |
|
} |
72 |
|
|
73 |
$path .= '.html' if $path !~ m/\.\w+$/; |
write_file( $path, $body ); |
74 |
|
warn "WW mirror $url -> $path ", -s $path, " bytes\n"; |
75 |
|
|
76 |
my $url = $self->uri; |
} else { |
77 |
|
$body = read_file( $path ); |
78 |
|
} |
79 |
|
|
80 |
|
warn "# $path ", -s $path, " == ", length($body), "bytes"; |
81 |
|
return $body; |
82 |
|
} |
83 |
|
|
84 |
mirror_design( $url . $c->req->path, $path ); |
sub html { |
85 |
|
my ( $self, $req ) = @_; |
86 |
|
|
87 |
$body .= read_file( $path ); |
my $body = $self->get_page; |
88 |
|
|
89 |
if ( $path =~ m/\.css$/ ) { |
# strip full hostname |
90 |
$c->res->content_type( "text/css" ); |
my $url = $self->uri; |
91 |
} elsif ( $path =~ m/\.(gif|jpe?g|png)$/ ) { |
$body =~ s{\Q$url\E}{/}gs; |
92 |
my $type = $1; |
# remove cookie variable from url |
93 |
$type =~ s/jpg/jpeg/; |
$body =~ s{CARNetweb=[0-9a-f]+}{}gs; |
94 |
$c->res->content_type( "image/$type" ); |
|
95 |
} else { |
=for pQuery |
96 |
$c->res->content_type( "text/html" ); |
|
97 |
|
my $dom = pQuery( $body ); |
98 |
|
# warn dump( $dom->find("body") ); |
99 |
|
$dom->find(".navigation")->each( sub { |
100 |
|
my $html = $_->innerHTML; |
101 |
|
warn $html; |
102 |
|
# $_->innerHTML(qq{ |
103 |
|
# <div style="border: 3px dashed black;">$html</div> |
104 |
|
# }); |
105 |
|
} ); |
106 |
|
|
107 |
# strip full hostname |
# $body = $dom->toHtml; |
|
$body =~ s{\Q$url\E}{/}gs; |
|
|
# remove cookie variable from url |
|
|
$body =~ s{CARNetweb=[0-9a-f]+}{}gs; |
|
|
|
|
|
my $dom = pQuery( $body ); |
|
|
# warn dump( $dom->find("body") ); |
|
|
$dom->find(".navigation")->each( sub { |
|
|
warn dump( $_->innerHTML ); |
|
|
} ); |
|
108 |
|
|
109 |
} |
=cut |
110 |
|
|
111 |
warn "<< ", $c->req->path, " ", -s $path, " ", $c->res->content_type, "\n"; |
my $dom = HTML::Query->new( |
112 |
|
text => $body, |
113 |
|
'body', |
114 |
|
); |
115 |
|
# warn dump( $dom->as_HTML ); |
116 |
|
# $body = $dom->as_HTML->[0]; |
117 |
|
|
118 |
|
warn "<< ", $self->uri, |
119 |
|
" ", -s $self->path, |
120 |
|
" ", $req->params ? dump( $req->params ) : '', |
121 |
|
"\n"; |
122 |
|
|
123 |
=for later |
=for later |
124 |
|
|
139 |
|
|
140 |
=cut |
=cut |
141 |
|
|
142 |
$c->res->body($body); |
warn $body; |
143 |
|
|
144 |
|
$req->print( $self->page( title => $self->uri, body => $body ) ); |
145 |
} |
} |
146 |
|
|
147 |
1; |
1; |