I want to split the contents of a CSS file into code blocks and push each block of code into a list using Python 3.5.
So, given this CSS:
h1 {color: #333, background-color: transparent}
h2 {
font-weight:300
}
h3
{
font-weight: 200
}
We can clearly tell that it has multiple styles and / or types of indentation meaning the CSS has to be tidied to get this:
h1 {
color: #333,background-color: transparent;
}
h2 {
font-weight: 300;
}
h3 {
font-weight: 200;
}
How can I use Python to read a tidied string of CSS and push every block of code inside it into a Python list like this:
styles = [
"h1 {\n color: #333,background-color: transparent;\n}",
"h2 {\n font-weight: 300;\n}",
"h3 {\n font-weight: 200;\n}"
]
I would also like to point out that RegExp is not really my forte and I'm not quite sure what RegEx to use, but I was thinking that I could use RegExp & [].split(...);
together to achieve this.
Possibly even use RegExp to eliminate the need to tidy the stylesheet before splitting the code-blocks in it.
NOTE: I've checked this this question out but unfortunately that didn't help either.