Sha256: d6471c728f32dc4e0d6e9f64f8155be96d4edd6dcfe201756ff275b80699e933

Contents?: true

Size: 1.03 KB

Versions: 5

Compression:

Stored size: 1.03 KB

Contents

# frozen_string_literal: true

module ThemeCheck
  module RegexHelpers
    LIQUID_TAG = /#{Liquid::TagStart}.*?#{Liquid::TagEnd}/om
    LIQUID_VARIABLE = /#{Liquid::VariableStart}.*?#{Liquid::VariableEnd}/om
    LIQUID_TAG_OR_VARIABLE = /#{LIQUID_TAG}|#{LIQUID_VARIABLE}/om
    START_OR_END_QUOTE = /(^['"])|(['"]$)/

    def matches(s, re)
      start_at = 0
      matches = []
      while (m = s.match(re, start_at))
        matches.push(m)
        start_at = m.end(0)
      end
      matches
    end

    def href_to_file_size(href)
      # asset_url (+ optional stylesheet_tag) variables
      if href =~ /^#{LIQUID_VARIABLE}$/o && href =~ /asset_url/ && href =~ Liquid::QuotedString
        asset_id = Regexp.last_match(0).gsub(START_OR_END_QUOTE, "")
        asset = @theme.assets.find { |a| a.name.end_with?("/" + asset_id) }
        return if asset.nil?
        asset.gzipped_size

      # remote URLs
      elsif href =~ %r{^(https?:)?//}
        asset = RemoteAssetFile.from_src(href)
        asset.gzipped_size
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
theme-check-1.5.2 lib/theme_check/regex_helpers.rb
theme-check-1.5.1 lib/theme_check/regex_helpers.rb
theme-check-1.5.0 lib/theme_check/regex_helpers.rb
theme-check-1.4.0 lib/theme_check/regex_helpers.rb
theme-check-1.3.0 lib/theme_check/regex_helpers.rb