Skip to content

Commit

Permalink
Minor fixes
Browse files Browse the repository at this point in the history
* Use TagInfo.whitespace (why else did we create it...)
* Move regex to TagInfo (it is the class who parses the results)
* Hide/remove unused groups/results
  • Loading branch information
veger committed May 28, 2019
1 parent 845a2d2 commit 2901c88
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
16 changes: 10 additions & 6 deletions lib/ruby-bbcode/tag_info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ module RubyBBCode
# This class was made mostly just to keep track of all of the confusing the logic conditions that are checked.
#
class TagInfo
REGEX_STRING = '(?:(\[ (\/)? (\* | (?:\w+)) ((?:=[^\[\]]+) | (?:\s\w+=\w+)* | (?:[^\]]*))? \] (\s*)) | ([^\[]+))'.gsub(' ', '').freeze
REGEX = /#{REGEX_STRING}/i.freeze

COMPLETE_MATCH = 0
CLOSING_MATCH = 2
TAG_MATCH = 3
TAG_PARAM_MATCH = 5
WHITESPACE_AFTER_TAG = 9
CLOSING_MATCH = 1
TAG_MATCH = 2
TAG_PARAM_MATCH = 3
WHITESPACE_AFTER_TAG = 4
TEXT = 5

def initialize(tag_info, dictionary)
@tag_data = find_tag_info(tag_info, dictionary)
Expand Down Expand Up @@ -109,7 +113,7 @@ def default_tag_info(tag_info)
# Returns the tag hash
def find_tag_info(tag_info, dictionary)
ti = default_tag_info(tag_info)
ti[:is_tag] = (tag_info[COMPLETE_MATCH].start_with? '[')
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
Expand Down Expand Up @@ -148,7 +152,7 @@ def find_tag_info(tag_info, dictionary)
end
else
# Plain text
ti[:text] = tag_info[COMPLETE_MATCH]
ti[:text] = tag_info[TEXT]
end
ti
end
Expand Down
7 changes: 3 additions & 4 deletions lib/ruby-bbcode/tag_sifter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,14 @@ def valid?
# once this tree is built, the to_html method can be invoked where the tree is finally
# converted into HTML syntax.
def process_text
regex_string = '((\[ (\/)? ( \* | (\w+)) ((=[^\[\]]+) | (\s\w+=\w+)* | ([^\]]*))? \] (\s*)) | ([^\[]+))'
@text.scan(/#{regex_string}/ix) do |tag_info|
@text.scan(TagInfo::REGEX) do |tag_info|
@ti = TagInfo.new(tag_info, @dictionary)

validate_element

case @ti.type
when :opening_tag
element = { is_tag: true, tag: @ti[:tag], definition: @ti.definition, opening_whitespace: @ti[:whitespace], errors: @ti[:errors], nodes: TagCollection.new }
element = { is_tag: true, tag: @ti[:tag], definition: @ti.definition, opening_whitespace: @ti.whitespace, errors: @ti[:errors], nodes: TagCollection.new }
element[:invalid_quick_param] = true if @ti.invalid_quick_param?
element[:params] = get_formatted_element_params

Expand Down Expand Up @@ -76,7 +75,7 @@ def process_text

create_text_element
when :closing_tag
@bbtree.current_node[:closing_whitespace] = @ti[:whitespace]
@bbtree.current_node[:closing_whitespace] = @ti.whitespace
if @ti[:wrong_closing]
# Convert into text, so it
@ti.handle_tag_as_text
Expand Down

0 comments on commit 2901c88

Please sign in to comment.