css_sprites 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 420263174925e0ebdcc84dd97df633d5b7a184d1
4
+ data.tar.gz: 1fdd26c70c6fb297a03e412bb2874c4e0741a986
5
+ SHA512:
6
+ metadata.gz: c8a7bb7a49fffea8ae5665ae2b6f43d0aa85f0fcc55ddcbdd06db8aac36038031e83bf8aaabdaad3f667fa42dffa806f5bde50c1141cd8bdfc30e89765328c1e
7
+ data.tar.gz: c3c33e11b0334b38702339daf50f312e63f4fcec54eaa11dac013c3e66c14f5ed2b46dc09d902c0592add6de6cae5ffe8496a6732e612c42d8421b08d24d283e
data/.gitignore ADDED
@@ -0,0 +1,37 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.gem
11
+ .idea
12
+
13
+ .DS_Store
14
+ .AppleDouble
15
+ .LSOverride
16
+
17
+ # Icon must end with two \r
18
+ Icon
19
+
20
+
21
+ # Thumbnails
22
+ ._*
23
+
24
+ # Files that might appear in the root of a volume
25
+ .DocumentRevisions-V100
26
+ .fseventsd
27
+ .Spotlight-V100
28
+ .TemporaryItems
29
+ .Trashes
30
+ .VolumeIcon.icns
31
+
32
+ # Directories potentially created on remote AFP share
33
+ .AppleDB
34
+ .AppleDesktop
35
+ Network Trash Folder
36
+ Temporary Items
37
+ .apdisk
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.0
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sprites.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Angelika Tyborska
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,85 @@
1
+ # Sprites
2
+
3
+ Sprites finds all png files in the input directory, merges them together into a single PNG file and generates a CSS stylesheet for the output image.
4
+
5
+ ## Prerequisites
6
+
7
+ **ImageMagick** Version 6.4.9 or later. You can get ImageMagick from [www.imagemagick.org](http://www.imagemagick.org).
8
+
9
+ Or, if you're on Mac OS X and using Homebrew, you can install it by typing:
10
+
11
+ ```bash
12
+ $ brew install imagemagick
13
+ ```
14
+
15
+ And if you're using Ubuntu:
16
+
17
+ ```bash
18
+ $ apt-get imagemagick
19
+ ```
20
+
21
+ ## Installation
22
+
23
+ Add this line to your application's Gemfile:
24
+
25
+ ```ruby
26
+ gem 'css_sprites'
27
+ ```
28
+
29
+ And then execute:
30
+ ```bash
31
+ $ bundle install
32
+ ```
33
+
34
+ Or install it yourself:
35
+ ```bash
36
+ $ gem install css_sprites
37
+ ```
38
+
39
+ ## Usage
40
+
41
+ In your Ruby app:
42
+
43
+ ```ruby
44
+ require 'css_sprites'
45
+ CSSSprites.generate_sprite(path_to_images, options_hash)
46
+ ```
47
+
48
+ Or from the command line:
49
+
50
+ ```bash
51
+ $ css_sprites path/to/images
52
+ ```
53
+
54
+ ### Options
55
+
56
+ Option | Type | Default | Description
57
+ ------ | ---- | ------- | -----------
58
+ :stacking | :horizontal or :vertical | :horizontal | How to order images in the sprite
59
+ :output | string | input directory's parent's path | Where to save the generated files
60
+ :name | string | input directory's name | A name for the generated files
61
+
62
+
63
+ ### Examples with options
64
+ In your Ruby app:
65
+
66
+ ```ruby
67
+ options = {
68
+ stacking: :vertical,
69
+ name: 'so_awesome',
70
+ output: 'my_awesome_icons_output'
71
+ }
72
+ CSSSprites.generate_sprite('my_awesome_icons', options)
73
+ ```
74
+
75
+ From the command line:
76
+
77
+ ```bash
78
+ $ css_sprites --stacking vertical --name so_awesome --output my_awesome_icons_output my_awesome_icons
79
+ ```
80
+
81
+ For more details:
82
+
83
+ ```bash
84
+ $ css_sprites --help
85
+ ```
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/bin/css_sprites ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'css_sprites'
4
+ require 'css_sprites/cli'
5
+
6
+ cli = CSSSprites::CLI.new(ARGV)
7
+ cli.parse!
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'css_sprites/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "css_sprites"
8
+ spec.version = CSSSprites::VERSION
9
+ spec.authors = ["Angelika Tyborska"]
10
+ spec.email = ["atyborska93@gmail.com"]
11
+ spec.summary = %q{Merges many PNG images into a single image and generates a CSS stylesheet}
12
+ spec.homepage = "https://github.com/angelikatyborska/css_sprites"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
+ spec.bindir = "bin"
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.8"
21
+ spec.add_development_dependency "rake", "~> 10.0"
22
+ spec.add_development_dependency "rspec", "~> 3.4.0"
23
+ spec.add_runtime_dependency "rmagick", "~> 2.15.4"
24
+ end
@@ -0,0 +1,78 @@
1
+ require 'optparse'
2
+
3
+ class CSSSprites::CLI
4
+ def initialize(args)
5
+ @args = args
6
+ @options = {}
7
+ end
8
+
9
+ def parse!
10
+ opts = OptionParser.new(&method(:set_options))
11
+ opts.parse!(@args)
12
+ path = ARGV[0]
13
+
14
+ if path
15
+ begin
16
+ output = CSSSprites.generate_sprite(path, @options)
17
+ $stdout.puts "Files saved to #{ output }"
18
+ rescue ArgumentError => error
19
+ $stdout.puts error.message
20
+ end
21
+ else
22
+ $stderr.puts opts.banner
23
+ end
24
+ end
25
+
26
+ private
27
+
28
+ def set_options(opts)
29
+ opts.banner = <<END
30
+ Usage: sprites [options] input_dir
31
+
32
+ Description:
33
+ Merges png files from the input directory into one png file and generates an adequate stylesheet.
34
+ END
35
+
36
+ opts.on(
37
+ '-s',
38
+ '--stacking TYPE',
39
+ 'Image stacking orientation. Can be horizontal (default) or vertical.'
40
+ ) do |stacking|
41
+ @options[:stacking] = stacking.to_sym || :horizontal
42
+ end
43
+
44
+ opts.on(
45
+ '-n',
46
+ '--name FILENAME',
47
+ 'The name for the generated css and png files. Uses input directory name if no name given.'
48
+ ) do |name|
49
+ @options[:name] = name
50
+ end
51
+
52
+ opts.on(
53
+ '-o',
54
+ '--output PATH',
55
+ 'The path to the directory to which generated files should be saved. Uses input directory\'s path if no path given.'
56
+ ) do |output|
57
+ @options[:output] = output
58
+ end
59
+
60
+ opts.on_tail(
61
+ '-h',
62
+ '--help',
63
+ 'Show this message'
64
+ ) do
65
+ puts opts
66
+ exit
67
+ end
68
+
69
+ opts.on_tail(
70
+ '-v',
71
+ '--version',
72
+ 'Show version'
73
+ ) do
74
+ puts "Sprites #{ CSSSprites::VERSION }"
75
+ exit
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,88 @@
1
+ class CSSSprites::ImageGrid
2
+ def initialize(images, options = {})
3
+ @images = images
4
+ @name = options.fetch(:name, 'untitled')
5
+ @stacking = options.fetch(:stacking, :horizontal)
6
+
7
+ arrange_images!
8
+ end
9
+
10
+ def total_width
11
+ row_widths = @grid.map.with_index { |row, index| image_x_offset(index, row.size) }
12
+ row_widths.max
13
+ end
14
+
15
+ def total_height
16
+ image_y_offset(total_rows)
17
+ end
18
+
19
+ def name
20
+ @name
21
+ end
22
+
23
+ def image_name(row, column)
24
+ File.basename(@grid[row][column][:filename], '.*')
25
+ end
26
+
27
+ def image_x_offset(row, column)
28
+ return 0 if column == 0 || @images.empty?
29
+
30
+ @grid[row][0..(column - 1)].inject(0) do |sum, image|
31
+ sum += image[:image].columns
32
+ end
33
+ end
34
+
35
+ def image_y_offset(row, column = 0)
36
+ return 0 if row == 0 || @images.empty?
37
+
38
+ row_heights = @grid[0..(row - 1)].map do |row|
39
+ row.map { |image| image[:image].rows }.max
40
+ end
41
+
42
+ row_heights.inject(0, :+)
43
+ end
44
+
45
+ def to_image
46
+ return nil if @images.empty?
47
+
48
+ image_grid = Magick::Image.new(total_width, total_height) { self.background_color = 'transparent' }
49
+
50
+ each_with_index do |image, n, k|
51
+ image_grid = image_grid.composite(image, image_x_offset(n, k), image_y_offset(n), Magick::OverCompositeOp)
52
+ end
53
+
54
+ image_grid
55
+ end
56
+
57
+ def each_with_index(&block)
58
+ @grid.each.with_index do |row, n|
59
+ row.each.with_index do |image, k|
60
+ block.call(image[:image], n, k)
61
+ end
62
+ end
63
+ end
64
+
65
+ private
66
+
67
+ def arrange_images!
68
+ @grid = [[]]
69
+
70
+ if @stacking == :horizontal
71
+ @images.each do |image|
72
+ @grid[0] << image
73
+ end
74
+ else
75
+ @images.each_with_index do |image, index|
76
+ @grid[index] = [image]
77
+ end
78
+ end
79
+
80
+ def total_rows
81
+ @grid.size
82
+ end
83
+
84
+ def total_columns
85
+ @grid.map { |row| row.size }.max
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,48 @@
1
+ class CSSSprites::Sprite
2
+ def initialize(path, options = {})
3
+ validate_path(path)
4
+
5
+ options[:name] ||= File.basename(path)
6
+
7
+ @name = options[:name]
8
+ @images = load_images(path)
9
+
10
+ @image_grid = CSSSprites::ImageGrid.new(@images, options)
11
+ @stylesheet = CSSSprites::SpriteStylesheet.new(@image_grid)
12
+ end
13
+
14
+ def name
15
+ @name
16
+ end
17
+
18
+ def image
19
+ @image_grid.to_image
20
+ end
21
+
22
+ def stylesheet
23
+ @stylesheet.to_s
24
+ end
25
+
26
+ private
27
+
28
+ def validate_path(path)
29
+ fail ArgumentError, "Directory #{ path } does not exist" unless Dir.exists?(path)
30
+ fail ArgumentError, "You do not have reading permissions for directory #{ path }" unless File.readable?(path)
31
+ end
32
+
33
+ def load_images(path)
34
+ image_filenames = Dir.entries(path).select { |filename| filename =~ /.*\.png/ }
35
+
36
+ fail ArgumentError, "Directory #{ path } contains no png files" if image_filenames.empty?
37
+
38
+ image_filenames.each_with_object([]) do |filename, images|
39
+ filepath = "#{ path }/#{ filename }"
40
+ fail ArgumentError, "You do not have reading permissions for file #{ filename }" unless File.readable?(filepath)
41
+
42
+ images << {
43
+ filename: "#{filename}",
44
+ image: Magick::ImageList.new(filepath)
45
+ }
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,94 @@
1
+ class CSSSprites::SpriteStylesheet
2
+ INDENT = ' '
3
+ INVALID_CSS_NAME_CHARS = /\W/
4
+
5
+ def initialize(image_grid)
6
+ @image_grid = image_grid
7
+ end
8
+
9
+ def to_s
10
+ style = ''
11
+
12
+ rule_blocks.each do |block|
13
+ style << "\n" unless rule_blocks[0] == block
14
+ style << "#{ block[:selector] } {\n"
15
+ style << "#{ declarations_to_s(block[:declarations]) }"
16
+ style << "}\n"
17
+ end
18
+
19
+ style
20
+ end
21
+
22
+ private
23
+
24
+ def rule_blocks
25
+ rules = [
26
+ {
27
+ selector: ".#{ base_class }",
28
+ declarations: base_declarations
29
+ }
30
+ ]
31
+
32
+ @image_grid.each_with_index do |image, row, column|
33
+ rules << {
34
+ selector: ".#{ base_class }.#{ image_class(row, column) }",
35
+ declarations: declarations_for_image(image, row, column)
36
+ }
37
+ end
38
+
39
+ rules
40
+ end
41
+
42
+ def declarations_to_s(declarations)
43
+ declarations_to_s = ''
44
+
45
+ declarations.each do |declaration|
46
+ declarations_to_s << "#{ INDENT }#{ declaration[:property] }: #{ declaration[:value] };\n"
47
+ end
48
+
49
+ declarations_to_s
50
+ end
51
+
52
+ def base_class
53
+ @image_grid.name.gsub(INVALID_CSS_NAME_CHARS, '-')
54
+ end
55
+
56
+ def image_class(row, column)
57
+ "#{ base_class }-#{ @image_grid.image_name(row, column).gsub(INVALID_CSS_NAME_CHARS, '-') }"
58
+ end
59
+
60
+ def base_declarations
61
+ [
62
+ {
63
+ property: 'display',
64
+ value: 'inline-block'
65
+ },
66
+ {
67
+ property: 'background',
68
+ value: "url('#{ base_class }.png') no-repeat"
69
+ }
70
+ ]
71
+ end
72
+
73
+ def declarations_for_image(image, row, column)
74
+ [
75
+ {
76
+ property: 'width',
77
+ value: "#{ with_px(image.columns) }"
78
+ },
79
+ {
80
+ property: 'height',
81
+ value: "#{ with_px(image.rows) }"
82
+ },
83
+ {
84
+ property: 'background-position',
85
+ value: "#{ with_px(-1 * @image_grid.image_x_offset(row, column)) } #{ with_px(-1 * @image_grid.image_y_offset(row)) }"
86
+ },
87
+ ]
88
+ end
89
+
90
+ def with_px(value)
91
+ unit = 'px' unless value == 0
92
+ "#{ value }#{ unit }"
93
+ end
94
+ end
@@ -0,0 +1,3 @@
1
+ module CSSSprites
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,25 @@
1
+ require 'css_sprites/version'
2
+ require 'css_sprites/image_grid'
3
+ require 'css_sprites/sprite_stylesheet'
4
+ require 'css_sprites/sprite'
5
+ require 'rmagick'
6
+
7
+ module CSSSprites
8
+ # Generates a sprite from all PNG files in the directory specified by +path+.
9
+ # Saves a PNG file and a CSS file in the specified directory's parent directory.
10
+ # Returns the path to the directory to which those files were saved to.
11
+ def self.generate_sprite(path, options = {})
12
+ output_path = options[:output] || File.dirname(path)
13
+
14
+ fail ArgumentError, "Directory #{ output_path } does not exist" unless Dir.exists?(output_path)
15
+ fail ArgumentError, "You have no writing permissions for directory #{ output_path }" unless File.writable?(output_path)
16
+
17
+ sprite = Sprite.new(path, options)
18
+
19
+ output_file = "#{ output_path }/#{ sprite.name }"
20
+ sprite.image.write("#{ output_file }.png")
21
+ File.write("#{ output_file }.css", sprite.stylesheet)
22
+
23
+ File.realpath(output_path)
24
+ end
25
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: css_sprites
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Angelika Tyborska
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-12-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.8'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 3.4.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 3.4.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: rmagick
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 2.15.4
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 2.15.4
69
+ description:
70
+ email:
71
+ - atyborska93@gmail.com
72
+ executables:
73
+ - css_sprites
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".travis.yml"
80
+ - CODE_OF_CONDUCT.md
81
+ - Gemfile
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - bin/css_sprites
86
+ - css_sprites.gemspec
87
+ - lib/css_sprites.rb
88
+ - lib/css_sprites/cli.rb
89
+ - lib/css_sprites/image_grid.rb
90
+ - lib/css_sprites/sprite.rb
91
+ - lib/css_sprites/sprite_stylesheet.rb
92
+ - lib/css_sprites/version.rb
93
+ homepage: https://github.com/angelikatyborska/css_sprites
94
+ licenses:
95
+ - MIT
96
+ metadata: {}
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ requirements: []
112
+ rubyforge_project:
113
+ rubygems_version: 2.4.6
114
+ signing_key:
115
+ specification_version: 4
116
+ summary: Merges many PNG images into a single image and generates a CSS stylesheet
117
+ test_files: []