# HG changeset patch # User cmlenz # Date 1150734840 0 # Node ID 704f10b0650708c9f924819e61236eb54d941b7e # Parent 5420cfe42d365680f88e28bae605095c60bc1b2d Enable `ElementFactory` to create namespaced elements. diff --git a/markup/builder.py b/markup/builder.py --- a/markup/builder.py +++ b/markup/builder.py @@ -11,7 +11,7 @@ # individuals. For the exact contribution history, see the revision # history and logs, available at http://projects.edgewall.com/trac/. -from markup.core import Attributes, QName, Stream +from markup.core import Attributes, Namespace, QName, Stream __all__ = ['Fragment', 'Element', 'tag'] @@ -171,8 +171,16 @@ class ElementFactory(object): - def __getattribute__(self, name): - return Element(name.lower()) + def __init__(self, namespace=None): + if not isinstance(namespace, Namespace): + namespace = Namespace(namespace) + self.namespace = namespace + + def __getitem__(self, namespace): + return ElementFactory(namespace) + + def __getattr__(self, name): + return Element(self.namespace and self.namespace[name] or name) tag = ElementFactory() diff --git a/markup/output.py b/markup/output.py --- a/markup/output.py +++ b/markup/output.py @@ -103,9 +103,12 @@ tag = data tagname = tag.localname if tag.namespace: - prefix = ns_mapping[tag.namespace] - if prefix: - tagname = prefix + ':' + tag.localname + try: + prefix = ns_mapping[tag.namespace] + if prefix: + tagname = prefix + ':' + tag.localname + except KeyError: + pass yield Markup('' % tagname) elif kind is Stream.TEXT: