40 lines
1.1 KiB
Python
40 lines
1.1 KiB
Python
"""
|
|
File Strip.
|
|
|
|
Licensed under MIT
|
|
Copyright (c) 2012 - 2016 Isaac Muse <isaacmuse@gmail.com>
|
|
"""
|
|
import re
|
|
|
|
LINE_PRESERVE = re.compile(r"\r?\n", re.MULTILINE)
|
|
CSS_PATTERN = re.compile(
|
|
r'''(?x)
|
|
(?P<comments>
|
|
/\*[^*]*\*+(?:[^/*][^*]*\*+)*/ # multi-line comments
|
|
)
|
|
| (?P<code>
|
|
"(?:\\.|[^"\\])*" # double quotes
|
|
| '(?:\\.|[^'\\])*' # single quotes
|
|
| .[^/"']* # everything else
|
|
)
|
|
''',
|
|
re.DOTALL
|
|
)
|
|
|
|
|
|
def clean_css(text, preserve_lines=False):
|
|
"""Clean css."""
|
|
|
|
def remove_comments(group, preserve_lines=False):
|
|
"""Remove comments."""
|
|
|
|
return ''.join([x[0] for x in LINE_PRESERVE.findall(group)]) if preserve_lines else ''
|
|
|
|
def evaluate(m, preserve_lines):
|
|
"""Search for comments."""
|
|
|
|
g = m.groupdict()
|
|
return g["code"] if g["code"] is not None else remove_comments(g["comments"], preserve_lines)
|
|
|
|
return ''.join(map(lambda m: evaluate(m, preserve_lines), CSS_PATTERN.finditer(text.replace('\r', ''))))
|