Congrats on shipping RedCloth 4.0! It looks like to fix a few problems
that I’ve had with it.
I do have a quick question: how do I disable certain rules? For
example, Superusers/Administrators are allowed to use images, but
regular users are not. There does not appear to be much documentation
about disabling/custom rules.
Ryan Alyea
Fangamer Founder, Lead Developer
[email protected]
That’s correct; disabling specific rules is not something I
anticipated. Were you able to disable certain rules with 3.0.4? The
restrictions :filter_html and :sanitize_html persist in this new
version. Are you aware of those? Do they meet your needs?
I can think of a couple of image-specific workarounds: One is to
prefilter your text before it ever hits RedCloth. Just one regex
looking for images shouldn’t be too expensive. The other is to
modify the output of just images by redefining that method in a new
module that you use to extend the RedCloth object when the user isn’t
a superuser.
Best,
Jason
Yeah, it was really easy to disable whole rules with
RedCloth(string).to_html(array_of_rules_to_run). For example my
array_of_rules has images added for Administrators.
In addition, some of my users didn’t like how loose the strikethrough
was. It was really easy to disable by putting
RedCloth::QTAGS.reject!{|x| x[0] == “-”}
in my Rails initializers.
The HTML filtering is post rendering, and while it might work, I would
rather the filter chain not render what I do not want instead of post-
filtering.
I thought of extending the RedCloth::Formatters::HTML, or creating my
own. The problem is that it’s not very future-forward. I would have to
update my custom Formatters to fit the latest version.
If there are no explicit disablers, do you mind if I fork and play
around? Ideally I would love for my end to be:
body = RedCloth(body_original).to_html(:disable_images unless
@user.superuser?)
where disable_images is a rule from a custom module I included.
Ryan Alyea
Fangamer Founder, Lead Developer
[email protected]
Fork away. That’s what GitHub is for!
Okay, I forked and created a copy that works for me. You can take a
look at it here: GitHub - ROFISH/redcloth: RedCloth is a Ruby library for converting Textile into HTML. This fork is the customizations for use with the Fangamer Forums. It includes BBCode support and ability to disable parsing certain elements.
The syntax is a lovely RedCloth.new(“string”,
[:disable_inline=>:strong]). In addition to images, DRYing things up
allowed me to disable any inline element I wanted.
Ryan Alyea
Fangamer Founder, Lead Developer
[email protected]
Hello,
I am a Ruby newbie, but I would like to disable a specific rule. I do
not want © to become the copyright symbol and ® to become the
registered symbol. I commented our all the instances I could find of
these in the various RedCloth files, but it does not seem to be working.
How can I remove a rule? And will it affect my ability to upgrade when
the new RedCloth comes out?
Thanks,
Jonathan
Well, off the top of my head I don’t know how you can disable a rule
so it isn’t recognized, but probably the easiest way to have the same
net result is to change the formatting that happens when it is
recognized so that it’s unchanged.
$ irb -rubygems
irb(main):001:0> require ‘RedCloth’
=> true
irb(main):003:0> RedCloth.new(‘This is my trademark®’).to_html
=> “
This is my trademark®
”
irb(main):004:0> module RedCloth::Formatters::HTML
irb(main):005:1> def registered(opts)
irb(main):006:2> " ®"
irb(main):007:2> end
irb(main):008:1> end
=> nil
irb(main):009:0> RedCloth.new(‘This is just a normal ®.’).to_html
=> “
This is just a normal ®.
”
Now the problem is that by design the registered symbol consumes any
spaces before it. I don’t have a solution for that.
The other thing you could do is to preprocess your text and surround
any instance of ® or © with one of the Textile escaping patterns.
$ irb -rubygems
irb(main):001:0> require ‘RedCloth’
=> true
irb(main):002:0> text = “No trademarks ® in here!”
=> “No trademarks ® in here!”
irb(main):003:0> text.gsub!(/(([cr]))/, “==\1==”)
=> “No trademarks ==®== in here!”
irb(main):004:0> RedCloth.new(text).to_html
=> “
No trademarks ® in here!
”
This latter suggestion is likely to never break with an upgrade; the
former one will only break if the name of the method changes.
Good luck!
Jason