Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Regex for removing Template::Toolkit comments?

by LanX (Saint)
on Aug 24, 2018 at 17:33 UTC ( [id://1221048]=note: print w/replies, xml ) Need Help??


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

I never used TT!

... but after browsing thru the docs, I wouldn't be surprised if there was a way to process a template to another template, and filter the content by hooking in.

HTH! :)

update

(from another thread) "you could subclass Template::Parser / Template::Directive"

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

  • Comment on Re: Regex for removing Template::Toolkit comments?

Replies are listed 'Best First'.
Re^2: Regex for removing Template::Toolkit comments?
by haukex (Archbishop) on Aug 25, 2018 at 09:01 UTC
    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
      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

      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.

Re^2: Regex for removing Template::Toolkit comments?
by bliako (Monsignor) on Aug 24, 2018 at 18:01 UTC

    thanks though that looks a big investment. plus I have my own semantics for language switching on top of TT so If I let TT parse that it may freak out ...

      > have my own semantics ... it may freak out

      If TT may freak out, how do you expect us to provide a regex doing it ? :)

      Good luck! ;)

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (8)
As of 2024-04-18 09:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found