class RubyBBCode::TagInfo

TagInfo is basically what the regex scan get's converted into during the TagSifter#process_text method. This class was made mostly just to keep track of all of the confusing the logic conditions that are checked.

Constants

CLOSING_MATCH
COMPLETE_MATCH
REGEX
REGEX_STRING
TAG_MATCH
TAG_PARAM_MATCH
TEXT
WHITESPACE_AFTER_TAG

Attributes

definition[R]

Definition of this instance (when it represents a tag element)

Public Class Methods

new(tag_info, dictionary) click to toggle source
# File lib/ruby-bbcode/tag_info.rb, line 16
def initialize(tag_info, dictionary)
  @tag_data = find_tag_info(tag_info, dictionary)
end

Public Instance Methods

[](key) click to toggle source
# File lib/ruby-bbcode/tag_info.rb, line 20
def [](key)
  @tag_data[key]
end
[]=(key, value) click to toggle source
# File lib/ruby-bbcode/tag_info.rb, line 24
def []=(key, value)
  @tag_data[key] = value
end
allowed_in?(parent_tag) click to toggle source

Returns true if the tag element is allowed in the provided parent_tag

# File lib/ruby-bbcode/tag_info.rb, line 86
def allowed_in?(parent_tag)
  !only_allowed_in_parent_tags? || @definition[:only_in].include?(parent_tag)
end
can_have_quick_param?() click to toggle source

Returns true if this tag has quick parameter support

# File lib/ruby-bbcode/tag_info.rb, line 91
def can_have_quick_param?
  @definition[:allow_quick_param]
end
element_is_closing_tag?() click to toggle source

Returns true if this instance represents a closing tag element

# File lib/ruby-bbcode/tag_info.rb, line 71
def element_is_closing_tag?
  self[:is_tag] &&  self[:closing_tag]
end
element_is_opening_tag?() click to toggle source

Returns true if this instance represents an opening tag element

# File lib/ruby-bbcode/tag_info.rb, line 66
def element_is_opening_tag?
  self[:is_tag] && !self[:closing_tag]
end
element_is_tag?() click to toggle source

Returns true if this instance represents a tag element

# File lib/ruby-bbcode/tag_info.rb, line 56
def element_is_tag?
  self[:is_tag]
end
element_is_text?() click to toggle source

Returns true if this instance represents a text element

# File lib/ruby-bbcode/tag_info.rb, line 61
def element_is_text?
  !self[:is_tag]
end
handle_tag_as_text() click to toggle source

Converts this instance (from a tag) into a text element

# File lib/ruby-bbcode/tag_info.rb, line 49
def handle_tag_as_text
  self[:is_tag] = false
  self[:closing_tag] = false
  self[:text] = self[:complete_match]
end
invalid_quick_param?() click to toggle source

Returns true if the tag param matches the regex pattern defined in tags.rb

# File lib/ruby-bbcode/tag_info.rb, line 96
def invalid_quick_param?
  @tag_data.key? :invalid_quick_param
end
only_allowed_in_parent_tags?() click to toggle source

Returns true if the tag that is represented by this instance is restricted on where it is allowed, i.e. if it is restricted by certain parent tags.

# File lib/ruby-bbcode/tag_info.rb, line 81
def only_allowed_in_parent_tags?
  !@definition[:only_in].nil?
end
tag_in_dictionary?() click to toggle source

Returns true if this tag element is included in the set of available tags

# File lib/ruby-bbcode/tag_info.rb, line 76
def tag_in_dictionary?
  !@definition.nil?
end
text() click to toggle source

Returns the text (when this instance represents a text element)

# File lib/ruby-bbcode/tag_info.rb, line 32
def text
  @tag_data[:text]
end
type() click to toggle source

Returns the type of the cuvvrent tag/node, which is either :opening_tag, :closing_tag, or :text

# File lib/ruby-bbcode/tag_info.rb, line 42
def type
  return :opening_tag if element_is_opening_tag?
  return :text if element_is_text?
  return :closing_tag if element_is_closing_tag?
end
whitespace() click to toggle source

Returns the whitespace that was available directly after the tag definition

# File lib/ruby-bbcode/tag_info.rb, line 37
def whitespace
  @tag_data[:whitespace]
end

Protected Instance Methods

default_tag_info(tag_info) click to toggle source

Returns a default info structure used by all tags

# File lib/ruby-bbcode/tag_info.rb, line 103
def default_tag_info(tag_info)
  {
    errors: [],
    complete_match: tag_info[COMPLETE_MATCH],
    whitespace: tag_info[WHITESPACE_AFTER_TAG]
  }
end
find_tag_info(tag_info, dictionary) click to toggle source

Convert the result of the TagSifter#process_text regex into a more usable hash, that is used by the rest of the parser. tag_info should a result of the regex of TagSifter#process_text Returns the tag hash

# File lib/ruby-bbcode/tag_info.rb, line 114
def find_tag_info(tag_info, dictionary)
  ti = default_tag_info(tag_info)
  ti[:is_tag] = (tag_info[COMPLETE_MATCH]&.start_with? '[')
  if ti[:is_tag]
    ti[:closing_tag] = (tag_info[CLOSING_MATCH] == '/')
    ti[:tag] = tag_info[TAG_MATCH].to_sym.downcase
    ti[:params] = {}
    @definition = dictionary[ti[:tag]]
    if !tag_in_dictionary?
      # Tag is not defined in dictionary, so treat as text
      raise "unknown tag #{ti[:tag]}" if RubyBBCode.configuration.ignore_unknown_tags == :exception

      ti = default_tag_info(tag_info)
      ti[:is_tag] = false
      ti[:text] = if RubyBBCode.configuration.ignore_unknown_tags == :text
                    tag_info[COMPLETE_MATCH]
                  else
                    ''
                  end
    elsif (tag_info[TAG_PARAM_MATCH][0] == '=') && can_have_quick_param?
      quick_param = tag_info[TAG_PARAM_MATCH][1..-1]
      # Get list of parameter values and add them as (regular) parameters
      value_array = quick_param.scan(@definition[:quick_param_format])[0]
      if value_array.nil?
        ti[:invalid_quick_param] = quick_param
      else
        param_tokens = @definition[:param_tokens]
        value_array[0..param_tokens.length - 1].each.with_index do |value, i|
          ti[:params][param_tokens[i][:token]] = value
        end
      end
    elsif tag_info[TAG_PARAM_MATCH][0] == "\s"
      regex_string = '((\w+)=([\w#]+)) | ((\w+)="([^"]+)") | ((\w+)=\'([^\']+)\')'
      tag_info[TAG_PARAM_MATCH].scan(/#{regex_string}/ix) do |param_info|
        param = param_info[1] || param_info[4] || param_info[7]
        value = param_info[2] || param_info[5] || param_info[8]
        ti[:params][param.to_sym] = value
      end
    end
  else
    # Plain text
    ti[:text] = tag_info[TEXT]
  end
  ti
end