<commit_before>require 'mini_magick'

class Spriter
  class << self
    attr_accessor :assets_path
    attr_accessor :sprite_image_path
    attr_accessor :sprite_image_url

    def transform(*args)
      new(assets_path, sprite_image_path, sprite_image_url).transform(*args)
    end

    def transform_files(*args)
      new(assets_path, sprite_image_path, sprite_image_url).transform_files(*args)
    end

    def image_dimensions(path)
      magick_image = MiniMagick::Image.from_file(path)
      return magick_image[:width], magick_image[:height]
    end
  end

  def initialize(assets_path, sprite_image_path, sprite_image_url)
    @assets_path, @sprite_image_path, @sprite_image_url = assets_path, sprite_image_path, sprite_image_url
    @images = []
    @y_offsets = {}
    @current_offset, @max_width = 0, 0
  end

  def transform(*inputs)
    image_matcher = /([^a-z])-spriter-background:\s*([^;\}]+)([;\}])/
    new_css = inputs.inject([]) do |new_css, css|
      css = css.read if css.respond_to? :read
      new_css << css.gsub(image_matcher) do |matched|
        indent = $1
        terminator = $3
        image = $2.strip.gsub(/(?:^['"]|['"]$)/, '')
        add_image(image)
        "#{indent}background: url(#{@sprite_image_url}) no-repeat 0 #{-y_offset(image)}#{y_offset(image) == 0 ? '' : 'px'}#{terminator} /* #{image} */"
      end
    end<commit_msg>[+ECM] Refactor for readability.<commit_after>require 'mini_magick'

class Spriter
  class << self
    attr_accessor :assets_path
    attr_accessor :sprite_image_path
    attr_accessor :sprite_image_url

    def transform(*args)
      new(assets_path, sprite_image_path, sprite_image_url).transform(*args)
    end

    def transform_files(*args)
      new(assets_path, sprite_image_path, sprite_image_url).transform_files(*args)
    end

    def image_dimensions(path)
      magick_image = MiniMagick::Image.from_file(path)
      return magick_image[:width], magick_image[:height]
    end
  end

  def initialize(assets_path, sprite_image_path, sprite_image_url)
    @assets_path, @sprite_image_path, @sprite_image_url = assets_path, sprite_image_path, sprite_image_url
    @images = []
    @y_offsets = {}
    @current_offset, @max_width = 0, 0
  end

  def transform(*inputs)
    image_matcher = /([^a-z])-spriter-background:\s*([^;\}]+)([;\}])/
    new_css = inputs.inject([]) do |new_css, css|
      css = css.read if css.respond_to? :read
      new_css << css.gsub(image_matcher) do |matched|
        indent, image, terminator = $1, $2, $3
        image = image.strip.gsub(/(?:^['"]|['"]$)/, '')
        add_image(image)
        "#{indent}background: url(#{@sprite_image_url}) no-repeat 0 #{-y_offset(image)}#{y_offset(image) == 0 ? '' : 'px'}#{terminator} /* #{image} */"
      end
    end
{'fraction_non_alphanumeric': 0.14719710669077757, 'fraction_numerical': 0.005063291139240506, 'mean_word_length': 3.768965517241379, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 3, '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': '5061425', 'n_tokens_mistral': 1005, 'n_tokens_neox': 971, 'n_words': 174}