--- trunk/lib/Frey/Designer.pm 2008/08/11 19:32:29 166 +++ trunk/lib/Frey/Designer.pm 2008/10/28 18:33:53 195 @@ -1,112 +1,118 @@ package Frey::Designer; - use Moose; -use Moose::Util::TypeConstraints; -use URI (); +=head1 NAME + +Frey::Designer - modify html (sometime in future) + +=cut + +use Frey::Types; +#use MooseX::Types::URI qw(Uri FileUri DataUri); -subtype 'Uri' - => as 'Object' - => where { $_->isa('URI') }; - -coerce 'Uri' - => from 'Object' - => via { $_->isa('URI') - ? $_ - : Params::Coerce::coerce( 'URI', $_ ) } - => from 'Str' - => via { URI->new( $_ ) }; +extends 'Frey'; +with 'Frey::Web'; has 'uri' => ( is => 'rw', - isa => 'Uri', - coerce => 1, + isa => 'Uri', coerce => 1, required => 1, ); 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 ) = @_; - return unless $self->mirror; + my $path = $self->path; - my $url = $self->uri . '/' . $c->req->path . '?' . $c->req->uri->query; + if ( ! -e $path && $self->mirror ) { - 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; - warn ">> mirror $url -> $path\n"; + my $url = $self->uri; + 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 ( $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; + $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 @@ -128,7 +134,9 @@ =cut - $c->res->body($body); + warn $body; + + $req->print( $self->page( title => $self->uri, body => $body ) ); } 1;