from django.http import HttpResponse, Http404 from django.conf import settings def csspp(request, css): try: f = open("%s/%s.css" % (settings.CSS_DIR, css)) css = f.read().strip("\n").strip() f.close() except: raise Http404 css_dict = {} for line in [line.strip() for line in css.split("}") if line.find("{") > 0]: selector = line.split("{")[0].strip() line = line.split("{")[1].strip() rules = {} rule_list = [rule.strip() for rule in line.split(";") if rule.find(":") > 0] for rule in rule_list: prop = rule.split(":")[0].strip() value = rule.split(":")[1].strip() if value.startswith("$"): value = css_dict["@variables"][value] rules[prop] = value css_dict[selector] = rules for selector in [selector for selector in css_dict if selector.endswith(")") and selector.find("(") > 0]: old_selector = selector selector = selector.split("(")[0] css_dict[selector] = css_dict[old_selector] for extend in old_selector.split("(")[1].split(")")[0].split(","): try: if css_dict[extend]: for rule in css_dict[extend]: css_dict[selector][rule] = css_dict[extend][rule] except: pass del css_dict[old_selector] try: del css_dict["@variables"] except: pass css = "" for selector in css_dict: css += selector+" { " for rule in css_dict[selector]: css += "%s: %s; " % (rule, css_dict[selector][rule]) css += "} \n" return HttpResponse(css, mimetype="text/css; charset=utf-8")