<commit_before>module RubyBBCode
  # As you parse a string of text, say:
  #     "[b]I'm bold and the next word is [i]ITALLICS[/i][b]"
  # ...you build up a tree of nodes.  The above string converts to 2 nodes when it's completed the parse.
  # Node 1)  An opening tag node representing "[b]"
  # Node 2)  A text node         representing "I'm bold and the next word is "
  # Node 3)  An opening tag node representing "[i]"
  # Node 4)  A text node         representing "ITALLICS"
  #
  # The closing of the nodes seems to be implied which is fine by me --less to keep track of.  
  #  
  class BBTree
    def initialize(hash = {}, dictionary)
      @bbtree = hash
      
      # I have a stacking problem....
      # The @bbtree contains a current_node which I suppose should be of the type TagNode, OR just a plain hash...
      # But during the retrogress process, sometimes the @bbtree get's written to the @current_node... thus current node is of the type
      # BBTree.......  
      @current_node = TagNode.new(@bbtree)
      @tags_list = []
      @dictionary = dictionary
      @definition = nil
    end
    
    def [](key)
      @bbtree[key]
    end
    
    def []=(key, value)
      #binding.pry
      @bbtree[key] = value
    end
    
    def tags_list
      @tags_list
    end
    
    # Advance to next level (the node we just added)
    def escalate_bbtree(element)
      @tags_list.push element[:tag]
      @current_node = TagNode.new(element)
    end
    
    # Step down the bbtree a notch because we've reached a closing tag
    def retrogress_bbtree
      @tags_list.pop     # remove latest tag in tags_list since it's closed now... where's the manifestation of the parsed data go???

      # Since we just stepped down we should set the current node to be the @bbtree...
      # This works because the @bbtree includes everything except for the currently open node (which is being worked on)
      # ...But where does the node get stored...  
      @current_node = TagNode.new(@bbtree) # Set current_node to be the whole @bbtree
      
      if within_open_tag?
        # Set the current node to be the node we've just parsed over which is infact within another node??...
        @current_node = TagNode.new(@current_node[:nodes].last)
      else # if we're still at the root of the BBTree or have returned to the root via encountring closing tags...
        @current_node = TagNode.new(@bbtree)
      end
    end
    
    # STEPS TO GET ESCALATE AND RETROGRESS WITHIN THIS CLASS:
    #
    # 1)  Refactor @bbtree_current_node -> @bbtree.current_node
    # 2)  Move escalate_bbtree(element) and retrogress_bbtree into the class
    # 3)  Profit
    
    attr_accessor :current_node
    
    def parent_tag
      return nil if @tags_list.last.nil?
      @tags_list.last.to_sym
    end
    
    def parent_has_constraints_on_children?
      @dictionary[parent_tag][:only_allow] != nil
    end
    

    
    # This method might never be needed for anything other than determining within_open_tag?
    def depth
      @tags_list.length
    end
    
    def expecting_a_closing_tag?
      @tags_list.length > 0
    end
    
    def within_open_tag?
      @tags_list.length > 0
    end
    
  end
end<commit_msg>tidy'd up bbtree.rb<commit_after>module RubyBBCode
  # As you parse a string of text, say:
  #     "[b]I'm bold and the next word is [i]ITALLICS[/i][b]"
  # ...you build up a tree of nodes (@bbtree).  The above string converts to 4 nodes when the parse has completed.
  # Node 1)  An opening tag node representing "[b]"
  # Node 2)  A text node         representing "I'm bold and the next word is "
  # Node 3)  An opening tag node representing "[i]"
  # Node 4)  A text node         representing "ITALLICS"
  #
  # The closing of the nodes seems to be implied which is fine by me --less to keep track of.  
  #  
  class BBTree
    attr_accessor :current_node, :tags_list
    
    def initialize(hash = {}, dictionary)
      @bbtree = hash
      @current_node = TagNode.new(@bbtree)
      @tags_list = []
      @dictionary = dictionary
    end
    
    def [](key)
      @bbtree[key]
    end
    
    def []=(key, value)
      @bbtree[key] = value
    end
    
    def within_open_tag?
      @tags_list.length > 0
    end
    alias :expecting_a_closing_tag? :within_open_tag?  # just giving this method multiple names for semantical purposes
    
    def parent_tag
      return nil if !within_open_tag?
      @tags_list.last.to_sym
    end
    
    def parent_has_constraints_on_children?
      @dictionary[parent_tag][:only_allow] != nil
    end
    
    
    
    # Advance to next level (the node we just added)
    def escalate_bbtree(element)
      @tags_list.push element[:tag]
      @current_node = TagNode.new(element)
    end
    
    # Step down the bbtree a notch because we've reached a closing tag
    def retrogress_bbtree
      @tags_list.pop     # remove latest tag in tags_list since it's closed now... where's the manifestation of the parsed data go???

      # Since we just stepped down we should set the current node to be the @bbtree...
      # This works because the @bbtree includes everything except for the currently open node (which is being worked on)
      # ...But where does the node get stored...  
      @current_node = TagNode.new(@bbtree) # Set current_node to be the whole @bbtree
      
      if within_open_tag?
        # Set the current node to be the node we've just parsed over which is infact within another node??...
        @current_node = TagNode.new(@current_node[:nodes].last)
      else # if we're still at the root of the BBTree or have returned to the root via encountring closing tags...
        @current_node = TagNode.new(@bbtree)
      end
    end
    
  end
end
{'fraction_non_alphanumeric': 0.07811412665274878, 'fraction_numerical': 0.0027835768963117608, 'mean_word_length': 2.4363419007770473, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5868349', 'n_tokens_mistral': 1771, 'n_tokens_neox': 1743, 'n_words': 671}