No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

219 líneas
5.9 KiB

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