Using BlueCloth how can I turn off markdown for part of the text?

156 Views Asked by At

We're using Bluecloth for the text formatting on a rails website but I've run into a problem with some email addresses. When they have double underscores in the address BlueCloth is translating it into EM tags.

In the rails console:

BlueCloth::new("[email protected]").to_html
"<p>[email protected]</p>"

An email address with a single underscore works fine, but when there are two underscores in the address the underscores are changed to em tags:

BlueCloth::new("[email protected]").to_html
"<p>bob<em>jones</em>[email protected]</p>"

I need the formatting for the rest of the text but I need away to turn it off for the email addresses. I feel like I used to be able to use a notextile tag but that doesn't seem to work anymore.

BlueCloth::new("<notextile> [email protected] </notextile>").to_html
"<p><notextile> bob<em>jones</em>[email protected] </notextile></p>"

Anyone know how to deal with this? We're using bluecloth version 2.2.0.

2

There are 2 best solutions below

0
On BEST ANSWER

One option is to switch the :relaxed setting to true:

BlueCloth.new('[email protected]', relaxed: true).to_html

The option documentation is available online but the option names and the way they map to the options in the underlying C library is not always obvious.

If you want to use the default options for text where you need in-word emphasis in some places but not others you can explicitly escape the underscores:

escaped_email = '[email protected]'.gsub('_', '\_')
BlueCloth.new(escaped_email).to_html
1
On

Why don't you use string interpolation?

BlueCloth::new("don't format this: %s").to_html % '[email protected]' 
=> "<p>don't format this: [email protected]</p>"

Unless you don't know where you want to keep the original string, which would be... odd.