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.8.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
Returns a compressed version of 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:.
-
.stringify(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
Returns a compressed version of 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 executable. This option will default to using "java"
unless otherwise specified.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/yuicompressor/jruby.rb', line 52 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.
:charset
-
Charset. Default is utf8.
22 23 24 |
# File 'lib/yuicompressor.rb', line 22 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
. :charset
-
Charset. Default is utf8.
40 41 42 |
# File 'lib/yuicompressor.rb', line 40 def compress_js(stream_or_string, ={}, &block) compress(stream_or_string, .merge(:type => 'js'), &block) end |
.default_css_options ⇒ Object
:nodoc:
44 45 46 |
# File 'lib/yuicompressor.rb', line 44 def #:nodoc: { :line_break => nil, :charset => 'utf8' } end |
.default_js_options ⇒ Object
:nodoc:
48 49 50 51 52 53 54 55 |
# File 'lib/yuicompressor.rb', line 48 def #:nodoc: { :line_break => nil, :charset => 'utf8', :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:
57 58 59 60 61 62 63 64 65 |
# File 'lib/yuicompressor.rb', line 57 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 |
.stringify(stream_or_string) ⇒ Object
:nodoc:
67 68 69 70 71 72 73 |
# File 'lib/yuicompressor.rb', line 67 def stringify(stream_or_string) #:nodoc: case stream_or_string when IO then stream_or_string.read when String then stream_or_string else raise ArgumentError, 'Stream or string required' end end |