Method: Sprite#output_css

Defined in:
lib/css_sprite/sprite.rb

#output_css(directory, results) ⇒ Object

output the css sprite css



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/css_sprite/sprite.rb', line 129

def output_css(directory, results)
  unless results.empty?
    dest_image_name = dest_image_name(directory)
    dest_stylesheet_path = dest_stylesheet_path(directory)
    dest_image_time = File.new(dest_image_path(directory)).mtime
    File.open(dest_stylesheet_path, 'w') do |f|
      if @config['suffix']
        @config['suffix'].each do |key, value|
          cns = class_names(results, :suffix => key)
          unless cns.empty?
            f.print cns.join(",\n")
            f.print " \{\n"
            f.print value.split("\n").collect { |text| "  " + text }.join("\n")
            f.print "\}\n"
          end
        end
      end

      f.print class_names(results).join(",\n")
      if @config['css_images_path_relative']
        f.print " \{\n  background: url('#{@css_images_path}/#{dest_image_name}?#{dest_image_time.to_i}') no-repeat;\n\}\n"
      else
        f.print " \{\n  background: url('/#{@css_images_path}/#{dest_image_name}?#{dest_image_time.to_i}') no-repeat;\n\}\n"
      end

      results.each do |result|
        f.print "#{class_name(result[:name])} \{"
        f.print " background-position: #{-result[:x]}px #{-result[:y]}px;"
        f.print " width: #{result[:width]}px;" if result[:width]
        f.print " height: #{result[:height]}px;" if result[:height]
        f.print " \}\n"
      end
    end
  end
end