List Info

Thread: CHOMP feature request




CHOMP feature request
user name
2006-02-03 15:47:30
So, I have been thinking about it and I'd like to propose
two new features 
with whitespace chomping.  Both of these are low cost to
implement, but do 
add a bit of user convenience.

The first is to add a new chomp type.  Currently CHOMP_ALL
or the '-' modifier 
doesn't really CHOMP_ALL.  It chomps everything on the same
line.  This has 
caused several posts in the short time I've been on the
mailing list.  I'd 
like to see a new chomp type that removes ALL whitespace in
a preceding text 
chunk.  Since CHOMP_ALL is taken I was thinking maybe
CHOMP_KILL, or perhaps 
CHOMP_ALL_NO_REALLY_I_MEAN_ALL, OR CHOMP_DIE_DIE_DIE (But I
would settle for 
CHOMP_KILL).  Logically the constant would take the value of
3 (or I guess 4 
if you want chomp constants to work as a bitset - but I
think that is 
overkill).   To go along with CHOMP_KILL I think that
probably the best 
modifier would be '~'.

So the table would be:

Constant name          Constant Value      Tag Modifier 
CHOMP_NONE           0                             +
CHOMP_ALL               1                             -
CHOMP_COLLAPSE  2                             = # does this
modifier work?
CHOMP_KILL               3                             ~

So CHOMP_KILL would do the following fairly desirable thing
(no hacks 
required).

"
   [%~ 1 %]


             [%~ 2 ~%]

   [% 3 ~%]
"

Which would print 123 without any other whitespace.  Note
that the leading ~ 
on the 1 and the trailing ~ on the 3 do the same thing that
TRIM => 1 would 
do in this case, but would be very different with the
following case.

"      [%~ '    ' ~%]       "

With CHOMP_KILL the final string would be '    ' while with
TRIM => 1 the 
final string would be ''.

Codewise this is very simple to implement in the split_text
method of the 
Template/Parser.pm.  Sorry - no patch at this time.


OK - Request number two.  Can we use the Tag Modifier in
place of the CONSTANT 
name.  Consider:

use Template;
my $t = Template->new(PRE_CHOMP => '-',  POST_CHOMP
=> '~');

As opposed to:

use Template qw(:chomp);
my $t = Template->new(PRE_CHOMP => CHOMP_ALL, 
POST_CHOMP => CHOMP_KILL);

Or in a bad case:

use Template;
my $t = Template->new(PRE_CHOMP => 1,  POST_CHOMP
=> 3);


Allowing the modifier to be used isn't really saving that
much (it isn't that 
hard to import the constants) - but then people would be
less likely to 
hardcode the constants and they wouldn't have to remember
the constant names, 
and if the CHOMP_KILL proposal is accepted - they wouldn't
have to wonder 
which function CHOMP_ALL vs CHOMP_KILL does (which one
really gets rid of 
whitespace vs just the whitespace on the same line).

It also won't be much of a hit to allow the modifier names
as the check only 
needs to happen once at configuration time.

If there are any questions, let me know.  I think these
requests are sane 
enough for most people and won't cause any hardship for
those who don't want 
to use them.

Paul Seamons


_______________________________________________
templates mailing list
templatestemplate-toolkit.org
http://lists.template-toolkit.org/mailman/listinfo/t
emplates
CHOMP feature request
user name
2006-02-03 16:50:50
On Friday 03 February 2006 08:47 am, mailseamons.com wrote:
>  Since CHOMP_ALL is taken I was thinking maybe
> CHOMP_KILL, or perhaps CHOMP_ALL_NO_REALLY_I_MEAN_ALL,
OR CHOMP_DIE_DIE_DIE
> (But I would settle for CHOMP_KILL).  Logically the
constant would take the
> value of 3 (or I guess 4 if you want chomp constants to
work as a bitset -
> but I think that is overkill).   To go along with
CHOMP_KILL I think that
> probably the best modifier would be '~'.
>
> So the table would be:
>
> Constant name          Constant Value      Tag Modifier
> CHOMP_NONE           0                             +
> CHOMP_ALL               1                             -
> CHOMP_COLLAPSE  2                             = # does
this modifier work?
> CHOMP_KILL               3                            
~

Well if we go down this slippery slope (which hopefully
isn't too slippery) - 
we probably need to distinguish between

CHOMP_NONE (0, +)
CHOMP_ALL (1, -)
CHOMP_COLLAPSE (2, =)
CHOMP_KILL_ALL (3, ~)
CHOMP_KILL_COLLAPSE (4, ^) # can't think of a better char
(maybe '_')

So the mnemonics are:
'-' = remove space on the same line
'+' = positively keep all space
'=' = collapse space on this line
'~' = kill anything that looks like space across multiple
lines
'^' = collapse space across multiple lines

Paul

_______________________________________________
templates mailing list
templatestemplate-toolkit.org
http://lists.template-toolkit.org/mailman/listinfo/t
emplates
CHOMP feature request
user name
2006-02-03 21:58:50
> So, I have been thinking about it and I'd like to
propose two 
> new features with whitespace chomping.

> So CHOMP_KILL would do the following fairly desirable
thing (no
hacks 
> required).
> 
> "
>    [%~ 1 %]
> 
> 
>              [%~ 2 ~%]
> 
>    [% 3 ~%]
> "
> 
> Which would print 123 without any other whitespace.

That's the right idea. I do suffer a lot sometimes, having
to
write series of TT tags on one line without normal
indentation.
Adding that "hungry chomp" modifier would solve
the problem, so I
join this feature request.


-- 
Sergey Martynoff


_______________________________________________
templates mailing list
templatestemplate-toolkit.org
http://lists.template-toolkit.org/mailman/listinfo/t
emplates
CHOMP feature request
user name
2006-02-10 19:56:00
So - I went ahead and coded up the added chomp capabilities.
 These diffs are 
taken against the 2.14 release that is on CPAN (and may not
be compatible 
with HEAD).

I have taken care to ensure that the changes don't add
overhead to the parsing 
process (and in some cases may actually reduce it).

I have included an updated Config.pod and chomp.t that have
new documentation 
and new tests and I made sure that all other tests continue
to work.

The following is an excerpt from the new
Template/Manual/Config.pod

> For reference, PRE_CHOMP and POST_CHOMP may be set to
any of the following:
>     Constant        Value      Tag Modifier
>     ---------------------------------------
>     CHOMP_NONE      0          +
>     CHOMP_ALL       1          -
>     CHOMP_COLLAPSE  2          =
>     CHOMP_GREEDY    3          ~
>     CHOMP_HTML      4          ^

I think people will really like these features and hopefully
they will be 
suitable for the 2.15 release.  I would hazard a guess that
people who read 
the config will use the '~' modifier more often than the '-'
modifier as it 
comes closer to doing what people expect.

There is one change in functionality with these patches.  
There is a one-off 
exception that TT allows for CHOMP_COLLAPSE at the end of a
string.  If
POST_CHOMP is set to CHOMP_COLLAPSE, TT currently will
replace

 "[% 1  %]n" with "1"

This new patch will make this turn into "1 ".  The
new behavior is arguably 
more correct as "all whitespace" on the current
line is collapsed to a single 
space, and because the designer of the template could put
"[% 1 -%]n" in 
this one case to avoid the addition of of the space.  I had
written code to 
allow for this edge case - but have left it out in hopes of
having the 
behavior be more consistent.

The following is an excerpt from the new
Template/Manual/Config.pod

Any comments or feedback are welcome.

Paul Seamons
mailseamons.com
CHOMP feature request
user name
2006-02-16 08:58:01
Hi Paul,

Thanks for all your efforts on this.  Sorry to not reply
sooner but I've
been busy in the Real World[tm].  Just catching up with
things now.

That part of the codebase changed slightly in 2.14a with a
patch that
fixed a line counting bug.  I need to check it over and
manually apply 
the changes to make sure we don't screw up what's already
been fixed.  
I'll try and get that done before the week's out.

Here's the patch FYI:

  http://tt2.org/pipermail/templates/2005-December/008
157.html

I must admit I'm a little wary about adding new flags that
don't invoke 
any obvious meaning.  I want to try to avoid the slippery
slope into the 
kind of "line noise" that plagues some
markup/programming languages 
(although some might argue it's already too late...)

With '-' and '+' it's fairly clear (once you know what
they do) that '-' 
removes whitespace and '+' leaves it alone.  The same is
also true of '=' 
for collapsing, if you think of it as two '-' signs with a
space between 
them.  That's also analogous to what the operator does. 
However '~' and '^' 
are a little more cryptic.  Of course there are only so many
characters
to choose from (and those are probably as good as any), but
I'm wondering 
if we can optimise the solution a little and do away with
one of them 
altogether.

I propose we go ahead and add the '~' operator for a
greedy chomp (we 
tell people it's just a wiggly '-' that goes up to
eleven).  Then we make 
the '=' collapse operator do what it always should have
done, which is to 
replace *all* leading/trailing whitespace with a single
space.  In other
words, it will do what your CHOMP_HTML/^ does.

At present it replaces only the whitespace up to and
including the next
newline.  However, it was added to mimic the whitespace
collapsing 
behaviour provided by XML parsers (as defined in the XML
Schema spec.)
and that collapses all whitespace including newlines.  The
reason we
care about what XML has to say on the subject is because it
defines the 
rules for how browsers treat whitespace in (X)HTML
documents, and thus 
determines how we should go about creating them.

For those who don't already know, the purpose of the
collapse flag is to 
allow you to do this:

   [% forename =%]
   [% surname %]

in order to get this:

   [% forename %] [% surname %] 

This works exactly the same regardless of the implementation
(i.e. we're not
going to break anyone's existing templates).  However, with
the current 
implementation you can't do this to get the same effect.

   [% forename =%]

   [% surname %]

With the new behaviour, that will now work.  Although it's
feasible that
someone may have a template that specifically uses that last
example
because they want a space and a newline, it's far more
likely that they
would have just put the space at the end of the line and not
used the '=' 
flag at all.  

   [% forename %] <- space

   [% surname %]

So I'm fairly confident that we're not going to screw
anyone with this 
change, and there are plenty of ways to get the "old
skool" behaviour if
you really want it for some reason.  But if anyone knows
different then 
now is the time to speak...

Cheers
A


_______________________________________________
templates mailing list
templatestemplate-toolkit.org
http://lists.template-toolkit.org/mailman/listinfo/t
emplates
CHOMP feature request
user name
2006-02-16 13:30:24
> So I'm fairly confident that we're not going to screw
anyone 
> with this change

I think you are perfectly right! The strange '^' symbol
confused
me too, while '~' as greedy chomp is quite understandable.


-- 
Sergey Martynoff


_______________________________________________
templates mailing list
templatestemplate-toolkit.org
http://lists.template-toolkit.org/mailman/listinfo/t
emplates
CHOMP feature request
user name
2006-02-16 15:11:52
Beautiful.

I had added the '^'  for completeness, but I'd much
rather have '=' behave as 
I had proposed '^' to  behave.  I didn't know that we
could change '=' late 
in the game.

I'm looking forward to 2.15.

Paul

PS - No problem on the long reply.  I have plenty of
outstanding emails 
myself.

_______________________________________________
templates mailing list
templatestemplate-toolkit.org
http://lists.template-toolkit.org/mailman/listinfo/t
emplates
CHOMP feature request
user name
2006-02-16 18:37:08
On Thu, 16 Feb 2006, Andy Wardley wrote:

> With '-' and '+' it's fairly clear (once you know
what they do) that '-'
> removes whitespace and '+' leaves it alone.  The same
is also true of '='
> for collapsing, if you think of it as two '-' signs
with a space between
> them.  That's also analogous to what the operator
does.  However '~' and '^'
> are a little more cryptic.

Not sure if this would complicate parsing (because it's 2
characters 
long), but what about '--' for greedy chomp?


> Then we make the '=' collapse operator do what it
always should have 
> done, which is to replace *all* leading/trailing
whitespace with a 
> single space.

+1 on this


Larry

_______________________________________________
templates mailing list
templatestemplate-toolkit.org
http://lists.template-toolkit.org/mailman/listinfo/t
emplates
CHOMP feature request
user name
2006-02-16 19:09:56
Larry Leszczynski wrote:
> Not sure if this would complicate parsing (because
it's 2 characters 
> long), but what about '--' for greedy chomp?

I thought about that.  Then I tried typing it:

  [%-- foo --%]
  [%~ foo ~%]

I thought the '--' made it look too long, whereas the
'~' snuggled in
nicely.  

Also, it might interfere with a future --foo / foo--
decrement operator:

  [% --foo %]  vs  [%-- foo %]  vs  [%--foo%]
  [% foo-- %]  vs  [% foo --%]  vs  [%foo--%]

But rather than making a unilateral decision on such an
important matter, 
I took the scores from 12 judges, had the computer randomly
discard 3 of 
them, summed the remaining 9 scores, multiplied the
convenience factor by 
the inconvenience ratio, added the addition character
compensation correction 
amount and the result was...  

...the '~' won.    

A

(http://www.stat
.yale.edu/~jay/EC2006/ for those who haven't got a clue

why I'm talking about all those judges)


_______________________________________________
templates mailing list
templatestemplate-toolkit.org
http://lists.template-toolkit.org/mailman/listinfo/t
emplates
CHOMP feature request
user name
2006-05-25 09:10:15
mailseamons.com wrote:
> So - I went ahead and coded up the added chomp
capabilities.  These diffs are 
> taken against the 2.14 release that is on CPAN (and may
not be compatible 
> with HEAD).

Applied (eventually!).  Many thanks Paul.

We now have CHOMP_GREEDY (or '~') to greedily chomp all
whitespace 
including multiple newlines.

CHOMP_COLLAPSE (or '=') now does the same thing (chomping
multiple 
newlines greedily) but replaces it with a single space. 
Previously it 
only chomped a single line.

CHOMP_ONE is the new name for what used to be CHOMP_ALL. 
The behaviour 
remains unchanged.  The CHOMP_ALL constant is still
supported for 
backwards compatibility but will eventually be deprecated
(or possibly 
re-used as a better name for CHOMP_GREEDY, but not until
TT3).

Changes in CVS.  Version 2.15 coming soon.

A



_______________________________________________
templates mailing list
templatestemplate-toolkit.org
http://lists.template-toolkit.org/mailman/listinfo/t
emplates
[1-10]

about | contact  Other archives ( Real Estate discussion Medical topics )