jcoglan / packr

Ruby version of Dean Edwards' Packer

This URL has Read+Write access

packr / lib / packr.rb
100644 69 lines (57 sloc) 1.659 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
66
67
68
69
# PackR -- a Ruby port of Packer by Dean Edwards
# Packer version 3.1 copyright 2004-2009, Dean Edwards
# http://www.opensource.org/licenses/mit-license
 
[ '/string',
  '/packr/map',
  '/packr/collection',
  '/packr/regexp_group',
  '/packr/constants',
  '/packr/encoder',
  '/packr/parser',
  '/packr/minifier',
  '/packr/privates',
  '/packr/shrinker',
  '/packr/words',
  '/packr/base62'
].each do |path|
  require File.dirname(__FILE__) + path
end
 
class Packr
  
  VERSION = '3.1.0'
  
  DATA = Parser.new.
    put("STRING1", IGNORE).
    put('STRING2', IGNORE).
    put("CONDITIONAL", IGNORE). # conditional comments
    put("(OPERATOR)\\s*(REGEXP)", "\\1\\2")
  
  def self.encode62(c)
    (c < 62 ? '' : encode62((c / 62.0).to_i)) +
        ((c = c % 62) > 35 ? (c+29).chr : c.to_s(36))
  end
  
  def self.encode52(c)
    # Base52 encoding (a-Z)
    encode = lambda do |d|
      (d < 52 ? '' : encode.call((d / 52.0).to_i)) +
          ((d = d % 52) > 25 ? (d + 39).chr : (d + 97).chr)
    end
    encoded = encode.call(c.to_i)
    encoded = encoded[1..-1] + '0' if encoded =~ /^(do|if|in)$/
    encoded
  end
  
  def self.pack(script, options = {})
    @packr ||= self.new
    @packr.pack(script, options)
  end
  
  def initialize
    @minifier = Minifier.new
    @shrinker = Shrinker.new
    @privates = Privates.new
    @base62 = Base62.new
  end
  
  def pack(script, options = {})
    script = @minifier.minify(script)
    script = @shrinker.shrink(script, options[:protect]) if options[:shrink_vars]
    script = @privates.encode(script) if options[:private]
    script = @base62.encode(script) if options[:base62]
    script
  end
  
end