sprockets_relative_url 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f6828c7133914066d3801a9db6b921e185ff23d3
4
+ data.tar.gz: fd1d1436b1eef3bd5cb65160fb9460c8d98d9df6
5
+ SHA512:
6
+ metadata.gz: bbad6f0dad17a64b955d96be5ff18606cd1bf5edb228f3843f51c7044a1a67465ce386874d56ab57da95ef2a27ef55b3e61e4d15e5de00ce3b2193dfe6b5c76a
7
+ data.tar.gz: 898bdae3a5367c1e515204bb8a81d133913a1b5e0605aea57aea3828d9f568627112b629676c10caf51597caf05dff2a6c0b5e9f541b0a2edd22358af27b0e4f
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --require spec_helper
3
+ --format documentation
data/.rubocop.yml ADDED
@@ -0,0 +1,2 @@
1
+ Documentation:
2
+ Enabled: false
data/.simplecov ADDED
@@ -0,0 +1,3 @@
1
+ SimpleCov.start do
2
+ add_filter('spec')
3
+ end
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sprockets_relative_url.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,14 @@
1
+ guard :rspec, cmd: 'bundle exec rspec' do
2
+ watch(/^spec\/.+_spec\.rb$/)
3
+ watch(/^lib\/(.+)\.rb$/) { |m| "spec/#{m[1]}_spec.rb" }
4
+ watch('spec/spec_helper.rb') { 'spec' }
5
+ end
6
+
7
+ guard :rubocop do
8
+ watch('Gemfile')
9
+ watch('Guardfile')
10
+ watch('Rakefile')
11
+ watch(/.+\.gemspec$/)
12
+ watch(/.+\.rb$/)
13
+ watch(/(?:.+\/)?\.rubocop\.yml$/) { |m| File.dirname(m[0]) }
14
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Shaun Mangelsdorf
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # Sprockets Relative URL support
2
+
3
+ Fixes relative URLs in CSS assets using Sprockets.
4
+
5
+ Each asset is resolved from Sprockets, and its relative URL will be rewritten to
6
+ use a precompiled version. By adding this preprocessor, CSS frameworks can be
7
+ minified and combined into your `application.css` file without any need to
8
+ rewrite the `url()` values in their CSS.
9
+
10
+ Tested with:
11
+
12
+ - Rails
13
+ - TODO: Sinatra
14
+
15
+ ## Installation
16
+
17
+ Add this line to your application's Gemfile:
18
+
19
+ gem 'sprockets_relative_url'
20
+
21
+ ## Usage
22
+
23
+ ### Rails
24
+
25
+ Add the processor to the Rails asset pipeline:
26
+
27
+ Rails.application.assets
28
+ .register_postprocessor('text/css', SprocketsRelativeUrl::Processor)
29
+
30
+ ### Sinatra
31
+
32
+ TODO
33
+
34
+ ## Contributing
35
+
36
+ 1. Fork it ( https://github.com/smangelsdorf/sprockets_relative_url/fork )
37
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
38
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
39
+ 4. Push to the branch (`git push origin my-new-feature`)
40
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,37 @@
1
+ module SprocketsRelativeUrl
2
+ class Processor < Sprockets::Processor
3
+ CSS_URL_REGEXP = /
4
+ (?<=\burl\() # Match url\( prefix
5
+ \s*
6
+ (?<q>['"]?) # Opening quote
7
+ (?<path>[^\)]+?) # Capture actual path
8
+ \k<q> # Backref for closing quote
9
+ \s*
10
+ (?=\)) # Match \) terminator
11
+ /x
12
+
13
+ def evaluate(context, *)
14
+ data.gsub(CSS_URL_REGEXP) do
15
+ path = Regexp.last_match[:path]
16
+
17
+ next path if should_skip?(path)
18
+ root_relative_path(context, path)
19
+ end
20
+ end
21
+
22
+ private
23
+
24
+ def root_relative_path(context, path)
25
+ asset_full_path = context.pathname.parent.join(path)
26
+ asset = context.environment.find_asset(asset_full_path)
27
+
28
+ return path if asset.nil?
29
+
30
+ context.asset_path(asset.logical_path)
31
+ end
32
+
33
+ def should_skip?(path)
34
+ path.start_with?('/') || path.include?('\\') || URI.parse(path).absolute?
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,3 @@
1
+ module SprocketsRelativeUrl
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,9 @@
1
+ require 'sprockets'
2
+
3
+ require 'sprockets_relative_url/version'
4
+
5
+ module SprocketsRelativeUrl
6
+ # Your code goes here...
7
+ end
8
+
9
+ require 'sprockets_relative_url/processor'
@@ -0,0 +1,18 @@
1
+ require 'simplecov'
2
+
3
+ RSpec.configure do |config|
4
+ config.filter_run :focus
5
+ config.run_all_when_everything_filtered = true
6
+
7
+ config.order = :random
8
+ Kernel.srand config.seed
9
+
10
+ config.expect_with :rspec do |expectations|
11
+ expectations.syntax = :expect
12
+ end
13
+
14
+ config.mock_with :rspec do |mocks|
15
+ mocks.syntax = :expect
16
+ mocks.verify_partial_doubles = true
17
+ end
18
+ end
@@ -0,0 +1,148 @@
1
+ require 'sprockets_relative_url'
2
+
3
+ describe SprocketsRelativeUrl::Processor do
4
+ let(:env) do
5
+ Sprockets::Environment.new.tap do |env|
6
+ env.append_path('tmp')
7
+ end
8
+ end
9
+
10
+ let(:base_path) { Pathname.new(File.expand_path('./tmp')) }
11
+ let(:context) do
12
+ double(
13
+ pathname: base_path + 'css' + 'application.css',
14
+ environment: env
15
+ )
16
+ end
17
+ let(:asset_prefix) { '/assets' }
18
+
19
+ let(:files) { %w(images/background.gif css/extra/test.gif) }
20
+ let(:hash) { SecureRandom.hex(20) }
21
+
22
+ subject { SprocketsRelativeUrl::Processor.new { data } }
23
+
24
+ before do
25
+ files.each do |file|
26
+ pathname = base_path.join(file)
27
+ pathname.parent.mkpath
28
+ pathname.write('') unless pathname.exist?
29
+ end
30
+
31
+ allow(context).to receive(:asset_path) do |s|
32
+ base, _, suffix = s.rpartition('.')
33
+ File.join(asset_prefix, "#{base}-#{hash}.#{suffix}")
34
+ end
35
+ end
36
+
37
+ shared_examples 'rewrite the url' do
38
+ it 'rewrites the url' do
39
+ expect(subject.evaluate(context)).to match expected
40
+ end
41
+ end
42
+
43
+ shared_examples 'leave the url alone' do
44
+ it 'leaves the url alone' do
45
+ expect(subject.evaluate(context)).to be == data
46
+ end
47
+ end
48
+
49
+ context 'with a simple url' do
50
+ let(:data) { 'background: url(../images/background.gif)' }
51
+ let(:expected) do
52
+ %r{\Abackground: url\(/assets/images/background-\h+\.gif\)\z}
53
+ end
54
+
55
+ include_examples 'rewrite the url'
56
+
57
+ context 'with single quotes' do
58
+ let(:data) { "background: url('../images/background.gif')" }
59
+ include_examples 'rewrite the url'
60
+ end
61
+
62
+ context 'with double quotes' do
63
+ let(:data) { 'background: url("../images/background.gif")' }
64
+ include_examples 'rewrite the url'
65
+ end
66
+
67
+ context 'with surrounding whitespace' do
68
+ let(:data) { 'background: url( ../images/background.gif )' }
69
+ include_examples 'rewrite the url'
70
+
71
+ context 'with single quotes' do
72
+ let(:data) { "background: url( '../images/background.gif' )" }
73
+ include_examples 'rewrite the url'
74
+ end
75
+
76
+ context 'with double quotes' do
77
+ let(:data) { 'background: url( "../images/background.gif" )' }
78
+ include_examples 'rewrite the url'
79
+ end
80
+ end
81
+ end
82
+
83
+ context 'with an expected hash' do
84
+ let(:hash) { '1234abcd' }
85
+ let(:data) { 'background: url(../images/background.gif)' }
86
+ let(:expected) { 'background: url(/assets/images/background-1234abcd.gif)' }
87
+
88
+ include_examples 'rewrite the url'
89
+ end
90
+
91
+ context 'with a different asset prefix' do
92
+ let(:asset_prefix) { '/differentpath' }
93
+ let(:data) { 'background: url(../images/background.gif)' }
94
+ let(:expected) do
95
+ %r{\Abackground: url\(/differentpath/images/background-\h+\.gif\)\z}
96
+ end
97
+
98
+ include_examples 'rewrite the url'
99
+ end
100
+
101
+ context 'with trailing text' do
102
+ let(:data) { 'background: url(../images/background.gif) repeat-x' }
103
+ let(:expected) do
104
+ %r{\Abackground: url\(/assets/images/background-\h+\.gif\) repeat-x\z}
105
+ end
106
+
107
+ include_examples 'rewrite the url'
108
+ end
109
+
110
+ context 'with a lower relative url' do
111
+ let(:data) { 'background: url(extra/test.gif)' }
112
+ let(:expected) do
113
+ %r{\Abackground: url\(/assets/css/extra/test-\h+\.gif\)\z}
114
+ end
115
+
116
+ include_examples 'rewrite the url'
117
+ end
118
+
119
+ context 'with a root-relative url' do
120
+ let(:data) { 'background: url(/images/background.gif)' }
121
+ include_examples 'leave the url alone'
122
+ end
123
+
124
+ context 'with an absolute url' do
125
+ let(:data) { 'background: url(http://example.com/images/background.gif)' }
126
+ include_examples 'leave the url alone'
127
+ end
128
+
129
+ context 'with a non-existent file' do
130
+ let(:data) { 'background: url(../images/nonexistent.gif)' }
131
+ include_examples 'leave the url alone'
132
+ end
133
+
134
+ context 'with preceding characters' do
135
+ let(:data) { 'background: nurl(../images/background.gif)' }
136
+ include_examples 'leave the url alone'
137
+ end
138
+
139
+ context 'with escaped characters' do
140
+ let(:data) { 'background: url(../\ images/background.gif)' }
141
+ include_examples 'leave the url alone'
142
+ end
143
+
144
+ context 'with escaped parentheses' do
145
+ let(:data) { 'background: url(../\)images/background.gif)' }
146
+ include_examples 'leave the url alone'
147
+ end
148
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'sprockets_relative_url/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'sprockets_relative_url'
8
+ spec.version = SprocketsRelativeUrl::VERSION
9
+ spec.authors = ['Shaun Mangelsdorf']
10
+ spec.email = %w(s.mangelsdorf@gmail.com)
11
+ spec.summary = 'Fixes relative URLs in CSS assets using Sprockets.'
12
+ spec.homepage = 'https://github.com/smangelsdorf/sprockets_relative_url'
13
+ spec.license = 'MIT'
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(/^bin\//) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(/^(test|spec|features)\//)
18
+ spec.require_paths = %w(lib)
19
+
20
+ spec.add_development_dependency 'guard'
21
+ spec.add_development_dependency 'guard-rubocop'
22
+ spec.add_development_dependency 'guard-rspec'
23
+ spec.add_development_dependency 'rspec'
24
+ spec.add_development_dependency 'bundler', '~> 1.6'
25
+ spec.add_development_dependency 'rake'
26
+ spec.add_development_dependency 'simplecov'
27
+
28
+ spec.add_runtime_dependency 'sprockets', '~> 2.12'
29
+ end
metadata ADDED
@@ -0,0 +1,173 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sprockets_relative_url
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Shaun Mangelsdorf
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: guard
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: guard-rubocop
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: guard-rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.6'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.6'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: simplecov
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: sprockets
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '2.12'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '2.12'
125
+ description:
126
+ email:
127
+ - s.mangelsdorf@gmail.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".gitignore"
133
+ - ".rspec"
134
+ - ".rubocop.yml"
135
+ - ".simplecov"
136
+ - Gemfile
137
+ - Guardfile
138
+ - LICENSE.txt
139
+ - README.md
140
+ - Rakefile
141
+ - lib/sprockets_relative_url.rb
142
+ - lib/sprockets_relative_url/processor.rb
143
+ - lib/sprockets_relative_url/version.rb
144
+ - spec/spec_helper.rb
145
+ - spec/sprockets_relative_url/processor_spec.rb
146
+ - sprockets_relative_url.gemspec
147
+ homepage: https://github.com/smangelsdorf/sprockets_relative_url
148
+ licenses:
149
+ - MIT
150
+ metadata: {}
151
+ post_install_message:
152
+ rdoc_options: []
153
+ require_paths:
154
+ - lib
155
+ required_ruby_version: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ required_rubygems_version: !ruby/object:Gem::Requirement
161
+ requirements:
162
+ - - ">="
163
+ - !ruby/object:Gem::Version
164
+ version: '0'
165
+ requirements: []
166
+ rubyforge_project:
167
+ rubygems_version: 2.2.2
168
+ signing_key:
169
+ specification_version: 4
170
+ summary: Fixes relative URLs in CSS assets using Sprockets.
171
+ test_files:
172
+ - spec/spec_helper.rb
173
+ - spec/sprockets_relative_url/processor_spec.rb