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'; |
15 |
=> where { $_->isa('URI') }; |
with 'Frey::Path'; |
|
|
|
|
coerce 'Uri' |
|
|
=> from 'Object' |
|
|
=> via { $_->isa('URI') |
|
|
? $_ |
|
|
: Params::Coerce::coerce( 'URI', $_ ) } |
|
|
=> from 'Str' |
|
|
=> via { URI->new( $_ ) }; |
|
16 |
|
|
17 |
has 'uri' => ( |
has 'uri' => ( |
18 |
is => 'rw', |
is => 'rw', |
19 |
isa => 'Uri', |
isa => 'Uri', coerce => 1, |
|
coerce => 1, |
|
20 |
required => 1, |
required => 1, |
21 |
); |
); |
22 |
|
|
23 |
|
has 'mirror' => ( |
24 |
|
is => 'rw', |
25 |
|
isa => 'Bool', |
26 |
|
default => 1, |
27 |
|
); |
28 |
|
|
29 |
|
has 'resolve_links' => ( |
30 |
|
is => 'rw', |
31 |
|
isa => 'Bool', |
32 |
|
default => 1, |
33 |
|
); |
34 |
|
|
35 |
#use String::TT qw/strip tt/; |
#use String::TT qw/strip tt/; |
36 |
|
|
37 |
use pQuery; |
#use pQuery; |
38 |
|
use HTML::Query; |
39 |
use File::Slurp; |
use File::Slurp; |
40 |
use LWP::Simple; |
use LWP::Simple qw/get/; |
|
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 = join('/', ( 'templates', $self->uri->host, $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: $!"; |
$self->mkbasepath( $path ); |
|
} |
|
60 |
|
|
61 |
sub handler { |
my $url = $self->uri; |
62 |
my ( $self, $c ) = @_; |
warn ">> mirror $url -> $path\n"; |
63 |
|
|
64 |
my $req_dump = dump( $c->req ); |
$body = get( $url ) or die "can't mirror $url: $!"; |
|
my $raw = $c->req->raw_body; |
|
|
my $body; |
|
65 |
|
|
66 |
my $path = template_path( $c->req->path ); |
if ( $self->resolve_links ) { |
67 |
|
my $resolver = HTML::ResolveLink->new( base => $url ); |
68 |
|
$body = $resolver->resolve( $body ); |
69 |
|
} |
70 |
|
|
71 |
$path .= '.html' if $path !~ m/\.\w+$/; |
write_file( $path, $body ); |
72 |
|
warn "WW mirror $url -> $path ", -s $path, " bytes\n"; |
73 |
|
|
74 |
my $url = $self->uri; |
} else { |
75 |
|
$body = read_file( $path ); |
76 |
|
} |
77 |
|
|
78 |
|
warn "# $path ", -s $path, " == ", length($body), "bytes"; |
79 |
|
return $body; |
80 |
|
} |
81 |
|
|
82 |
mirror_design( $url . $c->req->path, $path ); |
sub markup { |
83 |
|
my ( $self ) = @_; |
84 |
|
|
85 |
$body .= read_file( $path ); |
my $body = $self->get_page; |
86 |
|
|
87 |
if ( $path =~ m/\.css$/ ) { |
# strip full hostname |
88 |
$c->res->content_type( "text/css" ); |
my $url = $self->uri; |
89 |
} elsif ( $path =~ m/\.(gif|jpe?g|png)$/ ) { |
$body =~ s{\Q$url\E}{/}gs; |
90 |
my $type = $1; |
# remove cookie variable from url |
91 |
$type =~ s/jpg/jpeg/; |
$body =~ s{CARNetweb=[0-9a-f]+}{}gs; |
92 |
$c->res->content_type( "image/$type" ); |
|
93 |
} else { |
=for pQuery |
94 |
$c->res->content_type( "text/html" ); |
|
95 |
|
my $dom = pQuery( $body ); |
96 |
|
# warn dump( $dom->find("body") ); |
97 |
|
$dom->find(".navigation")->each( sub { |
98 |
|
my $html = $_->innerHTML; |
99 |
|
warn $html; |
100 |
|
# $_->innerHTML(qq{ |
101 |
|
# <div style="border: 3px dashed black;">$html</div> |
102 |
|
# }); |
103 |
|
} ); |
104 |
|
|
105 |
# 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 ); |
|
|
} ); |
|
106 |
|
|
107 |
} |
=cut |
108 |
|
|
109 |
warn "<< ", $c->req->path, " ", -s $path, " ", $c->res->content_type, "\n"; |
my $dom = HTML::Query->new( |
110 |
|
text => $body, |
111 |
|
'body', |
112 |
|
); |
113 |
|
# warn dump( $dom->as_HTML ); |
114 |
|
# $body = $dom->as_HTML->[0]; |
115 |
|
|
116 |
|
warn "<< ", $self->uri, |
117 |
|
" ", -s $self->path, |
118 |
|
"\n"; |
119 |
|
|
120 |
=for later |
=for later |
121 |
|
|
136 |
|
|
137 |
=cut |
=cut |
138 |
|
|
139 |
$c->res->body($body); |
warn $body; |
140 |
|
|
141 |
|
return $self->page( title => $self->uri, body => $body ); |
142 |
} |
} |
143 |
|
|
144 |
1; |
1; |