reddavis-robotnik 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.textile ADDED
@@ -0,0 +1,69 @@
1
+ h1. Robotnik
2
+
3
+ Robotnik is a very simple tool for merging and compressing css (and very shortly javascript) files.
4
+
5
+ h2. Install
6
+
7
+ <pre>
8
+ $ sudo gem install reddavis-robotnik -s http://gems.github.com/
9
+ </pre>
10
+
11
+ h2. How To Use
12
+
13
+ <pre>
14
+ <code>
15
+ Robotnik.compress do |c|
16
+ c.robotniks_path = "#{File.dirname(__FILE___)}/css"
17
+ c.css_files = {:master => Dir["/../major_*.css"], :minor => Dir["/../minor_*.css"]}
18
+ c.stylesheet_compression = true
19
+ end
20
+ </code>
21
+ </pre>
22
+
23
+ Robotniks Path - This is where Robotnik will place its files
24
+
25
+ CSS Files - This is a hash of files you want to be merged. The syntax is like so:
26
+
27
+ <pre>
28
+ <code>
29
+ c.css_files = :name_of_file_that_robotnik_will_create => %w{array of all files you would like to be merged}
30
+ </code>
31
+ </pre>
32
+
33
+ If you would like to create a "master" css file and a "admin" css file you can do this...
34
+
35
+ <pre>
36
+ <code>
37
+ c.css_files = {
38
+ :master => %w{array of all files you would like to be merged},
39
+ :admin => %w{array of admin stylesheets}
40
+ }
41
+ </code>
42
+ </pre>
43
+
44
+
45
+
46
+ h2. License
47
+
48
+ (The MIT License)
49
+
50
+ Copyright (c) 2008 Red Davis
51
+
52
+ Permission is hereby granted, free of charge, to any person obtaining
53
+ a copy of this software and associated documentation files (the
54
+ 'Software'), to deal in the Software without restriction, including
55
+ without limitation the rights to use, copy, modify, merge, publish,
56
+ distribute, sublicense, and/or sell copies of the Software, and to
57
+ permit persons to whom the Software is furnished to do so, subject to
58
+ the following conditions:
59
+
60
+ The above copyright notice and this permission notice shall be
61
+ included in all copies or substantial portions of the Software.
62
+
63
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
64
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
65
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
66
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
67
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
68
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
69
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,24 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+
4
+ begin
5
+ gem 'technicalpickles-jeweler', '>= 0.11.0'
6
+ require 'jeweler'
7
+ Jeweler::Tasks.new do |s|
8
+ s.name = "robotnik"
9
+ s.summary = %Q{Simple tool to compress/merge css and javascript files.}
10
+ s.email = "reddavis@gmail.com"
11
+ s.homepage = "http://github.com/reddavis/robotnik"
12
+ s.description = "Simple tool to compress/merge css and javascript files."
13
+ s.authors = ["Red Davis"]
14
+ s.test_files.exclude 'test/test_examples/robotnik'
15
+ end
16
+ rescue LoadError
17
+ puts "Jeweler not available. Install it with: sudo gem install jeweler --version '>= 0.11.0'"
18
+ end
19
+
20
+ Rake::TestTask.new do |t|
21
+ t.libs << 'lib'
22
+ t.pattern = 'test/**/test_*.rb'
23
+ t.verbose = false
24
+ end
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :major: 0
3
+ :minor: 1
4
+ :patch: 0
data/lib/robotnik.rb ADDED
@@ -0,0 +1,60 @@
1
+ require 'FileUtils'
2
+
3
+ class Robotnik
4
+ class << self
5
+ attr_accessor :stylesheet_compression, :stylesheets_to_merge, :robotniks_path
6
+
7
+ def compress
8
+ yield(self)
9
+ start_compressing_css
10
+ end
11
+
12
+ def master_stylesheet_path(master_stylesheet_name)
13
+ "#{robotniks_path}/#{master_stylesheet_name}"
14
+ end
15
+
16
+ private
17
+
18
+ def start_compressing_css
19
+ stylesheets_to_merge.each_pair do |key,values|
20
+ key = "#{key}.css"
21
+ merge_and_compress_css(key, values)
22
+ end
23
+ end
24
+
25
+ def merge_and_compress_css(master_name, stylesheets)
26
+ validate_stylesheets_exists(stylesheets)
27
+
28
+ File.open(master_stylesheet_path(master_name), 'w+') do |file|
29
+
30
+ stylesheets.each do |css_file|
31
+ File.open(css_file) do |f|
32
+ f.each_line do |line|
33
+ if stylesheet_compression
34
+ file.write compress_css(line)
35
+ else
36
+ file.write line
37
+ end # if stylesheet_compressed
38
+ end # f.each_line
39
+
40
+ file.write "\n" unless stylesheet_compression
41
+
42
+ end # File.open(css_file)
43
+ end # css_files.each
44
+
45
+ end # File.open(master_stylesheet_name)
46
+ end
47
+
48
+ def compress_css(line)
49
+ line.gsub!(/\n+/, '') # Remove any newlines
50
+ line.gsub!(/\t+/, '')
51
+ line.gsub!(/\s{2,}/, ' ') # Remove any gaps larger than 2 spaces
52
+ line
53
+ end
54
+
55
+ def validate_stylesheets_exists(stylesheets)
56
+ stylesheets.each {|path| raise "Can't find the file #{path}" unless File.exist?(path)}
57
+ end
58
+
59
+ end
60
+ end
@@ -0,0 +1 @@
1
+ #first {color:black;}.second {border:solid 1px black;}
@@ -0,0 +1,3 @@
1
+ #first {
2
+ color:black;
3
+ }
@@ -0,0 +1 @@
1
+ #first {color:black;}.second {border:solid 1px black;}#first {color:black;}#to_compress {color:white;}#i_like_to_compress {background-color:red;}#to_compress {color:white;}#i_like_to_compress {background-color:red;}.second {border:solid 1px black;}html {color:red;}body {color:green;}#to_compress {color:white;}#i_like_to_compress {background-color:red;}#first {color:black;}.second {border:solid 1px black;}
@@ -0,0 +1 @@
1
+ #to_compress {color:white;}#i_like_to_compress {background-color:red;}
@@ -0,0 +1,5 @@
1
+ #to_compress {
2
+ color:white;
3
+ }
4
+
5
+ #i_like_to_compress {background-color:red;}
@@ -0,0 +1 @@
1
+ .second {border:solid 1px black;}
@@ -0,0 +1,7 @@
1
+ html {
2
+ color:red;
3
+ }
4
+
5
+ body {
6
+ color:green;
7
+ }
@@ -0,0 +1,5 @@
1
+ #to_compress {
2
+ color:white;
3
+ }
4
+
5
+ #i_like_to_compress {background-color:red;}
@@ -0,0 +1,4 @@
1
+ #first {
2
+ color:black;
3
+ }
4
+ .second {border:solid 1px black;}
@@ -0,0 +1,99 @@
1
+ require 'test/unit'
2
+ require 'FileUtils'
3
+ require File.dirname(__FILE__) + '/../lib/robotnik'
4
+
5
+ class TestRobotnik < Test::Unit::TestCase
6
+
7
+ def setup
8
+ Dir["#{File.dirname(__FILE__)}/test_examples/robotnik/css/*.css"].each do |file|
9
+ FileUtils.rm file
10
+ end
11
+ end
12
+
13
+ def test_that_a_master_css_file_is_created
14
+ create_compression
15
+ assert_equal(File.exists?(@master_css_paths[0]), true )
16
+ end
17
+
18
+ def test_that_css_file_gets_compressed
19
+ create_compression(:stylesheets_to_merge => {:master_compressed =>
20
+ ["#{File.dirname(__FILE__)}/test_examples/css/to_compress.css"]
21
+ })
22
+
23
+ @master_css_paths.each do |path|
24
+ File.open(path) do |file|
25
+ while line = file.gets
26
+ assert_no_match(/\s{2,}/, line)
27
+ assert_no_match(/\n+/, line)
28
+ assert_no_match(/\t+/, line)
29
+ end
30
+ end
31
+ end
32
+ end
33
+
34
+ def test_that_css_file_does_not_get_compressed
35
+ create_compression(:stylesheet_compression => false,
36
+ :stylesheets_to_merge => {:master_un_compressed =>
37
+ ["#{File.dirname(__FILE__)}/test_examples/css/to_compress.css"]
38
+ })
39
+ @master_css_paths.each do |path|
40
+ File.open(path) do |file|
41
+ assert_match(/\s+/, file.gets) # Just checks the first line has a \n
42
+ end
43
+ end
44
+ end
45
+
46
+ def test_that_css_files_are_being_merged_correctly_with_compression
47
+ create_compression(:stylesheets_to_merge => {
48
+ :compressed => [
49
+ "#{File.dirname(__FILE__)}/test_examples/css/first.css",
50
+ "#{File.dirname(__FILE__)}/test_examples/css/second.css"
51
+ ]
52
+ })
53
+ File.open(@master_css_paths[0]) do |file|
54
+ assert_match(/#first \{color:black;\}\.second \{border:solid 1px black;\}/, file.gets)
55
+ end
56
+ end
57
+
58
+ def test_that_css_files_are_being_merged_correctly_without_compression
59
+ create_compression(:stylesheets_to_merge => {
60
+ :un_compressed => [
61
+ "#{File.dirname(__FILE__)}/test_examples/css/first.css",
62
+ "#{File.dirname(__FILE__)}/test_examples/css/second.css"
63
+ ]
64
+ }, :stylesheet_compression => false)
65
+
66
+ File.open(@master_css_paths[0]) do |file|
67
+ assert_match(/#first \{\n\tcolor:black;\n\}\n.second \{border:solid 1px black;\}\n/, file.readlines.join)
68
+ end
69
+ end
70
+
71
+ private
72
+
73
+ def create_compression(user_options={})
74
+ @options = {
75
+ :robotniks_path => "#{File.dirname(__FILE__)}/test_examples/css",
76
+ :stylesheets_to_merge => {
77
+ :master => Dir["#{File.dirname(__FILE__)}/test_examples/css/*.css"]
78
+ },
79
+ :stylesheet_compression => true
80
+ }.merge!(user_options)
81
+
82
+ @master_css_paths = master_css_paths(@options[:stylesheets_to_merge])
83
+
84
+ Robotnik.compress do |c|
85
+ c.robotniks_path = @options[:robotniks_path]
86
+ c.stylesheets_to_merge = @options[:stylesheets_to_merge]
87
+ c.stylesheet_compression = @options[:stylesheet_compression]
88
+ end
89
+ end
90
+
91
+ def master_css_paths(stylesheets_to_compress)
92
+ paths = []
93
+ stylesheets_to_compress.each_key do |filename|
94
+ paths << "#{@options[:robotniks_path]}/#{filename}.css"
95
+ end
96
+ paths
97
+ end
98
+
99
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: reddavis-robotnik
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Red Davis
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-04-22 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Simple tool to compress/merge css and javascript files.
17
+ email: reddavis@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.textile
24
+ files:
25
+ - README.textile
26
+ - Rakefile
27
+ - VERSION.yml
28
+ - lib/robotnik.rb
29
+ - test/test_examples/css/first.css
30
+ - test/test_examples/css/compressed.css
31
+ - test/test_examples/css/master.css
32
+ - test/test_examples/css/master_compressed.css
33
+ - test/test_examples/css/master_un_compressed.css
34
+ - test/test_examples/css/un_compressed.css
35
+ - test/test_examples/css/second.css
36
+ - test/test_examples/css/style.css
37
+ - test/test_examples/css/to_compress.css
38
+ - test/test_robotnik.rb
39
+ has_rdoc: true
40
+ homepage: http://github.com/reddavis/robotnik
41
+ post_install_message:
42
+ rdoc_options:
43
+ - --charset=UTF-8
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ version:
58
+ requirements: []
59
+
60
+ rubyforge_project:
61
+ rubygems_version: 1.2.0
62
+ signing_key:
63
+ specification_version: 2
64
+ summary: Simple tool to compress/merge css and javascript files.
65
+ test_files:
66
+ - test/test_robotnik.rb