<commit_before>require 'rubygems'
require 'nokogiri'

module RubyXL
module Writer
  class StylesWriter
    attr_accessor :dirpath, :filepath, :workbook

    def initialize(dirpath, wb)
      @dirpath = dirpath
      @workbook = wb
      @filepath = @dirpath + '/xl/styles.xml'
    end

    def write()
      font_id_corrector = {}
      fill_id_corrector = {}
      border_id_corrector = {}
      style_id_corrector = {}

      builder = Nokogiri::XML::Builder.new do |xml|
        xml.styleSheet('xmlns'=>"http://schemas.openxmlformats.org/spreadsheetml/2006/main") {
          unless @workbook.num_fmts.nil? || @workbook.num_fmts[:attributes].nil?
            xml.numFmts('count'=>@workbook.num_fmts[:attributes][:count].to_s) {
              @workbook.num_fmts[:numFmt].each do |fmt|
                attributes = fmt[:attributes]
                xml.numFmt('numFmtId'=>attributes[:numFmtId].to_s,
                  'formatCode'=>attributes[:formatCode].to_s)
              end
            }
          end

          offset = 0
          #default font should stay the same
          font_id_corrector['0']=0
          1.upto(@workbook.fonts.size-1) do |i|
            font_id_corrector[i.to_s] = i-offset
            if @workbook.fonts[i.to_s][:count] == 0
              @workbook.fonts[i.to_s] = nil
              font_id_corrector[i.to_s] = nil
              offset += 1
            end
          end


          offset = 0
          #STARTS AT 2 because excel is stupid
          #and it seems to hard code access the first
          #2 styles.............
          fill_id_corrector['0']=0
          fill_id_corrector['1']=1
          2.upto(@workbook.fills.size-1) do |i|
            fill_id_corrector[i.to_s] = i-offset
            if @workbook.fills[i.to_s][:count] == 0
              @workbook.fills[i.to_s] = nil
              fill_id_corrector[i.to_s] = nil
              offset += 1
            end
          end

          #sets index to itself as init correction
          #if items deleted, indexes adjusted
          #that id 'corrects' to nil
          offset = 0

          #default border should stay the same
          border_id_corrector['0'] = 0
          1.upto(@workbook.borders.size-1) do |i|
            border_id_corrector[i.to_s] = i-offset
            if @workbook.borders[i.to_s][:count] == 0
              @workbook.borders[i.to_s] = nil
              border_id_corrector[i.to_s] = nil
              offset += 1
            end
          end

          offset = 0
          style_id_corrector['0']=0
          1.upto(@workbook.cell_xfs[:xf].size) do |i|
            style_id_corrector[i.to_s]= i-offset
            # style correction commented out until bug is fixed
            (i+1).upto(@workbook.cell_xfs[:xf].size) do |j|
              unless i == j
                if hash_equal(@workbook.cell_xfs[:xf][i],@workbook.cell_xfs[:xf][j]) #check if this is working
                  @workbook.cell_xfs[:xf].delete_at(i)
                  style_id_corrector.delete(i.to_s)
                  offset += 1
                end
              end
            end
          end
          @workbook.style_corrector = style_id_corrector

          xml.fonts('count'=>@workbook.fonts.size) {
            0.upto(@workbook.fonts.size-1) do |i|
              font = @workbook.fonts[i.to_s]
              unless font.nil?
                font = font[:font]<commit_msg>print statements to check. hash_equal works.<commit_after>require 'rubygems'
require 'nokogiri'

module RubyXL
module Writer
  class StylesWriter
    attr_accessor :dirpath, :filepath, :workbook

    def initialize(dirpath, wb)
      @dirpath = dirpath
      @workbook = wb
      @filepath = @dirpath + '/xl/styles.xml'
    end

    def write()
      font_id_corrector = {}
      fill_id_corrector = {}
      border_id_corrector = {}
      style_id_corrector = {}

      builder = Nokogiri::XML::Builder.new do |xml|
        xml.styleSheet('xmlns'=>"http://schemas.openxmlformats.org/spreadsheetml/2006/main") {
          unless @workbook.num_fmts.nil? || @workbook.num_fmts[:attributes].nil?
            xml.numFmts('count'=>@workbook.num_fmts[:attributes][:count].to_s) {
              @workbook.num_fmts[:numFmt].each do |fmt|
                attributes = fmt[:attributes]
                xml.numFmt('numFmtId'=>attributes[:numFmtId].to_s,
                  'formatCode'=>attributes[:formatCode].to_s)
              end
            }
          end

          offset = 0
          #default font should stay the same
          font_id_corrector['0']=0
          1.upto(@workbook.fonts.size-1) do |i|
            font_id_corrector[i.to_s] = i-offset
            if @workbook.fonts[i.to_s][:count] == 0
              @workbook.fonts[i.to_s] = nil
              font_id_corrector[i.to_s] = nil
              offset += 1
            end
          end


          offset = 0
          #STARTS AT 2 because excel is stupid
          #and it seems to hard code access the first
          #2 styles.............
          fill_id_corrector['0']=0
          fill_id_corrector['1']=1
          2.upto(@workbook.fills.size-1) do |i|
            fill_id_corrector[i.to_s] = i-offset
            if @workbook.fills[i.to_s][:count] == 0
              @workbook.fills[i.to_s] = nil
              fill_id_corrector[i.to_s] = nil
              offset += 1
            end
          end

          #sets index to itself as init correction
          #if items deleted, indexes adjusted
          #that id 'corrects' to nil
          offset = 0

          #default border should stay the same
          border_id_corrector['0'] = 0
          1.upto(@workbook.borders.size-1) do |i|
            border_id_corrector[i.to_s] = i-offset
            if @workbook.borders[i.to_s][:count] == 0
              @workbook.borders[i.to_s] = nil
              border_id_corrector[i.to_s] = nil
              offset += 1
            end
          end

          offset = 0
          style_id_corrector['0']=0
          1.upto(@workbook.cell_xfs[:xf].size) do |i|
            style_id_corrector[i.to_s]= i-offset
            # style correction commented out until bug is fixed
            (i+1).upto(@workbook.cell_xfs[:xf].size) do |j|
              unless i == j
                if hash_equal(@workbook.cell_xfs[:xf][i],@workbook.cell_xfs[:xf][j]) #check if this is working
                  puts "found match, #{i},#{j}"
                  puts "i = #{@workbook.cell_xfs[:xf][i].inspect}"
                  puts "j = #{@workbook.cell_xfs[:xf][j].inspect}"
                  @workbook.cell_xfs[:xf].delete_at(i)
                  style_id_corrector.delete(i.to_s)
                  offset += 1
                end
              end
            end
          end
          @workbook.style_corrector = style_id_corrector

          xml.fonts('count'=>@workbook.fonts.size) {
            0.upto(@workbook.fonts.size-1) do |i|
              font = @workbook.fonts[i.to_s]
              unless font.nil?
                font = font[:font]
{'fraction_non_alphanumeric': 0.11793330470874482, 'fraction_numerical': 0.010591097752969802, 'mean_word_length': 1.6651411136536995, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 16}, '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': '6689880', 'n_tokens_mistral': 2332, 'n_tokens_neox': 2232, 'n_words': 422}