Class: CssSpriter::DirectoryProcessor

Inherits:
Object
  • Object
show all
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

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, options = {})
  @options = options
  @dir = dir
  @sprite = Sprite.new
  @tracker = MtimeTracker.new(@dir,
                              :exclude => ['sprite.css', 'fragment.css', 'sprite.png'])
end

Instance Method Details

#cleanupObject



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

#cssObject



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_fileObject



48
49
50
# File 'lib/css-spriter/directory_processor.rb', line 48

def css_file
  @dir + "/fragment.css"
end

#dir_nameObject



52
53
54
# File 'lib/css-spriter/directory_processor.rb', line 52

def dir_name
  @dir.split('/').last
end

#image_locObject



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 = @options[:source]
  base = base.gsub(source, "") if source && source != "."
  base = @options[:path_prefix] + base if @options[:path_prefix]
  base
end

#imagesObject



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_fileObject



44
45
46
# File 'lib/css-spriter/directory_processor.rb', line 44

def sprite_file
  @dir + "/sprite.png"
end

#templateObject



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_fileObject



75
76
77
# File 'lib/css-spriter/directory_processor.rb', line 75

def template_file
  @dir + "/template.css"
end

#truncate_abs_pathObject



67
68
69
70
71
72
73
# File 'lib/css-spriter/directory_processor.rb', line 67

def truncate_abs_path
  return @dir unless @options[:source]
  path_elements = @options[:source].split('/')
  path_elements.pop #we want to remove everything above the root
  to_truncate = path_elements.join("/")
  @dir.gsub(to_truncate, "")
end

#writeObject



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