class GithubbishAssets::JSMinimizer

Constants

EOF

Attributes

input[RW]
output[RW]

Public Class Methods

minimize_content(content) click to toggle source
# File lib/vendor/js_minimizer.rb, line 207
def self.minimize_content(content)
  js_minimizer = new
  js_minimizer.input = StringIO.new(content)
  js_minimizer.output = StringIO.new
  
  js_minimizer.jsmin
  
  js_minimizer.output.string
end

Public Instance Methods

action(a) click to toggle source

action – do something! What you do is determined by the argument: 1 Output A. Copy B to A. Get the next B. 2 Copy B to A. Get the next B. (Delete A). 3 Get the next B. (Delete B). action treats a string as a single character. Wow! action recognizes a regular expression if it is preceded by ( or , or =.

# File lib/vendor/js_minimizer.rb, line 108
def action(a)
    if(a==1)
        @output.write @theA
    end
    if(a==1 || a==2)
        @theA = @theB
        if (@theA == "\'" || @theA == "\"")
            while (true)
                @output.write @theA
                @theA = get
                break if (@theA == @theB)
                raise "Unterminated string literal" if (@theA <= "\n")
                if (@theA == "\\")
                    @output.write @theA
                    @theA = get
                end
            end
        end
    end
    if(a==1 || a==2 || a==3)
        @theB = mynext
        if (@theB == "/" && (@theA == "(" || @theA == "," || @theA == "=" ||
                             @theA == ":" || @theA == "[" || @theA == "!" ||
                             @theA == "&" || @theA == "|" || @theA == "?" ||
                             @theA == "{" || @theA == "}" || @theA == ";" ||
                             @theA == "\n"))
            @output.write @theA
            @output.write @theB
            while (true)
                @theA = get
                if (@theA == "/")
                    break
                elsif (@theA == "\\")
                    @output.write @theA
                    @theA = get
                elsif (@theA <= "\n")
                    raise "Unterminated RegExp Literal"
                end
                @output.write @theA
            end
            @theB = mynext
        end
    end
end
get() click to toggle source

get – return the next character from input. Watch out for lookahead. If the character is a control character, translate it to a space or linefeed.

# File lib/vendor/js_minimizer.rb, line 55
def get()
  c = @input.getc
  return EOF if(!c)
  c = c.chr
  return c if (c >= " " || c == "\n" || c.unpack("c") == EOF)
  return "\n" if (c == "\r")
  return " "
end
isAlphanum(c) click to toggle source

isAlphanum – return true if the character is a letter, digit, underscore, dollar sign, or non-ASCII character

# File lib/vendor/js_minimizer.rb, line 46
def isAlphanum(c)
   return false if !c || c == EOF
   return ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') ||
           (c >= 'A' && c <= 'Z') || c == '_' || c == '$' ||
           c == '\\' || c[0] > 126)
end
jsmin() click to toggle source

jsmin – Copy the input to the output, deleting the characters which are insignificant to JavaScript. Comments will be removed. Tabs will be replaced with spaces. Carriage returns will be replaced with linefeeds. Most spaces and linefeeds will be removed.

# File lib/vendor/js_minimizer.rb, line 157
def jsmin
    @theA = "\n"
    action(3)
    while (@theA != EOF)
        case @theA
        when " "
            if (isAlphanum(@theB))
                action(1)
            else
                action(2)
            end
        when "\n"
            case (@theB)
            when "{","[","(","+","-"
                action(1)
            when " "
                action(3)
            else
                if (isAlphanum(@theB))
                    action(1)
                else
                    action(2)
                end
            end
        else
            case (@theB)
            when " "
                if (isAlphanum(@theA))
                    action(1)
                else
                    action(3)
                end
            when "\n"
                case (@theA)
                when "}","]",")","+","-","\"","\\", "'", '"'
                    action(1)
                else
                    if (isAlphanum(@theA))
                        action(1)
                    else
                        action(3)
                    end
                end
            else
                action(1)
            end
        end
    end
end
mynext() click to toggle source

mynext – get the next character, excluding comments. peek() is used to see if a ‘/’ is followed by a ‘/’ or ‘*’.

# File lib/vendor/js_minimizer.rb, line 73
def mynext()
    c = get
    if (c == "/")
        if(peek == "/")
            while(true)
                c = get
                if (c <= "\n")
                return c
                end
            end
        end
        if(peek == "*")
            get
            while(true)
                case get
                when "*"
                   if (peek == "/")
                        get
                        return " "
                    end
                when EOF
                    raise "Unterminated comment"
                end
            end
        end
    end
    return c
end
peek() click to toggle source

Get the next character without getting it.

# File lib/vendor/js_minimizer.rb, line 65
def peek()
    lookaheadChar = @input.getc
    @input.ungetc(lookaheadChar)
    return lookaheadChar.chr
end