Module: YUICompressor
- Defined in:
- lib/yuicompressor.rb,
lib/yuicompressor/jruby.rb,
lib/yuicompressor/shell.rb
Defined Under Namespace
Classes: ErrorReporter
Constant Summary collapse
- JAR_FILE =
The path to the YUI Compressor jar file.
File.('../yuicompressor-2.4.4.jar', __FILE__)
Class Method Summary collapse
-
.command_arguments(options = {}) ⇒ Object
Returns an array of flags that should be passed to the jar file on the command line for the given set of
options
. -
.compress(stream_or_string, options = {}) ⇒ Object
Compresses the given
stream_or_string
of code using the givenoptions
. -
.compress_css(stream_or_string, options = {}, &block) ⇒ Object
Compress the given CSS
stream_or_string
using the givenoptions
. -
.compress_js(stream_or_string, options = {}, &block) ⇒ Object
Compress the given JavaScript
stream_or_string
using the givenoptions
. -
.default_css_options ⇒ Object
:nodoc:.
-
.default_js_options ⇒ Object
:nodoc:.
-
.jruby? ⇒ Boolean
Returns
true
if the Ruby platform is JRuby. -
.streamify(stream_or_string) ⇒ Object
:nodoc:.
Class Method Details
.command_arguments(options = {}) ⇒ Object
Returns an array of flags that should be passed to the jar file on the command line for the given set of options
.
33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/yuicompressor/jruby.rb', line 33 def command_arguments(={}) args = [ [:line_break] ? [:line_break].to_i : -1 ] if [:type].to_s == 'js' args << !! [:munge] args << false # verbose? args << !! [:preserve_semicolons] args << ! [:optimize] # disable optimizations? end args end |
.compress(stream_or_string, options = {}) ⇒ Object
Compresses the given stream_or_string
of code using the given options
. When using this method directly, at least the :type
option must be specified, and should be one of "css"
or "js"
. See YUICompressor#compress_css and YUICompressor#compress_js for details about which options are acceptable for each type of compressor.
In addition to the standard options, this method also accepts a :java
option that can be used to specify the location of the Java binary. This option will default to using "java"
unless otherwise specified.
If a block is given, it will receive the IO output object. Otherwise the output will be returned as a string.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/yuicompressor/jruby.rb', line 54 def compress(stream_or_string, ={}) raise ArgumentError, 'Option :type required' unless .key?(:type) stream = streamify(stream_or_string) output = StringIO.new reader = InputStreamReader.new(stream.to_inputstream) writer = OutputStreamWriter.new(output.to_outputstream) compressor = case [:type].to_s when 'js' = .merge() JavaScriptCompressor.new(reader, ErrorReporter.new) when 'css' = .merge() CssCompressor.new(reader) else raise ArgumentError, 'Unknown resource type: %s' % [:type] end compressor.compress(writer, *command_arguments()) writer.flush output.rewind output.read end |
.compress_css(stream_or_string, options = {}, &block) ⇒ Object
Compress the given CSS stream_or_string
using the given options
. Options should be a Hash with any of the following keys:
:line_break
-
The maximum number of characters that may appear in a single line of compressed code. Defaults to no maximum length. If set to 0 each line will be the minimum length possible.
21 22 23 |
# File 'lib/yuicompressor.rb', line 21 def compress_css(stream_or_string, ={}, &block) compress(stream_or_string, .merge(:type => 'css'), &block) end |
.compress_js(stream_or_string, options = {}, &block) ⇒ Object
Compress the given JavaScript stream_or_string
using the given options
. Options should be a Hash with any of the following keys:
:line_break
-
The maximum number of characters that may appear in a single line of compressed code. Defaults to no maximum length. If set to 0 each line will be the minimum length possible.
:munge
-
Should be
true
if the compressor should shorten local variable names when possible. Defaults tofalse
. :preserve_semicolons
-
Should be
true
if the compressor should preserve all semicolons in the code. Defaults tofalse
. :optimize
-
Should be
true
if the compressor should enable all micro optimizations. Defaults totrue
.
38 39 40 |
# File 'lib/yuicompressor.rb', line 38 def compress_js(stream_or_string, ={}, &block) compress(stream_or_string, .merge(:type => 'js'), &block) end |
.default_css_options ⇒ Object
:nodoc:
42 43 44 |
# File 'lib/yuicompressor.rb', line 42 def #:nodoc: { :line_break => nil } end |
.default_js_options ⇒ Object
:nodoc:
46 47 48 49 50 51 52 |
# File 'lib/yuicompressor.rb', line 46 def #:nodoc: .merge( :munge => false, :preserve_semicolons => false, :optimize => true ) end |
.jruby? ⇒ Boolean
Returns true
if the Ruby platform is JRuby.
10 11 12 |
# File 'lib/yuicompressor.rb', line 10 def jruby? !! (RUBY_PLATFORM =~ /java/) end |
.streamify(stream_or_string) ⇒ Object
:nodoc:
54 55 56 57 58 59 60 61 62 |
# File 'lib/yuicompressor.rb', line 54 def streamify(stream_or_string) #:nodoc: if IO === stream_or_string || StringIO === stream_or_string stream_or_string elsif String === stream_or_string StringIO.new(stream_or_string.to_s) else raise ArgumentError, 'Stream or string required' end end |