[PATCH] New ERb mode

2 views
Skip to first unread message

Marc Haisenko

unread,
Mar 25, 2008, 11:07:29 AM3/25/08
to ruby...@ruby-lang.org
Hi folks,
I've implemented a new ERb mode and would like to share it with you guys since
we're using Ruby (with that modification) on our products and Ruby is GPL'd
(would share it anyways because we like being good Open Source citizens; one
hand washes the other :-))

The attached patch adds a mode "-T m" (for Marc ;-)). This mode strips out
lines that have nothing but white-space and <% ... %> sections. For example:

----------
<% 1.upto(3) do %>
Hello !
<% end %>
----------

With normal "erb" this produces:

----------
Hello !

Hello !

Hello !
----------

With "erb -T m" this produces:

----------
Hello !
Hello !
Hello !
----------

Same with stuff like this:

----------
<% bar = true %>
Foo
<% if bar %> <% one = "1" %>
Bar
<% end %>
Baz
----------

Normal mode:

----------

Foo

Bar

Baz Foo
<% if bar %>
Bar
<% end %>
Baz

----------

"erb -T m":

----------
Foo
Bar
Baz
----------

The code is not very beautiful and if someone considers integrating this stuff
in the official ERb then "-T m" might not be a good mode name but
nevertheless, here's the code.

Comments welcome.
Bye,
Marc


--
Marc Haisenko

Comdasys AG
Rüdesheimer Str. 7
80686 München
Germany

Tel.: +49 (0)89 548 433 321

ruby-1.8.6-akira-erb-mode.patch

Marc Haisenko

unread,
Mar 25, 2008, 11:14:46 AM3/25/08
to ruby...@ruby-lang.org
On Tuesday 25 March 2008, Marc Haisenko wrote:
> Same with stuff like this:
>
> ----------
> <% bar = true %>
> Foo
> <% if bar %> <% one = "1" %>
> Bar
> <% end %>
> Baz
> ----------
>
> Normal mode:
>
> ----------
>
> Foo
>
> Bar
>
> Baz Foo
> <% if bar %>
> Bar
> <% end %>
> Baz
>
> ----------

This is of course a copy&paste error... should be:

----------

Foo

Bar

Baz
----------

Bye,

Jason Roelofs

unread,
Mar 25, 2008, 11:19:41 AM3/25/08
to ruby...@ruby-lang.org
ERb already does this:

----------
<% 1.upto(3) do -%>
Hello !
<% end -%>
----------

produces

Hello !
Hello !
Hello !

It's the dash in "-%>" that signals this.

Jason

Marc Haisenko

unread,
Mar 25, 2008, 11:39:09 AM3/25/08
to ruby...@ruby-lang.org
On Tuesday 25 March 2008, Jason Roelofs wrote:
> ERb already does this:
>
> ----------
> <% 1.upto(3) do -%>
> Hello !
> <% end -%>
> ----------
>
> produces
>
> Hello !
> Hello !
> Hello !
>
> It's the dash in "-%>" that signals this.
>
> Jason

No, it doesn't. Try it :-) It produces:

----------

Hello !
Hello !
Hello !

----------

Notice the leading tab. Same with this:

---------
<% true -%>
foo
---------

The result is:

---------
foo
---------

But not with my patch.

Jason Roelofs

unread,
Mar 25, 2008, 12:01:41 PM3/25/08
to ruby...@ruby-lang.org


Your first post only talks about white-space where the <% %> was, not
about this leading tab-spacing issue.

Jason

Marc Haisenko

unread,
Mar 25, 2008, 12:07:52 PM3/25/08
to ruby...@ruby-lang.org

You're right, sorry. So my patch makes the line get omitted entirely (if it
contains nothing but white-space), "-%>" only ommits the newline.

Plus my mode doesn't need the explicit "-%>", only the normal "%>".

Phlip

unread,
Mar 25, 2008, 3:39:14 PM3/25/08
to ruby...@ruby-lang.org
Marc Haisenko wrote:

> You're right, sorry. So my patch makes the line get omitted entirely (if it
> contains nothing but white-space), "-%>" only ommits the newline.
>
> Plus my mode doesn't need the explicit "-%>", only the normal "%>".

Could a more aggressive patch read the <!DOCTYPE..>, then apply its XML DTD to
remove all non-significant blanks? I suspect that would save wear and tear on
servers. If the HTML DTDs actually specify which blanks are significant!

--
Phlip

Marc Haisenko

unread,
Mar 26, 2008, 5:34:01 AM3/26/08
to ruby...@ruby-lang.org

Sorry, but ERb doesn't do any XML processing, it's a simple templating system.

Phlip

unread,
Mar 26, 2008, 9:33:57 AM3/26/08
to ruby...@ruby-lang.org
Marc Haisenko wrote:

>> Could a more aggressive patch read the <!DOCTYPE..>, then apply its XML DTD

^


> to
>> remove all non-significant blanks? I suspect that would save wear and tear
> on
>> servers. If the HTML DTDs actually specify which blanks are significant!

> Sorry, but ERb doesn't do any XML processing, it's a simple templating system.

That's why I said "patch".

Marc Haisenko

unread,
Mar 26, 2008, 9:45:10 AM3/26/08
to ruby...@ruby-lang.org
On Wednesday 26 March 2008, Phlip wrote:
> Marc Haisenko wrote:
>
> > > Could a more aggressive patch read the <!DOCTYPE..>, then apply its XML
> > > DTD to remove all non-significant blanks? I suspect that would save wear
> > > and tear on servers. If the HTML DTDs actually specify which blanks are
> > > significant!
> >
> > Sorry, but ERb doesn't do any XML processing, it's a simple templating
> > system.
>
> That's why I said "patch".

Well, yes, you could write one :-) But I'd say that's not an easy task,
especially if you want to do it correctly (e.g. still have the correct line
numbers in exceptions).

Florian Gilcher

unread,
Mar 28, 2008, 4:03:30 AM3/28/08
to ruby...@ruby-lang.org
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


On Mar 26, 2008, at 2:45 PM, Marc Haisenko wrote:
> On Wednesday 26 March 2008, Phlip wrote:
>> Marc Haisenko wrote:
>>
>>>> Could a more aggressive patch read the <!DOCTYPE..>, then apply
>>>> its XML
>>>> DTD to remove all non-significant blanks? I suspect that would
>>>> save wear
>>>> and tear on servers. If the HTML DTDs actually specify which
>>>> blanks are
>>>> significant!
>>>
>>> Sorry, but ERb doesn't do any XML processing, it's a simple
>>> templating
>>> system.
>>
>> That's why I said "patch".
>

I have the impression that this goes beyond the scope of a templating
system. It
also could get messy if you your template has no doctype (Partials,
for example).

Why not use tidy for such a task?

Greetings
Florian Gilcher
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (Darwin)

iEYEARECAAYFAkfspjEACgkQJA/zY0IIRZZ6KACggTd2UW9iZa/d4S/GEFHNMqJH
RyIAoIvq/zX7iPhUSr76Zw/WlQfOBb52
=/wJQ
-----END PGP SIGNATURE-----

Reply all
Reply to author
Forward
0 new messages