Class: PluginTool::Minimiser
- Inherits:
-
Object
- Object
- PluginTool::Minimiser
- Defined in:
- lib/minimise.rb
Constant Summary collapse
- Context =
Java::OrgMozillaJavascript::Context
Instance Method Summary collapse
- #finish ⇒ Object
-
#initialize ⇒ Minimiser
constructor
A new instance of Minimiser.
- #process(data, filename) ⇒ Object
Constructor Details
#initialize ⇒ Minimiser
Returns a new instance of Minimiser.
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/minimise.rb', line 13 def initialize # Load UglifyJS into a JavaScript interpreter raise "Another JS Context is active" unless nil == Context.getCurrentContext() @cx = Context.enter(); @cx.setLanguageVersion(Context::VERSION_1_7) @javascript_scope = @cx.initStandardObjects() ['js_min.js','uglifyjs/parse-js.js','uglifyjs/process.js','uglifyjs/squeeze-more.js'].each do |filename| js = File.open("#{File.dirname(__FILE__)}/#{filename}") { |f| f.read } @cx.evaluateString(@javascript_scope, js, "<#{filename}>", 1, nil); end @js_min = @javascript_scope.get("js_min", @javascript_scope); end |
Instance Method Details
#finish ⇒ Object
67 68 69 |
# File 'lib/minimise.rb', line 67 def finish Context.exit() end |
#process(data, filename) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/minimise.rb', line 26 def process(data, filename) if filename =~ /\.js\z/ # JavaScript - use UglifyJS loaded into the JavaScript interpreter @js_min.call(@cx, @javascript_scope, @javascript_scope, [data]) elsif filename =~ /\.html\z/ # Simple processing of HTML # Remove HTML comments html = data.gsub(/\<\!\-\-.+?\-\-\>/m,'') # Remove indents html.gsub!(/^\s+/,'') # Remove any unnecessary line breaks (fairly conservative) html.gsub!(/\>[\r\n]+\</m,'><') html.gsub!(/([\>}])[\r\n]+([\<{])/m,'\1\2') html elsif filename =~ /\.css\z/ # Simple processing of CSS css = data.gsub(/(^|\s)\/\*.+?\*\/($|\s)/m,'') # remove C style comments out = [] css.split(/[\r\n]+/).each do |line| line.chomp!; line.gsub!(/^\s+/,''); line.gsub!(/\s+$/,'') line.gsub!(/\s+/,' ') # contract spaces line.gsub!(/\s*:\s*/,':') # remove unnecessary spaces if line =~ /\S/ out << line end end css = out.join("\n") # Remove unnecessary line endings css.gsub!(/[\r\n]*(\{[^\}]+\})/m) do |m| $1.gsub(/[\r\n]/m,'') end css else # No processing data end end |