Forum: Ruby on Rails RedCloth Hard Breaks

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ryan H. (Guest)
on 2006-01-27 02:06
(Received via mailing list)
Has anybody gotten :hard_breaks to work with RedCloth?

I'm doing:

self.body_html = RedCloth.new(self.body).to_html(:textile)

which works, except that it doesn't transform hard breaks to <br />
tags, which is really important for me.

However, if I do:

self.body_html = RedCloth.new(self.body, [ :hard_breaks
]).to_html(:textile)

then it converts ALL breaks into <br /> tags, including what should be
paragraph breaks, which get transformed into two consecutive <br />
tags.

Anybody know a workaround for this?

Thanks!
Ryan
Paul B. (Guest)
on 2006-01-27 16:32
(Received via mailing list)
I'm not sure I understand the problem.  What do you expect it to do with
paragraph breaks?
Ryan H. (Guest)
on 2006-01-27 19:15
(Received via mailing list)
On 1/27/06, Paul B. <removed_email_address@domain.invalid> wrote:
> I'm not sure I understand the problem.  What do you expect it to do with
> paragraph breaks?
>

Ok, well let me go to Why's Textile Reference
(http://www.hobix.com/textile/).

Under the heading "LINE BREAKS" it says "Line breaks are converted to
HTML breaks."

So the text:

   I spoke.
   And none replied.

would be converted to

   <p>I spoke.
   And none replied.</p>

What I want is to insert a <br /> tag instead of an HTML break. So my
desired output would look like this:

   <p>I spoke. <br />
   And none replied.</p>

Any idea how I can do this?


Thanks!
Paul B. (Guest)
on 2006-01-27 23:02
(Received via mailing list)
irb(main):001:0> require 'RedCloth'
=> true
irb(main):002:0> RedCloth.new("hello\nworld",[ :hard_breaks
]).to_html(:textile).to_s
=> "<p>hello<br />world</p>"
Richard L. (Guest)
on 2006-01-27 23:14
(Received via mailing list)
Paul B. wrote:
> irb(main):001:0> require 'RedCloth'
> => true
> irb(main):002:0> RedCloth.new("hello\nworld",[ :hard_breaks
> ]).to_html(:textile).to_s
> => "<p>hello<br />world</p>"

I think the issue is this:

irb(main):002:0> RedCloth.new("hello\n\n\nworld\nhello\nagain",
[:hard_breaks]).
to_html(:textile).to_s
=> "<p>hello<br />\nworld<br />hello<br />again</p>"
irb(main):003:0>
RedCloth.new("hello\n\n\nworld\nhello\nagain").to_html(:textile
).to_s
=> "<p>hello</p>\n\n\n\t<p>world\nhello\nagain</p>"
irb(main):004:0>

IE at the moment this:

hello


world
hello
again

goes to either

<p>hello<br />
world<br />
hello<br />
again</p>

or:

<p>hello</p>


<p>world
hello
again</p>

What we really would like is this:

<p>hello</p>
<p>world<br/>
hello<br />
again</p>

IE 2+ breaks = paragraph, 1 break = br
Ryan H. (Guest)
on 2006-01-27 23:29
(Received via mailing list)
On 1/27/06, Richard L. <removed_email_address@domain.invalid> wrote:
> What we really would like is this:
>
> <p>hello</p>
> <p>world<br/>
> hello<br />
> again</p>
>
> IE 2+ breaks = paragraph, 1 break = br
>


Yes! Richard you nailed it.

I've been experimenting with using a custom block (as Why described on
http://redhanded.hobix.com/inspect/usingRedcloth3.html), without much
success.

Something like:

text = "My first paragraph
and second line.

My second paragraph
and second line.

My third paragraph."

class CustomRedCloth < RedCloth
  def textile_code( tag, atts, cite, content )
    content.gsub(/\n/, "<br />")
  end
end

t1 = CustomRedCloth.new(text).to_html
Jay L. (Guest)
on 2006-01-28 03:01
(Received via mailing list)
On Thu, 26 Jan 2006 16:03:32 -0800, Ryan H. wrote:

> However, if I do:
>
> self.body_html = RedCloth.new(self.body, [ :hard_breaks ]).to_html(:textile)
>
> then it converts ALL breaks into <br /> tags, including what should be
> paragraph breaks, which get transformed into two consecutive <br />
> tags.

You're not using RedCloth 3.0.4, are you?  That's horribly broken, and I
seem to remember running into exactly that bug with it... someone was
refactoring it but I haven't heard any updates lately, and so the
solution
is to fall back to 3.0.3.
Richard L. (Guest)
on 2006-01-28 21:18
(Received via mailing list)
Jay L. wrote:
> You're not using RedCloth 3.0.4, are you?  That's horribly broken, and I
> seem to remember running into exactly that bug with it... someone was
> refactoring it but I haven't heard any updates lately, and so the solution
> is to fall back to 3.0.3.
>

I was using 3.0.4 yes and can confirm that falling back to 3.0.3 gets me
what I want.

Thanks for that!
Ryan H. (Guest)
on 2006-01-29 01:43
(Received via mailing list)
Falling back to RedCloth 3.0.3 worked for me as well. Thanks Jay!
Juicy (Guest)
on 2006-02-04 07:57
My host, Dreamhost, has both 3.0.3 and 3.0.4 installed. How can I
preferentially load 3.0.3? I tried appending:

    require_gem 'RedCloth', '!= 3.0.4'

to the end of my config/environment.rb, and killing Rails:

    killall -9 dispatch.fcgi

(Yes, FastCGI was working before this.) But I get this warning:

    Application error
    Rails application failed to start properly

I've also tried this line instead:

    require_gem 'RedCloth', '!= 3.0.4'

No avail. Any insights?

-B...
This topic is locked and can not be replied to.