[Jifty-commit] r3840 - in Template-Declare: lib/Template/Declare t

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Fri Aug 10 07:21:49 EDT 2007


Author: agentz
Date: Fri Aug 10 07:21:45 2007
New Revision: 3840

Modified:
   Template-Declare/lib/Template/Declare/TagSet.pm
   Template-Declare/lib/Template/Declare/TagSet/HTML.pm
   Template-Declare/lib/Template/Declare/Tags.pm
   Template-Declare/t/tagset_html.t

Log:
[TD]
* added support for XML namespace:
   use Template::Declare::Tags 'XUL', 'HTML' => { namespace => 'html' };
 and
  ... 'HTML' => { namespace => 'html', package => 'MyHtml' };
 (Pod needed for this new feature. anyone? ;))
* added @Template::Declare::Tags::TagSubs which records all the tag subroutines
  generated on-the-fly, which is necessary for secondary symbol exporting
  in Jifty::View::Declare::Helpers.
* added a constructor to Template::Declare::TagSet and always use its
 instances rather than the package itself.
* added ro accessors C<package> and C<namespace> to Template::Declare::TagSet
 (and thus both its subclasses) which are constructed by passing the options
 from Template::Declare::Tags's import list.

Modified: Template-Declare/lib/Template/Declare/TagSet.pm
==============================================================================
--- Template-Declare/lib/Template/Declare/TagSet.pm	(original)
+++ Template-Declare/lib/Template/Declare/TagSet.pm	Fri Aug 10 07:21:45 2007
@@ -2,6 +2,11 @@
 
 use strict;
 use warnings;
+use base qw(Class::Accessor::Fast);
+
+__PACKAGE__->mk_ro_accessors(
+    qw{ namespace package implementor }
+);
 
 sub get_alternate_spelling {
     undef;

Modified: Template-Declare/lib/Template/Declare/TagSet/HTML.pm
==============================================================================
--- Template-Declare/lib/Template/Declare/TagSet/HTML.pm	(original)
+++ Template-Declare/lib/Template/Declare/TagSet/HTML.pm	Fri Aug 10 07:21:45 2007
@@ -28,8 +28,8 @@
 
 sub can_combine_empty_tags {
     my ($self, $tag) = @_;
-    $tag
-        =~ m{\A(?: base | meta | link | hr | br | param | img | area | input | col )\z}x;
+    return $tag
+        =~ m{^ (?: base | meta | link | hr | br | param | img | area | input | col ) $}x ? 1 : 0;
 }
 
 1;

Modified: Template-Declare/lib/Template/Declare/Tags.pm
==============================================================================
--- Template-Declare/lib/Template/Declare/Tags.pm	(original)
+++ Template-Declare/lib/Template/Declare/Tags.pm	Fri Aug 10 07:21:45 2007
@@ -2,6 +2,7 @@
 use warnings;
 use strict;
 #use Smart::Comments '####';
+#use Smart::Comments '#####';
 
 package Template::Declare::Tags;
 
@@ -17,6 +18,10 @@
           get_current_attr xml_decl
           smart_tag_wrapper current_template );
 
+# XXX TODO: Put @TagSubs into POD
+# record all the subs for XML tags generated on-the-fly
+our @TagSubs;
+
 our %ATTRIBUTES       = ();
 our %ELEMENT_ID_CACHE = ();
 our $TAG_NEST_DEPTH            = 0;
@@ -30,8 +35,15 @@
     if (!@_) {
         push @_, 'HTML';
     }
+    @TagSubs = ();
     while (@_) {
         my $lang = shift;
+        my $opts = { package => scalar(caller) };
+        if (ref $_[0] and ref $_[0] eq 'HASH') {
+            $opts = shift;
+            $opts->{package} ||= $opts->{namespace};
+            ### XXX TODO: carp if the derived package already exists?
+        }
         my $module = "Template::Declare::TagSet::$lang";
 
         eval "use $module";
@@ -39,9 +51,11 @@
             warn $@;
             croak "Failed to load tagset module $module";
         }
-        my $tags = $module->get_tag_list;
-        Template::Declare::Tags::install_tag($_, $module)
-            for @$tags;
+        ##### TagSet options: $opts
+        my $tagset = $module->new($opts);
+        my $tag_list = $tagset->get_tag_list;
+        Template::Declare::Tags::install_tag($_, $tagset)
+            for @$tag_list;
     }
    __PACKAGE__->export_to_level(1, $self);
 }
@@ -49,7 +63,7 @@
 sub _install {
     my ($no_override, $package, $subname, $coderef) = @_;
 
-    ### Installing sub: $subname
+    ###### Installing sub: $subname
 
     my $name = $package . '::' . $subname;
     my $slot = qualify_to_ref($name);
@@ -275,6 +289,10 @@
     my $name = $tag;
     my $tagset = $_[1];
 
+    # XXX We treat the 'link' tag specially...
+    # Yeah, maybe we should do it for all the perl builtins...
+    # I know this is hacky...
+
     ### caller: (caller(0))[0]
     my $alternative = $tagset->get_alternate_spelling($tag);
     if ( defined $alternative ) {
@@ -287,17 +305,23 @@
         );
         #### Exporting place-holder sub: $name
         # XXX TODO: more checking here
-        push @EXPORT, $name unless $name =~ /^(?:base|tr)$/;
+        if ($name !~ /^(?:base|tr)$/) {
+            push @EXPORT, $name;
+            push @TagSubs, $name;
+        }
         $name = $alternative or return;
     }
 
-    push @EXPORT, $name;
+    # We don't need this since we directly install
+    # subs into the target package.
+    #push @EXPORT, $name;
+    push @TagSubs, $name;
 
     no strict 'refs';
     no warnings 'redefine';
     #warn "Installing tag $name..." if $name eq 'base';
     # XXX TODO: use sub _install to insert subs into the caller's package so as to support XML packages
-    *$name = sub (&;$) {
+    my $code  = sub (&;$) {
         local *__ANON__ = $tag;
         if ( defined wantarray and not wantarray ) {
 
@@ -315,6 +339,12 @@
             _tag($tagset, @_);
         }
     };
+    ##### package: $tagset->package
+    ##### sub name: $name
+    _install(
+        0, # do override the existing sub with the same name
+        $tagset->package => $name => $code
+    );
 }
 
 
@@ -421,7 +451,9 @@
 
     my $tag = $subroutine;
     $tag =~ s/^.*\:\://;
-    #$tag = "html:$tag";
+    # "html:foo"
+    $tag = $tagset->namespace . ":$tag"
+        if defined $tagset->namespace;
 
     my $buf = "\n" . ( " " x $TAG_NEST_DEPTH ) . "<$tag"
         . join( '',
@@ -481,7 +513,7 @@
     if ($had_content) {
         $buf .= "\n" . ( " " x $TAG_NEST_DEPTH ) if ( $buf =~ /\>$/ );
         $buf .= "</$tag>";
-    } elsif ( $tagset->can_combine_empty_tags($tag)) {
+    } elsif ( $tagset->can_combine_empty_tags($tag) ) {
         $buf .= " />";
     } else {
         # Otherwise we supply a closing tag.

Modified: Template-Declare/t/tagset_html.t
==============================================================================
--- Template-Declare/t/tagset_html.t	(original)
+++ Template-Declare/t/tagset_html.t	Fri Aug 10 07:21:45 2007
@@ -7,6 +7,8 @@
 use Template::Declare::Tags qw/ HTML /;
 
 template main => sub {
+    caption { attr { id => 'a' } }
+    link {};
     table {
         row {
             cell { "Hello, world!" }
@@ -17,11 +19,20 @@
 };
 
 package main;
-use Test::More tests => 1;
+use Test::More tests => 4;
+use Template::Declare::TagSet::HTML;
+
+my $tagset = Template::Declare::TagSet::HTML->new();
+ok $tagset->can_combine_empty_tags('img'), '<img />';
+ok !$tagset->can_combine_empty_tags('label'), '<label></label>';
+ok !$tagset->can_combine_empty_tags('caption'), '<caption></caption>';
+
 Template::Declare->init( roots => ['MyApp::Templates']);
 my $out = Template::Declare->show('main') . "\n";
 is $out, <<_EOC_;
 
+<caption id="a"></caption>
+<link />
 <table>
  <tr>
   <td>Hello, world!</td>


More information about the Jifty-commit mailing list