lib/htmlcompressor/compressor.rb in htmlcompressor-0.0.7 vs lib/htmlcompressor/compressor.rb in htmlcompressor-0.1.0
- old
+ new
@@ -1,6 +1,6 @@
-require "yui/compressor"
+require "htmlcompressor/exceptions"
module HtmlCompressor
class Compressor
JS_COMPRESSOR_YUI = "yui";
@@ -90,18 +90,29 @@
TEMP_STYLE_PATTERN = Regexp.new("%%%~COMPRESS~STYLE~(\\d+?)~%%%")
TEMP_EVENT_PATTERN = Regexp.new("%%%~COMPRESS~EVENT~(\\d+?)~%%%")
TEMP_SKIP_PATTERN = Regexp.new("%%%~COMPRESS~SKIP~(\\d+?)~%%%")
TEMP_LINE_BREAK_PATTERN = Regexp.new("%%%~COMPRESS~LT~(\\d+?)~%%%")
+ JAVASCRIPT_COMPRESSORS_OPTIONS = {
+ :closure => { :compilation_level => 'ADVANCED_OPTIMIZATIONS' },
+ :yui => { :munge => true, :preserve_semicolons => true, :optimize => true, :line_break => nil }
+ }
+
+ CSS_COMPRESSORS_OPTIONS = {
+ :yui => { :line_break => -1 }
+ }
+
DEFAULT_OPTIONS = {
:enabled => true,
# default settings
:remove_comments => true,
:remove_multi_spaces => true,
# optional settings
+ :javascript_compressor => :yui,
+ :css_compressor => :yui,
:remove_intertag_spaces => false,
:remove_quotes => false,
:compress_javascript => false,
:compress_css => false,
:simple_doctype => false,
@@ -124,18 +135,49 @@
def initialize(options = {})
@options = DEFAULT_OPTIONS.merge(options)
- # YUICompressor settings
- @yuiCssLineBreak = -1
- @yuiJsNoMunge = false
- @yuiJsPreserveAllSemiColons = false
- @yuiJsDisableOptimizations = false
+ detect_external_compressors
+ end
+ def detect_external_compressors
+ @javascript_compressors = {}
+ @css_compressors = {}
+
+ # Try Closure.
+ begin
+ require 'closure-compiler'
+ @javascript_compressors[:closure] = Closure::Compiler
+ rescue LoadError
+ end
+
+ # Try YUI
+ begin
+ require 'yui/compressor'
+ @javascript_compressors[:yui] = YUI::JavaScriptCompressor
+ @css_compressors[:yui] = YUI::CssCompressor
+ rescue LoadError
+ end
end
+ def get_javascript_compressor(compressor_name)
+
+ if @javascript_compressors.has_key? compressor_name
+ @javascript_compressors[compressor_name].new JAVASCRIPT_COMPRESSORS_OPTIONS[compressor_name]
+ end
+
+ end
+
+ def get_css_compressor(compressor_name)
+
+ if @css_compressors.has_key? compressor_name
+ @css_compressors[compressor_name].new CSS_COMPRESSORS_OPTIONS[compressor_name]
+ end
+
+ end
+
def compress html
if not @options[:enabled] or html.nil? or html.length == 0
return html
end
@@ -463,53 +505,56 @@
end
end
def compress_javascript(source)
# set default javascript compressor
- javaScriptCompressor = @options[:javascript_compressor]
+ javascript_compressor = @options[:javascript_compressor]
- if javaScriptCompressor.nil?
- javaScriptCompressor = YUI::JavaScriptCompressor.new(
- :munge => !@yuiJsNoMunge,
- :preserve_semicolons => !@yuiJsDisableOptimizations,
- :optimize => !@yuiJsDisableOptimizations,
- :line_break => @yuiJsLineBreak
- )
+ if javascript_compressor.is_a?(Symbol)
+ javascript_compressor = get_javascript_compressor(javascript_compressor)
end
+ if javascript_compressor.nil?
+ raise MissingCompressorError, "No JavaScript Compressor. Please set the :javascript_compressor option"
+ end
+
# detect CDATA wrapper
cdataWrapper = false
if source =~ CDATA_PATTERN
cdataWrapper = true
source = $1
end
- result = javaScriptCompressor.compress(source).strip
+ result = javascript_compressor.compress(source).strip
if cdataWrapper
result = "<![CDATA[" + result + "]]>"
end
result
end
def compress_css_styles(source)
# set default css compressor
- cssCompressor = @options[:css_compressor]
+ css_compressor = @options[:css_compressor]
- if cssCompressor.nil?
- cssCompressor = YUI::CssCompressor.new(:line_break => @yuiCssLineBreak)
+ if css_compressor.is_a?(Symbol)
+ css_compressor = get_css_compressor(css_compressor)
end
+ if css_compressor.nil?
+ raise MissingCompressorError, "No CSS Compressor. Please set the :css_compressor option"
+ end
+
# detect CDATA wrapper
cdataWrapper = false
if source =~ CDATA_PATTERN
cdataWrapper = true
source = $1
end
- result = cssCompressor.compress(source)
+ result = css_compressor.compress(source)
if cdataWrapper
result = "<![CDATA[" + result + "]]>"
end
@@ -780,6 +825,6 @@
end
end
end
-end
\ No newline at end of file
+end