class YUI::Compressor

Constants

VERSION

Attributes

options[R]

Public Instance Methods

compress(stream_or_string) click to toggle source

Compress a stream or string of code with YUI Compressor. (A stream is any object that responds to read and close like an IO.) If a block is given, you can read the compressed code from the block's argument. Otherwise, compress returns a string of compressed code.

Example: Compress CSS

compressor = YUI::CssCompressor.new
compressor.compress(<<-END_CSS)
  div.error {
    color: red;
  }
  div.warning {
    display: none;
  }
END_CSS
# => "div.error{color:red;}div.warning{display:none;}"

Example: Compress JavaScript

compressor = YUI::JavaScriptCompressor.new
compressor.compress('(function () { var foo = {}; foo["bar"] = "baz"; })()')
# => "(function(){var foo={};foo.bar=\"baz\"})();"

Example: Compress and gzip a file on disk

File.open("my.js", "r") do |source|
  Zlib::GzipWriter.open("my.js.gz", "w") do |gzip|
    compressor.compress(source) do |compressed|
      while buffer = compressed.read(4096)
        gzip.write(buffer)
      end
    end
  end
end
# File lib/yui/compressor.rb, line 85
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

Private Instance Methods

command_option_for_charset(charset) click to toggle source
# File lib/yui/compressor.rb, line 149
def command_option_for_charset(charset)
  ["--charset", charset.to_s]
end
command_option_for_line_break(line_break) click to toggle source
# File lib/yui/compressor.rb, line 153
def command_option_for_line_break(line_break)
  line_break ? ["--line-break", line_break.to_s] : []
end
command_option_for_type() click to toggle source
# File lib/yui/compressor.rb, line 145
def command_option_for_type
  ["--type", self.class.compressor_type.to_s]
end
command_options() click to toggle source
# File lib/yui/compressor.rb, line 113
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
java_opts() click to toggle source
# File lib/yui/compressor.rb, line 129
def java_opts
  options.delete(:java_opts).to_s.split(/\s+/)
end
path_to_jar_file() click to toggle source
# File lib/yui/compressor.rb, line 133
def path_to_jar_file
        options.delete(:jar_file) || File.join(File.dirname(__FILE__), *%w".. yuicompressor-2.5.0-es.jar")
end
path_to_java() click to toggle source
# File lib/yui/compressor.rb, line 125
def path_to_java
  options.delete(:java) || "java"
end
streamify(stream_or_string) { |stream_or_string| ... } click to toggle source
# File lib/yui/compressor.rb, line 137
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