-
-
Save TheBits/566186 to your computer and use it in GitHub Desktop.
This file contains 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
# coding=utf-8 | |
import re | |
# simple css parser | |
SELECTOR = '^\s*([#-_\.a-z\s:\(\)]+?)\s*({)' | |
PROPERTIES = '^\s*([-a-z]*?\s*:?\s*.*?)\s*(})' | |
SELECTOR_SEPARATOR = ',' | |
PROPERTIES_SEPARATOR = ';' | |
def finder(text, regex, sep): | |
find = re.match(regex, text, re.IGNORECASE) | |
if find: | |
items = [i.strip() for i in find.group(1).split(sep) if i.strip()] | |
return items, find.end(2) | |
else: | |
return find | |
def selector(text): | |
return finder(text, SELECTOR, SELECTOR_SEPARATOR) | |
def propertie(text): | |
return finder(text, PROPERTIES, PROPERTIES_SEPARATOR) | |
def semantictree(text, semantic=None): | |
semantic = [] if semantic is None else semantic | |
next = selector(text) | |
if next: | |
selectors, selectorlast = next | |
properties, propertylast = propertie(text[selectorlast:]) | |
semantic.append([selectors, properties]) | |
return semantictree(text[selectorlast+propertylast:], semantic) | |
else: | |
return semantic | |
# css = open('style.css', 'r').read() | |
# print semantictree(css) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment