config/initializers/core_extensions_2.rb
# this method is designed to make sure a text string has equal numbers of opening and closing forms of certain
# tags in the proper order (shouln't open one after closing one for example - that is not balanced)
this method is designed to make sure a text string has equal numbers of opening and closing forms of certain
tags in the proper order (shouln't open one after closing one for example - that is not balanced)
this method is designed to make sure a text string has equal numbers of opening and closing forms of certain tags in the proper order ( shouln ' t open one after closing one for example - that is not balanced )
def balance_tags
    text = self.clone
    ['a', 'b', 'i', 'em', 'strong', 'span', 'blockquote', 'li', 'ul', 'p', 'div'].each do |tag|
      open_and_close_tags = text.scan(/\<(#{tag}[\> ]|\/ *#{tag}\>)/i)
      number_of_opening_tags_needed = 0
      total_open = 0
      total_closed = 0
      current_balance = 0
      previous_balance = 0
      open_and_close_tags.each do |match|
        t = match[0]
        if t[0, 1] == "/"  
          total_closed += 1
          current_balance -= 1
        else
          total_open += 1
          current_balance += 1
        end
        if current_balance < 0 && current_balance < previous_balance
          number_of_opening_tags_needed += 1
          current_balance += 1
        end
        previous_balance = current_balance
      end
      text = "<#{tag}>" * number_of_opening_tags_needed + text
      total_open += number_of_opening_tags_needed
      difference = total_open - total_closed
      if difference < 0  
        text = "<#{tag}>" * difference.abs + text
      elsif difference > 0  
        text += "</#{tag}>" * difference
      end
    end
    return text.gsub(/<em\/>/i, '')
  end
def balance_tags text = self . clone [ 'a' , 'b' , 'i' , 'em' , 'strong' , 'span' , 'blockquote' , 'li' , 'ul' , 'p' , 'div' ] . each do | tag | open_and_close_tags = text . scan ( / \< ( #{ tag } [ \>  ]| \/  * #{ tag } \> ) /i ) number_of_opening_tags_needed = 0 total_open = 0 total_closed = 0 current_balance = 0 previous_balance = 0 open_and_close_tags . each do | match | t = match [ 0 ] if t [ 0 , 1 ] == "/" total_closed += 1 current_balance -= 1 else total_open += 1 current_balance += 1 end if current_balance < 0 && current_balance < previous_balance number_of_opening_tags_needed += 1 current_balance += 1 end previous_balance = current_balance end text = "<#{tag}>" * number_of_opening_tags_needed + text total_open += number_of_opening_tags_needed difference = total_open - total_closed if difference < 0 text = "<#{tag}>" * difference . abs + text elsif difference > 0 text += "</#{tag}>" * difference end end return text . gsub ( / <em \/ > /i , '' ) end
this method is designed to make sure a text string has equal numbers of opening and closing forms of certain
tags in the proper order (shouln't open one after closing one for example - that is not balanced)
this method is designed to make sure a text string has equal numbers of opening and closing forms of certain tags in the proper order ( shouln ' t open one after closing one for example - that is not balanced )
# The order here matters. Tags that are typically containers should come LAST: # this will match <T_, <T>, </_T>, </T> # its a closing tag # adding to the beginning opening tags if there are close tags before open tags of the same type # more closed tags than open # more open tags than closed