Subject: | Patch to support \x{263A} notation in double quoted strings |
Hello,
In perl I can do (w/ "Wide character" warnings typically, but that isn't
really relevant at this point):
print "Smile \x{263A}\n"; and will get
Smile ☺
(hopefully rt doesn't corrupt the smiley face that should follow 'Smile')
It'd be great to be able to do the same with double quoted strings in
TT. (without having to override it and set PARSER)
[% "Smile \x{263A}" %]
The attached patch makes that work, without "Wide character" warnings,
all the way back to perl 5.6
Thanks, TT rocks!
--
Dan Muey
[ -- Example -- ]
Same results on 5.8.9 and 5.6.2
[ -- Before patch -- ]
hal9000$ perl -Mstrict -MTemplate -wle 'my
$tt=Template->new;$tt->process(\qq([% "$ARGV[0]" %]));' 'Smile \x{263A}'
Smile x{263A}
hal9000$
[ -- After patch -- ]
hal9000$ perl -Mstrict -MTemplate -wle 'my
$tt=Template->new;$tt->process(\qq([% "$ARGV[0]" %]));' 'Smile \x{263A}'
Smile ☺
hal9000$
(hopefully rt doesn't corrupt the smiley face that should follow 'Smile')
Subject: | add_slash_x_hex_support_to_TT_interpolation_of_double_quote_context.patch |
--- Parser.pm.orig 2010-05-13 09:10:48.000000000 -0500
+++ Parser.pm 2010-05-13 12:08:40.000000000 -0500
@@ -513,6 +513,7 @@
# is set and undef is returned.
#------------------------------------------------------------------------
+my $has_encode; # necessary for tokenise_directive() to handle \x{263A} notation
sub tokenise_directive {
my ($self, $text, $line) = @_;
my ($token, $uctoken, $type, $lookup);
@@ -568,6 +569,8 @@
# quoted string
if (defined ($token = $3)) {
# double-quoted string may include $variable references
+ $self->{'ENCODE_HEX_AS'} ||= 'utf-8'; # necessary for tokenise_directive() to handle \x{263A} notation
+
if ($2 eq '"') {
if ($token =~ /[\$\\]/) {
$type = 'QUOTED';
@@ -576,6 +579,21 @@
# as a variable reference
# $token =~ s/\\([\\"])/$1/g;
for ($token) {
+ # necessary for tokenise_directive() to handle \x{263A} notation
+ # only do regex once, only do require once (i.e. if Encode is not available why try to require it for each token?)
+ if (!defined $has_encode && m/\\x\{[0-9a-f]{1,4}\}/i) {
+ $has_encode = 0;
+ eval { require Encode; $has_encode = 1; };
+ }
+ if ( $has_encode ) { # perl 5.007003 and up
+ s{\\x\{([0-9a-f]{1,4})\}}{Encode::encode($self->{'ENCODE_HEX_AS'}, chr(hex("$1")))}egi;
+ }
+ else { # i.e. perl 5.6
+ # It could be argued that this only needs done when hex($_) < 0x100 but it works so leave it like this for consistency and in case it is needed under specific circumstances
+ s{\\x\{([0-9a-f]{1,4})\}}{eval '"\x{'."$1".'}"'}egi;
+ }
+ # /necessary for tokenise_directive() to handle \x{263A} notation
+
s/\\([^\$nrt])/$1/g;
s/\\([nrt])/$QUOTED_ESCAPES->{ $1 }/ge;
}