Module: RubyBBCode

Includes:
Tags
Defined in:
lib/ruby-bbcode.rb,
lib/tags/tags.rb,
lib/ruby-bbcode/bbtree.rb,
lib/ruby-bbcode/version.rb,
lib/ruby-bbcode/tag_info.rb,
lib/ruby-bbcode/tag_node.rb,
lib/ruby-bbcode/tag_sifter.rb,
lib/ruby-bbcode/tag_collection.rb

Overview

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.

Defined Under Namespace

Modules: Tags, Templates Classes: BBTree, TagCollection, TagInfo, TagNode, TagSifter

Constant Summary collapse

VERSION =

Version of RubyBBCode

Follows semantic versioning: semver.org/

"2.0.0"

Class Method Summary collapse

Methods included from Tags

tag_list

Class Method Details

.to_bbcode(text, additional_tags = {}, method = :disable, *tags) ⇒ Object

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



33
34
35
36
37
# File 'lib/ruby-bbcode.rb', line 33

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) ⇒ Object

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



19
20
21
22
23
24
25
26
27
# File 'lib/ruby-bbcode.rb', line 19

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) ⇒ Object

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



40
41
42
43
44
45
46
47
# File 'lib/ruby-bbcode.rb', line 40

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