1 |
package Frey::HTML::Designer; |
2 |
use Moose; |
3 |
|
4 |
=head1 NAME |
5 |
|
6 |
Frey::HTML::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 |
extends 'Frey'; |
14 |
with 'Frey::Web'; |
15 |
with 'Frey::Path'; |
16 |
|
17 |
has 'uri' => ( |
18 |
is => 'rw', |
19 |
isa => 'Uri', coerce => 1, |
20 |
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/; |
36 |
|
37 |
#use pQuery; |
38 |
use HTML::Query; |
39 |
use File::Slurp; |
40 |
use LWP::Simple qw/get/; |
41 |
use Data::Dump qw/dump/; |
42 |
use HTML::ResolveLink; |
43 |
|
44 |
sub path { |
45 |
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 get_page { |
52 |
my ( $self ) = @_; |
53 |
|
54 |
my $path = $self->path; |
55 |
my $body; |
56 |
|
57 |
if ( ! -e $path && $self->mirror ) { |
58 |
|
59 |
$self->mkbasepath( $path ); |
60 |
|
61 |
my $url = $self->uri; |
62 |
warn ">> mirror $url -> $path\n"; |
63 |
|
64 |
$body = get( $url ) or die "can't mirror $url: $!"; |
65 |
|
66 |
if ( $self->resolve_links ) { |
67 |
my $resolver = HTML::ResolveLink->new( base => $url ); |
68 |
$body = $resolver->resolve( $body ); |
69 |
} |
70 |
|
71 |
write_file( $path, $body ); |
72 |
warn "WW mirror $url -> $path ", -s $path, " bytes\n"; |
73 |
|
74 |
} else { |
75 |
$body = read_file( $path ); |
76 |
} |
77 |
|
78 |
warn "# $path ", -s $path, " == ", length($body), "bytes"; |
79 |
return $body; |
80 |
} |
81 |
|
82 |
sub as_markup { |
83 |
my ( $self ) = @_; |
84 |
|
85 |
my $body = $self->get_page; |
86 |
|
87 |
# strip full hostname |
88 |
my $url = $self->uri; |
89 |
$body =~ s{\Q$url\E}{/}gs; |
90 |
# remove cookie variable from url |
91 |
$body =~ s{CARNetweb=[0-9a-f]+}{}gs; |
92 |
|
93 |
=for pQuery |
94 |
|
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 |
# $body = $dom->toHtml; |
106 |
|
107 |
=cut |
108 |
|
109 |
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 |
121 |
|
122 |
$body .= strip tt q{ |
123 |
<form method="post"> |
124 |
<input type="text" name="id" /> |
125 |
<input type="submit" /> |
126 |
</form> |
127 |
|
128 |
<form method="post" enctype="multipart/form-data"> |
129 |
<input type="file" name="upload_file" /> |
130 |
<input type="submit" /> |
131 |
</form> |
132 |
|
133 |
<pre>[% raw | html %]</pre> |
134 |
<pre>[% req_dump | html %]</pre> |
135 |
}; |
136 |
|
137 |
=cut |
138 |
|
139 |
warn $body; |
140 |
|
141 |
return $self->html_page( title => $self->uri, body => $body ); |
142 |
} |
143 |
|
144 |
1; |