Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^2: Regex for removing Template::Toolkit comments?

by haukex (Archbishop)
on Aug 25, 2018 at 09:01 UTC ( [id://1221080]=note: print w/replies, xml ) Need Help??


in reply to Re: Regex for removing Template::Toolkit comments?
in thread Regex for removing Template::Toolkit comments?

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

Replies are listed 'Best First'.
Re^3: Regex for removing Template::Toolkit comments?
by LanX (Saint) on Aug 25, 2018 at 13:34 UTC
    Great, i think you just found a reply for Tidy for Template Toolkit Files =)

    Anyway ... please stop doing things "out of curiosity" and concentrate on running Perl in the browser! ;-)

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Re^3: Regex for removing Template::Toolkit comments?
by bliako (Monsignor) on Aug 26, 2018 at 09:55 UTC

    some 2-second later edits below...

    Thanks, your code's great. To do what LanX proposed looked to me too scary(=switch to another task and read their manuals, feel free to downvote my human ingredients). And using regex's is too fragile without knowing the full spec of TT, i.e. are nested comments allowed and how to deal with [% and [%#] and [% # inside strings as Corion said. So let TT do it seems the right way to me.

    Edit2: To be fair to the regex solutions: it was me who asked for a regex in the first place.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1221080]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-25 09:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found