Hi all!

I'm working on a fast wikitext-to-HTML translator and I'm embedding
the output in Haml templates. The trouble is that if the translated
text has a <pre> block in it:

<pre>foo
bar</pre>

And appears inside a Haml template:

.outer
  .inner
    =wikitext

The <pre> formatting is broken:

<div class="outer">
  <div class="inner">
    <pre>foo
    bar</pre>
  </div>
</div>

Note how the second line of the embedded <pre> block is indented and
unfortunately that means it will appear in the browser as:

foo
    bar

Rather than the intended:

foo
bar

So I looked at using the preserve() helper and that does indeed
produce output that renders correctly in the browser but looks like
this in the source:

<div class="outer">
  <div class="inner">
    <pre>foo&#x000A;bar</pre>
  </div>
</div>

While this would be fine for small blocks it isn't so nice for large
slabs of text, and large slabs of text are the most common use in a
wiki, unfortunately. The wikitext translator goes to some effort to
emit nicely aligned, easy-to-debug output text (much like Haml!) so it
is a shame to see it transformed into a big blob of hard to decipher
gibberish.

The other thing is that the wikitext parser is a C extension written
with speed as its number one design goal. It seems a shame to slow
things down by having Haml make another sweep over that text when it's
already been processed. Basically I am looking for a way of outputting
a block of literal text without Haml doing any manipulation of it at
all.

So I tried to investigate alternatives to the preserve() helper. I
tried to write my own helper method which basically did the following
(in psuedo-code):

def preserving &block
  tab_down back to 0
  yield
  tab_up back to where you were bfore
end

But that didn't work. Inspection of the Haml source code revealed that
there's "tabulation" but also "real_tabs" which get prepend. So my
next attempt was the following hack which directly manipulated the
@tabulation and @real_tabs instance variables (hideous I know):

def preserving &block
  tabs      = buffer.instance_variable_get :@tabulation
  real_tabs = buffer.instance_variable_get :@real_tabs
  buffer.instance_variable_set :@tabulation, 0
  buffer.instance_variable_set :@real_tabs, 0
  yield
  buffer.instance_variable_set :@tabulation, tabs
  buffer.instance_variable_set :@real_tabs, real_tabs
end

But unfortunately that didn't work either. There was still some
additional whitespace coming in from somewhere.

That was at 2:30 AM in the morning, so my investigations ended there.

Now it's a new day though, so I thought I'd ask here about
alternatives to preserve(). Is there a non-hacky way to do what I
want?

And ultimately, seeing as what I am looking for is a way to say to
Haml "render the following text without touching it _at all_", would
you accept a patch to introduce such a method if I can come up with
one?

Cheers,
Wincent


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Haml" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/haml?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to