def generate(self, tokens): pos = (None, -1, -1) span = QName('span') class_ = QName('class') def _generate(): for c, text in self._chunk(tokens): if c: attrs = Attrs([(class_, c)]) yield START, (span, attrs), pos yield TEXT, text, pos yield END, span, pos else: yield TEXT, text, pos return Stream(_generate())
def image_emit(self, node): target = node.content text = self.get_text(node) class_ = "internal" m = self.link_rules.addr_re.match(target) if m: if m.group('extern_addr'): class_ = "external" elif m.group('inter_wiki'): raise NotImplementedError yield START, (QName('img'), Attrs([ (QName('src'), target), (QName('alt'), text), (QName('class'), class_), ])), POS yield END, QName('img'), POS
def link_emit(self, node): target = node.content class_ = "internal" m = self.link_rules.addr_re.match(target) if m: if m.group('extern_addr'): class_ = "external" elif m.group('inter_wiki'): raise NotImplementedError yield START, (QName('a'), Attrs([ (QName('href'), target), (QName('class'), class_), ])), POS if node.children: for part in self.emit_children(node): yield part else: yield TEXT, target, POS yield END, QName('a'), POS
def label_filter(tag, attrs, stream, context, el): from genshi import QName if el is None: return tag, attrs, stream content = stream.render() if not content: label = el.label if not el.optional: label += ' *' stream = label if not el.optional: css = (attrs.get(u'class', u'') + u' required').strip() attrs |= [(QName(u'class'), css)] return tag, attrs, stream
def test_renameElement(): """ Check that renameElementFilter renames the root element of a stream. """ xmlStream = XML( '''<foo xmlns="http://example.com" flub="2"><bar baz="1">fofof</bar></foo>''' ) f = RenameElementFilter(QName('waze')) xml = (xmlStream | f).render() print xml assert xml == '<waze xmlns="http://example.com" flub="2"><bar baz="1">fofof</bar></waze>'
def preformatted_emit(self, node): yield START, (QName('pre'), Attrs()), POS yield TEXT, node.content, POS yield END, QName('pre'), POS
def break_emit(self, node): yield START, (QName('br'), Attrs()), POS yield END, QName('br'), POS
def header_emit(self, node): yield START, (QName('h%d' % node.level), Attrs()), POS yield TEXT, node.content, POS yield END, QName('h%d' % node.level), POS
def separator_emit(self, node): yield START, (QName('hr'), Attrs()), POS yield END, QName('hr'), POS
def wrap(self, tag, node): yield START, (QName(tag), Attrs()), POS for part in self.emit_children(node): yield part yield END, QName(tag), POS
import flatland from flatland.util import Maybe from .taglistener import TagListener, default_start __all__ = 'flatland_filter', log = logging.getLogger('flatland.out.genshi') NAMESPACE = Namespace(u'http://ns.discorporate.us/flatland/genshi') # filter attributes F_BIND = NAMESPACE[u'bind'] # HTML attributes H_CHECKED = QName(u'checked') H_FOR = QName(u'for') H_ID = QName(u'id') H_NAME = QName(u'name') H_SELECTED = QName(u'selected') H_TABINDEX = QName(u'tabindex') H_VALUE = QName(u'value') MAYBE = (u'auto', ) YES = (u'1', u'true', u't', u'on', u'yes') NO = (u'0', u'false', u'nil', u'off', u'no') # TODO:jek remember why mixing - and _ in these was a good idea and document CTX_CUR_TABINDEX = u'auto-tabindex_value' CTX_FMT_DOMID = u'auto-domid_format' CTX_FILTERS = u'auto-filter_filters'