sprockets-media_query_combiner 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
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
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sprockets-media-query-combiner.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Aaron Jensen
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,87 @@
1
+ # Sprockets::MediaQueryCombiner
2
+
3
+ Combines all matching media queries while compiling your assets with
4
+ sprockets/Rails asset pipeline. For example:
5
+
6
+ ```css
7
+ h3 {
8
+ color: orange
9
+ }
10
+ @media (max-width: 480px) {
11
+ h1 {
12
+ color: red
13
+ }
14
+ }
15
+ @media (max-width: 980px) {
16
+ h4 {
17
+ color: black
18
+ }
19
+ }
20
+ @media (max-width: 480px) {
21
+ h2 {
22
+ color: blue
23
+ }
24
+ }
25
+ ```
26
+
27
+ Would end up as (except the whitespace won't be so clean):
28
+
29
+ ```css
30
+ h3 {
31
+ color: orange
32
+ }
33
+ @media (max-width: 480px) {
34
+ h1 {
35
+ color: red
36
+ }
37
+ h2 {
38
+ color: blue
39
+ }
40
+ }
41
+ @media (max-width: 980px) {
42
+ h4 {
43
+ color: black
44
+ }
45
+ }
46
+ ```
47
+
48
+ ### Note
49
+
50
+ **This will change the order of your css, so be aware of that.** All the
51
+ `@media` queries will end up at the end of each css file in the order that
52
+ they are first encountered. In other words, if you're relying on only using
53
+ min-width or only using max-width in a specific order you'll want to be sure
54
+ define your media queries in the right order up front before you use them
55
+ randomly throughout your file.
56
+
57
+ ## Installation
58
+
59
+ Add this line to your application's Gemfile:
60
+
61
+ gem 'sprockets-media_query_combiner'
62
+
63
+ And then execute:
64
+
65
+ $ bundle
66
+
67
+ Or install it yourself as:
68
+
69
+ $ gem install sprockets-media_query_combiner
70
+
71
+ ## Usage
72
+
73
+ If you're using Rails you're done. Nothing more to do.
74
+
75
+ If you're using Sprockets on its own you'll need to register the bundle processor:
76
+
77
+ ```ruby
78
+ register_bundle_processor 'text/css', Sprockets::MediaQueryCombiner::Processor
79
+ ```
80
+
81
+ ## Contributing
82
+
83
+ 1. Fork it
84
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
85
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
86
+ 4. Push to the branch (`git push origin my-new-feature`)
87
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,13 @@
1
+ require 'sprockets/media_query_combiner/processor'
2
+
3
+ if defined?(Rails)
4
+ module Sprockets
5
+ module MediaQueryCombiner
6
+ class Engine < Rails::Engine
7
+ initializer :setup_media_query_combiner, after: 'sprockets.environment', group: :assets do |app|
8
+ app.assets.register_bundle_processor 'text/css', Sprockets::MediaQueryCombiner::Processor
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,19 @@
1
+ require 'tilt'
2
+
3
+ module Sprockets
4
+ module MediaQueryCombiner
5
+ class Processor < Tilt::Template
6
+ def prepare
7
+ end
8
+
9
+ def evaluate(context, locals, &block)
10
+ queries = Hash.new { |hash, key| hash[key] = '' }
11
+ filtered_data = data.gsub(/@media(?<query>[^{]+){(?<body>(?<braces>(?:[^{}]+)|({\g<braces>}))*)}\n?/m) do |match|
12
+ queries[$1.strip] << $2.strip; ''
13
+ end
14
+
15
+ "#{filtered_data}#{queries.map { |query, body| "@media #{query}{#{body}}" }.join}\n"
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,5 @@
1
+ module Sprockets
2
+ module MediaQueryCombiner
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ require "sprockets/media_query_combiner/version"
2
+ require "sprockets/media_query_combiner/processor"
3
+ require "sprockets/media_query_combiner/engine"
@@ -0,0 +1 @@
1
+ require "sprockets/media_query_combiner"
@@ -0,0 +1,17 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.filter_run :focus
11
+
12
+ # Run specs in random order to surface order dependencies. If you find an
13
+ # order dependency and want to debug it, you can fix the order by providing
14
+ # the seed, which is printed after each run.
15
+ # --seed 1234
16
+ config.order = 'random'
17
+ end
@@ -0,0 +1,44 @@
1
+ require 'spec_helper'
2
+ require 'sprockets/media_query_combiner/processor'
3
+
4
+ module Sprockets::MediaQueryCombiner
5
+ describe Processor do
6
+ it "should work" do
7
+ combiner = Processor.new do
8
+ <<CSS
9
+ h3 {
10
+ color: orange
11
+ }
12
+ @media (max-width: 480px) {
13
+ h1 {
14
+ color: red
15
+ }
16
+ }
17
+ @media (max-width: 980px) {
18
+ h4 {
19
+ color: black
20
+ }
21
+ }
22
+ @media (max-width: 480px) {
23
+ h2 {
24
+ color: blue
25
+ }
26
+ }
27
+ CSS
28
+ end
29
+ combiner.evaluate(nil, nil).should == <<CSS
30
+ h3 {
31
+ color: orange
32
+ }
33
+ @media (max-width: 480px){h1 {
34
+ color: red
35
+ }h2 {
36
+ color: blue
37
+ }}@media (max-width: 980px){h4 {
38
+ color: black
39
+ }}
40
+ CSS
41
+ end
42
+
43
+ end
44
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/sprockets/media_query_combiner/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Aaron Jensen"]
6
+ gem.email = ["aaronjensen@gmail.com"]
7
+ gem.summary = %q{Automatically combine media queries}
8
+ gem.description = %q{Sprockets bundle processor to combine all like media queries}
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "sprockets-media_query_combiner"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Sprockets::MediaQueryCombiner::VERSION
17
+
18
+ gem.add_runtime_dependency "sprockets", "~>2.1.3"
19
+
20
+ gem.add_development_dependency "rspec"
21
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sprockets-media_query_combiner
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Aaron Jensen
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-09-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: sprockets
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 2.1.3
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 2.1.3
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Sprockets bundle processor to combine all like media queries
47
+ email:
48
+ - aaronjensen@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - Gemfile
55
+ - LICENSE
56
+ - README.md
57
+ - Rakefile
58
+ - lib/sprockets-media_query_combiner.rb
59
+ - lib/sprockets/media_query_combiner.rb
60
+ - lib/sprockets/media_query_combiner/engine.rb
61
+ - lib/sprockets/media_query_combiner/processor.rb
62
+ - lib/sprockets/media_query_combiner/version.rb
63
+ - spec/spec_helper.rb
64
+ - spec/sprockets/media_query_combiner/processor_spec.rb
65
+ - sprockets-media_query_combiner.gemspec
66
+ homepage: ''
67
+ licenses: []
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ! '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubyforge_project:
86
+ rubygems_version: 1.8.24
87
+ signing_key:
88
+ specification_version: 3
89
+ summary: Automatically combine media queries
90
+ test_files:
91
+ - spec/spec_helper.rb
92
+ - spec/sprockets/media_query_combiner/processor_spec.rb