選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

238 行
7.0 KiB

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