documentationify
[scpubgit/HTML-String.git] / lib / HTML / String / TT / Directive.pm
CommitLineData
ed99cbb4 1package HTML::String::TT::Directive;
2
3use strictures 1;
4use HTML::String::Overload ();
cfb242dd 5use Data::Munge;
ed99cbb4 6use base qw(Template::Directive);
7
8sub template {
cfb242dd 9 return byval {
a31f2ca0 10 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, q{Template::Plugins} => 1 } };/;
cfb242dd 11 } Template::Directive::pad(shift->SUPER::template(@_), 2);
ed99cbb4 12}
13
5c65e9e1 14# TT code does &text(), no idea why
15
f27b509e 16sub textblock {
17 my ($self, $text) = @_;
18 return $Template::Directive::OUTPUT.' '.$self->text($text).';';
19}
20
5c65e9e1 21# https://rt.perl.org/rt3/Ticket/Display.html?id=49594
22
f27b509e 23sub text {
24 my ($class, $text) = @_;
25 for ($text) {
5bee64f9 26 s/(["\$\@\\])/"."\\$1"."/g;
f27b509e 27 s/\n/"."\\n"."/g;
28 }
29 return '"' . $text . '"';
30}
31
ed99cbb4 321;
d86bdf82 33
34__END__
35
36=head1 NAME
37
38HTML::String::TT::Directive - L<Template::Directive> overrides to forcibly escape HTML strings
39
40=head1 SYNOPSIS
41
42This is not user serviceable, and is documented only for your edification.
43
44Please use L<HTML::String::TT> as this module could change, be renamed, or
45if I figure out a better way of implementing the functionality disappear
46entirely at any moment.
47
48=head1 METHODS
49
50=head2 template
51
52We override this top-level method in order to pretend two things to the
53perl subroutine definition that TT has generated - firstly,
54
55 package HTML::String::TT::_TMPL;
56
57to ensure that no packages marked to be ignored are the current one when
58the template code is executed. Secondly,
59
60 use HTML::String::Overload { ignore => { ... } };
61
62where the C<...> contains a list of TT internal packages to ignore so that
63things actually work. This list is not duplicated here since it may also
64change without warning.
65
66Additionally, the hashref option to L<HTML::String::Overload> is not
67documented there since I'm not yet convinced that's a public API either.
68
69=head2 text
70
71Due to a perl bug (L<https://rt.perl.org/rt3/Ticket/Display.html?id=49594>)
72we overload this method to change
73
74 "<foo>\n<bar>"
75
76into
77
78 "<foo>"."\n"."<bar>"
79
80since any string containing a backslash escape doesn't get marked as HTML.
81Since we don't want to escape things that backslash escapes are normally
82used for, this isn't really a problem for us.
83
84=head2 textblock
85
86For no reason I can comprehend at all, L<Template::Directive>'s C<textblock>
87method calls C<&text> instead of using a method call so we have to override
88this as well.
89
90=head1 AUTHORS
91
92See L<HTML::String> for authors.
93
94=head1 COPYRIGHT AND LICENSE
95
96See L<HTML::String> for the copyright and license.
97
98=cut