<commit_before>module Capybara
  module SaveAndOpenPage
    extend(self)

    def save_and_open_page(html)
      name="capybara-#{Time.new.strftime("%Y%m%d%H%M%S")}.html"

      FileUtils.touch(name) unless File.exist?(name)

      tempfile = File.new(name,'w')
      tempfile.write(rewrite_css_and_image_references(html))
      tempfile.close

      open_in_browser(tempfile.path)
    end

    def open_in_browser(path) # :nodoc
      require "launchy"
      Launchy::Browser.run(path)
    rescue LoadError
      warn "Sorry, you need to install launchy to open pages: `gem install launchy`"
    end

    def rewrite_css_and_image_references(response_html) # :nodoc:
      return response_html unless Capybara.asset_root
      directories = Array.new
      Dir.new(Capybara.asset_root).entries.each do |name|
        directories << name if File.directory?(name) && !name.to_s[0].chr.eql?('.')
      end
      directories_string = directories*'|'
      response_html.gsub(/("|')\/(#{directories_string})/, '\1' + Capybara.asset_root + '/\2')
    end
  end
end<commit_msg>Improve save_and_open_page code<commit_after>module Capybara
  module SaveAndOpenPage
    extend(self)

    def save_and_open_page(html)
      name="capybara-#{Time.new.strftime("%Y%m%d%H%M%S")}.html"

      FileUtils.touch(name) unless File.exist?(name)

      tempfile = File.new(name,'w')
      tempfile.write(rewrite_css_and_image_references(html))
      tempfile.close

      open_in_browser(tempfile.path)
    end

    def open_in_browser(path) # :nodoc
      require "launchy"
      Launchy::Browser.run(path)
    rescue LoadError
      warn "Sorry, you need to install launchy to open pages: `gem install launchy`"
    end

    def rewrite_css_and_image_references(response_html) # :nodoc:
      return response_html unless Capybara.asset_root
      directories = Dir.new(Capybara.asset_root).entries.inject([]) do |list, name|
        list << name if File.directory?(name) and not name.to_s =~ /^\./
        list
      end
      response_html.gsub(/("|')\/(#{directories.join('|')})/, '\1' + Capybara.asset_root + '/\2')
    end
  end
end
{'fraction_non_alphanumeric': 0.1279508970727101, 'fraction_numerical': 0.002360717658168083, 'mean_word_length': 3.315682281059063, 'pattern_counts': {'":': 0, '<': 7, '<?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': '6868352', 'n_tokens_mistral': 741, 'n_tokens_neox': 729, 'n_words': 134}