Forum: Ruby on Rails Problems using the RedCloth gem !?!

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
525e0c75f4f5c1d0ab0b6cc91575b74f?d=identicon&s=25 Emil Kampp (emuen)
on 2007-01-21 11:42
Hi there.

I have this line in my view:

<%=, [:filter_html,
:filter_styles]).to_html(:textile) %>

but it doesn't filter out the html out, from this input:

h1. Header

some paragraph

<h1>BOOM</h1> #This should be filtered away ?

other paragraph

What am i doing wrong ? it renderes the h1. and all hte paragraph fine,
but it doesn't filter the <> type html tags.

 - Emil
70ca58d0e0e0eabbdb74d177417d09d7?d=identicon&s=25 (Guest)
on 2007-01-21 12:47
(Received via mailing list)
I think it filters some HTML tags, but not normal and safe ones like
<br />, <p>, <h1> etc.

On Jan 21, 11:42 am, Emil Kampp <>
525e0c75f4f5c1d0ab0b6cc91575b74f?d=identicon&s=25 Emil Kampp (emuen)
on 2007-01-21 21:53
Is there any way, that i can filter all html tags from the text, while
rendering it with the gem ?

 - Emil
70225136eacd3d870f64e03bff678655?d=identicon&s=25 Russell Norris (Guest)
on 2007-01-22 14:56
(Received via mailing list)
If there is, I haven't found it. I _have_ though found a useful Regex
doing that. It's a slightly modified version of the one used in Typo.
source code will go scavenged!] I know I might catch some flack for
doing so
but I choose to add this method to the String class itself, as I use it

class String
  def strip_html(leave_whitespace = false)
    name = /[\w:_-]+/
    value = /([A-Za-z0-9]+|('[^']*?'|"[^"]*?"))/
    attr = /(#{name}(\s*=\s*#{value})?)/
    rx =
    (leave_whitespace) ?  self.gsub(rx, "").strip : self.gsub(rx,
"").gsub(/\s+/, " ").strip

Be aware, though, that there is stil a lot of HTML entities left in the
Textilized string. [&#8482;, etc.] Depending on your end use, you may
to strip those entities as well. Let me know if you do need that because
I've written some really handy code for it, completely based on what
transformations RedCloth does. You know, only make the server work as
as it has to.

525e0c75f4f5c1d0ab0b6cc91575b74f?d=identicon&s=25 Emil Kampp (emuen)
on 2007-01-22 16:33
I do need to remove ALL HTML enteties from the text, before rendering

Is there any way to allow only some redcloth formatting to be
enterpreted ? such as not enterpreting lets say the h1. tag but accept
the h2. tag ?

 - Emil
70225136eacd3d870f64e03bff678655?d=identicon&s=25 Russell Norris (Guest)
on 2007-01-22 19:28
(Received via mailing list)
In what circumstances would you want an h1 and not an h2? Sounds like
definitely going to need to Regex that one.

Anyhow, here's the two additional methods [both on the String class as
before] for dealing with RedClothed HTML entities.

  def strip_accents

  def convert_entities
    dummy = self.dup
      "#822[01]" => "\"",
      "#821[67]" => "'",
      "#8230" => "...",
      "#8211" => "-",
      "#8212" => "--",
      "#215" => "x",
      "gt" => ">",
      "lt" => "<",
      "(#8482|trade)" => "(tm)",
      "(#174|reg)" => "(r)",
      "(#169|copy)" => "(c)",
      "(#38|amp)" => "&",
      "nbsp" => " ",
      "cent" => " cent",
      "pound" => " pound",
    }.each do |textiled, normal|
      dummy.gsub!(/&#{textiled};/, normal)
    dummy.gsub(/&[^;]+;/, "")
This topic is locked and can not be replied to.