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 |
if ( $path =~ m{/__bookmarklet} ) { |
57 |
$c->res->content_type( "text/html" ); |
58 |
my $js = read_file( 'static/xpath.js' ); |
59 |
$js =~ s{//.*}{}gm; # remove comments so that compaction below doesn't screw code |
60 |
$js =~ s/\s\s+/ /gs; |
61 |
$c->res->body(qq{ |
62 |
drag this look to bookmark <a href="javascript:void($js);">xpath?</a> to install XPATH inspector |
63 |
<p><a href="http://www.google.com/">link test</a> |
64 |
}); |
65 |
return; |
66 |
} |
67 |
|
68 |
$path .= '.html' if $path !~ m/\.\w+$/; |
69 |
|
70 |
my $url = $self->uri; |
71 |
|
72 |
$self->mirror_design( $c, $path ); |
73 |
|
74 |
$body .= read_file( $path ); |
75 |
|
76 |
if ( $path =~ m/\.css$/ ) { |
77 |
$c->res->content_type( "text/css" ); |
78 |
} elsif ( $path =~ m/\.(gif|jpe?g|png)$/ ) { |
79 |
my $type = $1; |
80 |
$type =~ s/jpg/jpeg/; |
81 |
$c->res->content_type( "image/$type" ); |
82 |
} else { |
83 |
$c->res->content_type( "text/html" ); |
84 |
|
85 |
# strip full hostname |
86 |
$body =~ s{\Q$url\E}{/}gs; |
87 |
# remove cookie variable from url |
88 |
$body =~ s{CARNetweb=[0-9a-f]+}{}gs; |
89 |
|
90 |
my $dom = pQuery( $body ); |
91 |
# warn dump( $dom->find("body") ); |
92 |
$dom->find(".navigation")->each( sub { |
93 |
my $html = $_->innerHTML; |
94 |
warn $html; |
95 |
# $_->innerHTML(qq{ |
96 |
# <div style="border: 3px dashed black;">$html</div> |
97 |
# }); |
98 |
} ); |
99 |
|
100 |
# $body = $dom->toHtml; |
101 |
|
102 |
} |
103 |
|
104 |
warn "<< ", $c->req->path, |
105 |
" ", -s $path, |
106 |
" ", $c->res->content_type, |
107 |
" ", $c->req->params ? dump( $c->req->params ) : '', |
108 |
"\n"; |
109 |
|
110 |
=for later |
111 |
|
112 |
$body .= strip tt q{ |
113 |
<form method="post"> |
114 |
<input type="text" name="id" /> |
115 |
<input type="submit" /> |
116 |
</form> |
117 |
|
118 |
<form method="post" enctype="multipart/form-data"> |
119 |
<input type="file" name="upload_file" /> |
120 |
<input type="submit" /> |
121 |
</form> |
122 |
|
123 |
<pre>[% raw | html %]</pre> |
124 |
<pre>[% req_dump | html %]</pre> |
125 |
}; |
126 |
|
127 |
=cut |
128 |
|
129 |
$c->res->body($body); |
130 |
} |
131 |
|
132 |
1; |