2

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.

zb226
  • 9,586
  • 6
  • 49
  • 79
Беров
  • 1,383
  • 10
  • 22
  • 1
    Hm, tidy isn't really meant to tidy up templates, but actual HTML. You can however set `fix-uri` to false to avoid your href's being processed, and there's a _small_ chance `assume-xml-procins` can do something with php tags, but I doubt it. – Wrikken Jul 25 '11 at 20:36
  • 1
    Ok, but all I want is make tidy ignore some content as it would ignore HTML comments. I will try however. – Беров Jul 25 '11 at 20:40
  • 1
    To put it another way: What is the proper tool for the job(if not tidy)? – Беров Jul 25 '11 at 20:41
  • That's another question, to which I unfortunately have to say: I know of none. Tidy has been stagnant for years now, maybe you can extend HTML Purifier somewhat, but it'd take some work. The easiest solution I can come up with is to remove all instructions from the file, substituting them with 'safe' placeholders, and after tidy's work edit them in again. – Wrikken Jul 25 '11 at 21:07
  • Surrounding Template/PHP/ASP/etc code with HTML comments and then removing comments worked for some of the code but not for tag attributes where tidy escaped them :). Pain in the ass. Thanks however. – Беров Jul 25 '11 at 21:26

2 Answers2

3

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.

socket puppet
  • 3,191
  • 20
  • 16
2

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;
bvr
  • 9,687
  • 22
  • 28
  • That's a good idea. I will try a combination of your code and the first approach. Thanks... it is getting harder to shoose the right answer :). This is called collaboration! – Беров Jul 27 '11 at 21:42
  • 1
    @Berov - I thought of tidying templates long time ago, but never proceed to actual experiments. You really pushed me forward by your question, thanks for that. – bvr Jul 28 '11 at 09:41
  • And I will implement tidying for templates in https://github.com/kberov/MYDLjE using your idea, @bvr. Thanks again. – Беров Jul 28 '11 at 13:25