Беров
Беров

Reputation: 1393

How to make tidy ignore TT code in html.tt templates?

I have some TT templates that I want tidy-up a little. I use tidy on the command-line.

my command looks like:

$ tidy -utf8  --preserve-entities y -indent -wrap 120 file.html.tt

Unfortunately if I have code like:

[% aoh.unshift({ label => '', value => 'All types' }); %]

It ends up in the resulting file as:

[% aoh.unshift({ label => '', value => 'All types' }); %]

The same happens with Template Toolkit code in tag attributes, eg:

<a href="[%%20%20c.url_for('/content/edit').query('data_type'%20=%3Edata_type%20)%20%]" >

What would be the needed options to make tidy ignore everything between "[%" and "%]"? Same question holds true for PHP start and end tags.

Thanks.

Upvotes: 2

Views: 874

Answers (2)

bvr
bvr

Reputation: 9697

Somehow extending the ideas here, why not replace TT snippets for something completely harmless and after tidy put original things back. In code below, I am replacing for comments like <!-- sn20 -->:

use File::Slurp;
my $template = read_file(shift);

# replace TT snippets with <!-- snNN -->
my %snip = ();
my $id   = 0;
$template =~ s/ \[% (.*?) %\] / $snip{++$id} = $1; "<!-- sn$id -->" /gxse;

# run tidy
open my $tidy_fh, '|-', 'tidy -utf8  --preserve-entities y -indent -wrap 120 >tidy_out'
    or die;
print $tidy_fh $template;
close $tidy_fh;

# fix code back
my $template_tidied = read_file('tidy_out');
$template_tidied =~ s/<!-- sn(\d+) -->/ "[%$snip{$1}%]" /ge;

# print the result
print $template_tidied;

Upvotes: 2

socket puppet
socket puppet

Reputation: 3219

What if you temporarily disguise your TT tags?

$ perl -pie 's/\[%/<!--\[ %/g; s/%\]/% \]-->/g' file.html.tt
$ tidy -utf8  --preserve-entities y -indent -wrap 120 file.html.tt
$ perl -pie 's/<!--\[ %/\[%/g; s/% \]-->/%\]/g' file.html.tt

The first command will convert all your TT elements into HTML comments, the last command changes them back.

Upvotes: 3

Related Questions