Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SASS fixes #58

Closed
wants to merge 10 commits into from
68 changes: 56 additions & 12 deletions libs/cssformatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,42 @@ def run(self, code, action='compact'):
if action == 'compress':
# Remove comments
code = re.sub(r'\s*\/\*[\s\S]*?\*\/\s*', '', code)

# Remove comments //
code = re.sub(r'\/\/.*\n', '', code)
else:
# Protect comments
commentReg = r'[ \t]*\/\*[\s\S]*?\*\/'
comments = re.findall(commentReg, code)
code = re.sub(commentReg, '!comment!', code)

# Protect comments //
comment2Reg = r'\/\/.*\n'
comments2 = re.findall(comment2Reg, code)
code = re.sub(comment2Reg, '!comment2!\n', code)


# Protect strings
stringReg = r'(content\s*:|[\w-]+\s*=)\s*(([\'\"]).*?\3)\s*'
strings = re.findall(stringReg, code)
code = re.sub(stringReg, r'\1!string!', code)

#Protect SASS variables
sassvarsReg = r'\#\{[^}]*\}'
sassvars = re.findall(sassvarsReg, code)
print(sassvars)
code = re.sub(sassvarsReg, '!sass!', code)

# Protect urls
urlReg = r'((?:url|url-prefix|regexp)\([^\)]+\))'
urls = re.findall(urlReg, code)
print(urls)
code = re.sub(urlReg, '!url!', code)

# Protect brackets
bracketsReg = r'\[[^(\])]*\"[^(")]*\"\]'
brackets = re.findall(bracketsReg, code)
code = re.sub(bracketsReg, r'!brackets!', code)

# Pre process
code = re.sub(r'\s*([\{\}:;,])\s*', r'\1', code) # remove \s before and after characters {}:;,
Expand Down Expand Up @@ -84,6 +105,7 @@ def run(self, code, action='compact'):
if action == 'compress':
# Remove last semicolon
code = code.replace(';}', '}')

else:
# Add blank line between each block in `expand-bs` mode
if action == 'expand-bs':
Expand All @@ -95,20 +117,34 @@ def run(self, code, action='compact'):
code = re.sub(r'\s*\n!comment!', '\n\n!comment!', code)

# Backfill comments
for i in range(len(comments)):
code = re.sub(r'[ \t]*!comment!', comments[i], code, 1)

for i in comments:
code = re.sub(r'[ \t]*!comment!', i, code, 1)
# Indent
code = self.indent_code(code)


# Backfill comments //
for i in comments2:
code = re.sub(r'!comment2!', i.strip(), code, 1)



# Backfill brackets
for i in brackets:
code = code.replace('!brackets!', i, 1)

# Backfill urls
for i in urls:
code = code.replace('!url!', i, 1)

# Backfill sass
for i in sassvars:
code = code.replace('!sass!', i, 1)

# Backfill strings
for i in range(len(strings)):
code = code.replace('!string!', strings[i][1], 1)

# Backfill urls
for i in range(len(urls)):
code = code.replace('!url!', urls[i], 1)

code = code.replace('!string!', strings[i][1], 1)

# Trim
code = re.sub(r'^\s*(\S+(\s+\S+)*)\s*$', r'\1', code)

Expand Down Expand Up @@ -264,10 +300,18 @@ def indent_code(self, code):
nextLevel = level + adjustment
thisLevel = level if adjustment > 0 else nextLevel
level = nextLevel

# Add indentation
lines[i] = self.indentation * thisLevel + lines[i] if lines[i] != '' else ''
lines[i] = self.indentation * thisLevel + lines[i]


#Test for SASS new lines
if i > 1:
if lines[i - 1].strip() == "":
if lines[i].strip() == "}":
lines[i-1] = "!stopnewline!";

lines = list(filter(lambda a: a != "!stopnewline!", lines))
code = '\n'.join(lines)

return code