Class: CssSpriter::DirectoryProcessor
- Inherits:
-
Object
- Object
- CssSpriter::DirectoryProcessor
- Defined in:
- lib/css-spriter/directory_processor.rb
Constant Summary collapse
- DEFAULT_TEMPLATE =
".<name>_<image_name> {\n background: transparent url(<image_loc>) <offset>px 0px no-repeat;\n width:<width>;\n height:<height>;\n text-indent:-5000px;\n}\n\n"
Instance Method Summary collapse
- #cleanup ⇒ Object
- #css ⇒ Object
- #css_file ⇒ Object
- #dir_name ⇒ Object
- #image_loc ⇒ Object
- #images ⇒ Object
-
#initialize(dir, options = {}) ⇒ DirectoryProcessor
constructor
A new instance of DirectoryProcessor.
- #sprite_file ⇒ Object
- #template ⇒ Object
- #template_file ⇒ Object
- #truncate_abs_path ⇒ Object
- #write ⇒ Object
Constructor Details
#initialize(dir, options = {}) ⇒ DirectoryProcessor
Returns a new instance of DirectoryProcessor.
14 15 16 17 18 19 20 |
# File 'lib/css-spriter/directory_processor.rb', line 14 def initialize(dir, = {}) = @dir = dir @sprite = Sprite.new @tracker = MtimeTracker.new(@dir, :exclude => ['sprite.css', 'fragment.css', 'sprite.png']) end |
Instance Method Details
#cleanup ⇒ Object
38 39 40 41 42 |
# File 'lib/css-spriter/directory_processor.rb', line 38 def cleanup File.delete(sprite_file) rescue nil File.delete(css_file) rescue nil @tracker.cleanup end |
#css ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/css-spriter/directory_processor.rb', line 86 def css @sprite.locations.inject("") do |out, image| image_name, properties = image out << template.gsub("<name>", dir_name). gsub("<image_name>", image_name.to_s). gsub("<width>", properties[:width].to_s). gsub("<height>", properties[:height].to_s). gsub("<offset>", properties[:x].to_s). gsub("<image_loc>", image_loc) end end |
#css_file ⇒ Object
48 49 50 |
# File 'lib/css-spriter/directory_processor.rb', line 48 def css_file @dir + "/fragment.css" end |
#dir_name ⇒ Object
52 53 54 |
# File 'lib/css-spriter/directory_processor.rb', line 52 def dir_name @dir.split('/').last end |
#image_loc ⇒ Object
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/css-spriter/directory_processor.rb', line 56 def image_loc #TODO: Lame! dir = truncate_abs_path base = ("/" + dir + "/sprite.png").gsub(/^\/.\//, "/").gsub("//", "/") source = [:source] base = base.gsub(source, "") if source && source != "." base = [:path_prefix] + base if [:path_prefix] base end |
#images ⇒ Object
22 23 24 |
# File 'lib/css-spriter/directory_processor.rb', line 22 def images Dir.glob(@dir + "/*.png").reject{|i| i.match /sprite\.png/} end |
#sprite_file ⇒ Object
44 45 46 |
# File 'lib/css-spriter/directory_processor.rb', line 44 def sprite_file @dir + "/sprite.png" end |
#template ⇒ Object
79 80 81 82 83 84 |
# File 'lib/css-spriter/directory_processor.rb', line 79 def template if File.exists?(template_file) return File.read(template_file) end return DEFAULT_TEMPLATE end |
#template_file ⇒ Object
75 76 77 |
# File 'lib/css-spriter/directory_processor.rb', line 75 def template_file @dir + "/template.css" end |
#truncate_abs_path ⇒ Object
67 68 69 70 71 72 73 |
# File 'lib/css-spriter/directory_processor.rb', line 67 def truncate_abs_path return @dir unless [:source] path_elements = [:source].split('/') path_elements.pop #we want to remove everything above the root to_truncate = path_elements.join("/") @dir.gsub(to_truncate, "") end |
#write ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/css-spriter/directory_processor.rb', line 26 def write return unless @tracker.has_changes? images.each {|f| @sprite.append_file(f)} @sprite.write(sprite_file) File.open(css_file, 'w') do |f| f.write(css) end @tracker.update end |