<commit_before>require 'digest/sha1'
require 'cgi'
require 'mini_magick'
require_relative 'resizer'

module Jekyll
  module SrcsetTag
    class Image

      attr_reader :source_path, :output_path, :web_output_path, :image_path, :ppi, :sources, :html_attributes

      def initialize(source_path:, output_path:, web_output_path:, image_path:, ppi:, sources:, html_attributes:)
        @source_path = source_path
        @output_path = output_path
        @web_output_path = web_output_path
        @image_path = image_path
        @ppi = ppi.respond_to?(:sort_by) ? ppi : ppi.split(',').map(&:to_i)
        @sources = sources
        @html_attributes = html_attributes
      end

      def generate_images!
        needed_instances = uniq_instances.select { |instance| !File.exist?(File.join(output_dir, instance.filename)) }
        resize_images! needed_instances
      end

      def instances
        @instances ||= create_instances
      end

      def uniq_instances
        instances.uniq { |instance| [ instance.output_width, instance.output_height ] }
      end

      def to_html
        srcs = image_srcs
        src = CGI.escape_html(srcs.last[0])
        srcset = CGI.escape_html(srcs.map {|path, size| "#{path} #{size}w" }.join(', '))
        sizes = CGI::escape_html(source_sizes.join(', '))
        "<img src=\"#{src}\" srcset=\"#{srcset}\" sizes=\"#{sizes}\" #{html_attributes}/>\n"
      end

      def digest
        unless @digest
          file = Digest::SHA1.file(File.join(source_path, image_path)).to_s<commit_msg>Don't output srcset if there's only one image<commit_after>require 'digest/sha1'
require 'cgi'
require 'mini_magick'
require_relative 'resizer'

module Jekyll
  module SrcsetTag
    class Image

      attr_reader :source_path, :output_path, :web_output_path, :image_path, :ppi, :sources, :html_attributes

      def initialize(source_path:, output_path:, web_output_path:, image_path:, ppi:, sources:, html_attributes:)
        @source_path = source_path
        @output_path = output_path
        @web_output_path = web_output_path
        @image_path = image_path
        @ppi = ppi.respond_to?(:sort_by) ? ppi : ppi.split(',').map(&:to_i)
        @sources = sources
        @html_attributes = html_attributes
      end

      def generate_images!
        needed_instances = uniq_instances.select { |instance| !File.exist?(File.join(output_dir, instance.filename)) }
        resize_images! needed_instances
      end

      def instances
        @instances ||= create_instances
      end

      def uniq_instances
        instances.uniq { |instance| [ instance.output_width, instance.output_height ] }
      end

      def to_html
        srcs = image_srcs
        src = CGI.escape_html(srcs.last[0])
        show_srcset = srcs.length > 1
        srcset = CGI.escape_html(srcs.map {|path, size| "#{path} #{size}w" }.join(', '))
        sizes = CGI::escape_html(source_sizes.join(', '))
        srcset_attrs = show_srcset ? " srcset=\"#{srcset}\" sizes=\"#{sizes}\"" : ''
        "<img src=\"#{src}\"#{srcset_attrs} #{html_attributes}/>\n"
      end

      def digest
        unless @digest
          file = Digest::SHA1.file(File.join(source_path, image_path)).to_s
{'fraction_non_alphanumeric': 0.12453183520599251, 'fraction_numerical': 0.002184769038701623, 'mean_word_length': 3.0213299874529485, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, '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': '6621256', 'n_tokens_mistral': 1078, 'n_tokens_neox': 1063, 'n_words': 205}