1 |
package Frey::Designer; |
2 |
|
3 |
use Moose; |
4 |
use Frey::Types; |
5 |
#use MooseX::Types::URI qw(Uri FileUri DataUri); |
6 |
|
7 |
has 'uri' => ( |
8 |
is => 'rw', |
9 |
isa => 'Uri', coerce => 1, |
10 |
required => 1, |
11 |
); |
12 |
|
13 |
has 'mirror' => ( |
14 |
is => 'rw', |
15 |
isa => 'Boolean', |
16 |
); |
17 |
|
18 |
#use String::TT qw/strip tt/; |
19 |
|
20 |
use pQuery; |
21 |
use File::Slurp; |
22 |
use LWP::Simple (); |
23 |
use File::Path; |
24 |
use Data::Dump qw/dump/; |
25 |
|
26 |
sub template_path { |
27 |
return 'templates/www.carnet.hr/' . shift; |
28 |
} |
29 |
|
30 |
sub mirror_design { |
31 |
my ( $self, $c, $path ) = @_; |
32 |
return if -e $path; |
33 |
|
34 |
return unless $self->mirror; |
35 |
|
36 |
my $url = $self->uri . '/' . $c->req->path . '?' . $c->req->uri->query; |
37 |
|
38 |
my $base_path = $path; |
39 |
$base_path =~ s{/[^/]+$}{}; |
40 |
mkpath $base_path if ! -e $base_path; |
41 |
|
42 |
warn ">> mirror $url -> $path\n"; |
43 |
|
44 |
LWP::Simple::mirror( $url, $path ) or die "can't mirror $url: $!"; |
45 |
} |
46 |
|
47 |
sub handler { |
48 |
my ( $self, $c ) = @_; |
49 |
|
50 |
my $req_dump = dump( $c->req ); |
51 |
my $raw = $c->req->raw_body; |
52 |
my $body; |
53 |
|
54 |
my $path = template_path( $c->req->path ); |
55 |
|
56 |
$path .= '.html' if $path !~ m/\.\w+$/; |
57 |
|
58 |
my $url = $self->uri; |
59 |
|
60 |
$self->mirror_design( $c, $path ); |
61 |
|
62 |
$body .= read_file( $path ); |
63 |
|
64 |
if ( $path =~ m/\.css$/ ) { |
65 |
$c->res->content_type( "text/css" ); |
66 |
} elsif ( $path =~ m/\.(gif|jpe?g|png)$/ ) { |
67 |
my $type = $1; |
68 |
$type =~ s/jpg/jpeg/; |
69 |
$c->res->content_type( "image/$type" ); |
70 |
} else { |
71 |
$c->res->content_type( "text/html" ); |
72 |
|
73 |
# strip full hostname |
74 |
$body =~ s{\Q$url\E}{/}gs; |
75 |
# remove cookie variable from url |
76 |
$body =~ s{CARNetweb=[0-9a-f]+}{}gs; |
77 |
|
78 |
my $dom = pQuery( $body ); |
79 |
# warn dump( $dom->find("body") ); |
80 |
$dom->find(".navigation")->each( sub { |
81 |
my $html = $_->innerHTML; |
82 |
warn $html; |
83 |
# $_->innerHTML(qq{ |
84 |
# <div style="border: 3px dashed black;">$html</div> |
85 |
# }); |
86 |
} ); |
87 |
|
88 |
# $body = $dom->toHtml; |
89 |
|
90 |
} |
91 |
|
92 |
warn "<< ", $c->req->path, |
93 |
" ", -s $path, |
94 |
" ", $c->res->content_type, |
95 |
" ", $c->req->params ? dump( $c->req->params ) : '', |
96 |
"\n"; |
97 |
|
98 |
=for later |
99 |
|
100 |
$body .= strip tt q{ |
101 |
<form method="post"> |
102 |
<input type="text" name="id" /> |
103 |
<input type="submit" /> |
104 |
</form> |
105 |
|
106 |
<form method="post" enctype="multipart/form-data"> |
107 |
<input type="file" name="upload_file" /> |
108 |
<input type="submit" /> |
109 |
</form> |
110 |
|
111 |
<pre>[% raw | html %]</pre> |
112 |
<pre>[% req_dump | html %]</pre> |
113 |
}; |
114 |
|
115 |
=cut |
116 |
|
117 |
$c->res->body($body); |
118 |
} |
119 |
|
120 |
1; |