package HTML::String::Overload;
use strictures 1;
-use HTML::String;
+use HTML::String::Value;
use B::Hooks::EndOfScope;
use overload ();
sub import {
- overload::constant q => \&html;
+ my ($class, @opts) = @_;
+ overload::constant q => sub {
+ HTML::String::Value->new($_[1], @opts);
+ };
on_scope_end { __PACKAGE__->unimport };
}
sub template {
return byval {
- s/sub {/sub { use HTML::String::Overload;/;
+ s/sub {/sub { package HTML::String::TT::_TMPL; use HTML::String::Overload { ignore => { q{Template::Provider} => 1, q{Template::Directive} => 1, q{Template::Document} => 1 } };/;
} Template::Directive::pad(shift->SUPER::template(@_), 2);
}
+sub textblock {
+ my ($self, $text) = @_;
+ return $Template::Directive::OUTPUT.' '.$self->text($text).';';
+}
+
+sub text {
+ my ($class, $text) = @_;
+ for ($text) {
+ s/(["\$\@\\])/\\$1/g;
+ s/\n/"."\\n"."/g;
+ }
+ return '"' . $text . '"';
+}
+
1;
use overload
'""' => 'escaped_string',
'.' => 'dot',
+ 'bool' => 'is_true',
fallback => 1,
;
sub new {
my ($class, @raw_parts) = @_;
+ my $opts = (ref($raw_parts[-1]) eq 'HASH') ? pop(@raw_parts) : {};
+
my @parts = map {
if (ref($_) eq 'ARRAY') {
$_
}
} @raw_parts;
- my $self = bless { parts => \@parts }, $class;
+ my $self = bless { parts => \@parts, %$opts }, $class;
return $self;
}
sub escaped_string {
my $self = shift;
+ if ($self->{ignore}{scalar caller}) {
+ return $self->unescaped_string;
+ }
+
return join '', map +(
$_->[1]
? byval {
push @parts, @new_parts;
}
- return ref($self)->new(@parts);
+ return ref($self)->new(@parts, { ignore => $self->{ignore} });
+}
+
+sub is_true {
+ my ($self) = @_;
+ return 1 if grep length($_), map $_->[0], @{$self->{parts}};
}
sub ref { '' }
is("$two", '<tag>Hi <bob></tag>');
+my $three = html('<tag>');
+
+$three .= $hi;
+
+$three .= html('</tag>');
+
+is("$three", '<tag>Hi <bob></tag>');
+
+my $four; {
+ use HTML::String::Overload { ignore => { lies => 1 } };
+
+ #$four = "<tag>".$hi."</tag>\n";
+ $four = "<tag>$hi</tag>"."\n";
+};
+
+chomp($four);
+
+is("$four", '<tag>Hi <bob></tag>');
+
done_testing;