class YUI::Compressor
:nodoc:
def self.compressor_type #:nodoc:
def self.compressor_type #:nodoc: raise Error, "create a CssCompressor or JavaScriptCompressor instead" end
def self.default_options #:nodoc:
def self.default_options #:nodoc: { :charset => "utf-8", :line_break => nil } end
def command #:nodoc:
def command #:nodoc: if RbConfig::CONFIG['host_os'] =~ /mswin|mingw/ # Shellwords is only for bourne shells, so windows shells get this # extremely remedial escaping escaped_cmd = @command.map do |word| if word =~ / / word = "\"%s\"" % word end word end else escaped_cmd = @command.map { |word| Shellwords.escape(word) } end escaped_cmd.join(" ") end
def command_option_for_charset(charset)
def command_option_for_charset(charset) ["--charset", charset.to_s] end
def command_option_for_line_break(line_break)
def command_option_for_line_break(line_break) line_break ? ["--line-break", line_break.to_s] : [] end
def command_option_for_type
def command_option_for_type ["--type", self.class.compressor_type.to_s] end
def command_options
def command_options options.inject([]) do |command_options, (name, argument)| method = begin method(:"command_option_for_#{name}") rescue NameError raise OptionError, "undefined option #{name.inspect}" end command_options.concat(method.call(argument)) end end
def compress(stream_or_string)
end
end
end
end
gzip.write(buffer)
while buffer = compressed.read(4096)
compressor.compress(source) do |compressed|
Zlib::GzipWriter.open("my.js.gz", "w") do |gzip|
File.open("my.js", "r") do |source|
==== Example: Compress and gzip a file on disk
# => "(function(){var foo={};foo.bar=\"baz\"})();"
compressor.compress('(function () { var foo = {}; foo["bar"] = "baz"; })()')
compressor = YUI::JavaScriptCompressor.new
==== Example: Compress JavaScript
# => "div.error{color:red;}div.warning{display:none;}"
END_CSS
}
display: none;
div.warning {
}
color: red;
div.error {
compressor.compress(<<-END_CSS)
compressor = YUI::CssCompressor.new
==== Example: Compress CSS
Otherwise, +compress+ returns a string of compressed code.
is given, you can read the compressed code from the block's argument.
any object that responds to +read+ and +close+ like an IO.) If a block
Compress a stream or string of code with YUI Compressor. (A stream is
def compress(stream_or_string) streamify(stream_or_string) do |stream| tempfile = Tempfile.new('yui_compress') tempfile.write stream.read tempfile.flush full_command = "%s %s" % [command, tempfile.path] begin output = `#{full_command}` rescue Exception => e # windows shells tend to blow up here when the command fails raise RuntimeError, "compression failed: %s" % e.message ensure tempfile.close! end if $?.exitstatus.zero? output else # Bourne shells tend to blow up here when the command fails, usually # because java is missing raise RuntimeError, "Command '%s' returned non-zero exit status" % full_command end end end
def initialize(options = {}) #:nodoc:
def initialize(options = {}) #:nodoc: @options = self.class.default_options.merge(options) @command = [path_to_java] @command.push(*java_opts) @command.push("-jar") @command.push(path_to_jar_file) @command.push(*(command_option_for_type + command_options)) @command.compact! end
def java_opts
def java_opts options.delete(:java_opts).to_s.split(/\s+/) end
def path_to_jar_file
def path_to_jar_file options.delete(:jar_file) || File.join(File.dirname(__FILE__), *%w".. yuicompressor-2.4.8.jar") end
def path_to_java
def path_to_java options.delete(:java) || "java" end
def streamify(stream_or_string)
def streamify(stream_or_string) if stream_or_string.respond_to?(:read) yield stream_or_string else yield StringIO.new(stream_or_string.to_s) end end