module RubyBBCode

RubyBBCode adds support for BBCode to Ruby. The BBCode is parsed by a parser before converted to HTML, allowing to convert nested BBCode tags in strings to their correct HTML equivalent. The used parser also checks whether the BBCode is valid and gives errors for incorrect BBCode texts.

Constants

VERSION

Version of RubyBBCode

Follows semantic versioning: semver.org/

Attributes

configuration[W]

Public Class Methods

configuration() click to toggle source
# File lib/ruby-bbcode.rb, line 19
def self.configuration
  @configuration ||= Configuration.new
end
configure() { |configuration| ... } click to toggle source
# File lib/ruby-bbcode.rb, line 27
def self.configure
  yield(configuration)
end
reset() click to toggle source
# File lib/ruby-bbcode.rb, line 23
def self.reset
  @configuration = Configuration.new
end
to_bbcode(text, additional_tags = {}, method = :disable, *tags) click to toggle source

This method converts the given text (with BBCode tags) into a HTML representation The additional_tags parameter is used to add additional BBCode tags that should be accepted The method parameter determines whether the tags parameter needs to be used to blacklist (when set to :disable) or whitelist (when not set to :disable) the list of BBCode tags The method raises an exception when the text could not be parsed due to errors

# File lib/ruby-bbcode.rb, line 50
def self.to_bbcode(text, additional_tags = {}, method = :disable, *tags)
  parse(text, true, additional_tags, method, *tags)
  use_tags = determine_applicable_tags(additional_tags, method, *tags)
  @tag_sifter.bbtree.to_bbcode(use_tags)
end
to_html(text, escape_html = true, additional_tags = {}, method = :disable, *tags) click to toggle source

This method converts the given text (with BBCode tags) into a HTML representation The escape_html parameter (default: true) escapes HTML tags that were present in the given text and therefore blocking (mallicious) HTML in the original text The additional_tags parameter is used to add additional BBCode tags that should be accepted The method parameter determines whether the tags parameter needs to be used to blacklist (when set to :disable) or whitelist (when not set to :disable) the list of BBCode tags The method raises an exception when the text could not be parsed due to errors

# File lib/ruby-bbcode.rb, line 36
def self.to_html(text, escape_html = true, additional_tags = {}, method = :disable, *tags)
  parse(text, escape_html, additional_tags, method, *tags)
  use_tags = determine_applicable_tags(additional_tags, method, *tags)

  # We cannot convert to HTML if the BBCode is not valid!
  raise @tag_sifter.errors.join(', ') unless @tag_sifter.valid?

  @tag_sifter.bbtree.to_html(use_tags)
end
validity_check(text, additional_tags = {}, method = :disable, *tags) click to toggle source

Returns true when valid, else returns array with error(s)

# File lib/ruby-bbcode.rb, line 57
def self.validity_check(text, additional_tags = {}, method = :disable, *tags)
  use_tags = determine_applicable_tags(additional_tags, method, *tags)
  @tag_sifter = TagSifter.new(text, use_tags)

  @tag_sifter.process_text
  return @tag_sifter.errors unless @tag_sifter.valid?

  true
end

Protected Class Methods

determine_applicable_tags(additional_tags, method, *tags) click to toggle source

This method provides the final set of bbcode tags, it merges the default tags with the given additional_tags and blacklists(method = :disable) or whitelists the list of tags with the given tags parameter.

# File lib/ruby-bbcode.rb, line 72
def determine_applicable_tags(additional_tags, method, *tags)
  use_tags = @@tags.merge(additional_tags)
  if method == :disable
    # if method is set to :disable blacklist (remove) the supplied tags
    tags.each { |t| use_tags.delete(t) }
  else
    # only use the supplied tags (whitelist) if method is not :disable
    new_use_tags = {}
    tags.each { |t| new_use_tags[t] = use_tags[t] if use_tags.key?(t) }
    use_tags = new_use_tags
  end
  use_tags
end
parse(text, escape_html = true, additional_tags = {}, method = :disable, *tags) click to toggle source

This method parses the given text (with BBCode tags) into a BBTree representation The escape_html parameter (default: true) escapes HTML tags that were present in the given text and therefore blocking (mallicious) HTML in the original text The additional_tags parameter is used to add additional BBCode tags that should be accepted The method parameter determines whether the tags parameter needs to be used to blacklist (when set to :disable) or whitelist (when not set to :disable) the list of BBCode tags The method raises an exception when the text could not be parsed due to errors

# File lib/ruby-bbcode.rb, line 91
def parse(text, escape_html = true, additional_tags = {}, method = :disable, *tags)
  text = text.clone
  use_tags = determine_applicable_tags(additional_tags, method, *tags)

  @tag_sifter = TagSifter.new(text, use_tags, escape_html)
  @tag_sifter.process_text
end