/[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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 164 by dpavlin, Sun Aug 10 16:01:07 2008 UTC revision 224 by dpavlin, Sat Nov 1 00:58:24 2008 UTC
# Line 1  Line 1 
1  package Frey::Designer;  package Frey::Designer;
   
2  use Moose;  use Moose;
 use Moose::Util::TypeConstraints;  
3    
4  use URI ();  =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  subtype 'Uri'  extends 'Frey';
14          => as 'Object'  with 'Frey::Web';
15          => where { $_->isa('URI') };  with 'Frey::Path';
     
 coerce 'Uri'  
         => from 'Object'  
         => via { $_->isa('URI')  
                 ? $_  
                 : Params::Coerce::coerce( 'URI', $_ ) }  
         => from 'Str'  
                 => via { URI->new( $_ ) };  
16    
17  has 'uri' => (  has 'uri' => (
18          is => 'rw',          is => 'rw',
19          isa => 'Uri',          isa => 'Uri', coerce => 1,
         coerce => 1,  
20          required => 1,          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/;  #use String::TT qw/strip tt/;
36    
37  use pQuery;  #use pQuery;
38    use HTML::Query;
39  use File::Slurp;  use File::Slurp;
40  use LWP::Simple;  use LWP::Simple qw/get/;
 use File::Path;  
41  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
42    use HTML::ResolveLink;
43    
44  sub template_path {  sub path {
45          return 'templates/www.carnet.hr/' . shift;          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 mirror_design {  sub get_page {
52          my ( $url, $path ) = @_;          my ( $self ) = @_;
         return if -e $path;  
53    
54          my $base_path = $path;          my $path = $self->path;
55          $base_path =~ s{/[^/]+$}{};          my $body;
         mkpath $base_path if ! -e $base_path;  
56    
57          warn ">> mirror $url -> $path\n";          if ( ! -e $path && $self->mirror ) {
58    
59          mirror( $url, $path ) or die "can't mirror $url: $!";                  $self->mkbasepath( $path );
 }  
60    
61  sub handler {                  my $url = $self->uri;
62      my ( $self, $c ) = @_;                  warn ">> mirror $url -> $path\n";
63    
64      my $req_dump = dump( $c->req );                  $body = get( $url ) or die "can't mirror $url: $!";
     my $raw      = $c->req->raw_body;  
     my $body;  
65    
66          my $path = template_path( $c->req->path );                  if ( $self->resolve_links ) {
67                            my $resolver = HTML::ResolveLink->new( base => $url );
68                            $body = $resolver->resolve( $body );
69                    }
70    
71          $path .= '.html' if $path !~ m/\.\w+$/;                  write_file( $path, $body );
72                    warn "WW mirror $url -> $path ", -s $path, " bytes\n";
73    
74          my $url = $self->uri;          } else {
75                    $body = read_file( $path );
76            }
77    
78            warn "# $path ", -s $path, " == ", length($body), "bytes";
79            return $body;
80    }
81    
82          mirror_design( $url . $c->req->path, $path );  sub request {
83        my ( $self, $req ) = @_;
84    
85          $body .= read_file( $path );          my $body = $self->get_page;
86    
87          if ( $path =~ m/\.css$/ ) {          # strip full hostname
88                  $c->res->content_type( "text/css" );          my $url = $self->uri;
89          } elsif ( $path =~ m/\.(gif|jpe?g|png)$/ ) {          $body =~ s{\Q$url\E}{/}gs;
90                  my $type = $1;          # remove cookie variable from url
91                  $type =~ s/jpg/jpeg/;          $body =~ s{CARNetweb=[0-9a-f]+}{}gs;
92                  $c->res->content_type( "image/$type" );  
93          } else {  =for pQuery
94                  $c->res->content_type( "text/html" );  
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                  # strip full hostname  #       $body = $dom->toHtml;
                 $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 {  
                         warn dump( $_->innerHTML );  
                 } );  
106    
107          }  =cut
108    
109          warn "<< ", $c->req->path, " ", -s $path, " ", $c->res->content_type, "\n";          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                    " ", $req->params ? dump( $req->params ) : '',
119                    "\n";
120    
121  =for later  =for later
122    
# Line 109  sub handler { Line 137  sub handler {
137    
138  =cut  =cut
139    
140      $c->res->body($body);          warn $body;
141    
142            $req->print( $self->page( title => $self->uri, body => $body ) );
143  }  }
144    
145  1;  1;

Legend:
Removed from v.164  
changed lines
  Added in v.224

  ViewVC Help
Powered by ViewVC 1.1.26