Skip to content

Commit

Permalink
add simple preview for layouts - moved to simpler parser for template…
Browse files Browse the repository at this point in the history
… rendering
  • Loading branch information
jgsmith committed Sep 1, 2012
1 parent 9763b49 commit 44535dc
Show file tree
Hide file tree
Showing 13 changed files with 588 additions and 351 deletions.
1 change: 1 addition & 0 deletions lib/OokOok.pm
Expand Up @@ -19,6 +19,7 @@ use Catalyst::Runtime 5.80;


#-Debug #-Debug
use Catalyst qw/ use Catalyst qw/
-Debug
ConfigLoader ConfigLoader
Static::Simple Static::Simple
Expand Down
127 changes: 39 additions & 88 deletions lib/OokOok/Base/TagLibrary.pm
@@ -1,103 +1,54 @@
package OokOok::Base::TagLibrary; use MooseX::Declare;


use Moose; class OokOok::Base::TagLibrary {
use MooseX::Types::Moose qw( ArrayRef ); use MooseX::Types::Moose qw( ArrayRef );
use namespace::autoclean;


use XML::LibXML; has c => ( is => 'rw', isa => 'Maybe[Object]',);


has c => ( has date => ( is => 'rw',);
is => 'rw',
isa => 'Maybe[Object]',
);


has date => ( method process_node ($ctx, $node) {
is => 'rw', # 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 { return if !$einfo -> {impl}; # Nothing to do, so don't do anything
my($self, $ctx, $node) = @_;


# we want to pull out attributes and such based on needs of tag my $context = $ctx -> localize;
my $name = (split(":", $node -> nodeName, 2))[1]; my $xmlns;
my $einfo = $self -> meta -> element( $name ); my $attributes = {};
return if !$einfo; # TODO: perhaps throw an error if not a defined element for my $ns (keys %{$einfo->{attributes}}) {

$xmlns = $ns;
return if !$einfo -> {impl}; # Nothing to do, so don't do anything if($ns eq '') {

$xmlns = $self -> meta -> namespace;
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;
}
} }
} $xmlns = $context -> get_prefix($xmlns);
} for my $a (keys %{$einfo->{attributes}->{$ns}||{}}) {

my $value = $node -> {attrs} -> {$xmlns} -> {$a};
my $content; if($einfo->{attributes}->{$ns}->{$a} eq 'Str') {
if($einfo -> {uses_content} && $node -> hasChildNodes() ) { if(defined $value) {
$content = sub { $attributes->{$a} = $value;
my $collector = [];
my $child = $node -> firstChild;
while($child) {
my $r = $context -> process_node( $child );
if(ref $r) {
if(is_ArrayRef($r)) {
push @$collector, @$r;
} }
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;
16 changes: 16 additions & 0 deletions lib/OokOok/Controller/Admin/Theme.pm
Expand Up @@ -198,6 +198,22 @@ controller OokOok::Controller::Admin::Theme
} }
$ctx -> stash -> {template} = "/admin/theme/content/layout/edit"; $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 { under theme_style_base {
Expand Down
33 changes: 33 additions & 0 deletions lib/OokOok/Controller/Style.pm
Expand Up @@ -16,6 +16,35 @@ controller OokOok::Controller::Style
c => $ctx, 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 { under play_base {
Expand All @@ -41,4 +70,8 @@ controller OokOok::Controller::Style
$ctx -> stash -> {template} = 'style/style.tt2'; $ctx -> stash -> {template} = 'style/style.tt2';
$ctx -> forward( $ctx -> view('HTML') ); $ctx -> forward( $ctx -> view('HTML') );
} }

method tstyle_GET ($ctx) {
$self -> style_GET($ctx);
}
} }
15 changes: 3 additions & 12 deletions lib/OokOok/Resource/Page.pm
Expand Up @@ -153,20 +153,11 @@ sub can_DELETE {
sub stylesheets { sub stylesheets {
my($self) = @_; 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 $layout = $self -> get_layout;
my @stylesheets; if($layout) {
while($layout) { return $layout -> stylesheets;
my $s = $layout -> theme_style;
if($s) {
push @stylesheets, $s -> id;
}
$layout = $layout -> parent_layout;
} }
return reverse @stylesheets; return;
} }


sub get_layout { sub get_layout {
Expand Down
38 changes: 23 additions & 15 deletions lib/OokOok/Resource/ThemeLayout.pm
Expand Up @@ -53,6 +53,20 @@ sub can_PUT {
$self -> theme -> 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 # 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 # Catalyst context, which means we want to avoid putting it in the
Expand All @@ -64,28 +78,22 @@ sub render {
my $layout = $self -> source_version; my $layout = $self -> source_version;
if($layout) { if($layout) {
my $template = $layout -> layout; my $template = $layout -> layout;
my $processor = OokOok::Template::Processor -> new(
c => $self -> c,
);
# now load in taglibs and prefixes # now load in taglibs and prefixes

my %ns; my %ns;
for my $lib ($self -> theme -> libraries) { for my $lib ($self -> theme -> libraries) {
my $prefix = $lib -> prefix; my $prefix = $lib -> prefix;
my $uin = "uin:uuid:" . $lib -> id; my $uin = "uin:uuid:" . $lib -> id;
$ns{$prefix} = $uin; $ns{$prefix} = $uin;
} }
my $div = "<div";
for my $p (keys %ns) { my $processor = OokOok::Template::Processor -> new(
$div .= " xmlns:$p='" . $ns{$p} . "'"; c => $self -> c,
} namespaces => \%ns,
$div .= ">"; );
my $doc = $processor -> parse($div . $template . "</div>");
my $ret = $doc -> render($context); my $ret = $processor -> parse($template) -> 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";
# now worry about parent layout # now worry about parent layout
if($self -> parent_layout) { if($self -> parent_layout) {
$context = $context -> localize; $context = $context -> localize;
Expand Down

0 comments on commit 44535dc

Please sign in to comment.