sprockets-sass 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -16,6 +16,7 @@ _Note: This works in Rails 3.1, thanks to the [sass-rails gem](http://github.com
16
16
  Directives from within imported files also work as expected.
17
17
  * Automatic integration with Compass.
18
18
  * Supports glob imports, like sass-rails.
19
+ * Asset path Sass functions. **New in 0.4!**
19
20
 
20
21
 
21
22
  Installation
@@ -126,6 +127,44 @@ button {
126
127
  }
127
128
  ```
128
129
 
130
+
131
+ Asset Path Sass Functions
132
+ -------------------------
133
+
134
+ As of version 0.4.0, asset path helpers have been added. Here's a quick guide to using them in your application (look at [sprockets-helpers](https://github.com/petebrowne/sprockets-helpers) for more information):
135
+
136
+ ``` ruby
137
+ map "/assets" do
138
+ environment = Sprockets::Environment.new
139
+ environment.append_path "assets/stylesheets"
140
+
141
+ Sprockets::Helpers.configure do |config|
142
+ config.environment = environment
143
+ config.prefix = "/assets"
144
+ config.digest = false
145
+ end
146
+
147
+ run environment
148
+ end
149
+
150
+ # etc...
151
+ ```
152
+
153
+ The Sass functions are based on the ones in sass-rails. So there is a `-path` and `-url` version of each helper:
154
+
155
+ ``` scss
156
+ background: url(asset-path("logo.jpg")); // background: url("/assets/logo.jpg");
157
+ background: asset-url("logo.jpg"); // background: url("/assets/logo.jpg");
158
+ ```
159
+
160
+ The API of the functions mimics the helpers provided by sprockets-helpers, using Sass keyword arguments as options:
161
+
162
+ ``` scss
163
+ background: asset-url("logo.jpg", $digest: true); // background: url("background: url("/assets/logo-27a8f1f96afd8d4c67a59eb9447f45bd.jpg");
164
+ background: asset-url("logo", $prefix: "/themes", $ext: "jpg"); // background: url("background: url("/themes/logo.jpg");
165
+ ```
166
+
167
+
129
168
  Copyright
130
169
  ---------
131
170
 
@@ -12,7 +12,18 @@ module Sprockets
12
12
  @options ||= {}
13
13
  end
14
14
  end
15
-
15
+
16
+ # Register the new templates
16
17
  register_engine ".sass", Sass::SassTemplate
17
18
  register_engine ".scss", Sass::ScssTemplate
19
+
20
+ # Attempt to add the Sass Functions
21
+ begin
22
+ require "sass"
23
+ require "sprockets/sass/functions"
24
+ rescue LoadError
25
+ # fail silently...
26
+ end
18
27
  end
28
+
29
+
@@ -0,0 +1,80 @@
1
+ require "sass"
2
+ require "sprockets-helpers"
3
+
4
+ module Sass
5
+ module Script
6
+ module Functions
7
+ # Using Sprockets::Helpers#asset_path, return the full path
8
+ # for the given +source+ as a Sass String. This supports keyword
9
+ # arguments that mirror the +options+.
10
+ #
11
+ # === Examples
12
+ #
13
+ # background: url(asset-path("image.jpg")); // background: url("/assets/image.jpg");
14
+ # background: url(asset-path("image.jpg", $digest: true)); // background: url("/assets/image-27a8f1f96afd8d4c67a59eb9447f45bd.jpg");
15
+ #
16
+ def asset_path(source, options = {})
17
+ Script::String.new context.asset_path(source.value, map_options(options)), :string
18
+ end
19
+ declare :asset_path, [:source], :var_kwargs => true
20
+
21
+ # Using Sprockets::Helpers#asset_path, return the url CSS
22
+ # for the given +source+ as a Sass String. This supports keyword
23
+ # arguments that mirror the +options+.
24
+ #
25
+ # === Examples
26
+ #
27
+ # background: asset-url("image.jpg"); // background: url("/assets/image.jpg");
28
+ # background: asset-url("image.jpg", $digest: true); // background: url("/assets/image-27a8f1f96afd8d4c67a59eb9447f45bd.jpg");
29
+ #
30
+ def asset_url(source, options = {})
31
+ Script::String.new "url(#{asset_path(source, options)})"
32
+ end
33
+ declare :asset_url, [:source], :var_kwargs => true
34
+
35
+ # Using Sprockets::Helpers#image_path, return the full path
36
+ # for the given +source+ as a Sass String. This supports keyword
37
+ # arguments that mirror the +options+.
38
+ #
39
+ # === Examples
40
+ #
41
+ # background: url(image-path("image.jpg")); // background: url("/assets/image.jpg");
42
+ # background: url(image-path("image.jpg", $digest: true)); // background: url("/assets/image-27a8f1f96afd8d4c67a59eb9447f45bd.jpg");
43
+ #
44
+ def image_path(source, options = {})
45
+ Script::String.new context.image_path(source.value, map_options(options)), :string
46
+ end
47
+ declare :image_path, [:source], :var_kwargs => true
48
+
49
+ # Using Sprockets::Helpers#image_path, return the url CSS
50
+ # for the given +source+ as a Sass String. This supports keyword
51
+ # arguments that mirror the +options+.
52
+ #
53
+ # === Examples
54
+ #
55
+ # background: asset-url("image.jpg"); // background: url("/assets/image.jpg");
56
+ # background: asset-url("image.jpg", $digest: true); // background: url("/assets/image-27a8f1f96afd8d4c67a59eb9447f45bd.jpg");
57
+ #
58
+ def image_url(source, options = {})
59
+ Script::String.new "url(#{image_path(source, options)})"
60
+ end
61
+ declare :image_url, [:source], :var_kwargs => true
62
+
63
+ protected
64
+
65
+ # Returns a reference to the Sprocket's context through
66
+ # the importer.
67
+ def context # :nodoc:
68
+ options[:importer].context
69
+ end
70
+
71
+ # Returns an options hash where the keys are symbolized
72
+ # and the values are unwrapped Sass literals.
73
+ def map_options(options = {}) # :nodoc:
74
+ Sass::Util.map_hash(options) do |key, value|
75
+ [key.to_sym, value.respond_to?(:value) ? value.value : value]
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
@@ -81,10 +81,18 @@ module Sprockets
81
81
  # we make Sprockets behave like Sass, and import partial
82
82
  # style paths.
83
83
  def resolve(path)
84
- path = Pathname.new(path) unless path.is_a?(Pathname)
85
- partial = path.dirname.join("_#{path.basename}")
84
+ path = Pathname.new(path) unless path.is_a?(Pathname)
86
85
 
87
- resolve_path(path) || resolve_path(partial)
86
+ # First look for the normal path
87
+ context.resolve(path) { |found| return found }
88
+
89
+ # Then look for the partial-style version
90
+ unless path.basename.to_s =~ /^_/
91
+ partial = path.dirname.join "_#{path.basename}"
92
+ context.resolve(partial) { |found| return found }
93
+ end
94
+
95
+ nil
88
96
  end
89
97
 
90
98
  # Finds all of the assets using the given glob.
@@ -97,13 +105,6 @@ module Sprockets
97
105
  end
98
106
  end
99
107
 
100
- # Finds the asset using the context from Sprockets.
101
- def resolve_path(path)
102
- context.resolve path, :content_type => :self
103
- rescue ::Sprockets::FileNotFound, ::Sprockets::ContentTypeMismatch
104
- nil
105
- end
106
-
107
108
  # Returns the Sass syntax of the given path.
108
109
  def syntax(path)
109
110
  path.to_s.include?(".sass") ? :sass : :scss
@@ -8,6 +8,12 @@ module Sprockets
8
8
  # A reference to the current Sprockets context
9
9
  attr_reader :context
10
10
 
11
+ # Add the Sass functions if they haven't already been added.
12
+ def initialize_engine
13
+ super
14
+ require_template_library "sprockets/sass/functions"
15
+ end
16
+
11
17
  # Define the expected syntax for the template
12
18
  def syntax
13
19
  :sass
@@ -1,5 +1,5 @@
1
1
  module Sprockets
2
2
  module Sass
3
- VERSION = "0.3.0"
3
+ VERSION = "0.4.0"
4
4
  end
5
5
  end
@@ -115,10 +115,10 @@ describe Sprockets::Sass do
115
115
  end
116
116
 
117
117
  it "works with the Compass framework" do
118
- @assets.file "main.css.scss", %(@import "compass/typography/text/nowrap";\np { @include nowrap; })
118
+ @assets.file "main.css.scss", %(@import "compass/css3";\nbutton { @include border-radius(5px); })
119
119
 
120
120
  asset = @env["main.css"]
121
- asset.to_s.should == "p {\n white-space: nowrap; }\n"
121
+ asset.to_s.should include("border-radius: 5px;")
122
122
  end
123
123
 
124
124
  it "imports globbed files" do
@@ -172,4 +172,30 @@ describe Sprockets::Sass do
172
172
 
173
173
  asset.should be_stale
174
174
  end
175
+
176
+ it "adds the #asset_path helper" do
177
+ @assets.file "asset_path.css.scss", %(body { background: url(asset-path("image.jpg")); })
178
+ @assets.file "asset_url.css.scss", %(body { background: asset-url("image.jpg"); })
179
+ @assets.file "asset_path_options.css.scss", %(body { background: url(asset-path("image.jpg", $digest: true, $prefix: "/themes")); })
180
+ @assets.file "asset_url_options.css.scss", %(body { background: asset-url("image.jpg", $digest: true, $prefix: "/themes"); })
181
+ @assets.file "image.jpg"
182
+
183
+ @env["asset_path.css"].to_s.should == %(body {\n background: url("/assets/image.jpg"); }\n)
184
+ @env["asset_url.css"].to_s.should == %(body {\n background: url("/assets/image.jpg"); }\n)
185
+ @env["asset_path_options.css"].to_s.should =~ %r(body \{\n background: url\("/themes/image-[0-9a-f]+.jpg"\); \}\n)
186
+ @env["asset_url_options.css"].to_s.should =~ %r(body \{\n background: url\("/themes/image-[0-9a-f]+.jpg"\); \}\n)
187
+ end
188
+
189
+ it "adds the #image_path helper" do
190
+ @assets.file "image_path.css.scss", %(body { background: url(image-path("image.jpg")); })
191
+ @assets.file "image_url.css.scss", %(body { background: image-url("image.jpg"); })
192
+ @assets.file "image_path_options.css.scss", %(body { background: url(image-path("image.jpg", $digest: true, $prefix: "/themes")); })
193
+ @assets.file "image_url_options.css.scss", %(body { background: image-url("image.jpg", $digest: true, $prefix: "/themes"); })
194
+ @assets.file "image.jpg"
195
+
196
+ @env["image_path.css"].to_s.should == %(body {\n background: url("/assets/image.jpg"); }\n)
197
+ @env["image_url.css"].to_s.should == %(body {\n background: url("/assets/image.jpg"); }\n)
198
+ @env["image_path_options.css"].to_s.should =~ %r(body \{\n background: url\("/themes/image-[0-9a-f]+.jpg"\); \}\n)
199
+ @env["image_url_options.css"].to_s.should =~ %r(body \{\n background: url\("/themes/image-[0-9a-f]+.jpg"\); \}\n)
200
+ end
175
201
  end
@@ -18,10 +18,12 @@ Gem::Specification.new do |s|
18
18
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
19
  s.require_paths = ["lib"]
20
20
 
21
- s.add_dependency "sprockets", "~> 2.0"
22
- s.add_development_dependency "rake"
23
- s.add_development_dependency "rspec", "~> 2.6"
21
+ s.add_dependency "sprockets", "~> 2.0"
22
+ s.add_dependency "sprockets-helpers", "~> 0.1"
23
+ s.add_dependency "tilt", "~> 1.1"
24
+ s.add_development_dependency "rspec", "~> 2.6"
24
25
  s.add_development_dependency "test-construct", "~> 1.2"
25
- s.add_development_dependency "sass", "~> 3.1"
26
- s.add_development_dependency "compass", "0.11"
26
+ s.add_development_dependency "sass", "~> 3.1"
27
+ s.add_development_dependency "compass", "~> 0.11"
28
+ s.add_development_dependency "rake"
27
29
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: sprockets-sass
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.3.0
5
+ version: 0.4.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Pete Browne
@@ -10,11 +10,10 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-10-01 00:00:00 Z
13
+ date: 2011-11-02 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: sprockets
17
- prerelease: false
18
17
  requirement: &id001 !ruby/object:Gem::Requirement
19
18
  none: false
20
19
  requirements:
@@ -22,62 +21,85 @@ dependencies:
22
21
  - !ruby/object:Gem::Version
23
22
  version: "2.0"
24
23
  type: :runtime
24
+ prerelease: false
25
25
  version_requirements: *id001
26
26
  - !ruby/object:Gem::Dependency
27
- name: rake
28
- prerelease: false
27
+ name: sprockets-helpers
29
28
  requirement: &id002 !ruby/object:Gem::Requirement
30
29
  none: false
31
30
  requirements:
32
- - - ">="
31
+ - - ~>
33
32
  - !ruby/object:Gem::Version
34
- version: "0"
35
- type: :development
33
+ version: "0.1"
34
+ type: :runtime
35
+ prerelease: false
36
36
  version_requirements: *id002
37
37
  - !ruby/object:Gem::Dependency
38
- name: rspec
39
- prerelease: false
38
+ name: tilt
40
39
  requirement: &id003 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ version: "1.1"
45
+ type: :runtime
46
+ prerelease: false
47
+ version_requirements: *id003
48
+ - !ruby/object:Gem::Dependency
49
+ name: rspec
50
+ requirement: &id004 !ruby/object:Gem::Requirement
41
51
  none: false
42
52
  requirements:
43
53
  - - ~>
44
54
  - !ruby/object:Gem::Version
45
55
  version: "2.6"
46
56
  type: :development
47
- version_requirements: *id003
57
+ prerelease: false
58
+ version_requirements: *id004
48
59
  - !ruby/object:Gem::Dependency
49
60
  name: test-construct
50
- prerelease: false
51
- requirement: &id004 !ruby/object:Gem::Requirement
61
+ requirement: &id005 !ruby/object:Gem::Requirement
52
62
  none: false
53
63
  requirements:
54
64
  - - ~>
55
65
  - !ruby/object:Gem::Version
56
66
  version: "1.2"
57
67
  type: :development
58
- version_requirements: *id004
68
+ prerelease: false
69
+ version_requirements: *id005
59
70
  - !ruby/object:Gem::Dependency
60
71
  name: sass
61
- prerelease: false
62
- requirement: &id005 !ruby/object:Gem::Requirement
72
+ requirement: &id006 !ruby/object:Gem::Requirement
63
73
  none: false
64
74
  requirements:
65
75
  - - ~>
66
76
  - !ruby/object:Gem::Version
67
77
  version: "3.1"
68
78
  type: :development
69
- version_requirements: *id005
79
+ prerelease: false
80
+ version_requirements: *id006
70
81
  - !ruby/object:Gem::Dependency
71
82
  name: compass
72
- prerelease: false
73
- requirement: &id006 !ruby/object:Gem::Requirement
83
+ requirement: &id007 !ruby/object:Gem::Requirement
74
84
  none: false
75
85
  requirements:
76
- - - "="
86
+ - - ~>
77
87
  - !ruby/object:Gem::Version
78
88
  version: "0.11"
79
89
  type: :development
80
- version_requirements: *id006
90
+ prerelease: false
91
+ version_requirements: *id007
92
+ - !ruby/object:Gem::Dependency
93
+ name: rake
94
+ requirement: &id008 !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: "0"
100
+ type: :development
101
+ prerelease: false
102
+ version_requirements: *id008
81
103
  description: When using Sprockets 2.0 with Sass you will eventually run into a pretty big issue. `//= require` directives will not allow Sass mixins, variables, etc. to be shared between files. So you'll try to use `@import`, and that'll also blow up in your face. `sprockets-sass` fixes all of this by creating a Sass::Importer that is Sprockets aware.
82
104
  email:
83
105
  - me@petebrowne.com
@@ -95,6 +117,7 @@ files:
95
117
  - Rakefile
96
118
  - lib/sprockets-sass.rb
97
119
  - lib/sprockets/sass.rb
120
+ - lib/sprockets/sass/functions.rb
98
121
  - lib/sprockets/sass/importer.rb
99
122
  - lib/sprockets/sass/sass_template.rb
100
123
  - lib/sprockets/sass/scss_template.rb
@@ -115,12 +138,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
115
138
  requirements:
116
139
  - - ">="
117
140
  - !ruby/object:Gem::Version
141
+ hash: 2155872013882302780
142
+ segments:
143
+ - 0
118
144
  version: "0"
119
145
  required_rubygems_version: !ruby/object:Gem::Requirement
120
146
  none: false
121
147
  requirements:
122
148
  - - ">="
123
149
  - !ruby/object:Gem::Version
150
+ hash: 2155872013882302780
151
+ segments:
152
+ - 0
124
153
  version: "0"
125
154
  requirements: []
126
155