filter the content by hooking in
Out of curiosity I looked into this a bit, and it turns out that hacking/hooking into Template::Parser (via Template::Directive, Template::Grammar, or even Parser.yp) is difficult, because it looks like Template::Parser::_parse drops the original source text and doesn't pass it into the handlers. But for a first step, all that's needed are the tokens, which can be provided by Template::Parser::split_text... but careful with the following, I haven't tested with a lot of different cases yet to see if there might be token types this doesn't handle.
#!/usr/bin/env perl
use warnings;
use strict;
use Data::Dump qw/dd pp/;
use Template::Parser;
my $text = <<'END';
before
[% # this is a comment to the end of line
foo = 'bar'
%]
<p>bw, bliako</p>
[%# placing the '#' immediately inside the directive
tag comments out the entire directive
%]
[% outside %]
after
END
my $parser = Template::Parser->new();
my $tokens = $parser->split_text($text);
#dd $tokens; # Debug
my $o = '';
for (my $i=0; $i<@$tokens; $i++) {
if (ref $tokens->[$i]) {
my $text = $tokens->[$i][0];
#dd $text; # Debug
$o .= "[% $text %]";
}
elsif ($tokens->[$i] eq 'TEXT') {
my $text = $tokens->[++$i];
#dd $text; # Debug
$o .= $text;
}
else { die pp($i,$tokens->[$i]) }
}
print $o;
__END__
before
[% # this is a comment to the end of line
foo = 'bar' %]
<p>bw, bliako</p>
[% outside %]
after
|