Trans
1/13/2009 2:16:00 AM
On Jan 12, 8:27=A0pm, Aaron Patterson <aa...@tenderlovemaking.com>
wrote:
> On Tue, Jan 13, 2009 at 02:13:13AM +0900, Trans wrote:
> > I was using:
>
> > =A0 =A0 Markaby::Builder.new(&block)
>
> > And it was working fine except it wouldn't let me use non-standard
> > attributes on an img tag, which blows. I couldn't find any way to
> > deactivate this strictness, so I decided to try Nokogiri instead:
>
> > =A0 =A0 =A0 =A0 Nokogiri::HTML::Builder.new(&block)
>
> > Unfortunately this doesn't work at all, b/c it turns out Nokogiri does
> > not seem to be using #instance_eval on the block. Without doing so I
> > don't see how it can be at all useful. For example in my case I have
> > method called #body:
>
> > =A0 =A0 def body(&block)
> > =A0 =A0 =A0 =A0 Nokogiri::HTML::Builder.new do
> > =A0 =A0 =A0 =A0 =A0 body(&block)
> > =A0 =A0 =A0 =A0 end
> > =A0 =A0 end
>
> It does use instance_eval, but it also checks the context in which it
> was instantiated to see if it responds to that method before creating a
> new node. =A0That way your builder can have access to methods outside of
> the builder.
>
> > By not using instance_eval Nokogiri doesn't recognize 'body' as a tag,
> > but tries to call the method again, resulting in an infinite loop. So
> > I think this is a bug in Nokogiri's builder.
>
> No. This was a requested feature. I gladly accept patches though! :-)
Ah, I see. I'm used to Markaby --when I needed to access an outside
method I first assign it to a local variable, then use the local var
in the block. I can see the advantage of accessing methods directly,
but it's also one of those things that can be a little worrisome b/c
if I add a method to the same scope, for instance say I add a 'def pre
()' or include a module, I may inadvertently cause unexpected markup.
That seems to me a bit too fragile. If you agree, I am willing to take
a look at making a patch.
> > In the mean time, can anyone offer a work around? Or is there a way to
> > turn off "strict-mode" for Markaby.
>
> You should be able to do this:
>
> =A0 def body(&block)
> =A0 =A0 Nokogiri::HTML::Builder.new do
> =A0 =A0 =A0 insert(Nokogiri::XML::Node.new('body', @doc), &block)
> =A0 =A0 end
> =A0 end
Thanks! Looks like that'll go it.
T.