Class: HtmlMinifier::Minifier
- Inherits:
-
Object
- Object
- HtmlMinifier::Minifier
- Defined in:
- lib/html_minifier/minifier.rb
Constant Summary collapse
- Error =
ExecJS::Error
- SourceBasePath =
File.("../../js/", __FILE__)
Instance Method Summary collapse
-
#initialize(options = nil) ⇒ Minifier
constructor
A new instance of Minifier.
- #minify(source) ⇒ Object
Constructor Details
#initialize(options = nil) ⇒ Minifier
Returns a new instance of Minifier.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/html_minifier/minifier.rb', line 13 def initialize( = nil) if .instance_of? Hash then = .dup @log = .delete :log elsif .nil? = nil else raise 'Unsupported option for HtmlMinifier: ' + .to_s end js = %w{console htmlparser htmllint htmlminifier}.map do |i| File.open("#{SourceBasePath}/#{i}.js", "r:UTF-8").read end.join("\n") @context = ExecJS.compile(js) end |
Instance Method Details
#minify(source) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/html_minifier/minifier.rb', line 29 def minify(source) source = source.respond_to?(:read) ? source.read : source.to_s js = [] if .nil? then js << "var min = minify(#{MultiJson.dump(source)});" else js << "var min = minify(#{MultiJson.dump(source)}, #{MultiJson.dump(@options)});" end js << "return {min:min, logs:console.clear()};" result = @context.exec js.join("\n") if @log.respond_to?(:info) result["logs"].each do |i| @log.info i end end result["min"] end |