jsmin.rb

Path: lib/jsmin.rb
Last Update: Mon Jul 07 20:09:46 +0100 2008

jsmin.rb 2007-07-20 Author: Uladzislau Latynski This work is a translation from C to Ruby of jsmin.c published by Douglas Crockford. Permission is hereby granted to use the Ruby version under the same conditions as the jsmin.c on which it is based.

/* jsmin.c

   2003-04-21

Copyright (c) 2002 Douglas Crockford (www.crockford.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

The Software shall be used for Good, not Evil.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Methods

action   get   isAlphanum   jsmin   mynext   peek  

Constants

EOF = -1   jsmin.rb 2007-07-20 Author: Uladzislau Latynski This work is a translation from C to Ruby of jsmin.c published by Douglas Crockford. Permission is hereby granted to use the Ruby version under the same conditions as the jsmin.c on which it is based.

/* jsmin.c

   2003-04-21

Copyright (c) 2002 Douglas Crockford (www.crockford.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

The Software shall be used for Good, not Evil.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Public Instance methods

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 =.

[Source]

     # File lib/jsmin.rb, line 102
102: def action(a)
103:     if(a==1)
104:         $stdout.write $theA
105:     end
106:     if(a==1 || a==2)
107:         $theA = $theB
108:         if ($theA == "\'" || $theA == "\"")
109:             while (true)
110:                 $stdout.write $theA
111:                 $theA = get
112:                 break if ($theA == $theB)
113:                 raise "Unterminated string literal" if ($theA <= "\n")
114:                 if ($theA == "\\")
115:                     $stdout.write $theA
116:                     $theA = get
117:                 end
118:             end
119:         end
120:     end
121:     if(a==1 || a==2 || a==3)
122:         $theB = mynext
123:         if ($theB == "/" && ($theA == "(" || $theA == "," || $theA == "=" ||
124:                              $theA == ":" || $theA == "[" || $theA == "!" ||
125:                              $theA == "&" || $theA == "|" || $theA == "?" ||
126:                              $theA == "{" || $theA == "}" || $theA == ";" ||
127:                              $theA == "\n"))
128:             $stdout.write $theA
129:             $stdout.write $theB
130:             while (true)
131:                 $theA = get
132:                 if ($theA == "/")
133:                     break
134:                 elsif ($theA == "\\")
135:                     $stdout.write $theA
136:                     $theA = get
137:                 elsif ($theA <= "\n")
138:                     raise "Unterminated RegExp Literal"
139:                 end
140:                 $stdout.write $theA
141:             end
142:             $theB = mynext
143:         end
144:     end
145: end

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

[Source]

    # File lib/jsmin.rb, line 49
49: def get()
50:   c = $stdin.getc
51:   return EOF if(!c)
52:   c = c.chr
53:   return c if (c >= " " || c == "\n" || c.unpack("c") == EOF)
54:   return "\n" if (c == "\r")
55:   return " "
56: end

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

[Source]

    # File lib/jsmin.rb, line 40
40: def isAlphanum(c)
41:    return false if !c || c == EOF
42:    return ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') ||
43:            (c >= 'A' && c <= 'Z') || c == '_' || c == '$' ||
44:            c == '\\' || c[0] > 126)
45: end

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.

[Source]

     # File lib/jsmin.rb, line 151
151: def jsmin
152:     $theA = "\n"
153:     action(3)
154:     while ($theA != EOF)
155:         case $theA
156:         when " "
157:             if (isAlphanum($theB))
158:                 action(1)
159:             else
160:                 action(2)
161:             end
162:         when "\n"
163:             case ($theB)
164:             when "{","[","(","+","-"
165:                 action(1)
166:             when " "
167:                 action(3)
168:             else
169:                 if (isAlphanum($theB))
170:                     action(1)
171:                 else
172:                     action(2)
173:                 end
174:             end
175:         else
176:             case ($theB)
177:             when " "
178:                 if (isAlphanum($theA))
179:                     action(1)
180:                 else
181:                     action(3)
182:                 end
183:             when "\n"
184:                 case ($theA)
185:                 when "}","]",")","+","-","\"","\\", "'", '"'
186:                     action(1)
187:                 else
188:                     if (isAlphanum($theA))
189:                         action(1)
190:                     else
191:                         action(3)
192:                     end
193:                 end
194:             else
195:                 action(1)
196:             end
197:         end
198:     end
199: end

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

[Source]

    # File lib/jsmin.rb, line 67
67: def mynext()
68:     c = get
69:     if (c == "/")
70:         if(peek == "/")
71:             while(true)
72:                 c = get
73:                 if (c <= "\n")
74:                 return c
75:                 end
76:             end
77:         end
78:         if(peek == "*")
79:             get
80:             while(true)
81:                 case get
82:                 when "*"
83:                    if (peek == "/")
84:                         get
85:                         return " "
86:                     end
87:                 when EOF
88:                     raise "Unterminated comment"
89:                 end
90:             end
91:         end
92:     end
93:     return c
94: end

Get the next character without getting it.

[Source]

    # File lib/jsmin.rb, line 59
59: def peek()
60:     lookaheadChar = $stdin.getc
61:     $stdin.ungetc(lookaheadChar)
62:     return lookaheadChar.chr
63: end

[Validate]