<commit_before>module Svgeez
  class SpriteBuilder
    def initialize(options)
      @source = File.expand_path(options.fetch('source', './'))
      @destination = File.expand_path(options.fetch('destination', './_svgeez/svgeez.svg'))
      @with_svgo = options['svgo']
    end

    def build
      unless source_is_destination?
        if source_file_paths.any?
          Svgeez.logger.info %{Generating sprite at `#{destination_file_path}` from #{source_file_paths.length} SVG#{'s' if source_file_paths.length > 1}...}

          # Make destination folder
          FileUtils.mkdir_p(destination_folder_path)

          # Notify if SVGO requested but not found
          if @with_svgo && !svgo_installed?
            Svgeez.logger.warn %{Unable to find `svgo` in your PATH. Continuing with standard sprite generation...}
          end

          # Write the file
          File.open(destination_file_path, 'w') do |f|
            f.write build_destination_file_contents
          end

          Svgeez.logger.info %{Successfully generated sprite at `#{destination_file_path}`.}
        else
          Svgeez.logger.warn %{No SVGs were found in `#{@source}`.}
        end
      else
        Svgeez.logger.error %{Setting `source` and `destination` to the same path isn't allowed!}
      end
    end

    def build_destination_file_contents
      %{<svg id="#{destination_file_id}" style="display: none;" version="1.1">}.tap do |destination_file_contents|
        # Loop over all source files, grabbing their content, and appending to `destination_file_contents`
        source_file_paths.each do |file_path|
          file_contents = use_svgo? ? `svgo -i #{file_path} -o -` : IO.read(file_path)
          pattern = /^<svg.*?(?<viewbox>viewBox=".*?").*?>(?<content>.*?)<\/svg>/m

          file_contents.match(pattern) do |matches|
            destination_file_contents << %{<symbol id="#{destination_file_id}-#{File.basename(file_path, '.svg').downcase}" #{matches[:viewbox]}>#{matches[:content]}</symbol>}
          end
        end

        destination_file_contents << '</svg>'
      end
    end

    def destination_file_id
      @destination_file_id ||= File.basename(destination_file_name, '.svg').gsub(' ', '-')
    end

    def destination_file_name
      if @destination.end_with?('.svg')<commit_msg>Escape quotes and slashes in file paths when passing to SVGO. Resolves #10<commit_after>module Svgeez
  class SpriteBuilder
    def initialize(options)
      @source = File.expand_path(options.fetch('source', './'))
      @destination = File.expand_path(options.fetch('destination', './_svgeez/svgeez.svg'))
      @with_svgo = options['svgo']
    end

    def build
      unless source_is_destination?
        if source_file_paths.any?
          Svgeez.logger.info %{Generating sprite at `#{destination_file_path}` from #{source_file_paths.length} SVG#{'s' if source_file_paths.length > 1}...}

          # Make destination folder
          FileUtils.mkdir_p(destination_folder_path)

          # Notify if SVGO requested but not found
          if @with_svgo && !svgo_installed?
            Svgeez.logger.warn %{Unable to find `svgo` in your PATH. Continuing with standard sprite generation...}
          end

          # Write the file
          File.open(destination_file_path, 'w') do |f|
            f.write build_destination_file_contents
          end

          Svgeez.logger.info %{Successfully generated sprite at `#{destination_file_path}`.}
        else
          Svgeez.logger.warn %{No SVGs were found in `#{@source}`.}
        end
      else
        Svgeez.logger.error %{Setting `source` and `destination` to the same path isn't allowed!}
      end
    end

    def build_destination_file_contents
      %{<svg id="#{destination_file_id}" style="display: none;" version="1.1">}.tap do |destination_file_contents|
        # Loop over all source files, grabbing their content, and appending to `destination_file_contents`
        source_file_paths.each do |file_path|
          file_contents = use_svgo? ? `svgo -i #{file_path.gsub(/['"\s]/, '\\\\\0')} -o -` : IO.read(file_path)
          pattern = /^<svg.*?(?<viewbox>viewBox=".*?").*?>(?<content>.*?)<\/svg>/m

          file_contents.match(pattern) do |matches|
            destination_file_contents << %{<symbol id="#{destination_file_id}-#{File.basename(file_path, '.svg').downcase}" #{matches[:viewbox]}>#{matches[:content]}</symbol>}
          end
        end

        destination_file_contents << '</svg>'
      end
    end

    def destination_file_id
      @destination_file_id ||= File.basename(destination_file_name, '.svg').gsub(' ', '-')
    end

    def destination_file_name
      if @destination.end_with?('.svg')
{'fraction_non_alphanumeric': 0.13010204081632654, 'fraction_numerical': 0.001913265306122449, 'mean_word_length': 2.875617792421746, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 21, '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': '4094289', 'n_tokens_mistral': 1449, 'n_tokens_neox': 1416, 'n_words': 322}