Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

211 lignes
5.8 KiB

  1. # This code is original from jsmin by Douglas Crockford, it was translated to
  2. # Python by Baruch Even. The original code had the following copyright and
  3. # license.
  4. #
  5. # /* jsmin.c
  6. # 2007-05-22
  7. #
  8. # Copyright (c) 2002 Douglas Crockford (www.crockford.com)
  9. #
  10. # Permission is hereby granted, free of charge, to any person obtaining a copy of
  11. # this software and associated documentation files (the "Software"), to deal in
  12. # the Software without restriction, including without limitation the rights to
  13. # use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  14. # of the Software, and to permit persons to whom the Software is furnished to do
  15. # so, subject to the following conditions:
  16. #
  17. # The above copyright notice and this permission notice shall be included in all
  18. # copies or substantial portions of the Software.
  19. #
  20. # The Software shall be used for Good, not Evil.
  21. #
  22. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  25. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  26. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  27. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  28. # SOFTWARE.
  29. # */
  30. from StringIO import StringIO
  31. def jsmin(js):
  32. ins = StringIO(js)
  33. outs = StringIO()
  34. JavascriptMinify().minify(ins, outs)
  35. str = outs.getvalue()
  36. if len(str) > 0 and str[0] == '\n':
  37. str = str[1:]
  38. return str
  39. def isAlphanum(c):
  40. """return true if the character is a letter, digit, underscore,
  41. dollar sign, or non-ASCII character.
  42. """
  43. return ((c >= 'a' and c <= 'z') or (c >= '0' and c <= '9') or
  44. (c >= 'A' and c <= 'Z') or c == '_' or c == '$' or c == '\\' or (c is not None and ord(c) > 126));
  45. class UnterminatedComment(Exception):
  46. pass
  47. class UnterminatedStringLiteral(Exception):
  48. pass
  49. class UnterminatedRegularExpression(Exception):
  50. pass
  51. class JavascriptMinify(object):
  52. def _outA(self):
  53. self.outstream.write(self.theA)
  54. def _outB(self):
  55. self.outstream.write(self.theB)
  56. def _get(self):
  57. """return the next character from stdin. Watch out for lookahead. If
  58. the character is a control character, translate it to a space or
  59. linefeed.
  60. """
  61. c = self.theLookahead
  62. self.theLookahead = None
  63. if c == None:
  64. c = self.instream.read(1)
  65. if c >= ' ' or c == '\n':
  66. return c
  67. if c == '': # EOF
  68. return '\000'
  69. if c == '\r':
  70. return '\n'
  71. return ' '
  72. def _peek(self):
  73. self.theLookahead = self._get()
  74. return self.theLookahead
  75. def _next(self):
  76. """get the next character, excluding comments. peek() is used to see
  77. if an unescaped '/' is followed by a '/' or '*'.
  78. """
  79. c = self._get()
  80. if c == '/' and self.theA != '\\':
  81. p = self._peek()
  82. if p == '/':
  83. c = self._get()
  84. while c > '\n':
  85. c = self._get()
  86. return c
  87. if p == '*':
  88. c = self._get()
  89. while 1:
  90. c = self._get()
  91. if c == '*':
  92. if self._peek() == '/':
  93. self._get()
  94. return ' '
  95. if c == '\000':
  96. raise UnterminatedComment()
  97. return c
  98. def _action(self, action):
  99. """do something! What you do is determined by the argument:
  100. 1 Output A. Copy B to A. Get the next B.
  101. 2 Copy B to A. Get the next B. (Delete A).
  102. 3 Get the next B. (Delete B).
  103. action treats a string as a single character. Wow!
  104. action recognizes a regular expression if it is preceded by ( or , or =.
  105. """
  106. if action <= 1:
  107. self._outA()
  108. if action <= 2:
  109. self.theA = self.theB
  110. if self.theA == "'" or self.theA == '"':
  111. while 1:
  112. self._outA()
  113. self.theA = self._get()
  114. if self.theA == self.theB:
  115. break
  116. if self.theA <= '\n':
  117. raise UnterminatedStringLiteral()
  118. if self.theA == '\\':
  119. self._outA()
  120. self.theA = self._get()
  121. if action <= 3:
  122. self.theB = self._next()
  123. if self.theB == '/' and (self.theA == '(' or self.theA == ',' or
  124. self.theA == '=' or self.theA == ':' or
  125. self.theA == '[' or self.theA == '?' or
  126. self.theA == '!' or self.theA == '&' or
  127. self.theA == '|' or self.theA == ';' or
  128. self.theA == '{' or self.theA == '}' or
  129. self.theA == '\n'):
  130. self._outA()
  131. self._outB()
  132. while 1:
  133. self.theA = self._get()
  134. if self.theA == '/':
  135. break
  136. elif self.theA == '\\':
  137. self._outA()
  138. self.theA = self._get()
  139. elif self.theA <= '\n':
  140. raise UnterminatedRegularExpression()
  141. self._outA()
  142. self.theB = self._next()
  143. def _jsmin(self):
  144. """Copy the input to the output, deleting the characters which are
  145. insignificant to JavaScript. Comments will be removed. Tabs will be
  146. replaced with spaces. Carriage returns will be replaced with linefeeds.
  147. Most spaces and linefeeds will be removed.
  148. """
  149. self.theA = '\n'
  150. self._action(3)
  151. while self.theA != '\000':
  152. if self.theA == ' ':
  153. if isAlphanum(self.theB):
  154. self._action(1)
  155. else:
  156. self._action(2)
  157. elif self.theA == '\n':
  158. if self.theB in ['{', '[', '(', '+', '-']:
  159. self._action(1)
  160. elif self.theB == ' ':
  161. self._action(3)
  162. else:
  163. if isAlphanum(self.theB):
  164. self._action(1)
  165. else:
  166. self._action(2)
  167. else:
  168. if self.theB == ' ':
  169. if isAlphanum(self.theA):
  170. self._action(1)
  171. else:
  172. self._action(3)
  173. elif self.theB == '\n':
  174. if self.theA in ['}', ']', ')', '+', '-', '"', '\'']:
  175. self._action(1)
  176. else:
  177. if isAlphanum(self.theA):
  178. self._action(1)
  179. else:
  180. self._action(3)
  181. else:
  182. self._action(1)
  183. def minify(self, instream, outstream):
  184. self.instream = instream
  185. self.outstream = outstream
  186. self.theA = '\n'
  187. self.theB = None
  188. self.theLookahead = None
  189. self._jsmin()
  190. self.instream.close()