Hi all, Is it possible to configure red cloth to escape a block such as: ------------------------------------------------------------------ Hello, _this_ works *beautifull* [code lang="ruby"] def foo bar end [/code] ------------------------------------------------------------------ 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 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060813/a35137b5/attachment.html
On 13/08/06, Harm de Laat <harmdelaat@gmail.com> 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[code lang=\"ruby\"]\n def foo\n bar\nend\n[/code]" toredcloth = str.split(/code/)[0] Red_Cloth::to_html toredcloth ... -- Cheers, Hasan Diwan <hasan.diwan@gmail.com> -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060814/f170109d/attachment-0001.html
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 On 8/13/06, Hasan Diwan <hasan.diwan@gmail.com> wrote:> On 13/08/06, Harm de Laat <harmdelaat@gmail.com> 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[code lang=\"ruby\"]\n def foo\n > bar\nend\n[/code]" > toredcloth = str.split(/code/)[0] > Red_Cloth::to_html toredcloth > ... > -- > Cheers, > Hasan Diwan <hasan.diwan@gmail.com> > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >
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 [code][/code] 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
On 8/14/06, Payton Swick <payton@foolord.com>
wrote:>
> 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
>
> On 8/13/06, Hasan Diwan <hasan.diwan@gmail.com> wrote:
> > On 13/08/06, Harm de Laat <harmdelaat@gmail.com> 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[code
lang=\"ruby\"]\n def
> foo\n
> > bar\nend\n[/code]"
> > toredcloth = str.split(/code/)[0]
> > Red_Cloth::to_html toredcloth
> > ...
> > --
> > Cheers,
> > Hasan Diwan <hasan.diwan@gmail.com>
> > _______________________________________________
> > Rails mailing list
> > Rails@lists.rubyonrails.org
> > http://lists.rubyonrails.org/mailman/listinfo/rails
> >
> >
> >
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://wrath.rubyonrails.org/pipermail/rails/attachments/20060817/0517e2c9/attachment-0001.html