rails-pixrem 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f9f0e0b6f6b318f15d49412d0e7d7e1c04cc03e6
4
+ data.tar.gz: 5870e83c3a2e6b56e6ee453b3d7d315933a63a50
5
+ SHA512:
6
+ metadata.gz: 6a75437b5fc76bb09590a0580fd8726c3b02c6e8b0c829b84f70cdb0cf37997c32d068f03dcf73898446f37481187e68898377bfe4a0e7f83fcce83e387d4b63
7
+ data.tar.gz: 36b58ec227f11e0cd4aadf800d108b0599fcca9817ef6b34283401edd6612be6ec109e75dd1bd6888c18bc25d2113ebb997de06da2b4ff0994fb37a08e17a15b
data/.fs.yml ADDED
@@ -0,0 +1 @@
1
+ ci: "https://semaphoreapp.com/vast/rails-pixrem/branches/%{branch}"
@@ -0,0 +1,18 @@
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
+ spec/dummy/log/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rails-pixrem.gemspec
4
+ gemspec
5
+
6
+ gem 'rails'
7
+ gem 'sass-rails'
8
+ gem 'rspec-rails'
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Vasily Polovnyov
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 all
13
+ 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 THE
21
+ SOFTWARE.
@@ -0,0 +1,69 @@
1
+ Rails-pixrem
2
+ ===================================================================
3
+
4
+ [![Build Status](https://semaphoreapp.com/api/v1/projects/db183d30-2136-4ba5-99e4-d0d8dac5f09d/230344/badge.png)](https://semaphoreapp.com/vast/rails-pixrem)
5
+
6
+ CSS post-processor which generates pixel fallbacks for rem units
7
+ in your Rails (Sinatra) application.
8
+
9
+
10
+ Usage
11
+ ===================================================================
12
+
13
+ ### Ruby on Rails
14
+
15
+ Add `rails-pixrem` to your `Gemfile`:
16
+
17
+ ```ruby
18
+ gem 'rails-pixrem', github: 'vast/rails-pixrem'
19
+ ```
20
+
21
+ Write your CSS (SASS, Stylus, LESS) using `rem`s:
22
+
23
+ ```scss
24
+ #header {
25
+ font-size: 2rem;
26
+ }
27
+ ```
28
+
29
+ Rails-pixrem will automatically add fallback rules with pixel values:
30
+
31
+ ```scss
32
+ #header {
33
+ font-size: 32px; // IE8, Opera Mini
34
+ font-size: 2rem; // modern browsers
35
+ }
36
+ ```
37
+
38
+
39
+ ### Sprockets
40
+
41
+ If you use a non-Rails framework with Sprockets, connect Sprockets environment
42
+ with `RailsPixrem`:
43
+
44
+
45
+ ```ruby
46
+ assets = Sprockets::Environment.new do |env|
47
+ # ...
48
+ end
49
+
50
+ require 'rails-pixrem'
51
+ RailsPixrem.install(assets)
52
+ ```
53
+
54
+ ## Configuration
55
+
56
+ You can specify root element font size (default: '16px') in px, em or percents:
57
+
58
+ ### Ruby On Rails
59
+
60
+ ```yaml
61
+ # config/pixrem.yml
62
+ root_value: 24px
63
+ ```
64
+
65
+ ### Sprockets
66
+
67
+ ```ruby
68
+ RailsPixrem.install(assets, root_value: '85%')
69
+ ```
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,17 @@
1
+ require_relative 'rails-pixrem/version'
2
+ require_relative 'rails-pixrem/processor'
3
+ require_relative 'rails-pixrem/railtie' if defined?(Rails)
4
+
5
+ module RailsPixrem
6
+ autoload :Sprockets, 'rails-pixrem/sprockets'
7
+
8
+ def self.install(assets, options = {})
9
+ Sprockets.new(processor(options)).install(assets)
10
+ end
11
+
12
+ def self.processor(options = {})
13
+ @cache ||= {}
14
+ cache_key = options.to_s
15
+ @cache[cache_key] ||= Processor.new(options)
16
+ end
17
+ end
@@ -0,0 +1,32 @@
1
+ require 'pathname'
2
+ require 'execjs'
3
+
4
+ module RailsPixrem
5
+ class Processor
6
+ def initialize(options = {})
7
+ @options = { root_value: '16px' }.merge(options)
8
+ end
9
+
10
+ def process(css)
11
+ processor.call('process', css, @options[:root_value])
12
+ end
13
+
14
+ private
15
+
16
+ def processor
17
+ @processor ||= ExecJS.compile(processor_js)
18
+ end
19
+
20
+ def processor_js
21
+ [pixrem_js, process_proxy].join(';')
22
+ end
23
+
24
+ def pixrem_js
25
+ @@pixrem_js ||= Pathname(__FILE__).join('../../../vendor/pixrem.js').read
26
+ end
27
+
28
+ def process_proxy
29
+ "var process = require('pixrem');"
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,17 @@
1
+ begin
2
+ require 'sprockets/railtie'
3
+
4
+ module RailsPixrem
5
+ class Railtie < ::Rails::Railtie
6
+ initializer :setup_pixrem, group: :all do |app|
7
+ RailsPixrem.install(app.assets, configuration(app))
8
+ end
9
+
10
+ def configuration(app)
11
+ file = app.root.join('config/pixrem.yml')
12
+ file.exist? ? YAML.load_file(file).symbolize_keys : {}
13
+ end
14
+ end
15
+ end
16
+ rescue LoadError
17
+ end
@@ -0,0 +1,45 @@
1
+ module RailsPixrem
2
+ class Sprockets
3
+ def initialize(processor)
4
+ @processor = processor
5
+ end
6
+
7
+ def process(context, css)
8
+ @processor.process(css)
9
+ end
10
+
11
+ def process_only_css(context, content)
12
+ process(context, content)
13
+ rescue ExecJS::ProgramError => e
14
+ if e.message =~ /Can't parse CSS/
15
+ content
16
+ else
17
+ raise e
18
+ end
19
+ end
20
+
21
+ def install(assets)
22
+ if ignore_syntax_error?
23
+ register(assets) { |context, css| process_only_css(context, css) }
24
+ else
25
+ register(assets) { |context, css| process(context, css) }
26
+ end
27
+ end
28
+
29
+ private
30
+
31
+ def register(assets, &block)
32
+ assets.register_postprocessor('text/css', :pixrem, &block)
33
+ end
34
+
35
+ # Return true if broken sass-rails is loaded
36
+ def ignore_syntax_error?
37
+ return false unless defined? Sass::Rails
38
+
39
+ fixed = Gem::Version.new('4.0.1')
40
+ current = Gem::Version.new(Sass::Rails::VERSION)
41
+
42
+ current < fixed
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,3 @@
1
+ module RailsPixrem
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rails-pixrem/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rails-pixrem"
8
+ spec.version = RailsPixrem::VERSION
9
+ spec.authors = ["Vasily Polovnyov"]
10
+ spec.email = ["vasily@polovnyov.ru"]
11
+ spec.summary = %q{Generate pixel fallbacks for rem units in Rails application}
12
+ spec.description = spec.summary
13
+ spec.homepage = "https://github.com/vast/rails-pixrem"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "execjs"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.5"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "rspec"
26
+ spec.add_development_dependency "rspec-its"
27
+ end
@@ -0,0 +1,2 @@
1
+ require File.expand_path('../config/application', __FILE__)
2
+ Dummy::Application.load_tasks
@@ -0,0 +1,3 @@
1
+ a {
2
+ margin: 3rem;
3
+ }
@@ -0,0 +1,3 @@
1
+ i {
2
+ padding: 1rem
3
+ }
@@ -0,0 +1,2 @@
1
+ class ApplicationController < ActionController::Base
2
+ end
@@ -0,0 +1,5 @@
1
+ class CssController < ApplicationController
2
+ def test
3
+ render text: Rails.application.assets['rails.css']
4
+ end
5
+ end
@@ -0,0 +1,2 @@
1
+ require ::File.expand_path('../config/environment', __FILE__)
2
+ run Dummy::Application
@@ -0,0 +1,14 @@
1
+ require File.expand_path('../boot', __FILE__)
2
+
3
+ require "action_controller/railtie"
4
+ require "sprockets/railtie"
5
+
6
+ if defined?(Bundler)
7
+ Bundler.require(*Rails.groups(assets: %w(development test)))
8
+ end
9
+
10
+ module Dummy
11
+ class Application < Rails::Application
12
+ config.sass.line_comments = false
13
+ end
14
+ end
@@ -0,0 +1,2 @@
1
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
2
+ require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])
@@ -0,0 +1,2 @@
1
+ require File.expand_path('../application', __FILE__)
2
+ Dummy::Application.initialize!
@@ -0,0 +1,11 @@
1
+ Dummy::Application.configure do
2
+ config.cache_classes = true
3
+ config.serve_static_assets = true
4
+ config.static_cache_control = "public, max-age=3600"
5
+ config.eager_load = false
6
+ config.consider_all_requests_local = true
7
+ config.action_controller.perform_caching = false
8
+ config.action_dispatch.show_exceptions = false
9
+ config.action_controller.allow_forgery_protection = false
10
+ config.active_support.deprecation = :stderr
11
+ end
@@ -0,0 +1 @@
1
+ Rails.application.config.assets.css_compressor = nil
@@ -0,0 +1 @@
1
+ Dummy::Application.config.secret_key_base = 'test'
@@ -0,0 +1 @@
1
+ root_value: 20px
@@ -0,0 +1,3 @@
1
+ Dummy::Application.routes.draw do
2
+ root to: 'css#test'
3
+ end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ describe RailsPixrem::Processor do
4
+ describe '#process' do
5
+ let(:sample_css) { '#foo { padding: 2rem }' }
6
+ let(:processor) { described_class.new }
7
+ subject { processor.process(sample_css) }
8
+
9
+ it { should include('padding: 32px;') }
10
+ it { should include('padding: 2rem') }
11
+
12
+ context 'when root value is passed' do
13
+ let(:processor) { described_class.new(root_value: '20px') }
14
+
15
+ it { should include('padding: 40px;') }
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ describe CssController, type: :controller do
4
+ subject { response }
5
+
6
+ before do
7
+ cache = Rails.root.join('tmp/cache')
8
+ cache.rmtree if cache.exist?
9
+
10
+ get :test
11
+ end
12
+
13
+ it { should be_success }
14
+ its(:body) { should include('margin: 3rem;') }
15
+ its(:body) { should include('margin: 60px;') }
16
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Sprockets integration' do
4
+ let(:assets) { Sprockets::Environment.new }
5
+
6
+ before do
7
+ assets.append_path(Pathname(__FILE__).dirname.join('../dummy/app/assets/stylesheets'))
8
+ end
9
+
10
+ context 'with default configuration' do
11
+ before { RailsPixrem.install(assets) }
12
+
13
+ it 'works with Sprockets' do
14
+ assets['test.css'].to_s.should == "i {\n" +
15
+ " padding: 16px;\n" +
16
+ " padding: 1rem\n" +
17
+ "}\n"
18
+ end
19
+ end
20
+
21
+ context 'when additional configuration passed' do
22
+ before { RailsPixrem.install(assets, root_value: '40px') }
23
+
24
+ it 'honors root value' do
25
+ assets['test.css'].to_s.should == "i {\n" +
26
+ " padding: 40px;\n" +
27
+ " padding: 1rem\n" +
28
+ "}\n"
29
+ end
30
+ end
31
+ end