# Problems using the RedCloth gem !?!

**URL:** <https://www.ruby-forum.com/t/problems-using-the-redcloth-gem/82815>\
**Category:** Rails\
**Created:** [January 21, 2007, 11:42am UTC](https://www.ruby-forum.com/t/problems-using-the-redcloth-gem/82815 "2007-01-21T11:42:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![emuen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/96bed5/32.png) [@emuen](https://www.ruby-forum.com/u/emuen)\
**Post date:** [January 21, 2007, 11:42am UTC](https://www.ruby-forum.com/t/problems-using-the-redcloth-gem/82815/1 "2007-01-21T11:42:00Z")

</div>

Hi there.

I have this line in my view:

\<%= RedCloth.new(@news.long, [:filter\_html,  
:filter\_styles]).to\_html(:textile) %\>

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

* * *

h1. Header

some paragraph

# BOOM
 #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

---

<div class="post-metadata">

**Author:** ![emuen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/96bed5/32.png) [@emuen](https://www.ruby-forum.com/u/emuen)\
**Post date:** [January 21, 2007, 12:47pm UTC](https://www.ruby-forum.com/t/problems-using-the-redcloth-gem/82815/2 "2007-01-21T12:47:51Z")

</div>

I think it filters some HTML tags, but not normal and safe ones like  
  
,

,

# etc.

On Jan 21, 11:42 am, Emil K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![emuen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/96bed5/32.png) [@emuen](https://www.ruby-forum.com/u/emuen)\
**Post date:** [January 21, 2007, 9:53pm UTC](https://www.ruby-forum.com/t/problems-using-the-redcloth-gem/82815/3 "2007-01-21T21:53:10Z")

</div>

Is there any way, that i can filter all html tags from the text, while  
rendering it with the gem ?

- Emil

---

<div class="post-metadata">

**Author:** ![emuen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/96bed5/32.png) [@emuen](https://www.ruby-forum.com/u/emuen)\
**Post date:** [January 22, 2007, 2:56pm UTC](https://www.ruby-forum.com/t/problems-using-the-redcloth-gem/82815/4 "2007-01-22T14:56:59Z")

</div>

If there is, I haven’t found it. I _have_ though found a useful Regex  
for  
doing that. It’s a slightly modified version of the one used in Typo.  
[No  
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  
a  
lot.

class String  
def strip\_html(leave\_whitespace = false)  
name = /[\w:\_-]+/  
value = /([A-Za-z0-9]+|(’[^’]_?’|"[^"]_?"))/  
attr = /(#{name}(\s\*=\s\*#{value})?)/  
rx =  
/\<[!/?[]?(#{name}|–)(\s+(#{attr}(\s+#{attr})_))?\s_([!/?]]+|–)?\>/  
(leave\_whitespace) ? self.gsub(rx, “”).strip : self.gsub(rx,  
“”).gsub(/\s+/, " ").strip  
end  
end

Be aware, though, that there is stil a lot of HTML entities left in the  
Textilized string. [™, etc.] Depending on your end use, you may  
need  
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  
hard  
as it has to.

RSL

---

<div class="post-metadata">

**Author:** ![emuen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/96bed5/32.png) [@emuen](https://www.ruby-forum.com/u/emuen)\
**Post date:** [January 22, 2007, 7:28pm UTC](https://www.ruby-forum.com/t/problems-using-the-redcloth-gem/82815/5 "2007-01-22T19:28:15Z")

</div>

In what circumstances would you want an h1 and not an h2? Sounds like  
you’re  
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  
self.gsub(/&([A-Za-z])(grave|acute|circ|tilde|uml|ring|lig|cedil);/,  
‘\1’)  
end

def convert\_entities  
dummy = self.dup  
{  
“#822[01]” =\> “”",  
“#821[67]” =\> “’”,  
“#8230” =\> “…”,  
“#8211” =\> “-”,  
“#8212” =\> “–”,  
“#215” =\> “x”,  
“gt” =\> “\>”,  
“lt” =\> “\<”,  
“(#8482|trade)” =\> “™”,  
“(#174|reg)” =\> “®”,  
“(#169|copy)” =\> “©”,  
“(#38|amp)” =\> “&”,  
“nbsp” =\> " “,  
“cent” =\> " cent”,  
“pound” =\> " pound",  
}.each do |textiled, normal|  
dummy.gsub!(/&#{textiled};/, normal)  
end  
dummy.gsub(/&[^;]+;/, “”)  
end

---

<div class="post-metadata">

**Author:** ![emuen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/96bed5/32.png) [@emuen](https://www.ruby-forum.com/u/emuen)\
**Post date:** [January 22, 2007, 4:33pm UTC](https://www.ruby-forum.com/t/problems-using-the-redcloth-gem/82815/6 "2007-01-22T16:33:43Z")

</div>

I do need to remove ALL HTML enteties from the text, before rendering  
it.

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
