def return_comp_style(parts, comp_styles): curr_dict = comp_styles; styles = StyledObject(); for part in parts: try: curr_dict = curr_dict[part]; styles.add_Object(curr_dict['$style'], 90); except Exception, e: continue;
def main_style_parse(parser, write=True): curr_time = time.clock(); curr_time_counter = 0; objs = {}; for key in parser.styled_objects.keys(): parts = key.lstrip().rstrip().split(' '); parts.reverse(); style = StyledObject(); #the number of elements in this whole line tag num_elements = len(parts); #add any composite styles style.add_Object(return_comp_style(parts, parser.styleParser.comp_styles), 100+num_elements); #start the current element to 0 curr_el = 0; for part in parts: #split up this tag into its tag, id and class fields sections = re.match('([a-z A-Z 0-9 \- _]+)?(#[a-z A-Z 0-9 \- _]+)?(\.[a-z A-Z 0-9 \- _]+)?', part); groups = sections.groups(); if groups[1] and groups[2]: #if there is a class assigned to an id style.add_Object(return_style(groups[1]+groups[2], parser.styleParser.id_class_styles), 90+num_elements-curr_el); if groups[0] and groups[1]: #if there is an id assigned to a tag, it is most specific style.add_Object(return_style(groups[0]+groups[1], parser.styleParser.id_specific_styles), 80+num_elements-curr_el); if groups[0] and groups[2]: #if there is a class assigned to a tag it is more specific style.add_Object(return_style(groups[0]+groups[2], parser.styleParser.class_specific_styles), 60+num_elements-curr_el); if groups[1]: #if there is an id it is specific style.add_Object(return_style(groups[1], parser.styleParser.id_styles), 40+num_elements-curr_el); if groups[2]: #if there is a class it is less specific style.add_Object(return_style(groups[2], parser.styleParser.class_styles), 20+num_elements-curr_el); if groups[0]: #if there is only a tag, it is least specific style.add_Object(return_style(groups[0], parser.styleParser.tag_styles), 10+num_elements-curr_el); curr_el += 1; try: objs[key].add_Object(style); except Exception, e: objs[key] = style; objs[key].text = parser.styled_objects[key];