<commit_before>require_relative "./utils.rb"

class Minifying
  def self.minify_js
    puts "-> Running R.js optimizer"
    %x[cd assets/javascripts && node ../../r.js -o name=app out=built.js baseUrl=. && cd ../../]
    puts "-> Replacing JS links in all files"
    Dir.glob("built/**/*.html").each do |file|
      content = Utils.read_from_file(file)
      content.gsub!(/<script src="\/assets\/javascripts\/require.js" data-main="\/assets\/javascripts\/app.js"><\/script>/) {
        '<script src="/assets/javascripts/require.js"></script>
         <script src="/assets/javascripts/built.js"></script>'
      }
      Utils.write_to_file(content, file)
    end
  end
  def self.minify_css
    puts "-> Running cleanCSS"
    Dir.glob("built/assets/css/*.css").each do |file|
      # ./node_modules/clean-css/bin/cleancss -o min.css assets/css/style.css
      file_name = file.split("/").last
      file_split = file_name.split(".")
      file_split.pop
      file_no_ext = file_split.join "."
      puts "--> minifying #{file} to assets/css/#{file_no_ext}.min.css"
      %x[./node_modules/clean-css/bin/cleancss -o assets/css/#{file_no_ext}.min.css #{file}]
      puts "--> updating stylesheet links"
      Dir.glob("built/**/*.html").each do |file|
        content = Utils.read_from_file(file)
        content.gsub!(/<link href="(.+)\/(.+?)\.css" (.+)>/) { |match|
          puts "Matched Link: #{match} in file #{file}"
          "<link href='#{$1}/#{$2}.min.css' #{$3}>"
        }
        Utils.write_to_file(content, file)
      end
    end

  end
end<commit_msg>Fix bug with replacing CSS links with minfied links<commit_after>require_relative "./utils.rb"

class Minifying
  def self.minify_js
    puts "-> Running R.js optimizer"
    %x[cd assets/javascripts && node ../../r.js -o name=app out=built.js baseUrl=. && cd ../../]
    puts "-> Replacing JS links in all files"
    Dir.glob("built/**/*.html").each do |file|
      content = Utils.read_from_file(file)
      content.gsub!(/<script src="\/assets\/javascripts\/require.js" data-main="\/assets\/javascripts\/app.js"><\/script>/) {
        '<script src="/assets/javascripts/require.js"></script>
         <script src="/assets/javascripts/built.js"></script>'
      }
      Utils.write_to_file(content, file)
    end
  end
  def self.minify_css
    puts "-> Running cleanCSS"
    Dir.glob("built/assets/css/*.css").each do |file|
      # ./node_modules/clean-css/bin/cleancss -o min.css assets/css/style.css
      file_name = file.split("/").last
      file_split = file_name.split(".")
      file_split.pop
      file_no_ext = file_split.join "."
      puts "--> minifying #{file} to assets/css/#{file_no_ext}.min.css"
      %x[./node_modules/clean-css/bin/cleancss -o assets/css/#{file_no_ext}.min.css #{file}]
      puts "--> updating stylesheet links"
      Dir.glob("built/**/*.html").each do |html_file|
        content = Utils.read_from_file(html_file)
        # content.gsub!(/<link href="(.+)\/(.+?)\.css" (.+)>/) { |match|
        #   puts "Matched Link: #{match} in file #{file}"
        #   "<link href='#{$1}/#{$2}.min.css' #{$3}>"
        # }
        file_name = file.split "/"
        file_name = file_name.last.split(".")[0]
        puts file_name
        content.gsub!(/\/assets\/css\/#{file_name}.css/) { |match|
          puts "Matched Link: #{match} in file #{file}"
          "/assets/css/#{file_name}.min.css"
        }
        Utils.write_to_file(content, html_file)
      end
    end

  end
end
{'fraction_non_alphanumeric': 0.19126938541068353, 'fraction_numerical': 0.002010338885697875, 'mean_word_length': 2.5110887096774195, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 29, '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': '1421511', 'n_tokens_mistral': 1207, 'n_tokens_neox': 1199, 'n_words': 219}