css_media 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +1 -0
- data/.travis.yml +16 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +34 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/css_media.gemspec +32 -0
- data/lib/css_media/processor.rb +47 -0
- data/lib/css_media/railtie.rb +29 -0
- data/lib/css_media/result.rb +24 -0
- data/lib/css_media/sprockets.rb +62 -0
- data/lib/css_media/version.rb +3 -0
- data/lib/css_media.rb +30 -0
- data/shared.gemfile +0 -0
- data/sprockets2.gemfile +8 -0
- data/sprockets3.gemfile +9 -0
- data/sprockets4.gemfile +9 -0
- metadata +162 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1acd0b88278dc20a83f3e21c44ed9dc541c7de3c
|
4
|
+
data.tar.gz: 644be716e3d02c80f40a70efe94632fcb2575105
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f7e6eff4116d25e317fb8f04779e8c539a993561456b4d967f02e6c937dade9c1c4619f7334e3255425721089eb6581df96798e58a8a95f309f458cfb0b23e17
|
7
|
+
data.tar.gz: de45bc9e2e0ecfb242e3055da6a633c9820bef9dc9d7ef33c23d6385c0da502c1fb1334995715f47ce3a79fd8710f7478e3b7bac0823b4bd88dee4ef6691b00c
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--format documentation --colour
|
data/.travis.yml
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
sudo: false
|
2
|
+
language: ruby
|
3
|
+
rvm:
|
4
|
+
- "2.0.0"
|
5
|
+
- "2.3.0"
|
6
|
+
cache: bundler
|
7
|
+
gemfile:
|
8
|
+
- sprockets2.gemfile
|
9
|
+
- sprockets3.gemfile
|
10
|
+
- sprockets4.gemfile
|
11
|
+
matrix:
|
12
|
+
exclude:
|
13
|
+
- rvm: "2.0.0"
|
14
|
+
gemfile: sprockets4.gemfile
|
15
|
+
- rvm: "2.0.0"
|
16
|
+
gemfile: sprockets3.gemfile
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Marc-Andre Lafortune
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# CssMedia
|
2
|
+
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/css-media.svg)](https://badge.fury.io/rb/inky-rb) [![Build Status](https://travis-ci.org/zurb/ss-media.svg?branch=master)](https://travis-ci.org/zurb/inky-rb)
|
4
|
+
|
5
|
+
A small rails extension to segregate `@media` CSS rules.
|
6
|
+
|
7
|
+
This can be helpful when sending emails, as non media rules should be inlined (with [`premailer-rails`](https://github.com/fphilipe/premailer-rails) or [`roadie`](https://github.com/Mange/roadie)) but media rules should be included with a `<style>` tag.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'css_media'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
If your filename contains `media-only`, then only the `@media` css rules will be kept. If the filename matches `non-media` or `no-media`, then all rules will be kept except the `@media` rules. Other assets will be kept intact
|
24
|
+
|
25
|
+
## Development
|
26
|
+
|
27
|
+
Much of the boilerplate code copied from [`autoprefixer-rails`](https://github.com/ai/autoprefixer-rails), thanks!
|
28
|
+
|
29
|
+
To run the tests: `rspec`. You'll need to initial run `bundle` of course.
|
30
|
+
|
31
|
+
## License
|
32
|
+
|
33
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
34
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "css_media"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/css_media.gemspec
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'css_media/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "css_media"
|
8
|
+
spec.version = CssMedia::VERSION
|
9
|
+
spec.authors = ["Marc-Andre Lafortune"]
|
10
|
+
spec.email = ["github@marc-andre.ca"]
|
11
|
+
|
12
|
+
spec.summary = %q{Filter CSS to produce only @media rules}
|
13
|
+
spec.description = %q{Filter CSS to produce only @media rules}
|
14
|
+
spec.homepage = "http://github.com/marcandre/css_media"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
|
18
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
19
|
+
f.match(%r{^(test|spec|features)/})
|
20
|
+
end
|
21
|
+
spec.bindir = "exe"
|
22
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
23
|
+
spec.require_paths = ["lib"]
|
24
|
+
|
25
|
+
spec.add_dependency "crass"
|
26
|
+
spec.add_dependency "rails"
|
27
|
+
spec.add_development_dependency "bundler"
|
28
|
+
spec.add_development_dependency "rake"
|
29
|
+
spec.add_development_dependency "rspec-rails"
|
30
|
+
spec.add_development_dependency "sass"
|
31
|
+
spec.add_development_dependency "pry-byebug"
|
32
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'crass'
|
2
|
+
|
3
|
+
module CssMedia
|
4
|
+
class Processor
|
5
|
+
|
6
|
+
def initialize(params = { })
|
7
|
+
@params = params || { }
|
8
|
+
end
|
9
|
+
|
10
|
+
# Process `css` and return result.
|
11
|
+
#
|
12
|
+
# Options can be:
|
13
|
+
# * `from` with input CSS file name. Will be used in error messages.
|
14
|
+
# * `to` with output CSS file name.
|
15
|
+
# * `map` with true to generate new source map or with previous map.
|
16
|
+
def process(css, opts = { })
|
17
|
+
fn = opts.fetch(:from, '').downcase.gsub('-', '_')
|
18
|
+
if fn =~ /[^a-z](media_only|non?_media)[^a-z]/
|
19
|
+
css = send($1, css)
|
20
|
+
end
|
21
|
+
Result.new(css)
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def is_media?(rule)
|
27
|
+
# rule[:node] == :style_rule && rule[:selector][:value] =~ /^media\W/i
|
28
|
+
rule[:node] == :at_rule && !!(rule[:name] =~ /media/i)
|
29
|
+
end
|
30
|
+
|
31
|
+
def filter_media(css, want_media)
|
32
|
+
tree = Crass.parse(css, :preserve_comments => true)
|
33
|
+
want = tree.select{|rule| is_media?(rule) == want_media}
|
34
|
+
Crass::Parser.stringify(want)
|
35
|
+
end
|
36
|
+
|
37
|
+
def non_media(css)
|
38
|
+
filter_media(css, false)
|
39
|
+
end
|
40
|
+
alias_method :no_media, :non_media
|
41
|
+
|
42
|
+
def media_only(css)
|
43
|
+
filter_media(css, true)
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
|
3
|
+
begin
|
4
|
+
module CssMedia
|
5
|
+
class Railtie < ::Rails::Railtie
|
6
|
+
if config.respond_to?(:assets) and not config.assets.nil?
|
7
|
+
config.assets.configure do |env|
|
8
|
+
CssMedia.install(env, config(env.root))
|
9
|
+
end
|
10
|
+
else
|
11
|
+
initializer :setup_css_media, group: :all do |app|
|
12
|
+
if defined? app.assets and not app.assets.nil?
|
13
|
+
CssMedia.install(app.assets, config(app.root))
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
# Read browsers requirements from application config
|
19
|
+
def config(root)
|
20
|
+
file = File.join(root, 'config/css_media.yml')
|
21
|
+
params = ::YAML.load_file(file) if File.exist?(file)
|
22
|
+
params ||= {}
|
23
|
+
params = params.symbolize_keys
|
24
|
+
params
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
rescue LoadError
|
29
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module CssMedia
|
2
|
+
# Container of prefixed CSS and source map with changes
|
3
|
+
class Result
|
4
|
+
# Prefixed CSS after Autoprefixer
|
5
|
+
attr_reader :css
|
6
|
+
|
7
|
+
# Source map of changes
|
8
|
+
attr_reader :map
|
9
|
+
|
10
|
+
# Warnings from Autoprefixer
|
11
|
+
attr_reader :warnings
|
12
|
+
|
13
|
+
def initialize(css, map = [], warnings = [])
|
14
|
+
@warnings = warnings
|
15
|
+
@css = css
|
16
|
+
@map = map
|
17
|
+
end
|
18
|
+
|
19
|
+
# Stringify prefixed CSS
|
20
|
+
def to_s
|
21
|
+
@css
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'pathname'
|
2
|
+
|
3
|
+
module CssMedia
|
4
|
+
# Register cssmedia postprocessor in Sprockets and fix common issues
|
5
|
+
class Sprockets
|
6
|
+
def self.register_processor(processor)
|
7
|
+
@processor = processor
|
8
|
+
end
|
9
|
+
|
10
|
+
# Sprockets 3 and 4 API
|
11
|
+
def self.call(input)
|
12
|
+
filename = input[:filename]
|
13
|
+
source = input[:data]
|
14
|
+
run(filename, source)
|
15
|
+
end
|
16
|
+
|
17
|
+
# Add prefixes to `css`
|
18
|
+
def self.run(filename, css)
|
19
|
+
output = filename.chomp(File.extname(filename)) + '.css'
|
20
|
+
result = @processor.process(css, from: filename, to: output)
|
21
|
+
|
22
|
+
result.warnings.each do |warning|
|
23
|
+
$stderr.puts "css_media: #{ warning }"
|
24
|
+
end
|
25
|
+
|
26
|
+
result.css
|
27
|
+
end
|
28
|
+
|
29
|
+
# Register postprocessor in Sprockets depend on issues with other gems
|
30
|
+
def self.install(env)
|
31
|
+
if ::Sprockets::VERSION.to_f < 4
|
32
|
+
env.register_postprocessor('text/css',
|
33
|
+
::CssMedia::Sprockets)
|
34
|
+
else
|
35
|
+
env.register_bundle_processor('text/css',
|
36
|
+
::CssMedia::Sprockets)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
# Register postprocessor in Sprockets depend on issues with other gems
|
41
|
+
def self.uninstall(env)
|
42
|
+
if ::Sprockets::VERSION.to_f < 4
|
43
|
+
env.unregister_postprocessor('text/css',
|
44
|
+
::CssMedia::Sprockets)
|
45
|
+
else
|
46
|
+
env.unregister_bundle_processor('text/css',
|
47
|
+
::CssMedia::Sprockets)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
# Sprockets 2 API new and render
|
52
|
+
def initialize(filename, &block)
|
53
|
+
@filename = filename
|
54
|
+
@source = block.call
|
55
|
+
end
|
56
|
+
|
57
|
+
# Sprockets 2 API new and render
|
58
|
+
def render(_, _)
|
59
|
+
self.class.run(@filename, @source)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
data/lib/css_media.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require "css_media/version"
|
2
|
+
|
3
|
+
module CssMedia
|
4
|
+
autoload :Sprockets, 'css_media/sprockets'
|
5
|
+
|
6
|
+
# Add prefixes to `css`. See `Processor#process` for options.
|
7
|
+
def self.process(css, opts = { })
|
8
|
+
params = { }
|
9
|
+
# params[:browsers] = opts.delete(:browsers) if opts.has_key?(:browsers)
|
10
|
+
Processor.new(params).process(css, opts)
|
11
|
+
end
|
12
|
+
|
13
|
+
# Add Autoprefixer for Sprockets environment in `assets`.
|
14
|
+
# You can specify `browsers` actual in your project.
|
15
|
+
def self.install(assets, opts = {})
|
16
|
+
Sprockets.register_processor(Processor.new(opts))
|
17
|
+
Sprockets.install(assets)
|
18
|
+
end
|
19
|
+
|
20
|
+
# Disable installed Autoprefixer
|
21
|
+
def self.uninstall(assets)
|
22
|
+
Sprockets.uninstall(assets)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
require_relative 'css_media/result'
|
27
|
+
require_relative 'css_media/version'
|
28
|
+
require_relative 'css_media/processor'
|
29
|
+
|
30
|
+
require_relative 'css_media/railtie' if defined?(Rails)
|
data/shared.gemfile
ADDED
File without changes
|
data/sprockets2.gemfile
ADDED
data/sprockets3.gemfile
ADDED
data/sprockets4.gemfile
ADDED
metadata
ADDED
@@ -0,0 +1,162 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: css_media
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Marc-Andre Lafortune
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-10-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: crass
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
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: rails
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
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: bundler
|
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: rake
|
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: rspec-rails
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: sass
|
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: pry-byebug
|
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
|
+
description: Filter CSS to produce only @media rules
|
112
|
+
email:
|
113
|
+
- github@marc-andre.ca
|
114
|
+
executables: []
|
115
|
+
extensions: []
|
116
|
+
extra_rdoc_files: []
|
117
|
+
files:
|
118
|
+
- ".gitignore"
|
119
|
+
- ".rspec"
|
120
|
+
- ".travis.yml"
|
121
|
+
- Gemfile
|
122
|
+
- LICENSE.txt
|
123
|
+
- README.md
|
124
|
+
- Rakefile
|
125
|
+
- bin/console
|
126
|
+
- bin/setup
|
127
|
+
- css_media.gemspec
|
128
|
+
- lib/css_media.rb
|
129
|
+
- lib/css_media/processor.rb
|
130
|
+
- lib/css_media/railtie.rb
|
131
|
+
- lib/css_media/result.rb
|
132
|
+
- lib/css_media/sprockets.rb
|
133
|
+
- lib/css_media/version.rb
|
134
|
+
- shared.gemfile
|
135
|
+
- sprockets2.gemfile
|
136
|
+
- sprockets3.gemfile
|
137
|
+
- sprockets4.gemfile
|
138
|
+
homepage: http://github.com/marcandre/css_media
|
139
|
+
licenses:
|
140
|
+
- MIT
|
141
|
+
metadata: {}
|
142
|
+
post_install_message:
|
143
|
+
rdoc_options: []
|
144
|
+
require_paths:
|
145
|
+
- lib
|
146
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
147
|
+
requirements:
|
148
|
+
- - ">="
|
149
|
+
- !ruby/object:Gem::Version
|
150
|
+
version: '0'
|
151
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
152
|
+
requirements:
|
153
|
+
- - ">="
|
154
|
+
- !ruby/object:Gem::Version
|
155
|
+
version: '0'
|
156
|
+
requirements: []
|
157
|
+
rubyforge_project:
|
158
|
+
rubygems_version: 2.5.1
|
159
|
+
signing_key:
|
160
|
+
specification_version: 4
|
161
|
+
summary: Filter CSS to produce only @media rules
|
162
|
+
test_files: []
|