TT support
Matt S Trout [Sat, 11 Aug 2012 17:08:14 +0000 (17:08 +0000)]
lib/HTML/String/Overload.pm
lib/HTML/String/TT.pm [new file with mode: 0644]
lib/HTML/String/TT/Directive.pm [new file with mode: 0644]
lib/HTML/String/Value.pm
t/simple.t

index e5f7c72..1191c4a 100644 (file)
@@ -3,13 +3,15 @@ package HTML::String::Overload;
 use strictures 1;
 use HTML::String;
 use B::Hooks::EndOfScope;
-use overload '';
+use overload ();
 
 sub import {
-  overload::constant q => \&html;
-  on_scope_end {
+    overload::constant q => \&html;
+    on_scope_end { __PACKAGE__->unimport };
+}
+
+sub unimport {
     overload::remove_constant('q');
-  }
 }
 
 1;
diff --git a/lib/HTML/String/TT.pm b/lib/HTML/String/TT.pm
new file mode 100644 (file)
index 0000000..fb1f1cb
--- /dev/null
@@ -0,0 +1,20 @@
+package HTML::String::TT;
+
+use strictures 1;
+use Template;
+use Template::Parser;
+use Template::Stash;
+use HTML::String::TT::Directive;
+
+sub new {
+    shift;
+    Template->new(
+        PARSER => Template::Parser->new(
+            FACTORY => 'HTML::String::TT::Directive'
+        ),
+        STASH => Template::Stash->new,
+        @_
+    );
+}
+
+1;
diff --git a/lib/HTML/String/TT/Directive.pm b/lib/HTML/String/TT/Directive.pm
new file mode 100644 (file)
index 0000000..11fa963
--- /dev/null
@@ -0,0 +1,13 @@
+package HTML::String::TT::Directive;
+
+use strictures 1;
+use HTML::String::Overload ();
+use base qw(Template::Directive);
+
+sub template {
+    my $result = Template::Directive::pad(shift->SUPER::template(@_), 2);
+    $result =~ s/sub {/sub { use HTML::String::Overload;/;
+    $result;
+}
+
+1;
index d03b287..d622694 100644 (file)
@@ -23,6 +23,7 @@ use overload
     '""'   => 'escaped_string',
     '.'    => 'dot',
     '.='   => 'dot_equals',
+    '='    => 'clone',
 
     'cmp' => op_factory('cmp'),
     'eq'  => op_factory('eq'),
@@ -109,4 +110,10 @@ sub dot_equals {
     return $self;
 }
 
+sub clone {
+    my $self = shift;
+
+    return ref($self)->new(@{$self->{parts}});
+}
+
 1;
index da35254..b77accd 100644 (file)
@@ -11,7 +11,7 @@ is("$one", '<tag>Hi &lt;bob&gt;</tag>');
 my $two = do {
   use HTML::String::Overload;
 
-  "<tag>${hi}</tag>"
+  "<tag>${hi}</tag>";
 };
 
 is("$two", '<tag>Hi &lt;bob&gt;</tag>');