<commit_before>#!/usr/bin/env ruby

require 'nokogiri'
require 'yaml'
require 'net/http'

module VimRFC

  $rfchash   = {}
  $rfcfile   = 'test.xml'
  $cachefile = 'test.yml'

  class Parser < Nokogiri::XML::SAX::Document
    def start_element(elem, attrs = [])
      case elem
      when 'rfc-entry' then @in_rfc   = true
      when 'doc-id'    then @in_docid = true
      when 'title'     then @in_title = true
      end
    end

    def end_element(elem)
      case elem
      when 'rfc-entry' then @in_rfc   = false and @docid = nil and @title = nil
      when 'doc-id'    then @in_docid = false
      when 'title'     then @in_title = false
      end
    end

    def characters(s)
      if @in_rfc
        if @in_docid
          @docid = s
        elsif @in_title
          @title = s
        end
        $rfchash[@title.to_sym] = @docid if @docid and @title
      end
    end
  end

  class Handling
    def initialize
      update unless File.exist? $rfcfile
    end

    def update
      Net::HTTP.start('www.ietf.org') do |http|
        f = open($rfcfile, 'w')
        begin
          http.request_get('/rfc/new-rfc-index.xml') do |res|
            res.read_body do |segment|
              f.write(segment)
            end
          end
        rescue => err
          puts "Exception: #{err}"
          err
        ensure
          f.close
        end
      end
    end

    def search
      counter = 0
      f = File.new($rfcfile)
      begin
        while (line = f.gets)
          puts "#{counter}: #{line}"
          counter += 1
        end
      rescue => err
        puts "Exception: #{err}"
        err
      ensure
        f.close
      end
    end

    def load_from_file
      require 'yaml'
      $rfchash = YAML::load_file($cachefile)
      puts $rfchash.inspect
    end

    def save_to_file
      File.open($cachefile, 'w') do |f|
        f.write $rfchash.to_yaml
      end
    end

    def parse
      Nokogiri::XML::SAX::Parser.new(VimRFC::Parser.new).parse_file($rfcfile)
    end
  end
end

foo = VimRFC::Handling.new
foo.parse
foo.save_to_file<commit_msg>refactoring<commit_after>#!/usr/bin/env ruby

require 'nokogiri'
require 'net/http'
require 'yaml'

module VimRFC

  #
  # Settings.
  #
  $rfcfile   = 'test.xml'
  $cachefile = 'test.yml'

  $rfchash   = {}

  #
  # Event-driven XML parsing. Basically a state machine that fills a hash.
  #
  class Parser < Nokogiri::XML::SAX::Document
    def start_element(elem, attrs = [])
      case elem
      when 'rfc-entry' then @in_rfc   = true
      when 'doc-id'    then @in_docid = true
      when 'title'     then @in_title = true
      end
    end

    def end_element(elem)
      case elem
      when 'rfc-entry' then @in_rfc   = false and @docid = nil and @title = nil
      when 'doc-id'    then @in_docid = false
      when 'title'     then @in_title = false
      end
    end

    def characters(s)
      if @in_rfc
        if @in_docid
          @docid = s
        elsif @in_title
          @title = s
        end
        $rfchash[@docid] = @title if @docid and @title
      end
    end
  end

  #
  # Main class of this module.
  #
  class Handling
    def initialize
      if File.exist? $cachefile
        load_from_cachefile
      elsif File.exist? $rfcfile
        read_xml_into_hash
        write_to_cachefile
      else
        get_rfcfile
        read_xml_into_hash
        write_to_cachefile
      end
    end

    def get_rfcfile
      Net::HTTP.start('www.ietf.org') do |http|
        f = open($rfcfile, 'w')
        begin
          http.request_get('/rfc/new-rfc-index.xml') do |res|
            res.read_body do |segment|
              f.write(segment)
            end
          end
        rescue => err
          puts "Exception: #{err}"
          err
        ensure
          f.close
        end
      end
    end

    def search(regex)
      load_from_cachefile
      $rfchash.each_pair do |id,title|
        if title =~ /#{regex}/i
          puts "#{id}: #{title}"
        end
      end
    end

    def write_to_cachefile
      File.open($cachefile, 'w') do |f|
        f.write($rfchash.to_yaml)
      end
    end

    def load_from_cachefile
      require 'yaml'
      $rfchash = YAML::load_file($cachefile)
    end

    def read_xml_into_hash
      Nokogiri::XML::SAX::Parser.new(VimRFC::Parser.new).parse_file($rfcfile)
    end
  end
end

VimRFC::Handling.new.search($*[0])
{'fraction_non_alphanumeric': 0.10191518467852258, 'fraction_numerical': 0.0006839945280437756, 'mean_word_length': 1.747025673137132, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 7}, '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': '6086647', 'n_tokens_mistral': 1544, 'n_tokens_neox': 1521, 'n_words': 373}