comparison markup/builder.py @ 119:cc2aee07f53b trunk

Allow creating fragments from the `tag` object in `markup.builder`.
author cmlenz
date Wed, 02 Aug 2006 08:03:31 +0000
parents 44af12832c5a
children 79f445396cd7
comparison
equal deleted inserted replaced
118:c392d38694d9 119:cc2aee07f53b
202 >>> print factory.foo 202 >>> print factory.foo
203 <foo/> 203 <foo/>
204 >>> print factory.foo(id=2) 204 >>> print factory.foo(id=2)
205 <foo id="2"/> 205 <foo id="2"/>
206 206
207 Markup fragments (lists of nodes without a parent element) can be created
208 by calling the factory:
209
210 >>> print factory('Hello, ', factory.em('world'), '!')
211 Hello, <em>world</em>!
212
207 A factory can also be bound to a specific namespace: 213 A factory can also be bound to a specific namespace:
208 214
209 >>> factory = ElementFactory('http://www.w3.org/1999/xhtml') 215 >>> factory = ElementFactory('http://www.w3.org/1999/xhtml')
210 >>> print factory.html(lang="en") 216 >>> print factory.html(lang="en")
211 <html lang="en" xmlns="http://www.w3.org/1999/xhtml"/> 217 <html lang="en" xmlns="http://www.w3.org/1999/xhtml"/>
229 """ 235 """
230 if namespace and not isinstance(namespace, Namespace): 236 if namespace and not isinstance(namespace, Namespace):
231 namespace = Namespace(namespace) 237 namespace = Namespace(namespace)
232 self.namespace = namespace 238 self.namespace = namespace
233 239
240 def __call__(self, *args):
241 return Fragment()(*args)
242
234 def __getitem__(self, namespace): 243 def __getitem__(self, namespace):
235 """Return a new factory that is bound to the specified namespace.""" 244 """Return a new factory that is bound to the specified namespace."""
236 return ElementFactory(namespace) 245 return ElementFactory(namespace)
237 246
238 def __getattr__(self, name): 247 def __getattr__(self, name):
Copyright (C) 2012-2017 Edgewall Software