/[Frey]/branches/no-pager/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 /branches/no-pager/lib/Frey/Designer.pm

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

revision 174 by dpavlin, Sat Aug 16 13:03:44 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;
3    
4    =head1 NAME
5    
6    Frey::Designer - modify html (sometime in future)
7    
8    =cut
9    
10  use Frey::Types;  use Frey::Types;
11  #use MooseX::Types::URI qw(Uri FileUri DataUri);  #use MooseX::Types::URI qw(Uri FileUri DataUri);
12    
13    extends 'Frey';
14    with 'Frey::Web';
15    with 'Frey::Path';
16    
17  has 'uri' => (  has 'uri' => (
18          is => 'rw',          is => 'rw',
19          isa => 'Uri', coerce => 1,          isa => 'Uri', coerce => 1,
# Line 12  has 'uri' => ( Line 22  has 'uri' => (
22    
23  has 'mirror' => (  has 'mirror' => (
24          is => 'rw',          is => 'rw',
25          isa => 'Boolean',          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 ( $self, $c, $path ) = @_;          my ( $self ) = @_;
         return if -e $path;  
   
         return unless $self->mirror;  
53    
54          my $url = $self->uri . '/' . $c->req->path . '?' . $c->req->uri->query;          my $path = $self->path;
55            my $body;
56    
57          my $base_path = $path;          if ( ! -e $path && $self->mirror ) {
         $base_path =~ s{/[^/]+$}{};  
         mkpath $base_path if ! -e $base_path;  
58    
59          warn ">> mirror $url -> $path\n";                  $self->mkbasepath( $path );
60    
61          LWP::Simple::mirror( $url, $path ) or die "can't mirror $url: $!";                  my $url = $self->uri;
62  }                  warn ">> mirror $url -> $path\n";
   
 sub handler {  
     my ( $self, $c ) = @_;  
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          $self->mirror_design( $c, $path );          warn "# $path ", -s $path, " == ", length($body), "bytes";
79            return $body;
80    }
81    
82          $body .= read_file( $path );  sub request {
83        my ( $self, $req ) = @_;
84    
85          if ( $path =~ m/\.css$/ ) {          my $body = $self->get_page;
                 $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" );  
86    
87                  # strip full hostname          # strip full hostname
88                  $body =~ s{\Q$url\E}{/}gs;          my $url = $self->uri;
89                  # remove cookie variable from url          $body =~ s{\Q$url\E}{/}gs;
90                  $body =~ s{CARNetweb=[0-9a-f]+}{}gs;          # remove cookie variable from url
91            $body =~ s{CARNetweb=[0-9a-f]+}{}gs;
92                  my $dom = pQuery( $body );  
93  #               warn dump( $dom->find("body") );  =for pQuery
94                  $dom->find(".navigation")->each( sub {  
95                          my $html = $_->innerHTML;          my $dom = pQuery( $body );
96                          warn $html;  #       warn dump( $dom->find("body") );
97  #                       $_->innerHTML(qq{          $dom->find(".navigation")->each( sub {
98  #                               <div style="border: 3px dashed black;">$html</div>                  my $html = $_->innerHTML;
99  #                       });                  warn $html;
100                  } );  #               $_->innerHTML(qq{
101    #                       <div style="border: 3px dashed black;">$html</div>
102    #               });
103            } );
104    
105  #               $body = $dom->toHtml;  #       $body = $dom->toHtml;
106    
107          }  =cut
108    
109          warn "<< ", $c->req->path,          my $dom = HTML::Query->new(
110                  " ", -s $path,                  text => $body,
111                  " ", $c->res->content_type,                  'body',
112                  " ", $c->req->params ? dump( $c->req->params ) : '',          );
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";                  "\n";
120    
121  =for later  =for later
# Line 114  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.174  
changed lines
  Added in v.224

  ViewVC Help
Powered by ViewVC 1.1.26