Permalink
Browse files

add simple preview for layouts - moved to simpler parser for template…

… rendering
  • Loading branch information...
jgsmith committed Sep 1, 2012
1 parent 9763b49 commit 44535dc29a3e1e91688abc3228a859ab967e6a66
View
@@ -19,6 +19,7 @@ use Catalyst::Runtime 5.80;
#-Debug
use Catalyst qw/
+ -Debug
ConfigLoader
Static::Simple
@@ -1,103 +1,54 @@
-package OokOok::Base::TagLibrary;
+use MooseX::Declare;
-use Moose;
-use MooseX::Types::Moose qw( ArrayRef );
-use namespace::autoclean;
+class OokOok::Base::TagLibrary {
+ use MooseX::Types::Moose qw( ArrayRef );
-use XML::LibXML;
+ has c => ( is => 'rw', isa => 'Maybe[Object]',);
-has c => (
- is => 'rw',
- isa => 'Maybe[Object]',
-);
+ has date => ( is => 'rw',);
-has date => (
- is => 'rw',
-);
+ method process_node ($ctx, $node) {
+ # we want to pull out attributes and such based on needs of tag
+ my $name = $node -> {local};
+ my $einfo = $self -> meta -> element( $name );
+ return if !$einfo; # TODO: perhaps throw an error if not a defined element
-sub process_node {
- my($self, $ctx, $node) = @_;
+ return if !$einfo -> {impl}; # Nothing to do, so don't do anything
- # we want to pull out attributes and such based on needs of tag
- my $name = (split(":", $node -> nodeName, 2))[1];
- my $einfo = $self -> meta -> element( $name );
- return if !$einfo; # TODO: perhaps throw an error if not a defined element
-
- return if !$einfo -> {impl}; # Nothing to do, so don't do anything
-
- my $context = $ctx -> localize;
- my $xmlns;
- my $attributes = {};
- for my $ns (keys %{$einfo->{attributes}}) {
- $xmlns = $ns;
- if($ns eq '') {
- $xmlns = $self -> meta -> namespace;
- }
- for my $a (keys %{$einfo->{attributes}->{$ns}||{}}) {
- my $value = $node -> getAttributeNS($xmlns, $a);
- if($einfo->{attributes}->{$ns}->{$a} eq 'Str') {
- if(defined $value) {
- $attributes->{$a} = $value;
- }
- }
- elsif($einfo->{attributes}->{$ns}->{$a} eq 'Bool') {
- if(defined($value) && $value =~ m{^\s*(yes|true|on|1)\s*$}i) {
- $attributes->{$a} = 1;
- }
- else {
- $attributes->{$a} = 0;
- }
+ my $context = $ctx -> localize;
+ my $xmlns;
+ my $attributes = {};
+ for my $ns (keys %{$einfo->{attributes}}) {
+ $xmlns = $ns;
+ if($ns eq '') {
+ $xmlns = $self -> meta -> namespace;
}
- }
- }
-
- my $content;
- if($einfo -> {uses_content} && $node -> hasChildNodes() ) {
- $content = sub {
- my $collector = [];
- my $child = $node -> firstChild;
- while($child) {
- my $r = $context -> process_node( $child );
- if(ref $r) {
- if(is_ArrayRef($r)) {
- push @$collector, @$r;
+ $xmlns = $context -> get_prefix($xmlns);
+ for my $a (keys %{$einfo->{attributes}->{$ns}||{}}) {
+ my $value = $node -> {attrs} -> {$xmlns} -> {$a};
+ if($einfo->{attributes}->{$ns}->{$a} eq 'Str') {
+ if(defined $value) {
+ $attributes->{$a} = $value;
}
- elsif($r -> isa('XML::LibXML::Node')) {
- push @$collector, $r;
+ }
+ elsif($einfo->{attributes}->{$ns}->{$a} eq 'Bool') {
+ if(defined($value)) {
+ $attributes->{$a} = map {
+ m{^\s*(yes|true|on|1)\s*$} ? 1 : 0
+ } @{$value};
}
}
- $child = $child -> nextSibling;
}
- return $collector;
- };
- }
-
- my $impl = $einfo -> {impl};
- my $r;
- if(ref $impl) {
- $r = $einfo -> {impl} -> ($self, $context, $attributes, $content);
- }
- else {
- $r = $self -> $impl($context, $attributes, $content);
- }
-
- if(defined($r) && !ref $r) { # $r is a string
- if(!defined($einfo -> {escape_text}) || $einfo->{escape_text}) {
- $r = $node -> ownerDocument -> createTextNode( $r );
- }
- elsif($r ne '') {
- $r =~ s{&}{&}g;
- $r = XML::LibXML -> new(
- expand_entities => 0,
- xinclude => 0,
- load_ext_dtd => 0,
- ) -> parse_balanced_chunk( $r );
}
- else {
- $r = undef;
+
+ my $content;
+ if($einfo -> {uses_content} && @{$node -> {children}||[]}) {
+ $content = sub {
+ $context -> process_node( $node -> {children} || [''] );
+ };
}
+
+ my $impl = $einfo -> {impl};
+ $self -> $impl($context, $attributes, $content);
}
- $r;
}
-
-1;
@@ -198,6 +198,22 @@ controller OokOok::Controller::Admin::Theme
}
$ctx -> stash -> {template} = "/admin/theme/content/layout/edit";
}
+
+ final action theme_layout_preview as 'preview' {
+ my $context = OokOok::Template::Context -> new(
+ c => $ctx,
+ is_mockup => 1,
+ );
+ my $layout = $ctx -> stash -> {theme_layout};
+ $ctx -> stash -> {rendering} = $layout -> render($context);
+ $ctx -> stash -> {stylesheets} = [
+ map {
+ $ctx -> uri_for( "/ts/".$layout->theme->id."/style/$_" )
+ } $layout -> stylesheets
+ ];
+ $ctx -> stash -> {template} = "view/play.tt2";
+ $ctx -> forward( $ctx -> view('HTML') );
+ }
}
under theme_style_base {
@@ -16,6 +16,35 @@ controller OokOok::Controller::Style
c => $ctx,
);
}
+
+ action tbase as 'ts' {
+ $ctx -> stash -> {collection} = OokOok::Collection::Theme -> new(
+ c => $ctx,
+ );
+ }
+ }
+
+ under tbase {
+ action play_tbase ($uuid) as '' {
+ $self -> play_base($ctx, $uuid);
+ }
+ }
+
+ under play_tbase {
+ action tstyle_base ($uuid) as 'style' {
+ my $theme = $ctx -> stash -> {theme};
+ my $style = $theme -> style($uuid);
+
+ if(!$style) {
+ $ctx -> detach(qw/Controller::Root default/);
+ }
+
+ $ctx -> stash -> {resource} = $style;
+ }
+ }
+
+ under tstyle_base {
+ final action tstyle as '' isa REST;
}
under play_base {
@@ -41,4 +70,8 @@ controller OokOok::Controller::Style
$ctx -> stash -> {template} = 'style/style.tt2';
$ctx -> forward( $ctx -> view('HTML') );
}
+
+ method tstyle_GET ($ctx) {
+ $self -> style_GET($ctx);
+ }
}
@@ -153,20 +153,11 @@ sub can_DELETE {
sub stylesheets {
my($self) = @_;
- #my $theme = $self -> project -> theme;
- #return unless $theme;
- #my $layout_uuid = $self -> source_version -> layout;
- #my $layout = $theme -> layout( $layout_uuid );
my $layout = $self -> get_layout;
- my @stylesheets;
- while($layout) {
- my $s = $layout -> theme_style;
- if($s) {
- push @stylesheets, $s -> id;
- }
- $layout = $layout -> parent_layout;
+ if($layout) {
+ return $layout -> stylesheets;
}
- return reverse @stylesheets;
+ return;
}
sub get_layout {
@@ -53,6 +53,20 @@ sub can_PUT {
$self -> theme -> can_PUT;
}
+sub stylesheets {
+ my($layout) = @_;
+
+ my @stylesheets;
+ while($layout) {
+ my $s = $layout -> theme_style;
+ if($s) {
+ push @stylesheets, $s -> id;
+ }
+ $layout = $layout -> parent_layout;
+ }
+ return reverse @stylesheets;
+}
+
#
# For now, we place the rendering stuff here since it depends on the
# Catalyst context, which means we want to avoid putting it in the
@@ -64,28 +78,22 @@ sub render {
my $layout = $self -> source_version;
if($layout) {
my $template = $layout -> layout;
- my $processor = OokOok::Template::Processor -> new(
- c => $self -> c,
- );
# now load in taglibs and prefixes
+
my %ns;
for my $lib ($self -> theme -> libraries) {
my $prefix = $lib -> prefix;
my $uin = "uin:uuid:" . $lib -> id;
$ns{$prefix} = $uin;
}
- my $div = "<div";
- for my $p (keys %ns) {
- $div .= " xmlns:$p='" . $ns{$p} . "'";
- }
- $div .= ">";
- my $doc = $processor -> parse($div . $template . "</div>");
- my $ret = $doc -> render($context);
- # we want to remove the outer div
- #print STDERR "Returned doc: [$ret]\n";
- $ret =~ s{\s*<div.*?>}{}s;
- $ret =~ s{</div>$}{}s;
- #print STDERR "After regex doc: [$ret]\n";
+
+ my $processor = OokOok::Template::Processor -> new(
+ c => $self -> c,
+ namespaces => \%ns,
+ );
+
+ my $ret = $processor -> parse($template) -> render($context);
+
# now worry about parent layout
if($self -> parent_layout) {
$context = $context -> localize;
Oops, something went wrong.

0 comments on commit 44535dc

Please sign in to comment.