beautiful-css 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,19 @@
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
+ bin
19
+ .rbenv-version
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in beautiful-css.gemspec
4
+ gemspec
5
+ gem 'rake'
6
+ gem 'sass'
7
+
8
+ group :test, :development do
9
+ gem 'minitest'
10
+ end
11
+
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Lex Childs
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,39 @@
1
+ [![Build
2
+ Status](https://secure.travis-ci.org/lex148/beautiful-css.png)](http://travis-ci.org/lex148/beautiful-css)
3
+ # Beautiful::Css
4
+
5
+ Beautiful Css is a gem to help clean up your css. It reorders your style to
6
+ remove duplication.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'beautiful-css'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install beautiful-css
21
+
22
+ ## Usage
23
+
24
+ Beautiful Css can be used from the command line:
25
+
26
+ beautifulcss style.css
27
+
28
+ Or directly from code:
29
+
30
+ styles = 'a {color:red}'
31
+ BeautifulCss::Engine.new(styles).render
32
+
33
+ ## Contributing
34
+
35
+ 1. Fork it
36
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
37
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
38
+ 4. Push to the branch (`git push origin my-new-feature`)
39
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'bundler'
4
+ require 'rake/testtask'
5
+ Bundler::GemHelper.install_tasks
6
+
7
+ Rake::TestTask.new(:test) do |test|
8
+ test.libs << 'lib' << 'spec'
9
+ test.pattern = './spec/**/*_spec.rb'
10
+ test.verbose = true
11
+ end
12
+
13
+ task :default => :test
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'beautiful-css/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "beautiful-css"
8
+ gem.version = BeautifulCss::VERSION
9
+ gem.authors = ["Lex Childs"]
10
+ gem.email = ["lexchilds@gmail.com"]
11
+ gem.description = %q{A tool to help cleanup css}
12
+ gem.summary = %q{reworks your css to make it simple and mantainable}
13
+ gem.homepage = "https://github.com/lex148/beautiful-css"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ end
@@ -0,0 +1,52 @@
1
+ require "beautiful-css/rule"
2
+ require "sass"
3
+
4
+ module BeautifulCss
5
+ class Engine
6
+
7
+ def initialize input
8
+ @input = input
9
+ end
10
+
11
+ def scss_to_css scss
12
+ Sass::Engine.new(scss,{:syntax => :scss}).render
13
+ end
14
+
15
+ def to_s
16
+ render
17
+ end
18
+
19
+ def render
20
+ return nil if @input.nil?
21
+
22
+ text = scss_to_css @input
23
+ text = text.gsub( /[\n\r\t]/m, " " )
24
+ text = text.gsub( / +/m, " " )
25
+ text = text.gsub( /\/\*[^*]*\*\//m, " " )
26
+ rules = text.split('}')
27
+
28
+ rules = rules.map{|r| Rule.new(r) }
29
+
30
+ hash = {}
31
+
32
+ ##BUILD
33
+ rules.each do |r|
34
+ r.props.each do |p|
35
+ hash[p] = [] if !( hash.has_key? p)
36
+ r.selectors.each { |s| hash[p].push s }
37
+ end
38
+ end
39
+
40
+ ##PRINT
41
+ output = ""
42
+ hash.keys.sort.each do |key|
43
+ output += "\n"
44
+ output += hash[key].join(",\n") + "\n"
45
+ output += key + "\n"
46
+ end
47
+
48
+ output.gsub( /: +/, ':' )
49
+ end
50
+
51
+ end
52
+ end
@@ -0,0 +1,23 @@
1
+ module BeautifulCss
2
+ module Exec
3
+ class Css
4
+
5
+ def initialize args
6
+ @args = args
7
+ end
8
+
9
+
10
+ def run
11
+ file = File.open(@args.first, "rb")
12
+ input = file.read
13
+ file.close
14
+ clean = BeautifulCss::Engine.new(input).render
15
+ file = File.open(@args.first, "wb")
16
+ file.puts clean
17
+ file.close
18
+ end
19
+
20
+ end
21
+
22
+ end
23
+ end
@@ -0,0 +1,28 @@
1
+
2
+ module BeautifulCss
3
+
4
+ class Rule
5
+ def initialize(str)
6
+ @body = str
7
+ end
8
+
9
+ def selectors
10
+ @body.match(/[^{]*/).to_s.split(/,/).map{|s| s.strip}.select{|s| !s.empty? }
11
+ end
12
+
13
+ def props
14
+ begin
15
+ p = @body.match(/\{([^}]*)/)[1].split(';').select{|s| !s.strip.empty? }
16
+ p.map{|s| "{ " + s.strip + " }"}
17
+ rescue
18
+ []
19
+ end
20
+ end
21
+
22
+ def body
23
+ @body
24
+ end
25
+
26
+ end
27
+
28
+ end
@@ -0,0 +1,3 @@
1
+ module BeautifulCss
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,5 @@
1
+ require "beautiful-css/version"
2
+ require "beautiful-css/exec"
3
+ require "beautiful-css/engine"
4
+
5
+
@@ -0,0 +1,17 @@
1
+ require 'helper'
2
+
3
+ describe BeautifulCss::Engine do
4
+
5
+ before do
6
+ @subject = BeautifulCss::Engine.new ''
7
+ end
8
+
9
+ it 'should be a class' do
10
+ @subject.class.name.must_match 'Css'
11
+ end
12
+
13
+ it 'should respond to render' do
14
+ assert_respond_to @subject, :render
15
+ end
16
+
17
+ end
@@ -0,0 +1,94 @@
1
+ require 'helper'
2
+
3
+ describe BeautifulCss::Engine do
4
+
5
+ it 'nothing to nothing' do
6
+ assert_renders '', ''
7
+ end
8
+
9
+
10
+
11
+ it 'vanila style' do
12
+ dirty = <<D
13
+ a
14
+ { color:red }
15
+ D
16
+ assert_renders dirty, dirty
17
+ end
18
+
19
+
20
+
21
+
22
+
23
+ it 'two styles' do
24
+ dirty = <<DIRTY
25
+ a
26
+ {
27
+ color:red;
28
+ background-color:blue;
29
+ }
30
+ DIRTY
31
+ clean = <<CLEAN
32
+ a
33
+ { background-color:blue }
34
+
35
+ a
36
+ { color:red }
37
+ CLEAN
38
+ assert_renders dirty, clean
39
+ end
40
+
41
+
42
+
43
+ it 'should be alphabetical' do
44
+ dirty = <<DIRTY
45
+ a
46
+ { color:red }
47
+
48
+ a
49
+ { background-color:blue }
50
+
51
+ DIRTY
52
+ clean = <<CLEAN
53
+ a
54
+ { background-color:blue }
55
+
56
+ a
57
+ { color:red }
58
+ CLEAN
59
+ assert_renders dirty, clean
60
+ end
61
+
62
+
63
+
64
+
65
+
66
+
67
+ it 'should be able to handle scss' do
68
+ dirty = <<DIRTY
69
+ .classy {
70
+ a
71
+ { color:red }
72
+ }
73
+ DIRTY
74
+ clean = <<CLEAN
75
+ .classy a
76
+ { color:red }
77
+ CLEAN
78
+ assert_renders dirty, clean
79
+ end
80
+
81
+
82
+
83
+
84
+
85
+
86
+ def assert_renders(dirty,clean)
87
+ assert_equal clean.strip, convert(dirty).strip
88
+ end
89
+
90
+ def convert(css)
91
+ BeautifulCss::Engine.new(css).render
92
+ end
93
+
94
+ end
data/spec/helper.rb ADDED
@@ -0,0 +1,12 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'ostruct'
4
+ require 'minitest/autorun'
5
+ require 'minitest/spec'
6
+ require 'minitest/pride'
7
+
8
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
9
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
10
+
11
+ require "beautiful-css"
12
+
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: beautiful-css
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Lex Childs
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-01 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A tool to help cleanup css
15
+ email:
16
+ - lexchilds@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE.txt
24
+ - README.md
25
+ - Rakefile
26
+ - beautiful-css.gemspec
27
+ - lib/beautiful-css.rb
28
+ - lib/beautiful-css/engine.rb
29
+ - lib/beautiful-css/exec.rb
30
+ - lib/beautiful-css/rule.rb
31
+ - lib/beautiful-css/version.rb
32
+ - spec/beautiful_css_spec.rb
33
+ - spec/convertions_spec.rb
34
+ - spec/helper.rb
35
+ homepage: https://github.com/lex148/beautiful-css
36
+ licenses: []
37
+ post_install_message:
38
+ rdoc_options: []
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubyforge_project:
55
+ rubygems_version: 1.8.24
56
+ signing_key:
57
+ specification_version: 3
58
+ summary: reworks your css to make it simple and mantainable
59
+ test_files:
60
+ - spec/beautiful_css_spec.rb
61
+ - spec/convertions_spec.rb
62
+ - spec/helper.rb