# Escape block using red cloth

**URL:** <https://www.ruby-forum.com/t/escape-block-using-red-cloth/64798>\
**Category:** Rails\
**Created:** [August 15, 2006, 2:19pm UTC](https://www.ruby-forum.com/t/escape-block-using-red-cloth/64798 "2006-08-15T14:19:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Harm\_de\_Laat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/9de053/32.png) [@Harm\_de\_Laat](https://www.ruby-forum.com/u/Harm_de_Laat)\
**Post date:** [August 15, 2006, 2:19pm UTC](https://www.ruby-forum.com/t/escape-block-using-red-cloth/64798/1 "2006-08-15T14:19:07Z")

</div>

Hi all,

Is it possible to configure red cloth to escape a block such as:

* * *

Hello, _this_ works _beautifull_

```auto
def foo
   bar
end
```

* * *

The text in the [code] block should not be parsed by Red Cloth’s to\_html  
method.

I cannot find it in the docs.

Many regards,

Harm de Laat  
Kabisa ICT  
The Netherlands

---

<div class="post-metadata">

**Author:** ![Harm\_de\_Laat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/9de053/32.png) [@Harm\_de\_Laat](https://www.ruby-forum.com/u/Harm_de_Laat)\
**Post date:** [August 15, 2006, 2:19pm UTC](https://www.ruby-forum.com/t/escape-block-using-red-cloth/64798/2 "2006-08-15T14:19:29Z")

</div>

On 13/08/06, Harm de Laat [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> The text in the [code] block should not be parsed by Red Cloth’s to\_html  
> method.

I’d take out the code block, save it in another variable, hand the rest  
of  
it to Red Cloth’s to\_html method and put the variable back in the  
string.  
That seems as if it should work:  
str = “Hello, _this_ works _beautifull_\n\n`\n def 
foo\n
  bar\nend\n`”  
toredcloth = str.split(/code/)[0]  
Red\_Cloth::to\_html toredcloth  
…

---

<div class="post-metadata">

**Author:** ![Harm\_de\_Laat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/9de053/32.png) [@Harm\_de\_Laat](https://www.ruby-forum.com/u/Harm_de_Laat)\
**Post date:** [August 15, 2006, 2:19pm UTC](https://www.ruby-forum.com/t/escape-block-using-red-cloth/64798/3 "2006-08-15T14:19:50Z")

</div>

I’ve had this situation and ended up doing something very similar: I  
base64-encoded the block (leaving the marking tags) before running it  
through RedCloth and some of my other conversions and then decoded the  
blocks at the end (making sure to still run h() to prevent any harmful  
code).

-Payton

---

<div class="post-metadata">

**Author:** ![Harm\_de\_Laat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/9de053/32.png) [@Harm\_de\_Laat](https://www.ruby-forum.com/u/Harm_de_Laat)\
**Post date:** [August 17, 2006, 10:00pm UTC](https://www.ruby-forum.com/t/escape-block-using-red-cloth/64798/4 "2006-08-17T22:00:17Z")

</div>

For anyone who’s interesed, I ended up doing this:

require ‘syntaxi’

class Posting \< ActiveRecord::Base  
has\_many :comments  
belongs\_to :posting\_category

REGEX = /[code.\*?code]/m

# formattes the message using redcloth, colorizes ruby code using

syntax(i)  
def formatted\_message  
code\_blocks = get\_code\_blocks(self.message)  
red\_clothed = RedCloth.new(message.gsub(REGEX,  
‘${code\_block}’)).to\_html

```
code_blocks.each { |c|
  c = Syntaxi.new(c).process
  red_clothed.sub!(/\$\{code_block\}/, c)
}

red_clothed

```

end

# retrieves code in `` blocks

def get\_code\_blocks(contents)  
code\_blocks = []  
code\_block = contents.slice(REGEX)  
if (!code\_block.nil?)  
code\_blocks \<\< code\_block  
while code\_block = $’.slice(REGEX)  
code\_blocks \<\< code\_block  
end  
end  
code\_blocks  
end

end

This works fine!

Thanks all!

Regards,

Harm de Laat  
Kabisa ICT
