Class: YUI::Compressor

Inherits:
Object
  • Object
show all
Defined in:
lib/yui/compressor.rb

Direct Known Subclasses

CssCompressor, JavaScriptCompressor

Defined Under Namespace

Classes: Error, OptionError, RuntimeError

Constant Summary collapse

VERSION =
"0.12.0"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Compressor

:nodoc:



24
25
26
27
28
29
30
31
32
# File 'lib/yui/compressor.rb', line 24

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

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



14
15
16
# File 'lib/yui/compressor.rb', line 14

def options
  @options
end

Class Method Details

.compressor_typeObject

:nodoc:

Raises:



20
21
22
# File 'lib/yui/compressor.rb', line 20

def self.compressor_type #:nodoc:
  raise Error, "create a CssCompressor or JavaScriptCompressor instead"
end

.default_optionsObject

:nodoc:



16
17
18
# File 'lib/yui/compressor.rb', line 16

def self.default_options #:nodoc:
  { :charset => "utf-8", :line_break => nil }
end

Instance Method Details

#commandObject

:nodoc:



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/yui/compressor.rb', line 34

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

#compress(stream_or_string) ⇒ Object

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


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# 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