开发者

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%]" >
开发者_Python百科

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

Thanks.


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.


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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜