<commit_before>require "rubygems"
require "base64"


module Compass::ImgDelivery

    def scss_rule(basename, mime_type, dat_uri)
        rule = <<-SCSS
          .#{basename.downcase} {
            background-image: url("data:#{mime_type};base64,#{data_uri}");
            background-repeat: no-repeat;
          }

        SCSS
    end

end


module Sass::Script::Functions

    def img_delivery(images_dir = '', css_dir = '', js_dir = '')
        directory_path = Compass.configuration.images_path + "#{images_dir.value}"
        svg_scss_content = ""
        png_scss_content = ""
        fallback_scss_content = ""

        Dir["#{directory_path}svg/*.svg"].each do |file|
            basename = File.basename(file, ".svg")
            outputfile = directory_path + 'png/' + basename + '.png'
            svg_data_uri = svg_base64(file)
            png_data_uri = png_base64(outputfile)

            svg_scss_content << <<-SCSS
              .#{basename.downcase} {
                background-image: #{svg_data_uri};
                background-repeat: no-repeat;
              }

            SCSS

            # svg_scss_content << scss_rule(basename, "image/svg+xml", svg_data_uri )

            png_scss_content << <<-SCSS
              .#{basename.downcase} {
                background-image: #{png_data_uri};
                background-repeat: no-repeat;
              }

            SCSS

            fallback_img_path =  "/" + Compass.configuration.images_dir + "#{images_dir.value}png"
            fallback_scss_content << <<-SCSS
              .#{basename.downcase} {
                background-image: url("#{fallback_img_path}/#{basename}.png");
                background-repeat: no-repeat;
              }

            SCSS

            # svg2png command
            cmd = "svg2png #{file} #{outputfile}"
            puts "Writing #{outputfile}"

            # exec shell command
            shell = %x[ #{cmd} ]

            if !File.exists?(outputfile)
                d = directory_path + 'png'
                Dir.mkdir(d) unless Dir.exists?(d)
            end

        end

        # create svg.scss file
        css_path = Compass.configuration.css_path + "#{css_dir.value}"
        svg_scss_file = css_path + '-svg.css.scss'
        png_scss_file = css_path + '-png.css.scss'
        fallback_scss_file = css_path + '-fallback.css.scss'

        if !File.exists?(svg_scss_file)
            d = File.dirname(svg_scss_file)
            Dir.mkdir(d) unless Dir.exists?(d)
        end

        File.open(svg_scss_file, 'w') do |f|
            puts "Writing #{svg_scss_file}"
            f.write(svg_scss_content)
        end

        # create png.scss file
        File.open(png_scss_file, 'w') do |f|
            puts "Writing #{png_scss_file}"
            f.write(png_scss_content)
        end

        # create fallback.scss file
        File.open(fallback_scss_file, 'w') do |f|
            puts "Writing #{fallback_scss_file}"
            f.write(fallback_scss_content)
        end

        # create js file
        js_content = File.open(File.join(File.dirname(__FILE__), "../javascripts/img-delivery.js"), "rb")
        js_content = js_content.read
        js_path = Compass.configuration.javascripts_path + "#{js_dir.value}"

        js_file = js_path + '.js'
        if !File.exists?(js_file)
            d = File.dirname(js_file)
            Dir.mkdir(d) unless Dir.exists?(d)
        end

        File.open(js_file, 'w') do |f|
            puts "Writing #{js_file}"
            f.write(js_content)
        end
    end

  def svg_base64(file)
    data = Base64.encode64(File.read(file)).gsub("\n", '')
    uri  = "data:image/svg+xml;base64,#{data}"

    "url('#{uri}')"
  end

  def png_base64(file)
    data = Base64.encode64(File.read(file)).gsub("\n", '')
    uri  = "data:image/png;base64,#{data}"

    "url('#{uri}')"
  end
end<commit_msg>cleaning and DRYing<commit_after>require "rubygems"
require "base64"


module Compass::ImgDelivery

    def scss_rule(basename, mime_type, data_uri)
        rule = <<-SCSS
          .#{basename.downcase} {
            background-image: url("data:#{mime_type};base64,#{data_uri}");
            background-repeat: no-repeat;
          }

        SCSS
    end

    def fallback_scss_rule(basename, img_dir)
        rule = <<-SCSS
          .#{basename.downcase} {
            background-image: url("#{img_dir}/#{basename}.png");
            background-repeat: no-repeat;
          }

        SCSS
    end

    def mkdir_if_not_exists(file)
        if !File.exists?(file)
            d = File.dirname(file)
            Dir.mkdir(d) unless Dir.exists?(d)
        end
    end

    def write_file(file, content)
        File.open(file, 'w') do |f|
            puts "Writing #{file}"
            f.write(content)
        end
    end

end


module Sass::Script::Functions

    include Compass::ImgDelivery

    def img_delivery(images_dir = '', css_dir = '', js_dir = '')
        directory_path = Compass.configuration.images_path + "#{images_dir.value}"
        svg_scss_content = ""
        png_scss_content = ""
        fallback_scss_content = ""

        Dir["#{directory_path}svg/*.svg"].each do |file|
            basename = File.basename(file, ".svg")
            outputfile = directory_path + 'png/' + basename + '.png'

            # svg data-uri css rule
            svg_data_uri = img2b64(file)
            svg_scss_content << scss_rule(basename, "image/svg+xml", svg_data_uri )

            # make png/ dir if not exists
            mkdir_if_not_exists(outputfile)

            # create png files with svg2png command
            cmd = "svg2png #{file} #{outputfile}"
            puts "Writing #{outputfile}"
            # exec shell command
            shell = %x[ #{cmd} ]

            # png data-uri css rule
            png_data_uri = img2b64(outputfile)
            png_scss_content << scss_rule(basename, "image/png", png_data_uri)

            # png fallback css rule
            fallback_img_path =  "/" + Compass.configuration.images_dir + "#{images_dir.value}png"
            fallback_scss_content << fallback_scss_rule(basename, fallback_img_path)
        end

        # create svg.scss file
        css_path = Compass.configuration.css_path + "#{css_dir.value}"
        svg_scss_file = css_path + '-svg.css.scss'
        png_scss_file = css_path + '-png.css.scss'
        fallback_scss_file = css_path + '-fallback.css.scss'

        mkdir_if_not_exists(svg_scss_file)

        # create svg.scss file
        write_file(svg_scss_file, svg_scss_content)

        # create png.scss file
        write_file(png_scss_file, png_scss_content)

        # create fallback.scss file
        write_file(fallback_scss_file, fallback_scss_content)

        # create js file
        js_content = File.open(File.join(File.dirname(__FILE__), "../javascripts/img-delivery.js"), "rb")
        js_content = js_content.read
        js_path = Compass.configuration.javascripts_path + "#{js_dir.value}"
        js_file = js_path + '.js'

        mkdir_if_not_exists(js_file)
        write_file(js_file, js_content)
    end

    def img2b64(file)
        Base64.encode64(File.read(file)).gsub("\n", '')
    end

end
{'fraction_non_alphanumeric': 0.12183235867446393, 'fraction_numerical': 0.006265664160401002, 'mean_word_length': 2.011740041928721, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 3}, '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': '4486182', 'n_tokens_mistral': 2424, 'n_tokens_neox': 2326, 'n_words': 403}