html2haml converts characters inside RHTML tags to HTML entities?

17 views
Skip to first unread message

Noah Sussman

unread,
Oct 14, 2008, 8:29:12 PM10/14/08
to Haml
Does anyone know why html2haml is converting characters inside my
RHTML tags to HTML entities?

That is,
<%= in_place_editor_field :publication, "publication", {}, :rows =>
1 %>
becomes
= in_place_editor_field :publication, &quot;publication&quot;,
{}, :rows =&gt; 1

Have I just missed something completely obvious here?

I was originally using stable (2.0.3) and then I tried the current
trunk (2.1.0) as well, but no luck. I'm new to HAML (and to Rails)
and using it for a large project. I have a bunch of legacy RHTMLs
that I'm going to be converting to HAML over the next few weeks.
Assuming this actually is a bug, is there a patch for floating
around? If not, is there someone who understands the nature of the
problem, and can toss me a clue? I'd be happy to work toward a patch
if one is needed.

For now, I wound up turning entity escaping off completely by editing
line 183 of lib/haml/html.rb (2.1.0)

I changed
"<haml:#{tag}>#{CGI.escapeHTML($1)}</haml:#{tag}>"
to
"<haml:#{tag}>#{$1}</haml:#{tag}>"

Mislav Marohnić

unread,
Oct 15, 2008, 8:06:12 PM10/15/08
to ha...@googlegroups.com
On Wed, Oct 15, 2008 at 02:29, Noah Sussman <blin...@gmail.com> wrote:

Does anyone know why html2haml is converting characters inside my
RHTML tags to HTML entities?

Have I just missed something completely obvious here?

No, it was like this (broken) since I remember -- and that's a really long time. If you could fix this I'm sure lots of people (including me) would be very grateful. Myself, I somehow never got around on doing it.

Nathan Weizenbaum

unread,
Oct 15, 2008, 8:24:45 PM10/15/08
to ha...@googlegroups.com
The core of the issue is that HPricot does some weird and possibly
inconsistent things with HTML entities. I haven't sat down and puzzled
through possible ways to fix it, though, so one might well exist. I'll
look into it when I have a chance.

Chris Eppstein

unread,
Oct 15, 2008, 9:27:42 PM10/15/08
to ha...@googlegroups.com, ha...@googlegroups.com
That hpricot bug is very annoying. Over 9000 internets to the person
who fixes it.

Hunt & pecked on my iPhone... Sorry if it's brief!

Mislav Marohnić

unread,
Oct 16, 2008, 5:55:36 AM10/16/08
to ha...@googlegroups.com
On Thu, Oct 16, 2008 at 03:27, Chris Eppstein <ch...@eppsteins.net> wrote:

That hpricot bug is very annoying. Over 9000 internets to the person
who fixes it.

Hello, I came to claim my 9000 internets pls k thx:

Chris Eppstein

unread,
Oct 16, 2008, 10:14:04 AM10/16/08
to ha...@googlegroups.com
WINNER!

Mislav Marohnić

unread,
Oct 16, 2008, 12:29:48 PM10/16/08
to ha...@googlegroups.com
2008/10/16 Chris Eppstein <ch...@eppsteins.net>
WINNER!

Noah Sussman

unread,
Oct 16, 2008, 12:34:14 PM10/16/08
to Haml
On Oct 16, 5:55 am, "Mislav Marohnić" <mislav.maroh...@gmail.com>
wrote:
Wow, thanks, that was extremely helpful and amazingly fast :)

I will see to it that your internets are paid in small, unmarked cats.

Mislav Marohnić

unread,
Oct 16, 2008, 4:34:59 PM10/16/08
to ha...@googlegroups.com
2008/10/16 Noah Sussman <blin...@gmail.com>


Wow, thanks, that was extremely helpful and amazingly fast :)

Yeah and Nathan was amazingly fast to spot 2 bugs :(

But now they're fixed!

Nathan: don't expect this to handle all the spaghetti erb out there, but I think html2haml is actually much more usable with my commits and greatly reduces (but not eliminates!) the amount of hand-sanitizing that we had to do when converting from ugly erb views.

Noah Sussman

unread,
Oct 16, 2008, 4:58:08 PM10/16/08
to Haml


On Oct 16, 4:34 pm, "Mislav Marohnić" <mislav.maroh...@gmail.com>
wrote:
> 2008/10/16 Noah Sussman <blink...@gmail.com>
>
> don't expect this to handle all the spaghetti erb out there, but I
> think html2haml is actually much more usable with my commits and greatly
> reduces (but not eliminates!) the amount of hand-sanitizing

I have been converting ERBs with html2haml all week, and today, with
your fix, it's going much easier and faster. Thanks again. With that
bug out of the way I feel pretty confident about being able to handle
any ERB-to-HAML conversions that come up in the future as well.

As far as I can tell, html2haml seems pretty robust over all. I do
have to fix the indentation by hand for some complex ERB blocks, but I
think that's only to be expected.

I might mention another (minor) issue I noticed is that on line 28 of
html.rb (the one from Mislav's commit mentioned earlier in this
thread) there is this regex:
match_to_html(template, /<%(.*?)-?%>/m, 'silent')

but there is a dash missing I think. It should be:
match_to_html(template, /<%-(.*?)-?%>/m, 'silent')

because otherwise "silent" RHTML tags like
<%- form_for ... -%>

get translated with two leading dashes, not one, as in:
- - form_for ...

but if I add that dash in the regex, it works as I expect it should.

"Silent" end blocks also don't get stripped, they wind up in the HAML
as
- end

but so far I haven't read deeply enough to understand how end blocks
are stripped, so I don't understand yet why that's happening.

Mislav Marohnić

unread,
Oct 16, 2008, 5:39:57 PM10/16/08
to ha...@googlegroups.com
2008/10/16 Noah Sussman <blin...@gmail.com>


but there is a dash missing I think.  It should be:
 match_to_html(template, /<%-(.*?)-?%>/m,  'silent')

because otherwise "silent" RHTML tags like
 <%- form_for ... -%>

get translated with two leading dashes, not one, as in:
 - - form_for ...

Fixed and pushed to my stable branch.
 
"Silent" end blocks also don't get stripped, they wind up in the HAML as
 - end

I'd rather not tackle this yet. Removing "end" in your converted erb would make hand-modifications (like proper indenting) much harder because you will not know where the block finishes. Best to leave to the humans to take em out. 

Noah Sussman

unread,
Oct 16, 2008, 5:59:34 PM10/16/08
to Haml


On Oct 16, 5:39 pm, "Mislav Marohnić" <mislav.maroh...@gmail.com>
wrote:
> 2008/10/16 Noah Sussman <blink...@gmail.com>
>
>
>
> > but there is a dash missing I think.  
>
> Fixed and pushed to my stable branch.

Thanks, that rocks.

>
> > "Silent" end blocks also don't get stripped, they wind up in the HAML as
> >  - end
>
> I'd rather not tackle this yet. Removing "end" in your converted erb would
> make hand-modifications (like proper indenting) much harder

Ah ok, thanks for clarifying the purpose of keeping the end blocks.
Yes, since the indenting in the output is not always correct, having a
the "- end" marker in the coded is often convenient.

I converted a ton of RHTML today with the help of html2haml. In most
cases I was able to use the output without any editing at all, and
where I did need to edit it was basically fixing indentation within
Ruby blocks, and then deleting the "- end" markers. Thanks again,
your bug fix has been hugely helpful.

Nathan Weizenbaum

unread,
Oct 16, 2008, 8:37:18 PM10/16/08
to ha...@googlegroups.com
Mislav Marohnić wrote:
> Nathan: don't expect this to handle all the spaghetti erb out there,
> but I think html2haml is actually much more usable with my commits and
> greatly reduces (but not eliminates!) the amount of hand-sanitizing
> that we had to do when converting from ugly erb views.
Certainly not... both html2haml and css2sass were conceived as
quick-and-dirty ways to get some of the manual work done when converting
HTML/ERB and CSS to Haml and Sass. This is especially true for
html2haml; the fact is that we'd need a full-on Ruby parser and a huge
amount of logic to fully support all the legal ERB.

Neil Funk

unread,
Oct 17, 2008, 11:31:34 AM10/17/08
to ha...@googlegroups.com
>
> > "Silent" end blocks also don't get stripped, they wind up in the HAML as
> > - end
>
> I'd rather not tackle this yet. Removing "end" in your converted erb would
> make hand-modifications (like proper indenting) much harder

I agree with leaving them in as helpful comments. But since "- end"
gives an error, wouldn't it be better for html2haml to generate them
as comments ("- # end") instead? I know I've hand-fixed the
indentation before and forgotten to remove the end's, only to be
confronted with an error.
-Neil

Reply all
Reply to author
Forward
0 new messages