morganchristiansson-css-sprites 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Ayose Cazorla
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,78 @@
1
+ = CssSprites - A Rails plugin for generate CSS sprites
2
+
3
+ This plugin helps to generate CSS sprites with the images of the
4
+ <tt>public/images/</tt> directory.
5
+
6
+
7
+ == Installation
8
+
9
+ $ script/plugin install git://github.com/setepo/css-sprites
10
+
11
+ The installer will copy <tt>blank.gif</tt> in <tt>public/images</tt>, and a
12
+ configuration file in <tt>config/</tt>. Before copy the files it checks if there is any
13
+ file with the same name, and will not override existing files.
14
+
15
+
16
+ == Usage
17
+
18
+ You don't need to change your code.
19
+
20
+ The first step is generate the index:
21
+
22
+ $ rake css_sprites:update
23
+
24
+ This will find all the images and make a bundle with them. Animated images
25
+ (like GIF or APNG with two or more frames) are always excluded.
26
+
27
+ The plugin will create one image per format (a file with JPEG images, another
28
+ file with PNGs, ...).
29
+
30
+ And that is all.
31
+
32
+ When you use <tt>image_tag</tt> in your views, the plugin will check if the
33
+ requested image is available in the index, and, if it is found, generate
34
+ something like this:
35
+
36
+ <img alt="Some text" height="16" src="/images/blank.gif?12345678" style="background: url(/images/css-sprites-image-bundle.png?12345678) -0px -466px; " width="16" />
37
+
38
+ This HTML is generated with the original <tt>image_tag</tt>, so all the Rails
39
+ features (like assets, timestamp suffix, URL prefix) are available.
40
+
41
+ If you don't want to use sprites, just remove the generated bundles.
42
+
43
+ The plugin doesn't check the timestamps, so you have to call <tt>rake
44
+ css_sprites:update</tt> again when you modify the images of the index, or you want
45
+ to add new images. The bundles are generated from scratch, so if you remove
46
+ images, they will disappear from the bundles after <tt>rake
47
+ css_sprites:update</tt>
48
+
49
+ == Example configuration
50
+
51
+ The initial configuration is this:
52
+
53
+ max-width: 100
54
+ max-height: 100
55
+ background: blank.gif
56
+ file-bundle: css-sprites-image-bundle
57
+ max-files-per-bundle: 75
58
+ files:
59
+ - **/*.png
60
+ - **/*.gif
61
+ - **/*.jpeg
62
+ - **/*.jpg
63
+
64
+ The most interesting key is <tt>files</tt>: with it, you can specify what files
65
+ do you want to add to the index. By default it includes all the most common
66
+ files, recursively. Take a look to <tt>Dir.glob</tt> documentation if you want to know
67
+ the details of the patterns.
68
+
69
+ == Dependencies
70
+
71
+ * RMagick
72
+
73
+ == TODO
74
+
75
+ * Tests
76
+
77
+
78
+ Copyright (C) 2009 Ayose Cazorla, released under the MIT license
@@ -0,0 +1,45 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the css_sprites plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.libs << 'test'
12
+ t.pattern = 'test/**/*_test.rb'
13
+ t.verbose = true
14
+ end
15
+
16
+ desc 'Generate documentation for the css_sprites plugin.'
17
+ Rake::RDocTask.new(:rdoc) do |rdoc|
18
+ rdoc.rdoc_dir = 'rdoc'
19
+ rdoc.title = 'CSSSprites'
20
+ rdoc.options << '--line-numbers' << '--inline-source'
21
+ rdoc.rdoc_files.include('README.rdoc')
22
+ rdoc.rdoc_files.include('lib/**/*.rb')
23
+ end
24
+
25
+ begin
26
+ require 'jeweler'
27
+ Jeweler::Tasks.new do |gemspec|
28
+ gemspec.name = "css-sprites"
29
+ gemspec.summary = "A Rails plugin for generate CSS sprites"
30
+ gemspec.description = "The first step is generate the index:
31
+
32
+ $ rake css_sprites:update
33
+ This will find all the images and make a bundle with them. Animated images (like GIF or APNG with two or more frames) are always excluded.
34
+
35
+ The plugin will create one image per format (a file with JPEG images, another file with PNGs, ...).
36
+
37
+ And that is all."
38
+ gemspec.email = "code@mog.se"
39
+ gemspec.homepage = "http://github.com/morganchristiansson/css-sprites"
40
+ gemspec.authors = ["Ayose (setepo)", "Morgan Chrisitansson"]
41
+ end
42
+ rescue LoadError
43
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
44
+ end
45
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.0
@@ -0,0 +1,63 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{css-sprites}
8
+ s.version = "0.0.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Ayose (setepo)", "Morgan Chrisitansson"]
12
+ s.date = %q{2009-09-17}
13
+ s.description = %q{The first step is generate the index:
14
+
15
+ $ rake css_sprites:update
16
+ This will find all the images and make a bundle with them. Animated images (like GIF or APNG with two or more frames) are always excluded.
17
+
18
+ The plugin will create one image per format (a file with JPEG images, another file with PNGs, ...).
19
+
20
+ And that is all.}
21
+ s.email = %q{code@mog.se}
22
+ s.extra_rdoc_files = [
23
+ "README.rdoc"
24
+ ]
25
+ s.files = [
26
+ "MIT-LICENSE",
27
+ "README.rdoc",
28
+ "Rakefile",
29
+ "VERSION",
30
+ "css-sprites.gemspec",
31
+ "init.rb",
32
+ "install.rb",
33
+ "lib/css_sprites.rb",
34
+ "lib/css_sprites/generator.rb",
35
+ "lib/css_sprites/view_methods.rb",
36
+ "lib/tasks/css_sprites.rake",
37
+ "lib/tasks/css_sprites.rb",
38
+ "templates/blank.gif",
39
+ "templates/css-sprites.yml",
40
+ "test/css_sprites_test.rb",
41
+ "test/test_helper.rb",
42
+ "uninstall.rb"
43
+ ]
44
+ s.homepage = %q{http://github.com/morganchristiansson/css-sprites}
45
+ s.rdoc_options = ["--charset=UTF-8"]
46
+ s.require_paths = ["lib"]
47
+ s.rubygems_version = %q{1.3.5}
48
+ s.summary = %q{A Rails plugin for generate CSS sprites}
49
+ s.test_files = [
50
+ "test/css_sprites_test.rb",
51
+ "test/test_helper.rb"
52
+ ]
53
+
54
+ if s.respond_to? :specification_version then
55
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
56
+ s.specification_version = 3
57
+
58
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
59
+ else
60
+ end
61
+ else
62
+ end
63
+ end
data/init.rb ADDED
@@ -0,0 +1,5 @@
1
+
2
+ class ActionView::Base
3
+ include CSSSprites::ViewMethods
4
+ end
5
+
@@ -0,0 +1,16 @@
1
+
2
+
3
+ TemplatesDir = File.join(File.dirname(__FILE__), "templates")
4
+
5
+ [
6
+ [ "blank.gif", "public/images" ],
7
+ [ "css-sprites.yml", "config" ]
8
+ ].each do |source, dest|
9
+
10
+ dest = File.join(dest, source)
11
+ unless File.exist?(dest)
12
+ puts "Copying #{source} to #{dest}"
13
+ FileUtils.cp(File.join(TemplatesDir, source), dest)
14
+ end
15
+
16
+ end
@@ -0,0 +1,22 @@
1
+
2
+ module CSSSprites
3
+
4
+ ConfigFileName = "css-sprites.yml"
5
+ IndexFileName = File.join(RAILS_ROOT, "tmp", "css-sprites-index.marshal")
6
+
7
+
8
+ def self.read_config
9
+ filename = File.join(RAILS_ROOT, "config", ConfigFileName)
10
+ unless File.exist?(filename)
11
+ STDERR.puts "Error in CSS Sprites: config/#{ConfigFileName} not found."
12
+ return {}
13
+ end
14
+
15
+ HashWithIndifferentAccess.new(YAML::load(File.read(filename)))
16
+ end
17
+
18
+ mattr_accessor :config
19
+ self.config = read_config
20
+
21
+ end
22
+
@@ -0,0 +1,136 @@
1
+
2
+ require 'RMagick'
3
+
4
+ module CSSSprites
5
+ module Generator
6
+ extend self
7
+
8
+ class BundleFile
9
+ cattr_accessor :file_count
10
+ self.file_count = 0
11
+
12
+ attr_accessor :file_name, :y
13
+
14
+ def initialize(mimetype)
15
+ @suffix = mimetype.gsub(/^.*\W/, '')
16
+ new
17
+ end
18
+
19
+ def new
20
+ close if @draw
21
+ @draw = Magick::Draw.new
22
+ @image_count = @width = @height = @y = 0
23
+
24
+ self.class.file_count += 1
25
+ @file_name = "#{CSSSprites.config["file-bundle"] || "css-sprites-image-bundle"}-#{self.class.file_count}.#{@suffix}"
26
+ end
27
+
28
+ def close
29
+ bundle = Magick::Image.new(@width, @height) {|image| image.background_color = CSSSprites.config["background-color"] || "#ffff" }
30
+
31
+ @draw.draw bundle
32
+ bundle.write(File.join(RAILS_ROOT, "public", "images", file_name))
33
+
34
+ @draw = nil
35
+ end
36
+
37
+ def add(image)
38
+ new if @image_count > (CSSSprites.config["max-files-per-bundle"] || 75)
39
+ @width = image.columns if image.columns > @width
40
+ @height += image.rows
41
+
42
+ @draw.composite 0, @y, 0, 0, image
43
+ @y += image.rows
44
+ @image_count += 1
45
+ end
46
+
47
+ end
48
+
49
+ def update!
50
+ config = CSSSprites.read_config
51
+
52
+ if File.exist?(CSSSprites::IndexFileName)
53
+ index = (Marshal.load(File.read(CSSSprites::IndexFileName)) rescue {})
54
+
55
+ bundles = []
56
+ index.each_value do |item|
57
+ bundles << item[:bundle]
58
+ end
59
+
60
+ bundles.uniq.each do |file|
61
+ file = File.join(RAILS_ROOT, "public", "images", file)
62
+ File.delete(file) if File.exist?(file)
63
+ end
64
+ end
65
+
66
+ # Find all the images
67
+ image_types = find_images(:max_width => (config["max-width"] || 100).to_i,
68
+ :max_height => (config["max-height"] || 100).to_i,
69
+ :files => (config["files"] || "**/*"))
70
+
71
+
72
+ index = {}
73
+
74
+ # Render the bundle
75
+
76
+ image_types.each_pair do |mimetype, images|
77
+ puts "Generating bundle for #{mimetype} (#{images.size} images)"
78
+
79
+ bundle = BundleFile.new(mimetype)
80
+ images.each do |file_image, image|
81
+ index[file_image] = {
82
+ :x => 0,
83
+ :y => bundle.y,
84
+ :width => image.columns,
85
+ :height => image.rows,
86
+ :bundle => bundle.file_name
87
+ }
88
+
89
+ bundle.add image
90
+ end
91
+
92
+ bundle.close
93
+ end
94
+
95
+ # Dump the index
96
+ File.open(CSSSprites::IndexFileName, "w") {|f| f.write Marshal.dump(index) }
97
+ end
98
+
99
+
100
+ def find_images(options)
101
+ puts "Reading images..."
102
+ max_width = options[:max_width]
103
+ max_height = options[:max_height]
104
+ root_dir = File.join(RAILS_ROOT, "public", "images")
105
+
106
+ image_types = {}
107
+
108
+ options[:files].each do |pattern|
109
+ Dir.glob(File.join(root_dir, pattern)).each do |filename|
110
+ begin
111
+ image = Magick::Image.read(filename)
112
+ rescue Magick::ImageMagickError
113
+ # Ignore this file
114
+ next
115
+ end
116
+
117
+ # We have to ignore animated images
118
+ next if image.size > 1
119
+
120
+ image = image.first
121
+
122
+ # Invalid image
123
+ next if image.nil? or image.columns > max_width or image.rows > max_height
124
+
125
+ # Register the image
126
+ filename = filename[(root_dir.size + 1).. -1]
127
+ image_types[image.mime_type] ||= []
128
+ image_types[image.mime_type] << [filename, image]
129
+ end
130
+ end
131
+
132
+ image_types
133
+ end
134
+
135
+ end
136
+ end
@@ -0,0 +1,22 @@
1
+ module CSSSprites
2
+ module ViewMethods
3
+
4
+ def self.included(base)
5
+ base.alias_method_chain :image_tag, :css_sprite
6
+ end
7
+
8
+ def image_tag_with_css_sprite(source, options = {})
9
+ @__css_sprites_index ||= (Marshal.load(File.read(CSSSprites::IndexFileName)) rescue nil) || {}
10
+ if info = @__css_sprites_index[source.to_s]
11
+ source = CSSSprites.config[:background]
12
+ options[:style] = "background: url(#{image_path info[:bundle]}) -#{info[:x]}px -#{info[:y]}px; #{options[:style]}"
13
+ options[:width] = info[:width]
14
+ options[:height] = info[:height]
15
+ end
16
+
17
+ image_tag_without_css_sprite(source, options)
18
+ end
19
+
20
+ end
21
+ end
22
+
@@ -0,0 +1,12 @@
1
+
2
+ namespace :css_sprites do
3
+
4
+ desc "Updates the CSSSprites index"
5
+ task :update do
6
+ $LOAD_PATH << File.join(File.dirname(__FILE__), "..", "lib")
7
+ require 'css_sprites'
8
+ require 'css_sprites/generator'
9
+ CSSSprites::Generator.update!
10
+ end
11
+
12
+ end
@@ -0,0 +1,3 @@
1
+ $VERBOSE = nil
2
+ Dir["#{Gem.searcher.find('css_sprites').full_gem_path}/lib/tasks/*.rake"].each { |ext| load ext }
3
+
Binary file
@@ -0,0 +1,10 @@
1
+ file-bundle: css-sprites-image-bundle
2
+ max-width: 100
3
+ max-height: 100
4
+ background: blank.gif
5
+ max-files-per-bundle: 75
6
+ files:
7
+ - **/*.png
8
+ - **/*.gif
9
+ - **/*.jpeg
10
+ - **/*.jpg
@@ -0,0 +1,8 @@
1
+ require 'test_helper'
2
+
3
+ class CssSpritesTest < ActiveSupport::TestCase
4
+ # Replace this with your real tests.
5
+ test "the truth" do
6
+ assert true
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ require 'rubygems'
2
+ require 'active_support'
3
+ require 'active_support/test_case'
@@ -0,0 +1 @@
1
+ # Uninstall hook code here
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: morganchristiansson-css-sprites
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Ayose (setepo)
8
+ - Morgan Chrisitansson
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2009-09-17 00:00:00 -07:00
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ description: "The first step is generate the index: $ rake css_sprites:update This will find all the images and make a bundle with them. Animated images (like GIF or APNG with two or more frames) are always excluded. The plugin will create one image per format (a file with JPEG images, another file with PNGs, ...). And that is all."
18
+ email: code@mog.se
19
+ executables: []
20
+
21
+ extensions: []
22
+
23
+ extra_rdoc_files:
24
+ - README.rdoc
25
+ files:
26
+ - MIT-LICENSE
27
+ - README.rdoc
28
+ - Rakefile
29
+ - VERSION
30
+ - css-sprites.gemspec
31
+ - init.rb
32
+ - install.rb
33
+ - lib/css_sprites.rb
34
+ - lib/css_sprites/generator.rb
35
+ - lib/css_sprites/view_methods.rb
36
+ - lib/tasks/css_sprites.rake
37
+ - lib/tasks/css_sprites.rb
38
+ - templates/blank.gif
39
+ - templates/css-sprites.yml
40
+ - test/css_sprites_test.rb
41
+ - test/test_helper.rb
42
+ - uninstall.rb
43
+ has_rdoc: false
44
+ homepage: http://github.com/morganchristiansson/css-sprites
45
+ licenses:
46
+ post_install_message:
47
+ rdoc_options:
48
+ - --charset=UTF-8
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ version:
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ version:
63
+ requirements: []
64
+
65
+ rubyforge_project:
66
+ rubygems_version: 1.3.5
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: A Rails plugin for generate CSS sprites
70
+ test_files:
71
+ - test/css_sprites_test.rb
72
+ - test/test_helper.rb