Class: Inkcite::Minifier
- Inherits:
-
Object
- Object
- Inkcite::Minifier
- Defined in:
- lib/inkcite/minifier.rb
Constant Summary collapse
- IMAGE_CACHE =
Directory of optimized images
"images-optim"- MAXIMUM_LINE_LENGTH =
Maximum line length for CSS and HTML - lines exceeding this length cause problems in certain email clients.
800
Class Method Summary collapse
- .css(code, ctx) ⇒ Object
- .html(lines, ctx) ⇒ Object
- .images(email, force = false) ⇒ Object
- .js(code, ctx) ⇒ Object
- .remove_comments(html, ctx) ⇒ Object
Class Method Details
.css(code, ctx) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/inkcite/minifier.rb', line 11 def self.css code, ctx # Do nothing to the code unless minification is enabled. if minify?(ctx) # After YUI's CSS compressor started introducing problems into CSS3 # animations, I've switched to a homegrown, extremely simple compression # algorithm for CSS. Instead of messing with parameters, we're simply # eliminating whitespace. # Replace all line breaks with spaces code.gsub!(/[\n\r\f]/, ' ') # Remove whitespace at the beginning or ending of the code code.gsub!(/^\s+/, '') code.gsub!(/\s+$/, '') # Compress multiple whitespace characters into a single space. code.gsub!(/\s{2,}/, ' ') # Remove whitespace preceding or following open and close curly brackets. code.gsub!(/\s*([{};:])\s*/, "\\1") # Remove semicolons preceding close brackets. code.gsub!(';}', '}') # Certain versions of outlook have a problem with excessively long lines # so if this minified code now exceeds the maximum line limit, re-introduce # wrapping in spots where it won't break anything to do so - e.g. following # a semicolon or close bracket. if ctx.email? && code.length > MAXIMUM_LINE_LENGTH # Position at which a line break will be inserted at. break_at = 0 # Work through the code injecting line breaks until either no further # breakable characters are found or we've reached the end of the code. while break_at < code.length break_at = code.rindex(/[;}]/, break_at + MAXIMUM_LINE_LENGTH) + 1 code.insert(break_at, "\n") if break_at && break_at < code.length end end end code end |
.html(lines, ctx) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/inkcite/minifier.rb', line 60 def self.html lines, ctx if minify?(ctx) # Will hold the assembled, minified HTML as it is prepared. html = '' # Will hold the line being assembled until it reaches the maximum # allowed line length. packed_line = '' lines.each do |line| next if line.blank? line.strip! ## Compress all in-line styles. #Parser.each line, INLINE_STYLE_REGEX do |style| # style.gsub!(/: +/, ':') # style.gsub!(/; +/, ';') # style.gsub!(/;+/, ';') # style.gsub!(/;+$/, '') # "style=\"#{style}\"" #end # If the length of the packed line with the addition of this line of content would # exceed the maximum allowed line length, then push the collected lines onto the # html and start a new line. if !packed_line.blank? && packed_line.length + line.length > MAXIMUM_LINE_LENGTH html << packed_line html << NEW_LINE packed_line = '' end packed_line << ' ' unless packed_line.blank? packed_line << line end # Make sure to get any last lines assembled on the packed line. html << packed_line unless packed_line.blank? html else lines.join(NEW_LINE) end end |
.images(email, force = false) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/inkcite/minifier.rb', line 110 def self.images email, force=false images_path = email.image_dir cache_path = email.project_file(IMAGE_CACHE) # Check to see if there is an image optim configuration file. config_path = email.project_file(IMAGE_OPTIM_CONFIG_YML) config_last_modified = Util.last_modified(config_path) # If the image cache exists, we need to check to see if any images have been # removed since the last build. if File.exist?(cache_path) # Get a list of the files in the cache that do not also exist in the # project's images/ directory. removed_images = Dir.entries(cache_path) - Dir.entries(images_path) unless removed_images.blank? # Convert the images to fully-qualified paths and then remove # those files from the cache removed_images = removed_images.collect { |img| File.join(cache_path, img) } FileUtils.rm(removed_images) end end # Check to see if there are new or updated images that need to be re-optimized. # Compare existing images against both the most recently cached version and # the timestamp of the config file. updated_images = Dir.glob(File.join(images_path, '*.*')).select do |img| cached_img = File.join(cache_path, File.basename(img)) cache_last_modified = Util.last_modified(cached_img) force || config_last_modified > cache_last_modified || Util.last_modified(img) > cache_last_modified end # Return unless there is something to compress return if updated_images.blank? FileUtils.mkpath(cache_path) # Check to see if there is an image_optim.yml file in this directory that # overrides the default settings. image_optim_opts = if config_last_modified > 0 { :config_paths => [IMAGE_OPTIM_CONFIG_YML] } else { :allow_lossy => true, :gifsicle => { :level => 3 }, :jpegoptim => { :max_quality => 50 }, :jpegrecompress => { :quality => 1 }, :pngout => false, :svgo => false } end image_optim = ImageOptim.new(image_optim_opts) # Copy all of the images that need updating into the temporary directory. # Specifically joining the images_path to the image to avoid Email's # image_path which may change it's directory if optimization is enabled. updated_images.each do |img| cached_img = File.join(cache_path, File.basename(img)) FileUtils.cp(img, cached_img) image_optim.optimize_image!(cached_img) end end |
.js(code, ctx) ⇒ Object
181 182 183 |
# File 'lib/inkcite/minifier.rb', line 181 def self.js code, ctx minify?(ctx) ? js_compressor(ctx).compress(code) : code end |
.remove_comments(html, ctx) ⇒ Object
185 186 187 |
# File 'lib/inkcite/minifier.rb', line 185 def self.remove_comments html, ctx remove_comments?(ctx) ? html.gsub(HTML_COMMENT_REGEX, '') : html end |