def handle_tag(self, token, after, context): # Parses the TAG line, extracting the id, classes, arguments and the # contents. try: tag = self.__parser.parse_tag(token.line) except Exception as e: reraise(e, 'While parsing tag in line %d' % token.line_no) raise result = [] have_content = hasattr(tag, 'content') and tag.content have_children = len(token.children) > 0 if have_content and have_children: raise RuntimeError('A tag should have contents OR children nodes.') if after: if not have_content and have_children: result.append( token.indentation + '</{name}>'.format( name=tag.name ) ) else: args = getattr(tag, 'args', []) args = { i.key: self._eval(getattr(i, 'value', 'True'), context) for i in args } tag_text = create_tag( tag.name, args, klass=tag.classes, id_=tag.id ) if have_content or have_children: tag_format = '<{}>' else: tag_format = '<{} />' line = token.indentation + tag_format.format(tag_text) if have_content: # With content, close the tag in the same line. end_tag = '</{}>'.format(tag.name) content = self._eval(tag.content, context) line += content + end_tag result.append(line) return result
def test_tag(): assert create_tag('alpha') == 'alpha' assert create_tag('alpha', id_='id_alpha') == 'alpha id="id_alpha"' assert create_tag('alpha', klass=['zulu', 'zebra']) == 'alpha class="zulu zebra"'