/[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 175 by dpavlin, Sat Aug 16 23:37:42 2008 UTC revision 196 by dpavlin, Tue Oct 28 19:34:10 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    
16  has 'uri' => (  has 'uri' => (
17          is => 'rw',          is => 'rw',
18          isa => 'Uri', coerce => 1,          isa => 'Uri', coerce => 1,
# Line 12  has 'uri' => ( Line 21  has 'uri' => (
21    
22  has 'mirror' => (  has 'mirror' => (
23          is => 'rw',          is => 'rw',
24          isa => 'Boolean',          isa => 'Bool',
25            default => 1,
26    );
27    
28    has 'resolve_links' => (
29            is => 'rw',
30            isa => 'Bool',
31            default => 1,
32  );  );
33    
34  #use String::TT qw/strip tt/;  #use String::TT qw/strip tt/;
35    
36  use pQuery;  #use pQuery;
37    use HTML::Query;
38  use File::Slurp;  use File::Slurp;
39  use LWP::Simple ();  use LWP::Simple qw/get/;
40  use File::Path;  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 = 'templates/www.carnet.hr/' . $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 ) = @_;
53          return if -e $path;  
54            my $path = $self->path;
55            my $body;
56    
57            if ( ! -e $path && $self->mirror ) {
58    
59                    my $base_path = $path;
60                    $base_path =~ s{/[^/]+$}{};
61                    mkpath $base_path if ! -e $base_path;
62    
63          return unless $self->mirror;                  my $url = $self->uri;
64                    warn ">> mirror $url -> $path\n";
65    
66          my $url = $self->uri . '/' . $c->req->path . '?' . $c->req->uri->query;                  $body = get( $url ) or die "can't mirror $url: $!";
67    
68          my $base_path = $path;                  if ( $self->resolve_links ) {
69          $base_path =~ s{/[^/]+$}{};                          my $resolver = HTML::ResolveLink->new( base => $url );
70          mkpath $base_path if ! -e $base_path;                          $body = $resolver->resolve( $body );
71                    }
72    
73          warn ">> mirror $url -> $path\n";                  write_file( $path, $body );
74                    warn "WW mirror $url -> $path ", -s $path, " bytes\n";
75    
76          LWP::Simple::mirror( $url, $path ) or die "can't mirror $url: $!";          } else {
77                    $body = read_file( $path );
78            }
79    
80            warn "# $path ", -s $path, " == ", length($body), "bytes";
81            return $body;
82  }  }
83    
84  sub handler {  sub html {
85      my ( $self, $c ) = @_;      my ( $self, $req ) = @_;
86    
     my $req_dump = dump( $c->req );  
     my $raw      = $c->req->raw_body;  
87      my $body;      my $body;
88    
89          my $path = template_path( $c->req->path );          if ( $self->uri->path =~ m{/__bookmarklet} ) {
   
         if ( $path =~ m{/__bookmarklet} ) {  
                 $c->res->content_type( "text/html" );  
90                  my $js = read_file( 'static/xpath.js' );                  my $js = read_file( 'static/xpath.js' );
91                  $js =~ s{//.*}{}gm;     # remove comments so that compaction below doesn't screw code                  $js =~ s{//.*}{}gm;     # remove comments so that compaction below doesn't screw code
92                  $js =~ s/\s\s+/ /gs;                  $js =~ s/\s\s+/ /gs;
93          $c->res->body(qq{          $req->print(qq{
94                          drag this look to bookmark <a href="javascript:void($js);">xpath?</a> to install XPATH inspector                          Drag this <a href="javascript:void($js);">bookmarklet</a> to bookmark toolbar or menu to install XPATH inspector
                         <p><a href="http://www.google.com/">link test</a>  
95                  });                  });
96                  return;                  return;
97          }          }
98    
99          $path .= '.html' if $path !~ m/\.\w+$/;          $body .= $self->get_page;
100    
101            # strip full hostname
102          my $url = $self->uri;          my $url = $self->uri;
103            $body =~ s{\Q$url\E}{/}gs;
104            # remove cookie variable from url
105            $body =~ s{CARNetweb=[0-9a-f]+}{}gs;
106    
107    =for pQuery
108    
109            my $dom = pQuery( $body );
110    #       warn dump( $dom->find("body") );
111            $dom->find(".navigation")->each( sub {
112                    my $html = $_->innerHTML;
113                    warn $html;
114    #               $_->innerHTML(qq{
115    #                       <div style="border: 3px dashed black;">$html</div>
116    #               });
117            } );
118    
119          $self->mirror_design( $c, $path );  #       $body = $dom->toHtml;
   
         $body .= read_file( $path );  
120    
121          if ( $path =~ m/\.css$/ ) {  =cut
                 $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{  
 #                               <div style="border: 3px dashed black;">$html</div>  
 #                       });  
                 } );  
   
 #               $body = $dom->toHtml;  
   
         }  
122    
123          warn "<< ", $c->req->path,          my $dom = HTML::Query->new(
124                  " ", -s $path,                  text => $body,
125                  " ", $c->res->content_type,                  'body',
126                  " ", $c->req->params ? dump( $c->req->params ) : '',          );
127    #       warn dump( $dom->as_HTML );
128    #       $body = $dom->as_HTML->[0];
129    
130            warn "<< ", $self->uri,
131                    " ", -s $self->path,
132                    " ", $req->params ? dump( $req->params ) : '',
133                  "\n";                  "\n";
134    
135  =for later  =for later
# Line 126  sub handler { Line 151  sub handler {
151    
152  =cut  =cut
153    
154      $c->res->body($body);          warn $body;
155    
156            $req->print( $self->page( title => $self->uri, body => $body ) );
157  }  }
158    
159  1;  1;

Legend:
Removed from v.175  
changed lines
  Added in v.196

  ViewVC Help
Powered by ViewVC 1.1.26