From 03bdbff3d8812a8cbd323036e213b002fd66c0f3 Mon Sep 17 00:00:00 2001 From: Jeff Escalante Date: Thu, 17 Oct 2013 16:17:59 -0400 Subject: [PATCH 1/3] add image-path and image-url aliases --- lib/stylus/tilt/rails.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/stylus/tilt/rails.rb b/lib/stylus/tilt/rails.rb index ab3b909..0cfa0b7 100644 --- a/lib/stylus/tilt/rails.rb +++ b/lib/stylus/tilt/rails.rb @@ -25,6 +25,8 @@ def build_mixin_body(scope) return pair[1] if pair[0] == key for pair in #{assets_hash(scope)[:url]} () asset-path(key) return pair[1] if pair[0] == key for pair in #{assets_hash(scope)[:path]} () +image-url = asset-url +image-path = asset-path STYL else '' From f892b39a624b3254b04a118dd2ba3fd2cca02cc2 Mon Sep 17 00:00:00 2001 From: Jeff Escalante Date: Fri, 18 Oct 2013 10:55:23 -0400 Subject: [PATCH 2/3] more accurate image-path implementation --- lib/stylus/tilt/rails.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/stylus/tilt/rails.rb b/lib/stylus/tilt/rails.rb index 0cfa0b7..05797b8 100644 --- a/lib/stylus/tilt/rails.rb +++ b/lib/stylus/tilt/rails.rb @@ -25,8 +25,10 @@ def build_mixin_body(scope) return pair[1] if pair[0] == key for pair in #{assets_hash(scope)[:url]} () asset-path(key) return pair[1] if pair[0] == key for pair in #{assets_hash(scope)[:path]} () -image-url = asset-url -image-path = asset-path +image-url(key) + return pair[1] if pair[0] == key for pair in #{assets_hash(scope, type: 'image')[:url]} () +image-path(key) + return pair[1] if pair[0] == key for pair in #{assets_hash(scope, type: 'image')[:path]} () STYL else '' @@ -36,10 +38,10 @@ def build_mixin_body(scope) # Internal: Construct Hash with absolute/relative paths in stylus syntax. # # Returns string representations of hash in Stylus syntax - def assets_hash(scope) + def assets_hash(scope, options = {}) @assets_hash ||= scope.environment.each_logical_path.each_with_object({ :url => '', :path => '' }) do |logical_path, assets_hash| unless logical_path =~/.*\.(css|js)$/ - path_to_asset = scope.path_to_asset(logical_path) + path_to_asset = scope.path_to_asset(logical_path, options) assets_hash[:url] << "('#{logical_path}' url(#{path_to_asset})) " assets_hash[:path] << "('#{logical_path}' \"#{path_to_asset}\") " end From 836bacc7768f917272d3ae32682fc82147216ef2 Mon Sep 17 00:00:00 2001 From: Jeff Escalante Date: Fri, 18 Oct 2013 12:15:02 -0400 Subject: [PATCH 3/3] abstract mixin generation logic, add more helper functions --- lib/stylus/tilt/rails.rb | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/lib/stylus/tilt/rails.rb b/lib/stylus/tilt/rails.rb index 05797b8..2aa9185 100644 --- a/lib/stylus/tilt/rails.rb +++ b/lib/stylus/tilt/rails.rb @@ -17,24 +17,36 @@ def evaluate(scope, locals, &block) # Internal: Builds body of a mixin # - # Returns string representation of a mixin with asset helper functions + # Returns string representation of all asset helper mixins def build_mixin_body(scope) @mixin_body ||= if assets_hash(scope).values.all? {|value| value != '' } <<-STYL -asset-url(key) - return pair[1] if pair[0] == key for pair in #{assets_hash(scope)[:url]} () -asset-path(key) - return pair[1] if pair[0] == key for pair in #{assets_hash(scope)[:path]} () -image-url(key) - return pair[1] if pair[0] == key for pair in #{assets_hash(scope, type: 'image')[:url]} () -image-path(key) - return pair[1] if pair[0] == key for pair in #{assets_hash(scope, type: 'image')[:path]} () +#{generate_helper(scope, 'asset')} +#{generate_helper(scope, 'image')} +#{generate_helper(scope, 'audio')} +#{generate_helper(scope, 'video')} STYL else '' end end + # Internal: Generates stylus mixin pairs for asset types + # + # Returns string representations of mixins in stylus syntax + def generate_helper(scope, name) + res = "" + ['url', 'path'].each do |type| + asset_pair = assets_hash(scope)[type.to_sym] + asset_pair = assets_hash(scope, type: name)[type.to_sym] if name != 'asset' + res += <<-EOS +#{name}-#{type}(key) + return pair[1] if pair[0] == key for pair in #{asset_pair} () + EOS + end + return res + end + # Internal: Construct Hash with absolute/relative paths in stylus syntax. # # Returns string representations of hash in Stylus syntax