def parse_into_tree(abbr, syntax='html'): """ Parses abbreviation into a node set @param abbr: Abbreviation to transform @type abbr: str @param syntax: Document type (xsl, html), a key of dictionary where to search abbreviation settings @type syntax: str @return: Tag """ # remove filters from abbreviation filter_list = [] def filter_replace(m): filter_list.append(m.group(1)) return '' re_filter = re.compile(r'\|([\w\|\-]+)$') abbr = re_filter.sub(filter_replace, abbr) # try to parse abbreviation try: abbr_tree = zen_parser.parse(abbr) tree_root = Tag(None, syntax) abbr_tree = preprocess_parsed_tree(abbr_tree, syntax) except zen_parser.ZenInvalidAbbreviation: return None # then recursively expand each group item for child in abbr_tree.children: process_parsed_node(child, syntax, tree_root) tree_root.filters = filter_list return tree_root
def replace_expandos(node, syntax): """ Replaces expando nodes by its parsed content @type node: zen_parser.TreeNode @type syntax: str """ for i, n in enumerate(node.children): if not n.is_empty() and not n.is_text_node() and '+' in n.name: # it's expando a = get_abbreviation(syntax, n.name) if a: node.children[i] = zen_parser.parse(a.value) replace_expandos(node.children[i], syntax)
''' Created on Dec 26, 2010 @author: sergey ''' from zencoding.parser.abbreviation import parse if __name__ == "__main__": print(parse('a.sample+b[title=Hello]>a+(span{Test}+div)+em')) print('=============') print(parse('a.sample+b[title=Hello]>a+(span{Test}+div+(a+b)+a)+em')) print('=============') print(parse('a.sample+b[title=Hello]>a+(span{Test}+div>(a>b)+a)+em')) print('=============')