comparison markup/builder.py @ 28:15e59a9cb362

More docstrings for the builder module.
author cmlenz
date Wed, 28 Jun 2006 09:41:58 +0000
parents b8456279c444
children ae293292cbb1
comparison
equal deleted inserted replaced
27:b8456279c444 28:15e59a9cb362
15 15
16 __all__ = ['Fragment', 'Element', 'tag'] 16 __all__ = ['Fragment', 'Element', 'tag']
17 17
18 18
19 class Fragment(object): 19 class Fragment(object):
20 """Represents a markup fragment, which is basically just a list of element
21 or text nodes.
22 """
20 __slots__ = ['children'] 23 __slots__ = ['children']
21 24
22 def __init__(self): 25 def __init__(self):
23 self.children = [] 26 self.children = []
24 27
46 for arg in args: 49 for arg in args:
47 self.append(arg) 50 self.append(arg)
48 return self 51 return self
49 52
50 def generate(self): 53 def generate(self):
51 """Generator that yield tags and text nodes as strings.""" 54 """Return a markup event stream for the fragment."""
52 def _generate(): 55 def _generate():
53 for child in self.children: 56 for child in self.children:
54 if isinstance(child, Fragment): 57 if isinstance(child, Fragment):
55 for event in child.generate(): 58 for event in child.generate():
56 yield event 59 yield event
158 attr = attr.rstrip('_').replace('_', '-') 161 attr = attr.rstrip('_').replace('_', '-')
159 self.attrib.set(attr, value) 162 self.attrib.set(attr, value)
160 return Fragment.__call__(self, *args) 163 return Fragment.__call__(self, *args)
161 164
162 def generate(self): 165 def generate(self):
163 """Generator that yield tags and text nodes as strings.""" 166 """Return a markup event stream for the fragment."""
164 def _generate(): 167 def _generate():
165 yield Stream.START, (self.tag, self.attrib), (-1, -1) 168 yield Stream.START, (self.tag, self.attrib), (-1, -1)
166 for kind, data, pos in Fragment.generate(self): 169 for kind, data, pos in Fragment.generate(self):
167 yield kind, data, pos 170 yield kind, data, pos
168 yield Stream.END, self.tag, (-1, -1) 171 yield Stream.END, self.tag, (-1, -1)
169 return Stream(_generate()) 172 return Stream(_generate())
170 173
171 174
172 class ElementFactory(object): 175 class ElementFactory(object):
176 """Factory for `Element` objects.
177
178 A new element is created simply by accessing a correspondingly named
179 attribute of the factory object:
180
181 >>> factory = ElementFactory()
182 >>> print factory.foo
183 <foo/>
184 >>> print factory.foo(id=2)
185 <foo id="2"/>
186
187 A factory can also be bound to a specific namespace:
188
189 >>> factory = ElementFactory('http://www.w3.org/1999/xhtml')
190 >>> print factory.html(lang="en")
191 <html lang="en" xmlns="http://www.w3.org/1999/xhtml"/>
192
193 The namespace for a specific element can be altered on an existing factory
194 by specifying the new namespace using item access:
195
196 >>> factory = ElementFactory()
197 >>> print factory.html(factory['http://www.w3.org/2000/svg'].g(id=3))
198 <html><g id="3" xmlns="http://www.w3.org/2000/svg"/></html>
199
200 Usually, the `ElementFactory` class is not be used directly. Rather, the
201 `tag` instance should be used to create elements.
202 """
173 203
174 def __init__(self, namespace=None): 204 def __init__(self, namespace=None):
205 """Create the factory, optionally bound to the given namespace.
206
207 @param namespace: the namespace URI for any created elements, or `None`
208 for no namespace
209 """
175 if namespace and not isinstance(namespace, Namespace): 210 if namespace and not isinstance(namespace, Namespace):
176 namespace = Namespace(namespace) 211 namespace = Namespace(namespace)
177 self.namespace = namespace 212 self.namespace = namespace
178 213
179 def __getitem__(self, namespace): 214 def __getitem__(self, namespace):
215 """Return a new factory that is bound to the specified namespace."""
180 return ElementFactory(namespace) 216 return ElementFactory(namespace)
181 217
182 def __getattr__(self, name): 218 def __getattr__(self, name):
219 """Create an `Element` with the given name."""
183 return Element(self.namespace and self.namespace[name] or name) 220 return Element(self.namespace and self.namespace[name] or name)
184 221
185 222
186 tag = ElementFactory() 223 tag = ElementFactory()
Copyright (C) 2012-2017 Edgewall Software