Skip to content

Commit

Permalink
Fixed the bug reported in #7 (#7) where triple quoted, multiline stri…
Browse files Browse the repository at this point in the history
…ngs would cause breakage.

Fixed a bug where 'from . import whatever' was *still* being reduced to "from.import whatever" (thought I fixed it before but apparently not).
  • Loading branch information
liftoff committed Jun 5, 2014
1 parent 976f4bf commit bde3df8
Showing 1 changed file with 28 additions and 17 deletions.
45 changes: 28 additions & 17 deletions pyminifier/minification.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ def noop():
def reduce_operators(source):
"""
Remove spaces between operators in *source* and returns the result.
Example::
def foo(foo, bar, blah):
Expand Down Expand Up @@ -205,9 +204,9 @@ def foo(foo,bar,blah):
# This is just a regular operator; remove spaces
remove_columns.append(start_col) # Before OP
remove_columns.append(end_col+1) # After OP
else:
remove_columns.append(start_col) # Before OP
remove_columns.append(end_col+1) # After OP
#else:
#remove_columns.append(start_col) # Before OP
#remove_columns.append(end_col+1) # After OP
if token_string.endswith('\n'):
out_line += token_string
if remove_columns:
Expand Down Expand Up @@ -286,21 +285,31 @@ def join_multiline_pairs(text, pair="()"):
multline_match = multiline_quoted_string.search(line)
not_quoted_string_match = not_quoted_string.search(line)
if multline_match and not not_quoted_string_match and not quoted_string:
if len(line.split('"""')) > 1 or len(line.split("'''")):
# This is a single line that uses the triple quotes twice
# Treat it as if it were just a regular line:
output += line + '\n'
quoted_string = False
else:
output += line + '\n'
quoted_string = True
elif quoted_string and multiline_quoted_string.search(line):
if '"""' in line:
if len(line.split('"""')) % 2 == 0:
# Begin triple double quotes
output += line + '\n'
quoted_string = True
else:
# End triple double quotes
output += line + '\n'
quoted_string = False
elif "'''" in line:
if len(line.split("'''")) % 2 == 0:
# Begin triple single quotes
output += line + '\n'
quoted_string = True
else:
# End triple single quotes
output += line + '\n'
quoted_string = False
elif multline_match and quoted_string:
output += line + '\n'
quoted_string = False
# Now let's focus on the lines containing our opener and/or closer:
elif not quoted_string:
if opener_regex.search(line) or closer_regex.search(line) or inside_pair:
for character in line:
for n, character in enumerate(line):
if character == opener:
if not escaped and not inside_quotes:
openers += 1
Expand All @@ -316,8 +325,12 @@ def join_multiline_pairs(text, pair="()"):
openers = 0
inside_pair = False
output += character
if n == (len(line)-1):
output += '\n'
else:
closers += 1
if openers == closers:
output += '\n'
output += character
else:
escaped = False
Expand Down Expand Up @@ -370,10 +383,8 @@ def join_multiline_pairs(text, pair="()"):
output += line + '\n'
else:
output += line + '\n'

# Clean up
output = trailing_newlines.sub('\n', output)

return output

def dedent(source, use_tabs=False):
Expand Down Expand Up @@ -510,5 +521,5 @@ def minify(tokens, options):
result = join_multiline_pairs(result, '{}')
result = remove_blank_lines(result)
result = reduce_operators(result)
result = source = dedent(result, use_tabs=options.tabs)
result = dedent(result, use_tabs=options.tabs)
return result

0 comments on commit bde3df8

Please sign in to comment.