Class: Torba::CssUrlToErbAssetPath
- Inherits:
-
Object
- Object
- Torba::CssUrlToErbAssetPath
- Defined in:
- lib/torba/css_url_to_erb_asset_path.rb
Overview
Parses content of CSS file and converts its image assets paths into Sprockets’ logical paths.
Constant Summary collapse
- URL_RE =
/ ( # $1 url\( # url( \s* # optional space ['"]? # optional quote (?!data) # no data URIs (?!http[s]?:\/\/) # no remote URIs (?!\/) # only relative location ) ( # $2 [^'"?#]+? # location ) ( # $3 ([?#][^'"]+?)? # optional query or fragment ['"]? # optional quote \s* # optional space \) # ) ) /xm
Class Method Summary collapse
-
.call(content, file_path) {|image_file_path| ... } ⇒ String
CSS file content where image “url(…)” paths are replaced by ERB interpolations “url(<%= asset_path(…) %>)”.
Class Method Details
.call(content, file_path) {|image_file_path| ... } ⇒ String
Returns CSS file content where image “url(…)” paths are replaced by ERB interpolations “url(<%= asset_path(…) %>)”.
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/torba/css_url_to_erb_asset_path.rb', line 51 def self.call(content, file_path) content.gsub(URL_RE) do absolute_image_file_path = File.($2, File.dirname(file_path)) sprockets_file_path = yield absolute_image_file_path if sprockets_file_path "#{$1}<%= asset_path('#{sprockets_file_path}') %>#{$3}" else $& end end end |