<commit_before>module PremailerRails
  module CSSHelper
    extend self

    @@css_cache = {}

    def css_for_doc(doc)
      css = doc.search('link[@type="text/css"]').map { |link|
              url = link.attributes['href']
              load_css_at_path(url) unless url.blank?
            }.reject(&:blank?).join("\n")
      css = load_css_at_path(:default) if css.blank?
      css
    end

    private

    def load_css_at_path(path)
      if path.is_a? String
        # Remove everything after ? including ?
        path = path[0..(path.index('?') - 1)] if path.include? '?'
        # Remove the host
        path = path.gsub(/^https?\:\/\/[^\/]*/, '') if path.index('http') == 0
      end

      # Don't cache in development.
      if Rails.env.development? or not @@css_cache.include? path
        @@css_cache[path] =
          if defined? Hassle and Rails.configuration.middleware.include? Hassle
            file = path == :default ? '/stylesheets/email.css' : path
            File.read("#{Rails.root}/tmp/hassle#{file}")
          elsif assets_enabled?
            file = if path == :default
                     'email.css'
                   else
                     path.sub("#{Rails.configuration.assets.prefix}/", '')
                   end
            if asset = Rails.application.assets.find_asset(file)
              asset.to_s
            else
              raise "Couldn't find asset #{file} for premailer-rails3."
            end
          else
            file = path == :default ? '/stylesheets/email.css' : path
            File.read("#{Rails.root}/public#{file}")
          end
      end

      @@css_cache[path]
    rescue => ex
      # Print an error and store empty string as the CSS.<commit_msg>Strip asset digest from path<commit_after>module PremailerRails
  module CSSHelper
    extend self

    @@css_cache = {}

    def css_for_doc(doc)
      css = doc.search('link[@type="text/css"]').map { |link|
              url = link.attributes['href']
              load_css_at_path(url) unless url.blank?
            }.reject(&:blank?).join("\n")
      css = load_css_at_path(:default) if css.blank?
      css
    end

    private

    def load_css_at_path(path)
      if path.is_a? String
        # Remove everything after ? including ?
        path = path[0..(path.index('?') - 1)] if path.include? '?'
        # Remove the host
        path = path.gsub(/^https?\:\/\/[^\/]*/, '') if path.index('http') == 0
      end

      # Don't cache in development.
      if Rails.env.development? or not @@css_cache.include? path
        @@css_cache[path] =
          if defined? Hassle and Rails.configuration.middleware.include? Hassle
            file = path == :default ? '/stylesheets/email.css' : path
            File.read("#{Rails.root}/tmp/hassle#{file}")
          elsif assets_enabled?
            file = if path == :default
                     'email.css'
                   else
                     path.sub("#{Rails.configuration.assets.prefix}/", '') \
                         .sub(/-.*\.css$/, '.css')
                   end
            if asset = Rails.application.assets.find_asset(file)
              asset.to_s
            else
              raise "Couldn't find asset #{file} for premailer-rails3."
            end
          else
            file = path == :default ? '/stylesheets/email.css' : path
            File.read("#{Rails.root}/public#{file}")
          end
      end

      @@css_cache[path]
    rescue => ex
      # Print an error and store empty string as the CSS.
{'fraction_non_alphanumeric': 0.1340909090909091, 'fraction_numerical': 0.0022727272727272726, 'mean_word_length': 1.7210200927357033, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '2995668', 'n_tokens_mistral': 1065, 'n_tokens_neox': 1017, 'n_words': 237}