/[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 195 - (show annotations)
Tue Oct 28 18:33:53 2008 UTC (15 years, 6 months ago) by dpavlin
File size: 2737 byte(s)
Remove all use of HTTP::Engine which somehow managed to evolve into
state which make Designer unusable.

From previous encounter with pQuery, I knew that it won't fit my
need to re-model html pages on-the-fly. This verison uses
HTML::Query which seems to do a bit better job at this stage.
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 #use String::TT qw/strip tt/;
29
30 #use pQuery;
31 use HTML::Query;
32 use File::Slurp;
33 use LWP::Simple ();
34 use File::Path;
35 use Data::Dump qw/dump/;
36
37 sub path {
38 my $self = shift;
39 my $path = 'templates/www.carnet.hr/' . $self->uri->path;
40 $path .= '.html' if $path !~ m/\.\w+$/;
41 return $path;
42 }
43
44 sub get_page {
45 my ( $self ) = @_;
46
47 my $path = $self->path;
48
49 if ( ! -e $path && $self->mirror ) {
50
51 my $base_path = $path;
52 $base_path =~ s{/[^/]+$}{};
53 mkpath $base_path if ! -e $base_path;
54
55 my $url = $self->uri;
56 warn ">> mirror $url -> $path\n";
57
58 LWP::Simple::mirror( $url, $path ) or die "can't mirror $url: $!";
59 warn "WW mirror $url -> $path ", -s $path, " bytes\n";
60 }
61
62 my $body = read_file( $path );
63 warn "# $path ", -s $path, " == ", length($body), "bytes";
64 return $body;
65 }
66
67 sub html {
68 my ( $self, $req ) = @_;
69
70 my $body;
71
72 if ( $self->uri->path =~ m{/__bookmarklet} ) {
73 my $js = read_file( 'static/xpath.js' );
74 $js =~ s{//.*}{}gm; # remove comments so that compaction below doesn't screw code
75 $js =~ s/\s\s+/ /gs;
76 $req->print(qq{
77 Drag this <a href="javascript:void($js);">bookmarklet</a> to bookmark toolbar or menu to install XPATH inspector
78 });
79 return;
80 }
81
82 $body .= $self->get_page;
83
84 # strip full hostname
85 my $url = $self->uri;
86 $body =~ s{\Q$url\E}{/}gs;
87 # remove cookie variable from url
88 $body =~ s{CARNetweb=[0-9a-f]+}{}gs;
89
90 =for pQuery
91
92 my $dom = pQuery( $body );
93 # warn dump( $dom->find("body") );
94 $dom->find(".navigation")->each( sub {
95 my $html = $_->innerHTML;
96 warn $html;
97 # $_->innerHTML(qq{
98 # <div style="border: 3px dashed black;">$html</div>
99 # });
100 } );
101
102 # $body = $dom->toHtml;
103
104 =cut
105
106 my $dom = HTML::Query->new(
107 text => $body,
108 'body',
109 );
110 # warn dump( $dom->as_HTML );
111 $body = $dom->as_HTML->[0];
112
113 warn "<< ", $self->uri,
114 " ", -s $self->path,
115 " ", $req->params ? dump( $req->params ) : '',
116 "\n";
117
118 =for later
119
120 $body .= strip tt q{
121 <form method="post">
122 <input type="text" name="id" />
123 <input type="submit" />
124 </form>
125
126 <form method="post" enctype="multipart/form-data">
127 <input type="file" name="upload_file" />
128 <input type="submit" />
129 </form>
130
131 <pre>[% raw | html %]</pre>
132 <pre>[% req_dump | html %]</pre>
133 };
134
135 =cut
136
137 warn $body;
138
139 $req->print( $self->page( title => $self->uri, body => $body ) );
140 }
141
142 1;

  ViewVC Help
Powered by ViewVC 1.1.26