<commit_before>module Bliss
  class SaxParser < Nokogiri::XML::SAX::Document
    def initialize
      @depth = []
      # @settings = {} # downcased

      @root = nil
      @nodes = {}
      @current_node = {}

      @on_root = nil

      @on_tag_open = {}
      @on_tag_close = {}

      @closed = false

    end

    def on_root(&block)
      @on_root = block
    end

    def on_tag_open(element, block)
      @on_tag_open.merge!({element => block})
    end

    def on_tag_close(element, block)
      @on_tag_close.merge!({element => block})
    end

    def close
      @closed = true
    end

    def is_closed?
      @closed
    end

    def start_element(element, attributes)
      # element_transformation

      if @root == nil
        @root = element
        if @on_root.is_a? Proc
          @on_root.call(@root)
        end
      end

      @depth.push(element) if @depth.last != element
      
      if @on_tag_open.has_key? element
        @on_tag_open[element].call(@depth)
      elsif @on_tag_open.has_key? 'default'
        @on_tag_open['default'].call(@depth)
      end

      current = @nodes.pair_at_chain(@depth)

      value_at = @nodes.value_at_chain(@depth)
      
      if current.is_a? Hash
        if value_at.is_a? NilClass
          current[element] = {}
        elsif value_at.is_a? Hash
          if current[element].is_a? Array
            current[element].concat [{}]
          else
            current[element] = [current[element], {}]
            #current = @nodes.pair_at_chain(@depth)
          end
        elsif value_at.is_a? Array
          #puts @depth.inspect
          #puts current[element].inspect
          #puts current[element].inspect
        end
      elsif current.is_a? Array
      end

      @current_content = ''
    end

    def characters(string)
      concat_content(string)
    end

    def cdata_block(string)
      concat_content(string)
    end

    def end_element(element, attributes=[])<commit_msg>Stops SAX processing when SaxParser.is_closed? equals true<commit_after>module Bliss
  class SaxParser < Nokogiri::XML::SAX::Document
    def initialize
      @depth = []
      # @settings = {} # downcased

      @root = nil
      @nodes = {}
      @current_node = {}

      @on_root = nil

      @on_tag_open = {}
      @on_tag_close = {}

      @closed = false

    end

    def on_root(&block)
      @on_root = block
    end

    def on_tag_open(element, block)
      @on_tag_open.merge!({element => block})
    end

    def on_tag_close(element, block)
      @on_tag_close.merge!({element => block})
    end

    def close
      @closed = true
    end

    def is_closed?
      @closed
    end

    def start_element(element, attributes)
      return if is_closed?
      # element_transformation

      if @root == nil
        @root = element
        if @on_root.is_a? Proc
          @on_root.call(@root)
        end
      end

      @depth.push(element) if @depth.last != element
      
      if @on_tag_open.has_key? element
        @on_tag_open[element].call(@depth)
      elsif @on_tag_open.has_key? 'default'
        @on_tag_open['default'].call(@depth)
      end

      current = @nodes.pair_at_chain(@depth)

      value_at = @nodes.value_at_chain(@depth)
      
      if current.is_a? Hash
        if value_at.is_a? NilClass
          current[element] = {}
        elsif value_at.is_a? Hash
          if current[element].is_a? Array
            current[element].concat [{}]
          else
            current[element] = [current[element], {}]
            #current = @nodes.pair_at_chain(@depth)
          end
        elsif value_at.is_a? Array
          #puts @depth.inspect
          #puts current[element].inspect
          #puts current[element].inspect
        end
      elsif current.is_a? Array
      end

      @current_content = ''
    end

    def characters(string)
      return if is_closed?
      concat_content(string)
    end

    def cdata_block(string)
      return if is_closed?
      concat_content(string)
    end

    def end_element(element, attributes=[])
      return if is_closed?
{'fraction_non_alphanumeric': 0.10345672959058592, 'fraction_numerical': 0.0, 'mean_word_length': 2.004418262150221, 'pattern_counts': {'":': 0, '<': 5, '<?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': '1271604', 'n_tokens_mistral': 1337, 'n_tokens_neox': 1288, 'n_words': 269}