forked from jacquescrocker/sprite
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
136 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
.DS_Store | ||
spec/output | ||
pkg | ||
rspec_autotest.rb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
module Sprite | ||
class Config | ||
DEFAULT_CONFIG_PATH = 'config/sprite.yml' | ||
|
||
def self.read_config(path = nil) | ||
config_path = File.join(Sprite.root, path || DEFAULT_CONFIG_PATH) | ||
|
||
# read configuration | ||
if File.exists?(config_path) | ||
begin | ||
File.open(config_path) {|f| YAML::load(f)} || {} | ||
rescue => e | ||
puts "Error reading sprite config: #{config_path}" | ||
puts e.to_s | ||
{} | ||
end | ||
end | ||
end | ||
|
||
# chop off the trailing slash on a directory path (if it exists) | ||
def self.chop_trailing_slash(path) | ||
path = path[0...-1] if path[-1] == File::SEPARATOR | ||
path | ||
end | ||
|
||
# check if the path is set | ||
def self.path_present?(path) | ||
path.to_s.strip != "" | ||
end | ||
|
||
def initialize(settings_hash) | ||
@settings = settings_hash | ||
end | ||
|
||
# get the disk path for a location within the public folder (if set) | ||
def public_path(location, relative = false) | ||
path_parts = [] | ||
path_parts << Sprite.root unless relative | ||
path_parts << Config.chop_trailing_slash(@settings['public_path']) if Config.path_present?(@settings['public_path']) | ||
path_parts << location | ||
|
||
File.join(*path_parts) | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
module Sprite | ||
class ImageConfig | ||
def initialize(image_info, global_config_info) | ||
@image_info = image_info | ||
@global_config_info = global_config_info | ||
end | ||
|
||
def sources | ||
@image_info['sources'].to_a | ||
end | ||
|
||
def name | ||
@image_info['name'] | ||
end | ||
|
||
def format | ||
@image_info['format'] || @global_config_info["default_format"] | ||
end | ||
|
||
def spaced_by | ||
@image_info['spaced_by'] || @global_config_info["default_spacing"] || 0 | ||
end | ||
|
||
def resize_to | ||
@image_info['resize_to'] || @global_config_info['resize_to'] | ||
end | ||
|
||
def horizontal_layout? | ||
@image_info['align'].to_s == 'horizontal' | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module Sprite | ||
class ImageWriter | ||
def initialize(config) | ||
@config = config | ||
end | ||
|
||
def write(image, name, format) | ||
# set up path | ||
path = image_output_path(name, format) | ||
FileUtils.mkdir_p(File.dirname(path)) | ||
|
||
# write sprite image file to disk | ||
image.write(path) | ||
end | ||
|
||
# get the disk path for a location within the image output folder | ||
def image_output_path(name, format, relative = false) | ||
path_parts = [] | ||
path_parts << Config.chop_trailing_slash(@config['image_output_path']) if Config.path_present?(@config['image_output_path']) | ||
path_parts << "#{name}.#{format}" | ||
Config.new(@config).public_path(File.join(*path_parts), relative) | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters