PATCH: Give filters access to scope objects.

3 views
Skip to first unread message

Bob Aman

unread,
Sep 21, 2007, 10:11:57 PM9/21/07
to Haml
Index: lib/haml/engine.rb
===================================================================
--- lib/haml/engine.rb (revision 606)
+++ lib/haml/engine.rb (working copy)
@@ -590,12 +590,22 @@
raise HamlError.new("Filter \"#{filter}\" is not defined!")
end
else
- filtered = filter.new(@filter_buffer).render
+ if filter.instance_method(:render).arity == 0
+ filtered = filter.new(@filter_buffer).render

- unless filter == Haml::Filters::Preserve
- push_text(filtered.rstrip.gsub("\n", "\n#{' ' *
@output_tabs}"))
+ unless filter == Haml::Filters::Preserve
+ push_text(filtered.rstrip.gsub("\n", "\n#{' ' *
@output_tabs}"))
+ else
+ push_silent("_hamlout.buffer << #{filtered.dump} << \"\\n
\"\n")
+ end
else
- push_silent("_hamlout.buffer << #{filtered.dump} << \"\\n
\"\n")
+ push_silent(
+ "haml_temp = #{filter.name}" +
+ ".new(#{@filter_buffer.inspect})" +
+ ".render(self).rstrip" +
+ ".gsub(\"\\n\", \"\\n#{' ' * @output_tabs}\")\n", true)
+ @precompiled <<
+ "haml_temp = _hamlout.push_script(haml_temp, false, nil)
\n"
end
end

Bob Aman

unread,
Sep 21, 2007, 10:54:08 PM9/21/07
to Haml
By the way... apologies in advance, since this probably constitutes
"code fisting" (as defined by Zed).

Mislav Marohnić

unread,
Sep 22, 2007, 7:52:26 AM9/22/07
to ha...@googlegroups.com
Bob,

It's pretty hard to copy-paste patches from force-wrapped e-mails and apply them. You should have used Pastie.

Other thing: what do half of your patches do?? You could be nice enough to share this little useful info with us.

Tom Stuart

unread,
Sep 22, 2007, 8:00:17 AM9/22/07
to ha...@googlegroups.com
On 22 Sep 2007, at 12:52, Mislav Marohnić wrote:
> It's pretty hard to copy-paste patches from force-wrapped e-mails
> and apply them. You should have used Pastie.

I'm guessing that uploading them to http://groups.google.com/group/
haml/files is even better.

Mislav Marohnić

unread,
Sep 22, 2007, 8:24:13 AM9/22/07
to ha...@googlegroups.com
On 9/22/07, Tom Stuart <t...@experthuman.com> wrote:

I'm guessing that uploading them to http://groups.google.com/group/
haml/files is even better.

Pastie is better because you can apply patches directly from command-line.

curl pastie.caboo.se/12345.txt | patch -p0

Brett Rogers

unread,
Sep 22, 2007, 8:35:12 AM9/22/07
to ha...@googlegroups.com
Or even better:

sake pastie:patch PASTE=12345

Regards,
Brett

Nathan Weizenbaum

unread,
Sep 23, 2007, 2:25:17 AM9/23/07
to ha...@googlegroups.com
However you want to upload the patch, Bob, Mislav's right: it's a pain
to apply from one of these emails. Thanks for the patches, though; I
definitely want to include at least the first three. I'm having a little
trouble figuring out the usage of the oneline filter, though; what do
you have in mind for it?

- Nathan

Bob Aman

unread,
Sep 23, 2007, 3:04:39 AM9/23/07
to Haml
> However you want to upload the patch, Bob, Mislav's right: it's a pain
> to apply from one of these emails. Thanks for the patches, though; I
> definitely want to include at least the first three. I'm having a little
> trouble figuring out the usage of the oneline filter, though; what do
> you have in mind for it?
>
> - Nathan

Yeah, my bad. I didn't notice they all got line-wrapped. I'll go
throw them up on Pastie instead.

Regarding the :oneline filter, the intended use is this scenario:

Most likely:

:oneline
%pre
%code
= preserve("some multi-line code here")

But in general:

:oneline
%div
%span
= "Something that might normally cause line-wrapping in a place
where you don't actually want line-wrapping to occur for whatever
reason."


Nathan Weizenbaum

unread,
Sep 24, 2007, 7:34:33 PM9/24/07
to ha...@googlegroups.com
This is an interesting way to solve that problem. Why not go with a
helper function?

- online do
whatever

- Nathan

Bob Aman

unread,
Sep 24, 2007, 7:52:08 PM9/24/07
to Haml
http://pastie.caboo.se/100417

Also, I updated the patch to allow the ERB filter to take advantage of
this code.

Bob Aman

unread,
Sep 24, 2007, 8:08:12 PM9/24/07
to Haml
> This is an interesting way to solve that problem. Why not go with a
> helper function?
>
> - online do
> whatever
>
> - Nathan

Because I wanted the stuff within the "oneline" construct to be
specified as Haml. It seemed inelegant to me to have to drop into
Ruby when the content wasn't even dynamic to overcome what I felt was
a minor limitation of the templating system. Seemed to make more
sense to just go ahead and remove the limitation.

For example, what if I had:

:oneline
%pre
%code
:preserve
# This is some static ruby code
# which I want to display for the
# world to see.
puts eval(2 + 2)

Surely you don't disagree that that is a much nicer way of outputting
"<pre><code># This is some static ruby code&#x000A;# which I want to
display for the&#x000A;# world to see.&#x000A;puts eval(2 +
2)&#x000A;</code></pre>\n" than it would be if I tried to use a helper
method to generate that.

Nathan Weizenbaum

unread,
Sep 24, 2007, 11:57:14 PM9/24/07
to ha...@googlegroups.com
The thing is, filters exist expressly so that non-Haml syntaxes can be
used from within Haml. Using them to do processing on Haml code is
wrong; not only is it much easier with a helper method, that's what
helper methods are /for/.

Also, could you work up unit tests for the rest of your patches? Thanks.

- Nathan

Reply all
Reply to author
Forward
0 new messages