diff --git a/.rvmrc b/.rvmrc
deleted file mode 100644
index 3c66b3f..0000000
--- a/.rvmrc
+++ /dev/null
@@ -1 +0,0 @@
-rvm 1.9.3-perf@css_splitter --create
diff --git a/app/assets/images/css_splitter/.gitkeep b/app/assets/images/css_splitter/.gitkeep
deleted file mode 100644
index e69de29..0000000
diff --git a/app/assets/javascripts/css_splitter/application.js b/app/assets/javascripts/css_splitter/application.js
deleted file mode 100644
index 9097d83..0000000
--- a/app/assets/javascripts/css_splitter/application.js
+++ /dev/null
@@ -1,15 +0,0 @@
-// This is a manifest file that'll be compiled into application.js, which will include all the files
-// listed below.
-//
-// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
-// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
-//
-// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
-// the compiled file.
-//
-// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
-// GO AFTER THE REQUIRES BELOW.
-//
-//= require jquery
-//= require jquery_ujs
-//= require_tree .
diff --git a/app/assets/stylesheets/css_splitter/application.css b/app/assets/stylesheets/css_splitter/application.css
deleted file mode 100644
index 3192ec8..0000000
--- a/app/assets/stylesheets/css_splitter/application.css
+++ /dev/null
@@ -1,13 +0,0 @@
-/*
- * This is a manifest file that'll be compiled into application.css, which will include all the files
- * listed below.
- *
- * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
- * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
- *
- * You're free to add application-wide styles to this file and they'll appear at the top of the
- * compiled file, but it's generally better to create a new file per style scope.
- *
- *= require_self
- *= require_tree .
- */
diff --git a/app/controllers/css_splitter/application_controller.rb b/app/controllers/css_splitter/application_controller.rb
deleted file mode 100644
index b300733..0000000
--- a/app/controllers/css_splitter/application_controller.rb
+++ /dev/null
@@ -1,4 +0,0 @@
-module CssSplitter
- class ApplicationController < ActionController::Base
- end
-end
diff --git a/app/views/layouts/css_splitter/application.html.erb b/app/views/layouts/css_splitter/application.html.erb
deleted file mode 100644
index ba273ff..0000000
--- a/app/views/layouts/css_splitter/application.html.erb
+++ /dev/null
@@ -1,14 +0,0 @@
-
-
-
- CssSplitter
- <%= stylesheet_link_tag "css_splitter/application", :media => "all" %>
- <%= javascript_include_tag "css_splitter/application" %>
- <%= csrf_meta_tags %>
-
-
-
-<%= yield %>
-
-
-
diff --git a/config/routes.rb b/config/routes.rb
deleted file mode 100644
index 6c50fa3..0000000
--- a/config/routes.rb
+++ /dev/null
@@ -1,2 +0,0 @@
-CssSplitter::Engine.routes.draw do
-end
diff --git a/css_splitter.gemspec b/css_splitter.gemspec
index d9effe1..62998a8 100644
--- a/css_splitter.gemspec
+++ b/css_splitter.gemspec
@@ -16,5 +16,5 @@ Gem::Specification.new do |s|
s.files = Dir["{app,config,db,lib}/**/*"] + ["MIT-LICENSE", "Rakefile", "README.md"]
s.test_files = Dir["test/**/*"]
- s.add_dependency "rails", "~> 3.1"
+ s.add_dependency "rails", "> 3.1", "< 5.0"
end
diff --git a/lib/css_splitter.rb b/lib/css_splitter.rb
index 9200042..5fd7d7d 100644
--- a/lib/css_splitter.rb
+++ b/lib/css_splitter.rb
@@ -1,7 +1,30 @@
-require "css_splitter/engine"
-require "css_splitter/sprockets_engine"
-require "css_splitter/splitter"
-require "css_splitter/railtie"
+require 'tilt'
+require 'sprockets'
+require 'css_splitter/application_helper'
+require 'css_splitter/engine'
+require 'css_splitter/splitter'
+require 'css_splitter/version'
module CssSplitter
+ module Sprockets
+ class Template < ::Tilt::Template
+ def self.engine_initialized?
+ true
+ end
+
+ def initialize_engine
+ end
+
+ def prepare
+ end
+
+ def evaluate(scope, locals, &block)
+ part = scope.pathname.extname =~ /(\d+)$/ && $1 || 0
+ CssSplitter::Splitter.split data, part.to_i
+ end
+ end
+ end
end
+
+Sprockets::Engines
+Sprockets.register_engine '.split2', CssSplitter::Sprockets::Template
diff --git a/app/helpers/css_splitter/application_helper.rb b/lib/css_splitter/application_helper.rb
similarity index 76%
rename from app/helpers/css_splitter/application_helper.rb
rename to lib/css_splitter/application_helper.rb
index 644a071..2a0bef5 100644
--- a/app/helpers/css_splitter/application_helper.rb
+++ b/lib/css_splitter/application_helper.rb
@@ -4,7 +4,7 @@ def split_stylesheet_link_tag(*sources)
original_sources = sources.dup
options = sources.extract_options!
- sources.collect!{ |source| "#{source}_split2" }
+ sources.collect!{ |source| head, tail = source.split('.'); "#{head}_split2.#{Array.wrap(tail).join('.')}" }
sources << options
[
@@ -15,4 +15,4 @@ def split_stylesheet_link_tag(*sources)
].join("\n").html_safe
end
end
-end
\ No newline at end of file
+end
diff --git a/lib/css_splitter/engine.rb b/lib/css_splitter/engine.rb
index 52f992b..3e3c36f 100644
--- a/lib/css_splitter/engine.rb
+++ b/lib/css_splitter/engine.rb
@@ -1,11 +1,12 @@
module CssSplitter
- class Engine < ::Rails::Engine
- isolate_namespace CssSplitter
+ module Sprockets
+ class Engine < ::Rails::Engine
+ isolate_namespace CssSplitter
- initializer 'css_splitter.action_controller' do |app|
- ActiveSupport.on_load :action_controller do
- helper CssSplitter::ApplicationHelper
+ config.to_prepare do
+ ApplicationController.helper(CssSplitter::ApplicationHelper)
end
end
end
end
+
diff --git a/lib/css_splitter/railtie.rb b/lib/css_splitter/railtie.rb
deleted file mode 100644
index f8444f3..0000000
--- a/lib/css_splitter/railtie.rb
+++ /dev/null
@@ -1,7 +0,0 @@
-module CssSplitter
- class Railtie < Rails::Railtie
- initializer "css_splitter.initializer" do |app|
- app.assets.register_engine '.split2', CssSplitter::SprocketsEngine
- end
- end
-end
diff --git a/lib/css_splitter/splitter.rb b/lib/css_splitter/splitter.rb
index 035ee5e..5a58c5e 100644
--- a/lib/css_splitter/splitter.rb
+++ b/lib/css_splitter/splitter.rb
@@ -4,100 +4,33 @@ class Splitter
MAX_SELECTORS_DEFAULT = 4095
- def self.split(infile, outdir = File.dirname(infile), max_selectors = MAX_SELECTORS_DEFAULT)
-
- raise "infile could not be found" unless File.exists? infile
-
- rules = IO.readlines(infile, "}")
- return if rules.first.nil?
-
- charset_statement, rules[0] = rules.first.partition(/^\@charset[^;]+;/)[1,2]
- return if rules.nil?
-
- file_id = 1 # The infile remains the first file
- selectors_count = 0
- output = nil
-
- rules.each do |rule|
- rule_selectors_count = count_selectors_of_rule rule
- selectors_count += rule_selectors_count
-
- # Nothing happens until the selectors limit is reached for the first time
- if selectors_count > max_selectors
- # Close current file if there is already one
- output.close if output
-
- # Prepare next file
- file_id += 1
- filename = File.join(outdir, File.basename(infile, File.extname(infile)) + "_#{file_id.to_s}" + File.extname(infile))
- output = File.new(filename, "w")
- output.write charset_statement
-
- # Reset count with current rule count
- selectors_count = rule_selectors_count
- end
-
- output.write rule if output
+ class << self
+ def split(data, part=1, max_selectors=MAX_SELECTORS_DEFAULT)
+ rules = StringIO.new(data).readlines('}')
+ return if rules.first.nil?
+ charset_statement, rules[0] = rules.first.partition(/^\@charset[^;]+;/)[1,2]
+ output = charset_statement
+ output << rules[index_for_rule_with_max_selector(rules, max_selectors)..-1].join
+ output
end
- end
-
- def self.split_string(css_string, part = 1, max_selectors = MAX_SELECTORS_DEFAULT)
- rules = split_string_into_rules(css_string)
- extract_part rules, part, max_selectors
- end
-
- def self.split_string_into_rules(css_string)
- strip_comments(css_string).chomp.scan /[^}]*}/
- end
- def self.extract_part(rules, part = 1, max_selectors = MAX_SELECTORS_DEFAULT)
- return if rules.first.nil?
+ private
- charset_statement, rules[0] = rules.first.partition(/^\@charset[^;]+;/)[1,2]
- return if rules.nil?
-
- output = charset_statement
- selectors_count = 0
- selector_range = max_selectors * (part - 1) + 1 .. max_selectors * part
-
- rules.each do |rule|
- rule_selectors_count = count_selectors_of_rule rule
- selectors_count += rule_selectors_count
-
- if selector_range.cover? selectors_count
- output << rule
- elsif selectors_count > selector_range.end
- break
- end
+ def count_selectors_of_rule(rule)
+ rule.partition(/\{/).first.scan(/,/).count.to_i + 1
end
- output
- end
-
- def self.count_selectors(css_file)
- raise "file could not be found" unless File.exists? css_file
-
- rules = IO.readlines(css_file, '}')
- return if rules.first.nil?
-
- charset_statement, rules[0] = rules.first.partition(/^\@charset[^;]+;/)[1,2]
- return if rules.first.nil?
-
- rules.sum {|rule| count_selectors_of_rule(rule)}.tap do |result|
- puts File.basename(css_file) + " contains #{result} selectors."
+ def index_for_rule_with_max_selector(rules, max_selectors)
+ selectors_count = 0
+ rules.each_with_index do |rule, index|
+ rule_selectors_count = count_selectors_of_rule(rule)
+ selectors_count += rule_selectors_count
+ return index if selectors_count > max_selectors
+ end
+ rules.length -1
end
end
- def self.count_selectors_of_rule(rule)
- strip_comments(rule).partition(/\{/).first.scan(/,/).count.to_i + 1
- end
-
- private
-
- def self.strip_comments(s)
- s.gsub(/\/\/.*$/, "").gsub(/\/\*.*?\*\//, "")
- end
-
end
-end
\ No newline at end of file
+end
diff --git a/lib/css_splitter/sprockets_engine.rb b/lib/css_splitter/sprockets_engine.rb
deleted file mode 100644
index 0d64ef9..0000000
--- a/lib/css_splitter/sprockets_engine.rb
+++ /dev/null
@@ -1,19 +0,0 @@
-require 'tilt'
-
-module CssSplitter
-
- class SprocketsEngine < Tilt::Template
- def self.engine_initialized?
- true
- end
-
- def prepare
- end
-
- def evaluate(scope, locals, &block)
- part = scope.pathname.extname =~ /(\d+)$/ && $1 || 0
- CssSplitter::Splitter.split_string data, part.to_i
- end
- end
-
-end
\ No newline at end of file
diff --git a/lib/tasks/css_splitter_tasks.rake b/lib/tasks/css_splitter_tasks.rake
deleted file mode 100644
index a8091ef..0000000
--- a/lib/tasks/css_splitter_tasks.rake
+++ /dev/null
@@ -1,4 +0,0 @@
-# desc "Explaining what the task does"
-# task :css_splitter do
-# # Task goes here
-# end
diff --git a/script/rails b/script/rails
deleted file mode 100755
index a983071..0000000
--- a/script/rails
+++ /dev/null
@@ -1,8 +0,0 @@
-#!/usr/bin/env ruby
-# This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
-
-ENGINE_ROOT = File.expand_path('../..', __FILE__)
-ENGINE_PATH = File.expand_path('../../lib/css_splitter/engine', __FILE__)
-
-require 'rails/all'
-require 'rails/engine/commands'