RedCloth 4.1.1 vs. 4.2.3 - weird behaviour with notextile

For a community project I aim to combine RedCloth and Coderay and on doing this, I might have found an issue with RedCloth. I pushed a demo to Github: https://github.com/markusproske/redcloth_coderay_demo The index (http://localhost:3000/) demonstrates the issue.

In brief: A page consist of textile. The textile contains @@@ruby somecode @@@ The textile is first feed into Coderay via a helper method and then into RedCloth. To prevent RedCloth from applying markup to the code already rendered by Coderay it is wrapped with which should prevent RedCloth from touching this sections. This works with the fairly old RedCloth 4.1.1 but not with 4.1.9, 4.2.2 and 4.2.3. In these versions, odd effects occur doubling content (and destroying style). I am posting this here as I am quite new to Rails and uncertain, whether this is actually a bug or an error on my side :confused:

The Helper: def coderay_dressed(text) text.gsub!(/@@@(\w*?)\s+(.+?)\s*@@@/m) do $1 != ‘’ ? @lang = $1 : @lang = ‘none’ code = CodeRay.highlight($2, @lang, :css => :class) “#{code}” end return text.html_safe end

Second, there is another issue which I am sure is my fault. Some problem with safe strings I suppose - maybe you can help me out with this one: 3a.) that works: raw coderay_dressed(‘@@@ruby @products = Product.all *Redcloth…’) but if I push the text to @test and call “raw coderay_dressed(@test)” (example 3b in the index) I loose the tags. In the show view I directly feed the @article.body “sanitize textilize(coderay_dressed(@article.body))” this works too. Why the difference with using a self-assigned variable and how to fix it? (I tried @test.safe_html with no success).

cond, there is another issue which I am sure is my fault. Some problem

with safe strings I suppose - maybe you can help me out with this one: 3a.) that works: raw coderay_dressed('@@@ruby @products = Product.all *Redcloth....') but if I push the text to @test and call "raw coderay_dressed(@test)"
(example 3b in the index) I loose the <notextile> tags. In the show view I directly feed the @article.body "sanitize textilize(coderay_dressed(@article.body))" this works too. Why the difference with using a self-assigned variable and how to fix it?
(I tried @test.safe_html with no success).

Your various helpers modify their argument in place (you're calling gsub!), and you call your helpers on @test multiple times, so on the 2nd and later calls, @test probably doesn't contain what you expected anymore

Fred

Your various helpers modify their argument in place (you’re calling

gsub!), and you call your helpers on @test multiple times, so on the

2nd and later calls, @test probably doesn’t contain what you expected

anymore

This is embarrassing :confused: Thanks Fred, the minor issue is solved (and pushed to git). Remains the fact this works with ancient RedCloth but not current versions…

Markus