Hi all,
Is it possible to configure red cloth to escape a block such as:
Hello, this works beautifull
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
On 13/08/06, Harm de Laat [email protected] 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
…
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
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