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
Public Class Methods
# File lib/ruby-bbcode.rb, line 19 def self.configuration @configuration ||= Configuration.new end
# File lib/ruby-bbcode.rb, line 27 def self.configure yield(configuration) end
# File lib/ruby-bbcode.rb, line 23 def self.reset @configuration = Configuration.new end
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
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
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
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