3 # This code is original from jsmin by Douglas Crockford, it was translated to
4 # Python by Baruch Even. The original code had the following copyright and
10 # Copyright (c) 2002 Douglas Crockford (www.crockford.com)
12 # Permission is hereby granted, free of charge, to any person obtaining a copy of
13 # this software and associated documentation files (the "Software"), to deal in
14 # the Software without restriction, including without limitation the rights to
15 # use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
16 # of the Software, and to permit persons to whom the Software is furnished to do
17 # so, subject to the following conditions:
19 # The above copyright notice and this permission notice shall be included in all
20 # copies or substantial portions of the Software.
22 # The Software shall be used for Good, not Evil.
24 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 from StringIO import StringIO
39 JavascriptMinify().minify(ins, outs)
41 if len(str) > 0 and str[0] == '\n':
47 """return true if the character is a letter, digit, underscore,
48 dollar sign, or non-ASCII character.
50 return (c >= 'a' and c <= 'z') or (c >= 'A' and c <= 'Z') \
51 or (c >= '0' and c <= '9') or c == '_' or c == '$' or c == '\\' \
52 or (c is not None and ord(c) > 126)
55 class UnterminatedComment(Exception):
59 class UnterminatedStringLiteral(Exception):
63 class UnterminatedRegularExpression(Exception):
67 class JavascriptMinify(object):
70 self.outstream.write(self.theA)
73 self.outstream.write(self.theB)
76 """return the next character from stdin. Watch out for lookahead. If
77 the character is a control character, translate it to a space or
81 self.theLookahead = None
83 c = self.instream.read(1)
84 if c >= ' ' or c == '\n':
93 self.theLookahead = self._get()
94 return self.theLookahead
97 """get the next character, excluding comments. peek() is used to see
98 if a '/' is followed by a '/' or '*'.
113 if self._peek() == '/':
117 raise UnterminatedComment()
121 def _action(self, action):
122 """do something! What you do is determined by the argument:
123 1 Output A. Copy B to A. Get the next B.
124 2 Copy B to A. Get the next B. (Delete A).
125 3 Get the next B. (Delete B).
126 action treats a string as a single character. Wow!
127 action recognizes a regular expression if it is preceded by ( or , or =.
133 self.theA = self.theB
134 if self.theA == "'" or self.theA == '"':
137 self.theA = self._get()
138 if self.theA == self.theB:
140 if self.theA <= '\n':
141 raise UnterminatedStringLiteral()
142 if self.theA == '\\':
144 self.theA = self._get()
147 self.theB = self._next()
148 if self.theB == '/' and (self.theA == '(' or self.theA == ',' or
149 self.theA == '=' or self.theA == ':' or
150 self.theA == '[' or self.theA == '?' or
151 self.theA == '!' or self.theA == '&' or
152 self.theA == '|' or self.theA == ';' or
153 self.theA == '{' or self.theA == '}' or
158 self.theA = self._get()
161 elif self.theA == '\\':
163 self.theA = self._get()
164 elif self.theA <= '\n':
165 raise UnterminatedRegularExpression()
167 self.theB = self._next()
170 """Copy the input to the output, deleting the characters which are
171 insignificant to JavaScript. Comments will be removed. Tabs will be
172 replaced with spaces. Carriage returns will be replaced with linefeeds.
173 Most spaces and linefeeds will be removed.
178 while self.theA != '\000':
180 if isAlphanum(self.theB):
184 elif self.theA == '\n':
185 if self.theB in ['{', '[', '(', '+', '-']:
187 elif self.theB == ' ':
190 if isAlphanum(self.theB):
196 if isAlphanum(self.theA):
200 elif self.theB == '\n':
201 if self.theA in ['}', ']', ')', '+', '-', '"', '\'']:
204 if isAlphanum(self.theA):
211 def minify(self, instream, outstream):
212 self.instream = instream
213 self.outstream = outstream
216 self.theLookahead = None
219 self.instream.close()
221 if __name__ == '__main__':
223 jsm = JavascriptMinify()
224 jsm.minify(sys.stdin, sys.stdout)