X-Git-Url: https://git.mdrn.pl/librarian.git/blobdiff_plain/51396a4419bb3d4b89ad65a434dfeb9e81e6f6e2..2919eb4f57c7e1c9dead734c685a9b36f34e30ce:/librarian/xmlutils.py diff --git a/librarian/xmlutils.py b/librarian/xmlutils.py index 37a719b..97a3039 100644 --- a/librarian/xmlutils.py +++ b/librarian/xmlutils.py @@ -117,16 +117,30 @@ class Xmill(object): 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'', u'') + + """ + 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), "" % name + return tag_open_close(name_, classes_, **attrs) return _hnd