<commit_before>require_relative 'table_element'
# Style of Table
module OoxmlParser
  class TableStyle < OOXMLDocumentObject
    attr_accessor :id, :name, :whole_table, :banding_1_horizontal, :banding_2_horizontal, :banding_1_vertical, :banding_2_vertical,
                  :last_column, :first_column, :last_row, :first_row, :southeast_cell, :southwest_cell, :northeast_cell,
                  :northwest_cell

    # Parse TableStyle object
    # @param style_id [String] id to parse
    # @return [TableStyle] result of parsing
    def parse(style_id: nil)
      unless style_id.nil?
        nodes = get_style_node(style_id)
        return nil if nodes.nil?
        xpath = '*'
        attributes = 'style_node_child.name'
      end
      begin
        nodes.xpath(xpath).each do |style_node_child|
          case instance_eval(attributes)
          when 'wholeTbl'
            @whole_table = TableElement.new(parent: self).parse(style_node_child)
          when 'band1H'
            @banding_1_horizontal = TableElement.new(parent: self).parse(style_node_child)
          when 'band2H', 'band2Horz'
            @banding_2_horizontal = TableElement.new(parent: self).parse(style_node_child)
          when 'band1V'
            @banding_1_vertical = TableElement.new(parent: self).parse(style_node_child)
          when 'band2V'
            @banding_2_vertical = TableElement.new(parent: self).parse(style_node_child)
          when 'lastCol'
            @last_column = TableElement.new(parent: self).parse(style_node_child)
          when 'firstCol'
            @first_column = TableElement.new(parent: self).parse(style_node_child)
          when 'lastRow'
            @last_row = TableElement.new(parent: self).parse(style_node_child)
          when 'firstRow'
            @first_row = TableElement.new(parent: self).parse(style_node_child)
          when 'seCell'
            @southeast_cell = TableElement.new(parent: self).parse(style_node_child)
          when 'swCell'
            @southwest_cell = TableElement.new(parent: self).parse(style_node_child)
          when 'neCell'
            @northeast_cell = TableElement.new(parent: self).parse(style_node_child)
          when 'nwCell'
            @northwest_cell = TableElement.new(parent: self).parse(style_node_child)
          end
        end
      end
      @id = style_id unless style_id.nil?
      self
    end

    def get_style_node(style_id)
      begin
        doc = Nokogiri::XML(File.open(OOXMLDocumentObject.path_to_folder + 'ppt/tableStyles.xml'))
      rescue StandardError
        raise 'Can\'t find tableStyles.xml in ' + OOXMLDocumentObject.path_to_folder.to_s + '/ppt folder'
      end
      doc.xpath('a:tblStyleLst/a:tblStyle').each { |table_style_node| return table_style_node if table_style_node.attribute('styleId').value == style_id }
      nil
    end
  end
end<commit_msg>Simplify parsing TableStyle<commit_after>require_relative 'table_element'
# Style of Table
module OoxmlParser
  class TableStyle < OOXMLDocumentObject
    attr_accessor :id, :name, :whole_table, :banding_1_horizontal, :banding_2_horizontal, :banding_1_vertical, :banding_2_vertical,
                  :last_column, :first_column, :last_row, :first_row, :southeast_cell, :southwest_cell, :northeast_cell,
                  :northwest_cell

    # Parse TableStyle object
    # @param style_id [String] id to parse
    # @return [TableStyle] result of parsing
    def parse(style_id: nil)
      nodes = get_style_node(style_id)
      return nil unless nodes
      nodes.xpath('*').each do |style_node_child|
        case style_node_child.name
        when 'wholeTbl'
          @whole_table = TableElement.new(parent: self).parse(style_node_child)
        when 'band1H'
          @banding_1_horizontal = TableElement.new(parent: self).parse(style_node_child)
        when 'band2H', 'band2Horz'
          @banding_2_horizontal = TableElement.new(parent: self).parse(style_node_child)
        when 'band1V'
          @banding_1_vertical = TableElement.new(parent: self).parse(style_node_child)
        when 'band2V'
          @banding_2_vertical = TableElement.new(parent: self).parse(style_node_child)
        when 'lastCol'
          @last_column = TableElement.new(parent: self).parse(style_node_child)
        when 'firstCol'
          @first_column = TableElement.new(parent: self).parse(style_node_child)
        when 'lastRow'
          @last_row = TableElement.new(parent: self).parse(style_node_child)
        when 'firstRow'
          @first_row = TableElement.new(parent: self).parse(style_node_child)
        when 'seCell'
          @southeast_cell = TableElement.new(parent: self).parse(style_node_child)
        when 'swCell'
          @southwest_cell = TableElement.new(parent: self).parse(style_node_child)
        when 'neCell'
          @northeast_cell = TableElement.new(parent: self).parse(style_node_child)
        when 'nwCell'
          @northwest_cell = TableElement.new(parent: self).parse(style_node_child)
        end
      end
      @id = style_id
      self
    end

    def get_style_node(style_id)
      begin
        doc = Nokogiri::XML(File.open(OOXMLDocumentObject.path_to_folder + 'ppt/tableStyles.xml'))
      rescue StandardError
        raise 'Can\'t find tableStyles.xml in ' + OOXMLDocumentObject.path_to_folder.to_s + '/ppt folder'
      end
      doc.xpath('a:tblStyleLst/a:tblStyle').each { |table_style_node| return table_style_node if table_style_node.attribute('styleId').value == style_id }
      nil
    end
  end
end
{'fraction_non_alphanumeric': 0.09565217391304348, 'fraction_numerical': 0.004710144927536232, 'mean_word_length': 2.992046276211135, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, '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': '377809', 'n_tokens_mistral': 1758, 'n_tokens_neox': 1729, 'n_words': 319}