Skip to content

Instantly share code, notes, and snippets.

@kezabelle
Last active May 27, 2016 14:50
Show Gist options
  • Save kezabelle/ff0ca68df6dbe75060ec52ffe638f1ee to your computer and use it in GitHub Desktop.
Save kezabelle/ff0ca68df6dbe75060ec52ffe638f1ee to your computer and use it in GitHub Desktop.
parsing CSS comments?
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