abractated core sprite functions to a base class
This commit is contained in:
parent
c521908947
commit
fb61e54389
@ -1,7 +1,7 @@
|
|||||||
PATH
|
PATH
|
||||||
remote: .
|
remote: .
|
||||||
specs:
|
specs:
|
||||||
compass (0.11.beta.2.0dc38bc)
|
compass (0.11.beta.2.c521908)
|
||||||
chunky_png (~> 0.12.0)
|
chunky_png (~> 0.12.0)
|
||||||
sass (>= 3.1.0.alpha.218)
|
sass (>= 3.1.0.alpha.218)
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
require 'digest/md5'
|
require 'digest/md5'
|
||||||
|
require 'compass/sass_extensions/sprites/base'
|
||||||
module Compass::SassExtensions::Functions::Sprites
|
module Compass::SassExtensions::Functions::Sprites
|
||||||
ZERO = Sass::Script::Number::new(0)
|
ZERO = Sass::Script::Number::new(0)
|
||||||
|
|
||||||
@ -12,45 +12,7 @@ module Compass::SassExtensions::Functions::Sprites
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class SpriteMap < Sass::Script::Literal
|
class SpriteMap < Compass::SassExtensions::Sprites::Base
|
||||||
|
|
||||||
# Changing this string will invalidate all previously generated sprite images.
|
|
||||||
# We should do so only when the packing algorithm changes
|
|
||||||
SPRITE_VERSION = "1"
|
|
||||||
|
|
||||||
attr_accessor :image_names, :path, :name, :options
|
|
||||||
attr_accessor :images, :width, :height
|
|
||||||
|
|
||||||
def self.from_uri(uri, context, kwargs)
|
|
||||||
path, name = Compass::Sprites.path_and_name(uri.value)
|
|
||||||
sprites = Compass::Sprites.discover_sprites(uri.value).map do |sprite|
|
|
||||||
sprite.gsub(Compass.configuration.images_path+"/", "")
|
|
||||||
end
|
|
||||||
new(sprites, path, name, context, kwargs)
|
|
||||||
end
|
|
||||||
|
|
||||||
def initialize(image_names, path, name, context, options)
|
|
||||||
@image_names, @path, @name, @options = image_names, path, name, options
|
|
||||||
@images = nil
|
|
||||||
@width = nil
|
|
||||||
@height = nil
|
|
||||||
@evaluation_context = context
|
|
||||||
validate!
|
|
||||||
compute_image_metadata!
|
|
||||||
end
|
|
||||||
|
|
||||||
def sprite_names
|
|
||||||
image_names.map{|f| Compass::Sprites.sprite_name(f) }
|
|
||||||
end
|
|
||||||
|
|
||||||
def validate!
|
|
||||||
for sprite_name in sprite_names
|
|
||||||
unless sprite_name =~ /\A#{Sass::SCSS::RX::IDENT}\Z/
|
|
||||||
raise Sass::SyntaxError, "#{sprite_name} must be a legal css identifier"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Calculates the overal image dimensions
|
# Calculates the overal image dimensions
|
||||||
# collects image sizes and input parameters for each sprite
|
# collects image sizes and input parameters for each sprite
|
||||||
def compute_image_metadata!
|
def compute_image_metadata!
|
||||||
@ -120,19 +82,6 @@ module Compass::SassExtensions::Functions::Sprites
|
|||||||
[width, height]
|
[width, height]
|
||||||
end
|
end
|
||||||
|
|
||||||
# Generate a sprite image if necessary
|
|
||||||
def generate
|
|
||||||
if generation_required?
|
|
||||||
sprite_data = construct_sprite
|
|
||||||
save!(sprite_data)
|
|
||||||
Compass.configuration.run_callback(:sprite_generated, sprite_data)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def generation_required?
|
|
||||||
!File.exists?(filename) || outdated?
|
|
||||||
end
|
|
||||||
|
|
||||||
def require_png_library!
|
def require_png_library!
|
||||||
begin
|
begin
|
||||||
require 'oily_png'
|
require 'oily_png'
|
||||||
@ -160,11 +109,6 @@ module Compass::SassExtensions::Functions::Sprites
|
|||||||
output_png
|
output_png
|
||||||
end
|
end
|
||||||
|
|
||||||
# The on-the-disk filename of the sprite
|
|
||||||
def filename
|
|
||||||
File.join(Compass.configuration.images_path, "#{path}-#{uniqueness_hash}.png")
|
|
||||||
end
|
|
||||||
|
|
||||||
def uniqueness_hash
|
def uniqueness_hash
|
||||||
@uniqueness_hash ||= begin
|
@uniqueness_hash ||= begin
|
||||||
sum = Digest::MD5.new
|
sum = Digest::MD5.new
|
||||||
@ -180,53 +124,6 @@ module Compass::SassExtensions::Functions::Sprites
|
|||||||
@uniqueness_hash
|
@uniqueness_hash
|
||||||
end
|
end
|
||||||
|
|
||||||
# saves the sprite for later retrieval
|
|
||||||
def save!(output_png)
|
|
||||||
saved = output_png.save filename
|
|
||||||
Compass.configuration.run_callback(:sprite_saved, filename)
|
|
||||||
saved
|
|
||||||
end
|
|
||||||
|
|
||||||
# All the full-path filenames involved in this sprite
|
|
||||||
def image_filenames
|
|
||||||
image_names.map do |image_name|
|
|
||||||
File.join(Compass.configuration.images_path, image_name)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Checks whether this sprite is outdated
|
|
||||||
def outdated?
|
|
||||||
last_update = self.mtime
|
|
||||||
image_filenames.each do |image|
|
|
||||||
return true if File.mtime(image) > last_update
|
|
||||||
end
|
|
||||||
false
|
|
||||||
end
|
|
||||||
|
|
||||||
def mtime
|
|
||||||
File.mtime(filename)
|
|
||||||
end
|
|
||||||
|
|
||||||
def inspect
|
|
||||||
to_s
|
|
||||||
end
|
|
||||||
|
|
||||||
def to_s(options = self.options)
|
|
||||||
sprite_url(self).value
|
|
||||||
end
|
|
||||||
|
|
||||||
def respond_to?(meth)
|
|
||||||
super || @evaluation_context.respond_to?(meth)
|
|
||||||
end
|
|
||||||
|
|
||||||
def method_missing(meth, *args, &block)
|
|
||||||
if @evaluation_context.respond_to?(meth)
|
|
||||||
@evaluation_context.send(meth, *args, &block)
|
|
||||||
else
|
|
||||||
super
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Creates a SpriteMap object. A sprite map, when used in a property is the same
|
# Creates a SpriteMap object. A sprite map, when used in a property is the same
|
||||||
|
117
lib/compass/sass_extensions/sprites/base.rb
Normal file
117
lib/compass/sass_extensions/sprites/base.rb
Normal file
@ -0,0 +1,117 @@
|
|||||||
|
module Compass
|
||||||
|
module SassExtensions
|
||||||
|
module Sprites
|
||||||
|
class Base < Sass::Script::Literal
|
||||||
|
# Changing this string will invalidate all previously generated sprite images.
|
||||||
|
# We should do so only when the packing algorithm changes
|
||||||
|
SPRITE_VERSION = "1"
|
||||||
|
|
||||||
|
attr_accessor :image_names, :path, :name, :options
|
||||||
|
attr_accessor :images, :width, :height
|
||||||
|
|
||||||
|
def self.from_uri(uri, context, kwargs)
|
||||||
|
path, name = Compass::Sprites.path_and_name(uri.value)
|
||||||
|
sprites = Compass::Sprites.discover_sprites(uri.value).map do |sprite|
|
||||||
|
sprite.gsub(Compass.configuration.images_path+"/", "")
|
||||||
|
end
|
||||||
|
new(sprites, path, name, context, kwargs)
|
||||||
|
end
|
||||||
|
|
||||||
|
def initialize(image_names, path, name, context, options)
|
||||||
|
@image_names, @path, @name, @options = image_names, path, name, options
|
||||||
|
@images = nil
|
||||||
|
@width = nil
|
||||||
|
@height = nil
|
||||||
|
@evaluation_context = context
|
||||||
|
validate!
|
||||||
|
compute_image_metadata!
|
||||||
|
end
|
||||||
|
|
||||||
|
def sprite_names
|
||||||
|
image_names.map{|f| Compass::Sprites.sprite_name(f) }
|
||||||
|
end
|
||||||
|
|
||||||
|
def validate!
|
||||||
|
for sprite_name in sprite_names
|
||||||
|
unless sprite_name =~ /\A#{Sass::SCSS::RX::IDENT}\Z/
|
||||||
|
raise Sass::SyntaxError, "#{sprite_name} must be a legal css identifier"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# The on-the-disk filename of the sprite
|
||||||
|
def filename
|
||||||
|
File.join(Compass.configuration.images_path, "#{path}-#{uniqueness_hash}.png")
|
||||||
|
end
|
||||||
|
|
||||||
|
# Calculates the overal image dimensions
|
||||||
|
# collects image sizes and input parameters for each sprite
|
||||||
|
def compute_image_metadata!
|
||||||
|
end
|
||||||
|
|
||||||
|
# Generate a sprite image if necessary
|
||||||
|
def generate
|
||||||
|
if generation_required?
|
||||||
|
sprite_data = construct_sprite
|
||||||
|
save!(sprite_data)
|
||||||
|
Compass.configuration.run_callback(:sprite_generated, sprite_data)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def generation_required?
|
||||||
|
!File.exists?(filename) || outdated?
|
||||||
|
end
|
||||||
|
|
||||||
|
def uniqueness_hash
|
||||||
|
@uniqueness_hash ||= begin
|
||||||
|
sum = Digest::MD5.new
|
||||||
|
sum << SPRITE_VERSION
|
||||||
|
sum << path
|
||||||
|
images.each do |image|
|
||||||
|
[:relative_file, :height, :width, :repeat, :spacing, :position, :digest].each do |attr|
|
||||||
|
sum << image[attr].to_s
|
||||||
|
end
|
||||||
|
end
|
||||||
|
sum.hexdigest[0...10]
|
||||||
|
end
|
||||||
|
@uniqueness_hash
|
||||||
|
end
|
||||||
|
|
||||||
|
# saves the sprite for later retrieval
|
||||||
|
def save!(output_png)
|
||||||
|
saved = output_png.save filename
|
||||||
|
Compass.configuration.run_callback(:sprite_saved, filename)
|
||||||
|
saved
|
||||||
|
end
|
||||||
|
|
||||||
|
# All the full-path filenames involved in this sprite
|
||||||
|
def image_filenames
|
||||||
|
image_names.map do |image_name|
|
||||||
|
File.join(Compass.configuration.images_path, image_name)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def inspect
|
||||||
|
to_s
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_s(options = self.options)
|
||||||
|
sprite_url(self).value
|
||||||
|
end
|
||||||
|
|
||||||
|
def respond_to?(meth)
|
||||||
|
super || @evaluation_context.respond_to?(meth)
|
||||||
|
end
|
||||||
|
|
||||||
|
def method_missing(meth, *args, &block)
|
||||||
|
if @evaluation_context.respond_to?(meth)
|
||||||
|
@evaluation_context.send(meth, *args, &block)
|
||||||
|
else
|
||||||
|
super
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in New Issue
Block a user