jzajpt-css_compressor 0.1.0

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/README.rdoc ADDED
@@ -0,0 +1,57 @@
1
+ css_compressor
2
+ by Jiri Zajpt
3
+ http://github.com/jzajpt/css_compressor
4
+
5
+ == DESCRIPTION:
6
+
7
+ CSS Compressor is simple Ruby library for compressing CSS files.
8
+
9
+ == INSTALL:
10
+
11
+ $ gem sources -a http://gems.github.com/ (you only need to do this once)
12
+ $ gem install jzajpt-css_compressor
13
+
14
+ == SOURCE:
15
+
16
+ CSS compressor's git repository is available on GitHub, which can be browsed at:
17
+
18
+ http://github.com/jzajpt/css_compressor
19
+
20
+ or cloned from:
21
+
22
+ git://github.com/jzajpt/css_compressor.git
23
+
24
+ == USAGE:
25
+
26
+ CSS Compressor is fairly simple to use:
27
+
28
+ csscomp = CssCompressor.new(input_file) # Open & read contents of input file
29
+ csscomp.compress # Compress read contents of CSS file
30
+ puts csscomp.compressed_css # Print compressed CSS
31
+
32
+ == LICENSE:
33
+
34
+ (The MIT License)
35
+
36
+ Copyright (c) 2009 Jiri Zajpt
37
+
38
+ Permission is hereby granted, free of charge, to any person obtaining
39
+ a copy of this software and associated documentation files (the
40
+ 'Software'), to deal in the Software without restriction, including
41
+ without limitation the rights to use, copy, modify, merge, publish,
42
+ distribute, sublicense, and/or sell copies of the Software, and to
43
+ permit persons to whom the Software is furnished to do so, subject to
44
+ the following conditions:
45
+
46
+ The above copyright notice and this permission notice shall be
47
+ included in all copies or substantial portions of the Software.
48
+
49
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
50
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
51
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
52
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
53
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
54
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
55
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
56
+
57
+
@@ -0,0 +1,18 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "css_compressor"
3
+ s.version = "0.1.0"
4
+ s.date = "2009-01-27"
5
+ s.summary = "CSS compressor library."
6
+ s.email = "jzajpt@blueberry.cz"
7
+ s.author = "Jiri Zajpt"
8
+ s.homepage = "http://github.com/jzajpt/css_compressor"
9
+ s.description = "CSS Compressor is a simple Ruby library for compressing CSS files."
10
+ s.require_path = "lib"
11
+ s.has_rdoc = true
12
+ s.rdoc_options = ["--main", "README.rdoc"]
13
+ s.extra_rdoc_files = ["README.rdoc"]
14
+ s.files = [
15
+ "README.rdoc",
16
+ "css_compressor.gemspec",
17
+ "lib/css_compressor.rb"]
18
+ end
@@ -0,0 +1,62 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # CSS compressor
4
+ #
5
+ # Author:: Jiri Zajpt (mailto:jzajpt@blueberry.cz)
6
+ # Copyright:: Copyright (c) 2009 Jiri Zajpt
7
+
8
+
9
+ class CssCompressor
10
+ attr_accessor :compressed_css
11
+ attr_accessor :original_css
12
+ attr_accessor :original_size
13
+ attr_accessor :compressed_size
14
+ attr_accessor :ratio
15
+
16
+ # Opens given input file.
17
+ def initialize(input_file)
18
+ @input_file = input_file
19
+ read_original_css
20
+ end
21
+
22
+ # Reads given input CSS file and compresses it in memory.
23
+ def compress
24
+ # Remove single & multi-line comments
25
+ @compressed_css = @original_css.gsub(/\/\*.*?\*\//m, '')
26
+
27
+ tokens = @compressed_css.split(/[ \t\n]/) - ['']
28
+ @compressed_css = ''
29
+
30
+ tokens.each_with_index do |token, i|
31
+ previous_token = tokens[i-1] if i > 0
32
+
33
+ unless (previous_token && previous_token.match(/[:;}{,]\Z/)) || token.match(/\A[\{]/)
34
+ @compressed_css << ' '
35
+ end
36
+ @compressed_css << token
37
+ end
38
+
39
+ @compressed_css.strip!
40
+ @compressed_size = @compressed_css.length
41
+ @ratio = @compressed_size / @original_size.to_f
42
+ end
43
+
44
+ # Reads given input CSS, compresses it and overwrites original file with compressed CSS.
45
+ def compress!
46
+ compress
47
+
48
+ File.open(@input_file, 'w') do |file|
49
+ file.puts @compressed_css
50
+ end
51
+ end
52
+
53
+ protected
54
+
55
+ # Reads input file contents and its size.
56
+ def read_original_css
57
+ File.open(@input_file) do |file|
58
+ @original_css = file.read
59
+ end
60
+ @original_size = File.size(@input_file)
61
+ end
62
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jzajpt-css_compressor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jiri Zajpt
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-01-27 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: CSS Compressor is a simple Ruby library for compressing CSS files.
17
+ email: jzajpt@blueberry.cz
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.rdoc
24
+ files:
25
+ - README.rdoc
26
+ - css_compressor.gemspec
27
+ - lib/css_compressor.rb
28
+ has_rdoc: true
29
+ homepage: http://github.com/jzajpt/css_compressor
30
+ post_install_message:
31
+ rdoc_options:
32
+ - --main
33
+ - README.rdoc
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: "0"
41
+ version:
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ version:
48
+ requirements: []
49
+
50
+ rubyforge_project:
51
+ rubygems_version: 1.2.0
52
+ signing_key:
53
+ specification_version: 2
54
+ summary: CSS compressor library.
55
+ test_files: []
56
+