Class: RubyBBCode::TagInfo

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-bbcode/tag_info.rb

Overview

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.

Constant Summary collapse

REGEX_STRING =
'(?:(\[ (\/)? (\* | (?:\w+)) ((?:=[^\[\]]+) | (?:\s\w+=\w+)* | (?:[^\]]*))? \] (\s*)) | ([^\[]+))'.gsub(' ', '').freeze
REGEX =
/#{REGEX_STRING}/i.freeze
COMPLETE_MATCH =
0
CLOSING_MATCH =
1
TAG_MATCH =
2
TAG_PARAM_MATCH =
3
WHITESPACE_AFTER_TAG =
4
TEXT =
5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tag_info, dictionary) ⇒ TagInfo

Returns a new instance of TagInfo.



16
17
18
# File 'lib/ruby-bbcode/tag_info.rb', line 16

def initialize(tag_info, dictionary)
  @tag_data = find_tag_info(tag_info, dictionary)
end

Instance Attribute Details

#definitionObject (readonly)

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



29
30
31
# File 'lib/ruby-bbcode/tag_info.rb', line 29

def definition
  @definition
end

Instance Method Details

#[](key) ⇒ Object



20
21
22
# File 'lib/ruby-bbcode/tag_info.rb', line 20

def [](key)
  @tag_data[key]
end

#[]=(key, value) ⇒ Object



24
25
26
# File 'lib/ruby-bbcode/tag_info.rb', line 24

def []=(key, value)
  @tag_data[key] = value
end

#allowed_in?(parent_tag) ⇒ Boolean

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

Returns:

  • (Boolean)


86
87
88
# 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?Boolean

Returns true if this tag has quick parameter support

Returns:

  • (Boolean)


91
92
93
# File 'lib/ruby-bbcode/tag_info.rb', line 91

def can_have_quick_param?
  @definition[:allow_quick_param]
end

#element_is_closing_tag?Boolean

Returns true if this instance represents a closing tag element

Returns:

  • (Boolean)


71
72
73
# 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?Boolean

Returns true if this instance represents an opening tag element

Returns:

  • (Boolean)


66
67
68
# File 'lib/ruby-bbcode/tag_info.rb', line 66

def element_is_opening_tag?
  self[:is_tag] && !self[:closing_tag]
end

#element_is_tag?Boolean

Returns true if this instance represents a tag element

Returns:

  • (Boolean)


56
57
58
# File 'lib/ruby-bbcode/tag_info.rb', line 56

def element_is_tag?
  self[:is_tag]
end

#element_is_text?Boolean

Returns true if this instance represents a text element

Returns:

  • (Boolean)


61
62
63
# File 'lib/ruby-bbcode/tag_info.rb', line 61

def element_is_text?
  !self[:is_tag]
end

#handle_tag_as_textObject

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



49
50
51
52
53
# 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?Boolean

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

Returns:

  • (Boolean)


96
97
98
# 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?Boolean

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.

Returns:

  • (Boolean)


81
82
83
# File 'lib/ruby-bbcode/tag_info.rb', line 81

def only_allowed_in_parent_tags?
  !@definition[:only_in].nil?
end

#tag_in_dictionary?Boolean

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

Returns:

  • (Boolean)


76
77
78
# File 'lib/ruby-bbcode/tag_info.rb', line 76

def tag_in_dictionary?
  !@definition.nil?
end

#textObject

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



32
33
34
# File 'lib/ruby-bbcode/tag_info.rb', line 32

def text
  @tag_data[:text]
end

#typeObject

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



42
43
44
45
46
# 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

#whitespaceObject

Returns the whitespace that was available directly after the tag definition



37
38
39
# File 'lib/ruby-bbcode/tag_info.rb', line 37

def whitespace
  @tag_data[:whitespace]
end