RbbCode has been updated! The new release (1.x.x) is not compatible with the old one (0.1.11). If you want to upgrade to 1.x.x, you'll need to adjust any calls to RbbCode in your code to match the new API, as described below.
RbbCode converts BBCode to HTML and Markdown. Basic usage:
RbbCode.new.convert('This is [b]BBCode[/b]')
RbbCode recovers gracefully from invalid markup. Any bad BBCode tags will remain in the output as-is, i.e. they will not be converted to HTML tags and will be visible to end users. All HTML output is passed through the Sanitize gem. This protects you against malicious HTML.
For the curious, the parser is built with Treetop. But you don't need to know anything about Treetop to use RbbCode.
gem install rbbcode
Both the constructor (RbbCode.new
) and RbbCode#convert
can accept an options hash. Options passed to convert
will override options passed to new
:
RbbCode
.new(:output_format => :html)
.convert('This is [b]BBCode[/b]', :output_format => :markdown)
# => "This is **BBCode**\n\n"
You can set the :output_format
to either :html
or :markdown
. If not specified, :output_format
defaults
to :html
.
RbbCode.new(:output_format => :html)
RbbCode.new(:output_format => :markdown)
To add emoticon support:
RbbCode.new(:emoticons => {':)' => 'http://example.com/path/to/your/smiley.png'})
You can supply a Sanitize config hash, which will
be passed through verbatim to the Sanitize gem. The default Sanitize config is in
rbbcode/sanitize.rb
. Usage:
RbbCode.new(:sanitize_config => my_sanitize_config_hash)
You can also turn Sanitize off altogether, but it's not recommended:
# Probably a bad idea
RbbCode.new(:sanitize => false)
Sanitization works for HTML output only. If you select a different output format, the :sanitize
option will be ignored, and the output won't be sanitized.
RbbCode supports the following BBCode features:
- [b]
- [i]
- [u]
- [s]
- [url]
- [img]
- [quote]
- [list]
- Emoticons
Some BBCode parsers in the wild have more features. This varies from forum to forum. If there's a certain tag or other feature you'd like to see supported, please open an issue on: