[Bps-public-commit] r15785 - in Prophet/trunk/lib/Prophet: CLI/Command Server

jesse at bestpractical.com jesse at bestpractical.com
Sun Sep 7 21:38:16 EDT 2008


Author: jesse
Date: Sun Sep  7 21:38:14 2008
New Revision: 15785

Added:
   Prophet/trunk/lib/Prophet/Server/ViewHelpers.pm
Modified:
   Prophet/trunk/lib/Prophet/CLI/Command/Publish.pm
   Prophet/trunk/lib/Prophet/CLI/Command/Server.pm
   Prophet/trunk/lib/Prophet/Server.pm
   Prophet/trunk/lib/Prophet/Server/View.pm

Log:
* Working to improve HTML templates to make them subclassable.



Modified: Prophet/trunk/lib/Prophet/CLI/Command/Publish.pm
==============================================================================
--- Prophet/trunk/lib/Prophet/CLI/Command/Publish.pm	(original)
+++ Prophet/trunk/lib/Prophet/CLI/Command/Publish.pm	Sun Sep  7 21:38:14 2008
@@ -6,6 +6,10 @@
 
 use Path::Class;
 
+sub view_classes {
+ return ['Prophet::Server::View'];
+}
+
 before run => sub {
     my $self = shift;
     die "Please specify a --to.\n" unless $self->has_arg('to');
@@ -26,24 +30,11 @@
     $export_replica = 1 if !$export_html;
 
     # if we have the html argument, populate the tempdir with rendered templates
-    if ($export_html) {
-        my $path = dir($self->arg('path'));
-
-        # if they specify both html and replica, then stick rendered templates
-        # into a subdirectory. if they specify only html, assume they really
-        # want to publish directly into the specified directory
-        if ($export_replica) {
-            $path = $path->subdir('html');
-            $path->mkpath;
-        }
-
-        $self->render_templates_into($path);
-    }
+    $self->export_html() if ($export_html);
 
     # otherwise, do the normal prophet export this replica
-    if ($export_replica) {
-        $self->$orig(@_);
-    }
+        $self->$orig(@_) if ($export_replica);
+    
 };
 
 # the tempdir is populated, now publish it
@@ -60,13 +51,28 @@
     print "Publish complete.\n";
 };
 
+sub export_html {
+	my $self = shift;
+        my $path = dir($self->arg('path'));
+
+        # if they specify both html and replica, then stick rendered templates
+        # into a subdirectory. if they specify only html, assume they really
+        # want to publish directly into the specified directory
+        if ($self->has_arg('replica')){
+            $path = $path->subdir('html');
+            $path->mkpath;
+        }
+
+        $self->render_templates_into($path);
+    }
+
 # helper methods for rendering templates
 sub render_templates_into {
     my $self = shift;
     my $dir  = shift;
 
     require Prophet::Server::View;
-    Template::Declare->init(roots => ['Prophet::Server::View']);
+    Template::Declare->init(roots => __PACKAGE__->view_classes);
 
     # allow user to specify a specific type to render
     my @types = $self->type || $self->types_to_render;

Modified: Prophet/trunk/lib/Prophet/CLI/Command/Server.pm
==============================================================================
--- Prophet/trunk/lib/Prophet/CLI/Command/Server.pm	(original)
+++ Prophet/trunk/lib/Prophet/CLI/Command/Server.pm	Sun Sep  7 21:38:14 2008
@@ -14,6 +14,7 @@
     my $self = shift;
     my $server = Prophet::Server->new( $self->arg('port') || 8080 );
     $server->app_handle( $self->app_handle );
+    $server->setup_template_roots();
     return $server;
 }
 

Modified: Prophet/trunk/lib/Prophet/Server.pm
==============================================================================
--- Prophet/trunk/lib/Prophet/Server.pm	(original)
+++ Prophet/trunk/lib/Prophet/Server.pm	Sun Sep  7 21:38:14 2008
@@ -32,9 +32,18 @@
     }
 };
 
-before new => sub {
-    Template::Declare->init(roots => ['Prophet::Server::View']);
-};
+sub setup_template_roots {
+    my $self = shift;
+    my $view_class = ref( $self->app_handle ) . "::Server::View";
+
+    if ( Prophet::App->try_to_require($view_class) ) {
+        Template::Declare->init( roots => [$view_class] );
+
+    }
+    else {
+        Template::Declare->init( roots => ['Prophet::Server::View'] );
+    }
+}
 
 override handle_request => sub {
     my ($self, $cgi) = validate_pos( @_, { isa => 'Prophet::Server'} ,  { isa => 'CGI' } );
@@ -52,20 +61,22 @@
     my ($cgi) = validate_pos( @_, { isa => 'CGI' } );
     my $p = $cgi->path_info;
 
+    if ($p =~ qr|^/+replica|) {
 
-    if ($p =~ qr{^/+replica/+(.*)$}) {
-        my $repo_file = $1;
-        return undef unless $self->handle->can('read_file');
+		$self->_handle_request_get_replica($cgi);
+	}
+    if ($p =~ m|^/+records|) {
+	$self->_handle_request_get_rest($cgi);			
+    }
 
-       my $content = $self->handle->read_file($repo_file);
-       return unless length($content);
-       return $self->_send_content(
-            content_type => 'application/x-prophet',
-            content      => $content
-        );
+    $self->_handle_request_get_template($cgi);
+}
 
+sub _handle_request_get_template {
+   my $self = shift;
+    my ($cgi) = validate_pos( @_, { isa => 'CGI' } );
+    my $p = $cgi->path_info;
 
-    }
 
     if (Template::Declare->has_template($p)) {
         my $content = Template::Declare->show($p);
@@ -74,8 +85,35 @@
             content_type => 'text/html',
             content      => $content,
         );
+
     }
 
+}
+
+sub _handle_request_get_replica {
+	my $self = shift;
+    my ($cgi) = validate_pos( @_, { isa => 'CGI' } );
+    my $p = $cgi->path_info;
+
+
+    if ($p =~ qr{^/+replica/+(.*)$}) {
+        my $repo_file = $1;
+        return undef unless $self->handle->can('read_file');
+
+       my $content = $self->handle->read_file($repo_file);
+       return unless length($content);
+       return $self->_send_content(
+            content_type => 'application/x-prophet',
+            content      => $content
+        );
+    }
+}
+
+sub _handle_request_get_rest {
+	my $self = shift;
+    my ($cgi) = validate_pos( @_, { isa => 'CGI' } );
+    my $p = $cgi->path_info;
+
     if ( $p =~ m|^/records\.json$| ) {
         $self->_send_content(
             content_type => 'text/x-json',
@@ -113,7 +151,6 @@
             content_type => 'text/x-json',
             content      => to_json( { map { $_->uuid => "/records/$type/" . $_->uuid . ".json" } @$col } )
             )
-
     }
 }
 

Modified: Prophet/trunk/lib/Prophet/Server/View.pm
==============================================================================
--- Prophet/trunk/lib/Prophet/Server/View.pm	(original)
+++ Prophet/trunk/lib/Prophet/Server/View.pm	Sun Sep  7 21:38:14 2008
@@ -1,17 +1,27 @@
-#!/usr/bin/env perl
-package Prophet::Server::View;
 use strict;
 use warnings;
+package Prophet::Server::View;
 use base 'Template::Declare';
 use Template::Declare::Tags;
+use Prophet::Server::ViewHelpers;
 use Params::Validate;
 
-template '/' => sub {
-    html {
-        body {
-            h1 { "Welcome!" }
-        }
+sub default_page_title { 'Prophet' }
+
+template head => sub {
+    my $self = shift;
+    my $args = shift;
+    head {
+        title { shift @$args };
     }
+
+};
+
+template footer => sub {};
+
+
+template '/' => page {
+            h1 { "This is a Prophet replica!" }
 };
 
 sub record_table {
@@ -69,23 +79,18 @@
     }
 }
 
-template record_table => sub {
+template record_table => 
+
+        page {
     my $self = shift;
     my $records = shift;
-
-    html {
-        body {
             record_table(records => $records);
-        }
-    }
 };
 
-template record => sub {
+template record => page {
     my $self = shift;
     my $record = shift;
 
-    html {
-        body {
             p {
                 a {
                     attr {
@@ -129,11 +134,9 @@
                 );
             }
 
-        }
-    }
 };
 
-template record_changesets => sub {
+private template record_changesets => sub {
     my $self = shift;
     my $record = shift;
     my $uuid = $record->uuid;
@@ -159,7 +162,7 @@
             }
         }
     }
-};
+        };
 
 sub generate_changeset_feed {
     my $self = shift;

Added: Prophet/trunk/lib/Prophet/Server/ViewHelpers.pm
==============================================================================
--- (empty file)
+++ Prophet/trunk/lib/Prophet/Server/ViewHelpers.pm	Sun Sep  7 21:38:14 2008
@@ -0,0 +1,38 @@
+use warnings;
+use strict;
+
+
+package Prophet::Server::ViewHelpers;
+use base 'Exporter::Lite';
+use Template::Declare::Tags;
+our @EXPORT = qw(page content);
+
+sub page (&;$) {
+    unshift @_, undef if $#_ == 0;
+    my ( $meta, $code ) = @_;
+
+    sub {
+        my $self  = shift;
+        my @args  = @_;
+        my $title = $self->default_page_title;
+        $title = $meta->( $self, @args ) if $meta;
+        html {
+            attr { xmlns => 'http://www.w3.org/1999/xhtml' };
+            show( 'head' => $title );
+            body {
+                h1 { $title };
+                $code->( $self, @args );
+            };
+            show('footer');
+        }
+
+      }
+}
+
+sub content (&) {
+    my $sub_ref = shift;
+    return $sub_ref;
+}
+
+
+1;



More information about the Bps-public-commit mailing list