1 |
package Frey::Designer; |
package Frey::Designer; |
|
|
|
2 |
use Moose; |
use Moose; |
3 |
|
|
4 |
|
=head1 NAME |
5 |
|
|
6 |
|
Frey::Designer - modify html (sometime in future) |
7 |
|
|
8 |
|
=cut |
9 |
|
|
10 |
use Frey::Types; |
use Frey::Types; |
11 |
#use MooseX::Types::URI qw(Uri FileUri DataUri); |
#use MooseX::Types::URI qw(Uri FileUri DataUri); |
12 |
|
|
13 |
|
extends 'Frey'; |
14 |
|
with 'Frey::Web'; |
15 |
|
|
16 |
has 'uri' => ( |
has 'uri' => ( |
17 |
is => 'rw', |
is => 'rw', |
18 |
isa => 'Uri', coerce => 1, |
isa => 'Uri', coerce => 1, |
21 |
|
|
22 |
has 'mirror' => ( |
has 'mirror' => ( |
23 |
is => 'rw', |
is => 'rw', |
24 |
isa => 'Boolean', |
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 ( $self, $c, $path ) = @_; |
my ( $self ) = @_; |
|
return if -e $path; |
|
|
|
|
|
return unless $self->mirror; |
|
53 |
|
|
54 |
my $url = $self->uri . '/' . $c->req->path . '?' . $c->req->uri->query; |
my $path = $self->path; |
55 |
|
my $body; |
56 |
|
|
57 |
my $base_path = $path; |
if ( ! -e $path && $self->mirror ) { |
|
$base_path =~ s{/[^/]+$}{}; |
|
|
mkpath $base_path if ! -e $base_path; |
|
58 |
|
|
59 |
warn ">> mirror $url -> $path\n"; |
my $base_path = $path; |
60 |
|
$base_path =~ s{/[^/]+$}{}; |
61 |
|
mkpath $base_path if ! -e $base_path; |
62 |
|
|
63 |
LWP::Simple::mirror( $url, $path ) or die "can't mirror $url: $!"; |
my $url = $self->uri; |
64 |
} |
warn ">> mirror $url -> $path\n"; |
65 |
|
|
66 |
sub handler { |
$body = get( $url ) or die "can't mirror $url: $!"; |
|
my ( $self, $c ) = @_; |
|
67 |
|
|
68 |
my $req_dump = dump( $c->req ); |
if ( $self->resolve_links ) { |
69 |
my $raw = $c->req->raw_body; |
my $resolver = HTML::ResolveLink->new( base => $url ); |
70 |
my $body; |
$body = $resolver->resolve( $body ); |
71 |
|
} |
|
my $path = template_path( $c->req->path ); |
|
|
|
|
|
if ( $path =~ m{/__bookmarklet} ) { |
|
|
$c->res->content_type( "text/html" ); |
|
|
my $js = read_file( 'static/xpath.js' ); |
|
|
$js =~ s{//.*}{}gm; # remove comments so that compaction below doesn't screw code |
|
|
$js =~ s/\s\s+/ /gs; |
|
|
$c->res->body(qq{ |
|
|
drag this look to bookmark <a href="javascript:void($js);">xpath?</a> to install XPATH inspector |
|
|
<p><a href="http://www.google.com/">link test</a> |
|
|
}); |
|
|
return; |
|
|
} |
|
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 |
$self->mirror_design( $c, $path ); |
warn "# $path ", -s $path, " == ", length($body), "bytes"; |
81 |
|
return $body; |
82 |
|
} |
83 |
|
|
84 |
$body .= read_file( $path ); |
sub html { |
85 |
|
my ( $self, $req ) = @_; |
86 |
|
|
87 |
if ( $path =~ m/\.css$/ ) { |
my $body = $self->get_page; |
|
$c->res->content_type( "text/css" ); |
|
|
} elsif ( $path =~ m/\.(gif|jpe?g|png)$/ ) { |
|
|
my $type = $1; |
|
|
$type =~ s/jpg/jpeg/; |
|
|
$c->res->content_type( "image/$type" ); |
|
|
} else { |
|
|
$c->res->content_type( "text/html" ); |
|
88 |
|
|
89 |
# strip full hostname |
# strip full hostname |
90 |
$body =~ s{\Q$url\E}{/}gs; |
my $url = $self->uri; |
91 |
# remove cookie variable from url |
$body =~ s{\Q$url\E}{/}gs; |
92 |
$body =~ s{CARNetweb=[0-9a-f]+}{}gs; |
# remove cookie variable from url |
93 |
|
$body =~ s{CARNetweb=[0-9a-f]+}{}gs; |
94 |
my $dom = pQuery( $body ); |
|
95 |
# warn dump( $dom->find("body") ); |
=for pQuery |
96 |
$dom->find(".navigation")->each( sub { |
|
97 |
my $html = $_->innerHTML; |
my $dom = pQuery( $body ); |
98 |
warn $html; |
# warn dump( $dom->find("body") ); |
99 |
# $_->innerHTML(qq{ |
$dom->find(".navigation")->each( sub { |
100 |
# <div style="border: 3px dashed black;">$html</div> |
my $html = $_->innerHTML; |
101 |
# }); |
warn $html; |
102 |
} ); |
# $_->innerHTML(qq{ |
103 |
|
# <div style="border: 3px dashed black;">$html</div> |
104 |
|
# }); |
105 |
|
} ); |
106 |
|
|
107 |
# $body = $dom->toHtml; |
# $body = $dom->toHtml; |
108 |
|
|
109 |
} |
=cut |
110 |
|
|
111 |
warn "<< ", $c->req->path, |
my $dom = HTML::Query->new( |
112 |
" ", -s $path, |
text => $body, |
113 |
" ", $c->res->content_type, |
'body', |
114 |
" ", $c->req->params ? dump( $c->req->params ) : '', |
); |
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"; |
"\n"; |
122 |
|
|
123 |
=for later |
=for later |
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; |