/[Frey]/trunk/lib/Frey/Designer.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /trunk/lib/Frey/Designer.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 203 - (show annotations)
Thu Oct 30 17:53:09 2008 UTC (15 years, 6 months ago) by dpavlin
File size: 2660 byte(s)
fix path creation
1 package Frey::Designer;
2 use Moose;
3
4 =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 extends 'Frey';
14 with 'Frey::Web';
15
16 has 'uri' => (
17 is => 'rw',
18 isa => 'Uri', coerce => 1,
19 required => 1,
20 );
21
22 has 'mirror' => (
23 is => 'rw',
24 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/;
35
36 #use pQuery;
37 use HTML::Query;
38 use File::Slurp;
39 use LWP::Simple qw/get/;
40 use File::Path;
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 my $base_path = $path;
60 $base_path =~ s{/[^/]+$}{};
61 mkpath $base_path if ! -e $base_path;
62
63 my $url = $self->uri;
64 warn ">> mirror $url -> $path\n";
65
66 $body = get( $url ) or die "can't mirror $url: $!";
67
68 if ( $self->resolve_links ) {
69 my $resolver = HTML::ResolveLink->new( base => $url );
70 $body = $resolver->resolve( $body );
71 }
72
73 write_file( $path, $body );
74 warn "WW mirror $url -> $path ", -s $path, " bytes\n";
75
76 } else {
77 $body = read_file( $path );
78 }
79
80 warn "# $path ", -s $path, " == ", length($body), "bytes";
81 return $body;
82 }
83
84 sub html {
85 my ( $self, $req ) = @_;
86
87 my $body = $self->get_page;
88
89 # strip full hostname
90 my $url = $self->uri;
91 $body =~ s{\Q$url\E}{/}gs;
92 # remove cookie variable from url
93 $body =~ s{CARNetweb=[0-9a-f]+}{}gs;
94
95 =for pQuery
96
97 my $dom = pQuery( $body );
98 # warn dump( $dom->find("body") );
99 $dom->find(".navigation")->each( sub {
100 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;
108
109 =cut
110
111 my $dom = HTML::Query->new(
112 text => $body,
113 'body',
114 );
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";
122
123 =for later
124
125 $body .= strip tt q{
126 <form method="post">
127 <input type="text" name="id" />
128 <input type="submit" />
129 </form>
130
131 <form method="post" enctype="multipart/form-data">
132 <input type="file" name="upload_file" />
133 <input type="submit" />
134 </form>
135
136 <pre>[% raw | html %]</pre>
137 <pre>[% req_dump | html %]</pre>
138 };
139
140 =cut
141
142 warn $body;
143
144 $req->print( $self->page( title => $self->uri, body => $body ) );
145 }
146
147 1;

  ViewVC Help
Powered by ViewVC 1.1.26