Last active
May 27, 2016 14:50
-
-
Save kezabelle/ff0ca68df6dbe75060ec52ffe638f1ee to your computer and use it in GitHub Desktop.
parsing CSS comments?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
x = r""" | |
/* comment 1 */ | |
/*** | |
comment 2 | |
***/ | |
/*================= | |
comment 3 | |
*/ | |
/*comment 4*/ | |
/* comment 5 | |
***/ | |
/***** | |
comment 6*/ | |
/* **** | |
comment 7*/ | |
/* **** comment 8 | |
*/ | |
/* **** comment 9 | |
*** */ | |
""" | |
y = re.compile(r'\/\*(?P<inner>.+?)\*\/', flags=re.UNICODE | re.MULTILINE | re.IGNORECASE | re.DOTALL) | |
z = y.findall(x) | |
a = [_.splitlines() for _ in z] | |
def trashline(line): | |
l = line.strip() | |
if l: | |
l0 = l[0] | |
if l.count(l0) == len(l): | |
return None | |
else: | |
return line | |
return None | |
def consumer(x): | |
xlen = len(x) | |
if xlen == 1: | |
return x | |
elif xlen > 2: | |
return x[1:-1] # consume the first line /** ... and the last line */// | |
elif xlen == 2: | |
if trashline(x[0]) is None: | |
return x[1] | |
elif trashline(x[1]) is None: | |
return x[0] | |
else: | |
raise ValueError("hmmm1") | |
raise ValueError("hmm2") | |
outs = [consumer(_) for _ in a] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment