--- trunk/lib/Frey/Designer.pm 2008/08/16 23:37:42 175 +++ trunk/lib/Frey/Designer.pm 2008/10/28 18:33:53 195 @@ -1,9 +1,18 @@ package Frey::Designer; - use Moose; + +=head1 NAME + +Frey::Designer - modify html (sometime in future) + +=cut + use Frey::Types; #use MooseX::Types::URI qw(Uri FileUri DataUri); +extends 'Frey'; +with 'Frey::Web'; + has 'uri' => ( is => 'rw', isa => 'Uri', coerce => 1, @@ -12,99 +21,98 @@ has 'mirror' => ( is => 'rw', - isa => 'Boolean', + isa => 'Bool', + default => 1, ); #use String::TT qw/strip tt/; -use pQuery; +#use pQuery; +use HTML::Query; use File::Slurp; use LWP::Simple (); use File::Path; use Data::Dump qw/dump/; -sub template_path { - return 'templates/www.carnet.hr/' . shift; +sub path { + my $self = shift; + my $path = 'templates/www.carnet.hr/' . $self->uri->path; + $path .= '.html' if $path !~ m/\.\w+$/; + return $path; } -sub mirror_design { - my ( $self, $c, $path ) = @_; - return if -e $path; +sub get_page { + my ( $self ) = @_; + + my $path = $self->path; - return unless $self->mirror; + if ( ! -e $path && $self->mirror ) { - my $url = $self->uri . '/' . $c->req->path . '?' . $c->req->uri->query; + my $base_path = $path; + $base_path =~ s{/[^/]+$}{}; + mkpath $base_path if ! -e $base_path; - my $base_path = $path; - $base_path =~ s{/[^/]+$}{}; - mkpath $base_path if ! -e $base_path; + my $url = $self->uri; + warn ">> mirror $url -> $path\n"; - warn ">> mirror $url -> $path\n"; + LWP::Simple::mirror( $url, $path ) or die "can't mirror $url: $!"; + warn "WW mirror $url -> $path ", -s $path, " bytes\n"; + } - LWP::Simple::mirror( $url, $path ) or die "can't mirror $url: $!"; + my $body = read_file( $path ); + warn "# $path ", -s $path, " == ", length($body), "bytes"; + return $body; } -sub handler { - my ( $self, $c ) = @_; +sub html { + my ( $self, $req ) = @_; - my $req_dump = dump( $c->req ); - my $raw = $c->req->raw_body; my $body; - my $path = template_path( $c->req->path ); - - if ( $path =~ m{/__bookmarklet} ) { - $c->res->content_type( "text/html" ); + if ( $self->uri->path =~ m{/__bookmarklet} ) { my $js = read_file( 'static/xpath.js' ); $js =~ s{//.*}{}gm; # remove comments so that compaction below doesn't screw code $js =~ s/\s\s+/ /gs; - $c->res->body(qq{ - drag this look to bookmark xpath? to install XPATH inspector -

link test + $req->print(qq{ + Drag this bookmarklet to bookmark toolbar or menu to install XPATH inspector }); return; } - $path .= '.html' if $path !~ m/\.\w+$/; + $body .= $self->get_page; + # strip full hostname my $url = $self->uri; + $body =~ s{\Q$url\E}{/}gs; + # remove cookie variable from url + $body =~ s{CARNetweb=[0-9a-f]+}{}gs; + +=for pQuery + + my $dom = pQuery( $body ); +# warn dump( $dom->find("body") ); + $dom->find(".navigation")->each( sub { + my $html = $_->innerHTML; + warn $html; +# $_->innerHTML(qq{ +#

$html
+# }); + } ); - $self->mirror_design( $c, $path ); - - $body .= read_file( $path ); - - if ( $path =~ m/\.css$/ ) { - $c->res->content_type( "text/css" ); - } elsif ( $path =~ m/\.(gif|jpe?g|png)$/ ) { - my $type = $1; - $type =~ s/jpg/jpeg/; - $c->res->content_type( "image/$type" ); - } else { - $c->res->content_type( "text/html" ); - - # strip full hostname - $body =~ s{\Q$url\E}{/}gs; - # remove cookie variable from url - $body =~ s{CARNetweb=[0-9a-f]+}{}gs; - - my $dom = pQuery( $body ); -# warn dump( $dom->find("body") ); - $dom->find(".navigation")->each( sub { - my $html = $_->innerHTML; - warn $html; -# $_->innerHTML(qq{ -#
$html
-# }); - } ); +# $body = $dom->toHtml; -# $body = $dom->toHtml; - - } +=cut - warn "<< ", $c->req->path, - " ", -s $path, - " ", $c->res->content_type, - " ", $c->req->params ? dump( $c->req->params ) : '', + my $dom = HTML::Query->new( + text => $body, + 'body', + ); +# warn dump( $dom->as_HTML ); + $body = $dom->as_HTML->[0]; + + warn "<< ", $self->uri, + " ", -s $self->path, + " ", $req->params ? dump( $req->params ) : '', "\n"; =for later @@ -126,7 +134,9 @@ =cut - $c->res->body($body); + warn $body; + + $req->print( $self->page( title => $self->uri, body => $body ) ); } 1;