Debian Codegraph @

gavo/imp/rjsmin.py

raw
1 #!/usr/bin/env python
2 # -*- coding: ascii -*-
3 u"""
4 =====================
5 Javascript Minifier
6 =====================
7
8 rJSmin is a javascript minifier written in python.
9
10 The minifier is based on the semantics of `jsmin.c by Douglas Crockford`_\\.
11
12 :Copyright:
13
14 Copyright 2011 - 2019
15 Andr\xe9 Malo or his licensors, as applicable
16
17 :License:
18
19 Licensed under the Apache License, Version 2.0 (the "License");
20 you may not use this file except in compliance with the License.
21 You may obtain a copy of the License at
22
23 http://www.apache.org/licenses/LICENSE-2.0
24
25 Unless required by applicable law or agreed to in writing, software
26 distributed under the License is distributed on an "AS IS" BASIS,
27 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28 See the License for the specific language governing permissions and
29 limitations under the License.
30
31 The module is a re-implementation aiming for speed, so it can be used at
32 runtime (rather than during a preprocessing step). Usually it produces the
33 same results as the original ``jsmin.c``. It differs in the following ways:
34
35 - there is no error detection: unterminated string, regex and comment
36 literals are treated as regular javascript code and minified as such.
37 - Control characters inside string and regex literals are left untouched; they
38 are not converted to spaces (nor to \\n)
39 - Newline characters are not allowed inside string and regex literals, except
40 for line continuations in string literals (ECMA-5).
41 - "return /regex/" is recognized correctly.
42 - More characters are allowed before regexes.
43 - Line terminators after regex literals are handled more sensibly
44 - "+ +" and "- -" sequences are not collapsed to '++' or '--'
45 - Newlines before ! operators are removed more sensibly
46 - (Unnested) template literals are supported (ECMA-6)
47 - Comments starting with an exclamation mark (``!``) can be kept optionally
48 - rJSmin does not handle streams, but only complete strings. (However, the
49 module provides a "streamy" interface).
50
51 Since most parts of the logic are handled by the regex engine it's way faster
52 than the original python port of ``jsmin.c`` by Baruch Even. The speed factor
53 varies between about 6 and 55 depending on input and python version (it gets
54 faster the more compressed the input already is). Compared to the
55 speed-refactored python port by Dave St.Germain the performance gain is less
56 dramatic but still between 3 and 50 (for huge inputs). See the docs/BENCHMARKS
57 file for details.
58
59 rjsmin.c is a reimplementation of rjsmin.py in C and speeds it up even more.
60
61 Supported python versions are 2.7 and 3.4+.
62
63 .. _jsmin.c by Douglas Crockford:
64 http://www.crockford.com/javascript/jsmin.c
65 """
66 __author__ = u"Andr\xe9 Malo"
67 __docformat__ = "restructuredtext en"
68 __license__ = "Apache License, Version 2.0"
69 __version__ = '1.1.0'
70 __all__ = ['jsmin']
71
72 import functools as _ft
73 import re as _re
74
75
76 def _make_jsmin(python_only=False):
77 """
78 Generate JS minifier based on `jsmin.c by Douglas Crockford`_
79
80 .. _jsmin.c by Douglas Crockford:
81 http://www.crockford.com/javascript/jsmin.c
82
83 :Parameters:
84 `python_only` : ``bool``
85 Use only the python variant. If true, the c extension is not even
86 tried to be loaded.
87
88 :Return: Minifier
89 :Rtype: ``callable``
90 """
91 # pylint: disable = unused-variable
92 # pylint: disable = too-many-locals
93
94 if not python_only:
95 try:
96 import _rjsmin
97 except ImportError:
98 pass
99 else:
100 # Ensure that the C version is in sync
101 # https://github.com/ndparker/rjsmin/issues/11
102 if getattr(_rjsmin, '__version__', None) == __version__:
103 return _rjsmin.jsmin
104 try:
105 xrange
106 except NameError:
107 xrange = range # pylint: disable = redefined-builtin
108
109 space_chars = r'[\000-\011\013\014\016-\040]'
110
111 line_comment = r'(?://[^\r\n]*)'
112 space_comment = r'(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/)'
113 space_comment_nobang = r'(?:/\*(?!!)[^*]*\*+(?:[^/*][^*]*\*+)*/)'
114 bang_comment = r'(?:/\*![^*]*\*+(?:[^/*][^*]*\*+)*/)'
115
116 string1 = r"(?:'[^'\\\r\n]*(?:\\(?:[^\r\n]|\r?\n|\r)[^'\\\r\n]*)*')"
117 string1 = string1.replace("'", r'\047') # portability
118 string2 = r'(?:"[^"\\\r\n]*(?:\\(?:[^\r\n]|\r?\n|\r)[^"\\\r\n]*)*")'
119 string3 = r'(?:`[^`\\]*(?:\\(?:[^\r\n]|\r?\n|\r)[^`\\]*)*`)'
120 string3 = string3.replace('`', r'\140') # portability
121 strings = r'(?:%s|%s|%s)' % (string1, string2, string3)
122
123 charclass = r'(?:\[[^\\\]\r\n]*(?:\\[^\r\n][^\\\]\r\n]*)*\])'
124 nospecial = r'[^/\\\[\r\n]'
125 regex = r'(?:/(?![\r\n/*])%s*(?:(?:\\[^\r\n]|%s)%s*)*/)' % (
126 nospecial, charclass, nospecial
127 )
128 space = r'(?:%s|%s)' % (space_chars, space_comment)
129 newline = r'(?:%s?[\r\n])' % line_comment
130
131 def fix_charclass(result):
132 """ Fixup string of chars to fit into a regex char class """
133 pos = result.find('-')
134 if pos >= 0:
135 result = r'%s%s-' % (result[:pos], result[pos + 1:])
136
137 def sequentize(string):
138 """
139 Notate consecutive characters as sequence
140
141 (1-4 instead of 1234)
142 """
143 first, last, result = None, None, []
144 for char in map(ord, string):
145 if last is None:
146 first = last = char
147 elif last + 1 == char:
148 last = char
149 else:
150 result.append((first, last))
151 first = last = char
152 if last is not None:
153 result.append((first, last))
154 return ''.join(['%s%s%s' % (
155 chr(first),
156 last > first + 1 and '-' or '',
157 last != first and chr(last) or ''
158 ) for first, last in result]) # noqa
159
160 return _re.sub(
161 r"([\000-\040'`])", # ' and ` for better portability
162 lambda m: '\\%03o' % ord(m.group(1)), (
163 sequentize(result)
164 .replace('\\', '\\\\')
165 .replace('[', '\\[')
166 .replace(']', '\\]')
167 )
168 )
169
170 def id_literal_(what):
171 """ Make id_literal like char class """
172 match = _re.compile(what).match
173 result = ''.join([
174 chr(c) for c in xrange(127) if not match(chr(c))
175 ])
176 return '[^%s]' % fix_charclass(result)
177
178 def not_id_literal_(keep):
179 """ Make negated id_literal like char class """
180 match = _re.compile(id_literal_(keep)).match
181 result = ''.join([
182 chr(c) for c in xrange(127) if not match(chr(c))
183 ])
184 return r'[%s]' % fix_charclass(result)
185
186 not_id_literal = not_id_literal_(r'[a-zA-Z0-9_$]')
187 preregex1 = r'[(,=:\[!&|?{};\r\n+*-]'
188 preregex2 = r'%(not_id_literal)sreturn' % locals()
189
190 id_literal = id_literal_(r'[a-zA-Z0-9_$]')
191 id_literal_open = id_literal_(r'[a-zA-Z0-9_${\[(!+-]')
192 id_literal_close = id_literal_(r'[a-zA-Z0-9_$}\])"\047\140+-]')
193 post_regex_off = id_literal_(r'[^\000-\040}\])?:|,;.&=+-]')
194
195 dull = r'[^\047"\140/\000-\040]'
196
197 space_sub_simple = _re.compile((
198 # noqa pylint: disable = bad-continuation
199
200 r'(%(dull)s+)' # 0
201 r'|(%(strings)s%(dull)s*)' # 1
202 r'|(?<=%(preregex1)s)'
203 r'%(space)s*(?:%(newline)s%(space)s*)*'
204 r'(%(regex)s)' # 2
205 r'(%(space)s*(?:%(newline)s%(space)s*)+' # 3
206 r'(?=%(post_regex_off)s))?'
207 r'|(?<=%(preregex2)s)'
208 r'%(space)s*(?:(%(newline)s)%(space)s*)*' # 4
209 r'(%(regex)s)' # 5
210 r'(%(space)s*(?:%(newline)s%(space)s*)+' # 6
211 r'(?=%(post_regex_off)s))?'
212 r'|(?<=%(id_literal_close)s)'
213 r'%(space)s*(?:(%(newline)s)%(space)s*)+' # 7
214 r'(?=%(id_literal_open)s)'
215 r'|(?<=%(id_literal)s)(%(space)s)+(?=%(id_literal)s)' # 8
216 r'|(?<=\+)(%(space)s)+(?=\+)' # 9
217 r'|(?<=-)(%(space)s)+(?=-)' # 10
218 r'|%(space)s+'
219 r'|(?:%(newline)s%(space)s*)+'
220 ) % locals()).sub
221
222 # print(space_sub_simple.__self__.pattern)
223
224 def space_subber_simple(match):
225 """ Substitution callback """
226 # pylint: disable = too-many-return-statements
227
228 groups = match.groups()
229 if groups[0]:
230 return groups[0]
231 elif groups[1]:
232 return groups[1]
233 elif groups[2]:
234 if groups[3]:
235 return groups[2] + '\n'
236 return groups[2]
237 elif groups[5]:
238 return "%s%s%s" % (
239 groups[4] and '\n' or '',
240 groups[5],
241 groups[6] and '\n' or '',
242 )
243 elif groups[7]:
244 return '\n'
245 elif groups[8] or groups[9] or groups[10]:
246 return ' '
247 else:
248 return ''
249
250 space_sub_banged = _re.compile((
251 # noqa pylint: disable = bad-continuation
252
253 r'(%(dull)s+)' # 0
254 r'|(%(strings)s%(dull)s*)' # 1
255 r'|(?<=%(preregex1)s)'
256 r'(%(space)s*(?:%(newline)s%(space)s*)*)' # 2
257 r'(%(regex)s)' # 3
258 r'(%(space)s*(?:%(newline)s%(space)s*)+' # 4
259 r'(?=%(post_regex_off)s))?'
260 r'|(?<=%(preregex2)s)'
261 r'(%(space)s*(?:(%(newline)s)%(space)s*)*)' # 5, 6
262 r'(%(regex)s)' # 7
263 r'(%(space)s*(?:%(newline)s%(space)s*)+' # 8
264 r'(?=%(post_regex_off)s))?'
265 r'|(?<=%(id_literal_close)s)'
266 r'(%(space)s*(?:%(newline)s%(space)s*)+)' # 9
267 r'(?=%(id_literal_open)s)'
268 r'|(?<=%(id_literal)s)(%(space)s+)(?=%(id_literal)s)' # 10
269 r'|(?<=\+)(%(space)s+)(?=\+)' # 11
270 r'|(?<=-)(%(space)s+)(?=-)' # 12
271 r'|(%(space)s+)' # 13
272 r'|((?:%(newline)s%(space)s*)+)' # 14
273 ) % locals()).sub
274
275 # print(space_sub_banged.__self__.pattern)
276
277 keep = _re.compile((
278 r'%(space_chars)s+|%(space_comment_nobang)s+|%(newline)s+'
279 r'|(%(bang_comment)s+)'
280 ) % locals()).sub
281 keeper = lambda m: m.groups()[0] or ''
282
283 # print(keep.__self__.pattern)
284
285 def space_subber_banged(match):
286 """ Substitution callback """
287 # pylint: disable = too-many-return-statements
288
289 groups = match.groups()
290 if groups[0]:
291 return groups[0]
292 elif groups[1]:
293 return groups[1]
294 elif groups[3]:
295 return "%s%s%s%s" % (
296 keep(keeper, groups[2]),
297 groups[3],
298 keep(keeper, groups[4] or ''),
299 groups[4] and '\n' or '',
300 )
301 elif groups[7]:
302 return "%s%s%s%s%s" % (
303 keep(keeper, groups[5]),
304 groups[6] and '\n' or '',
305 groups[7],
306 keep(keeper, groups[8] or ''),
307 groups[8] and '\n' or '',
308 )
309 elif groups[9]:
310 return keep(keeper, groups[9]) + '\n'
311 elif groups[10] or groups[11] or groups[12]:
312 return keep(keeper, groups[10] or groups[11] or groups[12]) or ' '
313 else:
314 return keep(keeper, groups[13] or groups[14])
315
316 banged = _ft.partial(space_sub_banged, space_subber_banged)
317 simple = _ft.partial(space_sub_simple, space_subber_simple)
318
319 def jsmin(script, keep_bang_comments=False):
320 r"""
321 Minify javascript based on `jsmin.c by Douglas Crockford`_\.
322
323 Instead of parsing the stream char by char, it uses a regular
324 expression approach which minifies the whole script with one big
325 substitution regex.
326
327 .. _jsmin.c by Douglas Crockford:
328 http://www.crockford.com/javascript/jsmin.c
329
330 :Parameters:
331 `script` : ``str``
332 Script to minify
333
334 `keep_bang_comments` : ``bool``
335 Keep comments starting with an exclamation mark? (``/*!...*/``)
336
337 :Return: Minified script
338 :Rtype: ``str``
339 """
340 # pylint: disable = redefined-outer-name
341
342 is_bytes, script = _as_str(script)
343 script = (banged if keep_bang_comments else simple)(
344 '\n%s\n' % script
345 ).strip()
346 if is_bytes:
347 return script.encode('latin-1')
348 return script
349
350 return jsmin
351
352 jsmin = _make_jsmin()
353
354
355 def _as_str(script):
356 """ Make sure the script is a text string """
357 is_bytes = False
358 if str is bytes:
359 if not isinstance(script, basestring): # noqa pylint: disable = undefined-variable
360 raise TypeError("Unexpected type")
361 elif isinstance(script, (bytes, bytearray)):
362 is_bytes = True
363 script = script.decode('latin-1')
364 elif not isinstance(script, str):
365 raise TypeError("Unexpected type")
366
367 return is_bytes, script
368
369
370 def jsmin_for_posers(script, keep_bang_comments=False):
371 r"""
372 Minify javascript based on `jsmin.c by Douglas Crockford`_\.
373
374 Instead of parsing the stream char by char, it uses a regular
375 expression approach which minifies the whole script with one big
376 substitution regex.
377
378 .. _jsmin.c by Douglas Crockford:
379 http://www.crockford.com/javascript/jsmin.c
380
381 :Warning: This function is the digest of a _make_jsmin() call. It just
382 utilizes the resulting regexes. It's here for fun and may
383 vanish any time. Use the `jsmin` function instead.
384
385 :Parameters:
386 `script` : ``str``
387 Script to minify
388
389 `keep_bang_comments` : ``bool``
390 Keep comments starting with an exclamation mark? (``/*!...*/``)
391
392 :Return: Minified script
393 :Rtype: ``str``
394 """
395 if not keep_bang_comments:
396 rex = (
397 r'([^\047"\140/\000-\040]+)|((?:(?:\047[^\047\\\r\n]*(?:\\(?:[^'
398 r'\r\n]|\r?\n|\r)[^\047\\\r\n]*)*\047)|(?:"[^"\\\r\n]*(?:\\(?:[^'
399 r'\r\n]|\r?\n|\r)[^"\\\r\n]*)*")|(?:\140[^\140\\]*(?:\\(?:[^\r\n'
400 r']|\r?\n|\r)[^\140\\]*)*\140))[^\047"\140/\000-\040]*)|(?<=[(,='
401 r':\[!&|?{};\r\n+*-])(?:[\000-\011\013\014\016-\040]|(?:/\*[^*]*'
402 r'\*+(?:[^/*][^*]*\*+)*/))*(?:(?:(?://[^\r\n]*)?[\r\n])(?:[\000-'
403 r'\011\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))*)*('
404 r'(?:/(?![\r\n/*])[^/\\\[\r\n]*(?:(?:\\[^\r\n]|(?:\[[^\\\]\r\n]*'
405 r'(?:\\[^\r\n][^\\\]\r\n]*)*\]))[^/\\\[\r\n]*)*/))((?:[\000-\011'
406 r'\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))*(?:(?:('
407 r'?://[^\r\n]*)?[\r\n])(?:[\000-\011\013\014\016-\040]|(?:/\*[^*'
408 r']*\*+(?:[^/*][^*]*\*+)*/))*)+(?=[^\000-\040&)+,.:;=?\]|}-]))?|'
409 r'(?<=[\000-#%-,./:-@\[-^\140{-~-]return)(?:[\000-\011\013\014\0'
410 r'16-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))*(?:((?:(?://[^\r'
411 r'\n]*)?[\r\n]))(?:[\000-\011\013\014\016-\040]|(?:/\*[^*]*\*+(?'
412 r':[^/*][^*]*\*+)*/))*)*((?:/(?![\r\n/*])[^/\\\[\r\n]*(?:(?:\\[^'
413 r'\r\n]|(?:\[[^\\\]\r\n]*(?:\\[^\r\n][^\\\]\r\n]*)*\]))[^/\\\[\r'
414 r'\n]*)*/))((?:[\000-\011\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^/'
415 r'*][^*]*\*+)*/))*(?:(?:(?://[^\r\n]*)?[\r\n])(?:[\000-\011\013'
416 r'\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))*)+(?=[^\000'
417 r'-\040&)+,.:;=?\]|}-]))?|(?<=[^\000-!#%&(*,./:-@\[\\^{|~])(?:['
418 r'\000-\011\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/)'
419 r')*(?:((?:(?://[^\r\n]*)?[\r\n]))(?:[\000-\011\013\014\016-\040'
420 r']|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))*)+(?=[^\000-\040"#%-\047'
421 r')*,./:-@\\-^\140|-~])|(?<=[^\000-#%-,./:-@\[-^\140{-~-])((?:['
422 r'\000-\011\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/)'
423 r'))+(?=[^\000-#%-,./:-@\[-^\140{-~-])|(?<=\+)((?:[\000-\011\013'
424 r'\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/)))+(?=\+)|(?<'
425 r'=-)((?:[\000-\011\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]'
426 r'*\*+)*/)))+(?=-)|(?:[\000-\011\013\014\016-\040]|(?:/\*[^*]*\*'
427 r'+(?:[^/*][^*]*\*+)*/))+|(?:(?:(?://[^\r\n]*)?[\r\n])(?:[\000-'
428 r'\011\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))*)+'
429 )
430
431 def subber(match):
432 """ Substitution callback """
433 groups = match.groups()
434 return (
435 groups[0] or
436 groups[1] or
437 (groups[3] and (groups[2] + '\n')) or
438 groups[2] or
439 (groups[5] and "%s%s%s" % (
440 groups[4] and '\n' or '',
441 groups[5],
442 groups[6] and '\n' or '',
443 )) or
444 (groups[7] and '\n') or
445 (groups[8] and ' ') or
446 (groups[9] and ' ') or
447 (groups[10] and ' ') or
448 ''
449 )
450 else:
451 rex = (
452 r'([^\047"\140/\000-\040]+)|((?:(?:\047[^\047\\\r\n]*(?:\\(?:[^'
453 r'\r\n]|\r?\n|\r)[^\047\\\r\n]*)*\047)|(?:"[^"\\\r\n]*(?:\\(?:[^'
454 r'\r\n]|\r?\n|\r)[^"\\\r\n]*)*")|(?:\140[^\140\\]*(?:\\(?:[^\r\n'
455 r']|\r?\n|\r)[^\140\\]*)*\140))[^\047"\140/\000-\040]*)|(?<=[(,='
456 r':\[!&|?{};\r\n+*-])((?:[\000-\011\013\014\016-\040]|(?:/\*[^*]'
457 r'*\*+(?:[^/*][^*]*\*+)*/))*(?:(?:(?://[^\r\n]*)?[\r\n])(?:[\000'
458 r'-\011\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))*)*'
459 r')((?:/(?![\r\n/*])[^/\\\[\r\n]*(?:(?:\\[^\r\n]|(?:\[[^\\\]\r\n'
460 r']*(?:\\[^\r\n][^\\\]\r\n]*)*\]))[^/\\\[\r\n]*)*/))((?:[\000-\0'
461 r'11\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))*(?:(?'
462 r':(?://[^\r\n]*)?[\r\n])(?:[\000-\011\013\014\016-\040]|(?:/\*['
463 r'^*]*\*+(?:[^/*][^*]*\*+)*/))*)+(?=[^\000-\040&)+,.:;=?\]|}-]))'
464 r'?|(?<=[\000-#%-,./:-@\[-^\140{-~-]return)((?:[\000-\011\013\01'
465 r'4\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))*(?:((?:(?://[^'
466 r'\r\n]*)?[\r\n]))(?:[\000-\011\013\014\016-\040]|(?:/\*[^*]*\*+'
467 r'(?:[^/*][^*]*\*+)*/))*)*)((?:/(?![\r\n/*])[^/\\\[\r\n]*(?:(?:'
468 r'\\[^\r\n]|(?:\[[^\\\]\r\n]*(?:\\[^\r\n][^\\\]\r\n]*)*\]))[^/'
469 r'\\\[\r\n]*)*/))((?:[\000-\011\013\014\016-\040]|(?:/\*[^*]*\*+'
470 r'(?:[^/*][^*]*\*+)*/))*(?:(?:(?://[^\r\n]*)?[\r\n])(?:[\000-\01'
471 r'1\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))*)+(?=['
472 r'^\000-\040&)+,.:;=?\]|}-]))?|(?<=[^\000-!#%&(*,./:-@\[\\^{|~])'
473 r'((?:[\000-\011\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*'
474 r'+)*/))*(?:(?:(?://[^\r\n]*)?[\r\n])(?:[\000-\011\013\014\016-'
475 r'\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))*)+)(?=[^\000-\040"#%'
476 r'-\047)*,./:-@\\-^\140|-~])|(?<=[^\000-#%-,./:-@\[-^\140{-~-])('
477 r'(?:[\000-\011\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+'
478 r')*/))+)(?=[^\000-#%-,./:-@\[-^\140{-~-])|(?<=\+)((?:[\000-\011'
479 r'\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))+)(?=\+)'
480 r'|(?<=-)((?:[\000-\011\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*]'
481 r'[^*]*\*+)*/))+)(?=-)|((?:[\000-\011\013\014\016-\040]|(?:/\*[^'
482 r'*]*\*+(?:[^/*][^*]*\*+)*/))+)|((?:(?:(?://[^\r\n]*)?[\r\n])(?:'
483 r'[\000-\011\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/'
484 r'))*)+)'
485 )
486
487 keep = _re.compile(
488 r'[\000-\011\013\014\016-\040]+|(?:/\*(?!!)[^*]*\*+(?:[^/*][^*]*'
489 r'\*+)*/)+|(?:(?://[^\r\n]*)?[\r\n])+|((?:/\*![^*]*\*+(?:[^/*][^'
490 r'*]*\*+)*/)+)'
491 ).sub
492 keeper = lambda m: m.groups()[0] or ''
493
494 def subber(match):
495 """ Substitution callback """
496 groups = match.groups()
497 return (
498 groups[0] or
499 groups[1] or
500 groups[3] and "%s%s%s%s" % (
501 keep(keeper, groups[2]),
502 groups[3],
503 keep(keeper, groups[4] or ''),
504 groups[4] and '\n' or '',
505 ) or
506 groups[7] and "%s%s%s%s%s" % (
507 keep(keeper, groups[5]),
508 groups[6] and '\n' or '',
509 groups[7],
510 keep(keeper, groups[8] or ''),
511 groups[8] and '\n' or '',
512 ) or
513 groups[9] and (keep(keeper, groups[9]) + '\n') or
514 groups[10] and (keep(keeper, groups[10]) or ' ') or
515 groups[11] and (keep(keeper, groups[11]) or ' ') or
516 groups[12] and (keep(keeper, groups[12]) or ' ') or
517 keep(keeper, groups[13] or groups[14])
518 )
519
520 is_bytes, script = _as_str(script)
521 script = _re.sub(rex, subber, '\n%s\n' % script).strip()
522 if is_bytes:
523 return script.encode('latin-1')
524 return script
525
526
527 if __name__ == '__main__':
528 def main():
529 """ Main """
530 import sys as _sys
531
532 argv = _sys.argv[1:]
533 keep_bang_comments = '-b' in argv or '-bp' in argv or '-pb' in argv
534 if '-p' in argv or '-bp' in argv or '-pb' in argv:
535 xjsmin = _make_jsmin(python_only=True)
536 else:
537 xjsmin = jsmin
538
539 _sys.stdout.write(xjsmin(
540 _sys.stdin.read(), keep_bang_comments=keep_bang_comments
541 ))
542
543 main()