Class: CssCompressor

Inherits:
Compressor show all
Defined in:
lib/compressors/cssCompressor.rb

Overview

Compresses stylesheets using the YUI compressor, creating three new files in the target directory:

stylesheets.min.css

compressed and concatenated stylesheets without embedded assets (suitable for all browsers).

stylesheets.datauir.css

compressed and concatenated stylesheets with assets embedded as data-uris (suitable for both Webkit and Geko based browser, and ie8).

stylesheets.mhtml.css

compressed and concatenated stylesheets with assets embedded as data-uris (suitable for IE6 and IE7).

Note: some of the methods used by this class are copied from Jash kenas’s excellent Jammit, an asset optimisation tool for Rails.

Constant Summary collapse

MAX_IMAGE_SIZE =

32k maximum size for embeddable images (an IE8 limitation).

32768
EMBED_DETECTOR =

CSS asset-embedding regexes for URL rewriting.

/url\(['"]?([^\s)]+\.[a-z]+)(\?\d+)?['"]?\)/
MHTML_START =

MHTML file constants.

"/*\nContent-Type: multipart/related; boundary=\"SQWEEZED_ASSET\"\n"
MHTML_SEPARATOR =
"--SQWEEZED_ASSET"
MHTML_END =
"*/\n"

Constants included from SqweezeUtils

SqweezeUtils::EMBED_MIME_TYPES

Instance Attribute Summary

Attributes inherited from Compressor

#byteweight_after, #byteweight_before, #input_file_extensions

Instance Method Summary collapse

Methods inherited from Compressor

#collect_filepaths, #compress, #concatenate_files, #filextension2regexpstr, #print_summary, #set_command, #size_check

Methods included from SqweezeUtils

#ansi_bold, #ansi_green, #ansi_nocolour, #ansi_red, #ansi_yellow, #byteweight, #compression_percentage, #encoded_contents, #find_file_in_targetdir, #mime_type, #notify, #remap_filepath, #write_file

Constructor Details

#initializeCssCompressor

Returns a new instance of CssCompressor.



26
27
28
29
30
31
32
33
34
35
# File 'lib/compressors/cssCompressor.rb', line 26

def initialize
  super('css')  
  
  @concatenate_input=true    
  @concatenated_file=nil   
  # This hash is populated while generating the data uris
  # in order to be reused later in the MHTML file 
  @assets={}
  # TODO fonts..
end

Instance Method Details

#process(input_str, cmd = nil) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/compressors/cssCompressor.rb', line 37

def process(input_str,cmd=nil)
  fout= (@concatenate_input)? "#{@cm.target_dir}/all.min.css" : @cm.get_target_path(inputpath)
  compressed_output=YUI::CssCompressor.new.compress(input_str)
  
  unless compressed_output.chomp.empty?
   write_file(compressed_output,"#{@cm.target_dir}/stylesheets.min.css") 
   
   # set the total byte-weight 
   @byteweight_after=byteweight(compressed_output) 
   embed_datauris( compressed_output )     
   embed_mhtml( compressed_output ) if @cm.get_conf(:mhtml_root) 
  end
  # this return is pointless
  fout
end