tagname = None
# from nose.tools import set_trace
- if isinstance(element, etree._Comment): return None
-
if element.tag[0] == '{':
for nshort, nhref in element.nsmap.items():
try:
if element is None: return None # end of tree
def _handle_element(self, element):
+ if isinstance(element, etree._Comment): return None
+
handler = self._handle_for_element(element)
# How many scopes
try:
if handler is None:
pre = [self.filter_text(element.text)]
- post = []
+ post = [self.filter_text(element.tail)]
else:
vals = handler(element)
# depending on number of returned values, vals can be None, a value, or a tuple.
# how poorly designed is that? 9 lines below are needed just to unpack this.
if vals is None:
- return []
+ return [self.filter_text(element.tail)]
else:
if not isinstance(vals, tuple):
return [vals, self.filter_text(element.tail)]
return out
-def tag(name, classes=None, **attrs):
+def tag_open_close(name_, classes_=None, **attrs):
+ u"""Creates tag beginning and end.
+
+ >>> tag_open_close("a", "klass", x=u"ą<")
+ (u'<a x="\\u0105<" class="klass">', u'</a>')
+
+ """
+ if classes_:
+ if isinstance(classes_, (tuple, list)): classes_ = ' '.join(classes_)
+ attrs['class'] = classes_
+
+ e = etree.Element(name_)
+ e.text = " "
+ for k, v in attrs.items():
+ e.attrib[k] = v
+ pre, post = etree.tostring(e, encoding=unicode).split(u"> <")
+ return pre + u">", u"<" + post
+
+def tag(name_, classes_=None, **attrs):
"""Returns a handler which wraps node contents in tag `name', with class attribute
set to `classes' and other attributes according to keyword paramters
"""
- if classes:
- if isinstance(classes, (tuple, list)): classes = ' '.join(classes)
- attrs['class'] = classes
- a = ''.join([' %s="%s"' % (k,v) for (k,v) in attrs.items()])
def _hnd(self, element):
- return "<%s%s>" % (name, a), "</%s>" % name
+ return tag_open_close(name_, classes_, **attrs)
return _hnd