annotate genshi/builder.py @ 430:e7a4d43c438a

Moved the `builder` document into the API docs.
author cmlenz
date Thu, 22 Mar 2007 17:14:09 +0000
parents 5b248708bbed
children 747baa1cd597
rev   line source
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
1 # -*- coding: utf-8 -*-
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
2 #
408
49a3bae5a8bb Update copyright year for files modified this year.
cmlenz
parents: 403
diff changeset
3 # Copyright (C) 2006-2007 Edgewall Software
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
4 # All rights reserved.
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
5 #
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
6 # This software is licensed as described in the file COPYING, which
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
7 # you should have received as part of this distribution. The terms
230
24757b771651 Renamed Markup to Genshi in repository.
cmlenz
parents: 182
diff changeset
8 # are also available at http://genshi.edgewall.org/wiki/License.
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
9 #
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
10 # This software consists of voluntary contributions made by many
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
11 # individuals. For the exact contribution history, see the revision
230
24757b771651 Renamed Markup to Genshi in repository.
cmlenz
parents: 182
diff changeset
12 # history and logs, available at http://genshi.edgewall.org/log/.
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
13
430
e7a4d43c438a Moved the `builder` document into the API docs.
cmlenz
parents: 425
diff changeset
14 """Support for programmatically generating markup streams from Python code using
e7a4d43c438a Moved the `builder` document into the API docs.
cmlenz
parents: 425
diff changeset
15 a very simple syntax. The main entry point to this module is the `tag` object
e7a4d43c438a Moved the `builder` document into the API docs.
cmlenz
parents: 425
diff changeset
16 (which is actually an instance of the ``ElementFactory`` class). You should
e7a4d43c438a Moved the `builder` document into the API docs.
cmlenz
parents: 425
diff changeset
17 rarely (if ever) need to directly import and use any of the other classes in
e7a4d43c438a Moved the `builder` document into the API docs.
cmlenz
parents: 425
diff changeset
18 this module.
e7a4d43c438a Moved the `builder` document into the API docs.
cmlenz
parents: 425
diff changeset
19
e7a4d43c438a Moved the `builder` document into the API docs.
cmlenz
parents: 425
diff changeset
20 Elements can be created using the `tag` object using attribute access. For
e7a4d43c438a Moved the `builder` document into the API docs.
cmlenz
parents: 425
diff changeset
21 example:
e7a4d43c438a Moved the `builder` document into the API docs.
cmlenz
parents: 425
diff changeset
22
e7a4d43c438a Moved the `builder` document into the API docs.
cmlenz
parents: 425
diff changeset
23 >>> doc = tag.p('Some text and ', tag.a('a link', href='http://example.org/'), '.')
e7a4d43c438a Moved the `builder` document into the API docs.
cmlenz
parents: 425
diff changeset
24 >>> doc
e7a4d43c438a Moved the `builder` document into the API docs.
cmlenz
parents: 425
diff changeset
25 <Element "p">
e7a4d43c438a Moved the `builder` document into the API docs.
cmlenz
parents: 425
diff changeset
26
e7a4d43c438a Moved the `builder` document into the API docs.
cmlenz
parents: 425
diff changeset
27 This produces an `Element` instance which can be further modified to add child
e7a4d43c438a Moved the `builder` document into the API docs.
cmlenz
parents: 425
diff changeset
28 nodes and attributes. This is done by "calling" the element: positional
e7a4d43c438a Moved the `builder` document into the API docs.
cmlenz
parents: 425
diff changeset
29 arguments are added as child nodes (alternatively, the `Element.append` method
e7a4d43c438a Moved the `builder` document into the API docs.
cmlenz
parents: 425
diff changeset
30 can be used for that purpose), whereas keywords arguments are added as
e7a4d43c438a Moved the `builder` document into the API docs.
cmlenz
parents: 425
diff changeset
31 attributes:
e7a4d43c438a Moved the `builder` document into the API docs.
cmlenz
parents: 425
diff changeset
32
e7a4d43c438a Moved the `builder` document into the API docs.
cmlenz
parents: 425
diff changeset
33 >>> doc(tag.br)
e7a4d43c438a Moved the `builder` document into the API docs.
cmlenz
parents: 425
diff changeset
34 <Element "p">
e7a4d43c438a Moved the `builder` document into the API docs.
cmlenz
parents: 425
diff changeset
35 >>> print doc
e7a4d43c438a Moved the `builder` document into the API docs.
cmlenz
parents: 425
diff changeset
36 <p>Some text and <a href="http://example.org/">a link</a>.<br/></p>
e7a4d43c438a Moved the `builder` document into the API docs.
cmlenz
parents: 425
diff changeset
37
e7a4d43c438a Moved the `builder` document into the API docs.
cmlenz
parents: 425
diff changeset
38 If an attribute name collides with a Python keyword, simply append an underscore
e7a4d43c438a Moved the `builder` document into the API docs.
cmlenz
parents: 425
diff changeset
39 to the name:
e7a4d43c438a Moved the `builder` document into the API docs.
cmlenz
parents: 425
diff changeset
40
e7a4d43c438a Moved the `builder` document into the API docs.
cmlenz
parents: 425
diff changeset
41 >>> doc(class_='intro')
e7a4d43c438a Moved the `builder` document into the API docs.
cmlenz
parents: 425
diff changeset
42 <Element "p">
e7a4d43c438a Moved the `builder` document into the API docs.
cmlenz
parents: 425
diff changeset
43 >>> print doc
e7a4d43c438a Moved the `builder` document into the API docs.
cmlenz
parents: 425
diff changeset
44 <p class="intro">Some text and <a href="http://example.org/">a link</a>.<br/></p>
e7a4d43c438a Moved the `builder` document into the API docs.
cmlenz
parents: 425
diff changeset
45
e7a4d43c438a Moved the `builder` document into the API docs.
cmlenz
parents: 425
diff changeset
46 As shown above, an `Element` can easily be directly rendered to XML text by
e7a4d43c438a Moved the `builder` document into the API docs.
cmlenz
parents: 425
diff changeset
47 printing it or using the Python ``str()`` function. This is basically a
e7a4d43c438a Moved the `builder` document into the API docs.
cmlenz
parents: 425
diff changeset
48 shortcut for converting the `Element` to a stream and serializing that
e7a4d43c438a Moved the `builder` document into the API docs.
cmlenz
parents: 425
diff changeset
49 stream:
e7a4d43c438a Moved the `builder` document into the API docs.
cmlenz
parents: 425
diff changeset
50
e7a4d43c438a Moved the `builder` document into the API docs.
cmlenz
parents: 425
diff changeset
51 >>> stream = doc.generate()
e7a4d43c438a Moved the `builder` document into the API docs.
cmlenz
parents: 425
diff changeset
52 >>> stream
e7a4d43c438a Moved the `builder` document into the API docs.
cmlenz
parents: 425
diff changeset
53 <genshi.core.Stream object at ...>
e7a4d43c438a Moved the `builder` document into the API docs.
cmlenz
parents: 425
diff changeset
54 >>> print stream
e7a4d43c438a Moved the `builder` document into the API docs.
cmlenz
parents: 425
diff changeset
55 <p class="intro">Some text and <a href="http://example.org/">a link</a>.<br/></p>
e7a4d43c438a Moved the `builder` document into the API docs.
cmlenz
parents: 425
diff changeset
56
e7a4d43c438a Moved the `builder` document into the API docs.
cmlenz
parents: 425
diff changeset
57
e7a4d43c438a Moved the `builder` document into the API docs.
cmlenz
parents: 425
diff changeset
58 The `tag` object also allows creating "fragments", which are basically lists
e7a4d43c438a Moved the `builder` document into the API docs.
cmlenz
parents: 425
diff changeset
59 of nodes (elements or text) that don't have a parent element. This can be useful
e7a4d43c438a Moved the `builder` document into the API docs.
cmlenz
parents: 425
diff changeset
60 for creating snippets of markup that are attached to a parent element later (for
e7a4d43c438a Moved the `builder` document into the API docs.
cmlenz
parents: 425
diff changeset
61 example in a template). Fragments are created by calling the `tag` object, which
e7a4d43c438a Moved the `builder` document into the API docs.
cmlenz
parents: 425
diff changeset
62 returns an object of type `Fragment`:
e7a4d43c438a Moved the `builder` document into the API docs.
cmlenz
parents: 425
diff changeset
63
e7a4d43c438a Moved the `builder` document into the API docs.
cmlenz
parents: 425
diff changeset
64 >>> fragment = tag('Hello, ', tag.em('world'), '!')
e7a4d43c438a Moved the `builder` document into the API docs.
cmlenz
parents: 425
diff changeset
65 >>> fragment
e7a4d43c438a Moved the `builder` document into the API docs.
cmlenz
parents: 425
diff changeset
66 <Fragment>
e7a4d43c438a Moved the `builder` document into the API docs.
cmlenz
parents: 425
diff changeset
67 >>> print fragment
e7a4d43c438a Moved the `builder` document into the API docs.
cmlenz
parents: 425
diff changeset
68 Hello, <em>world</em>!
e7a4d43c438a Moved the `builder` document into the API docs.
cmlenz
parents: 425
diff changeset
69 """
425
5b248708bbed Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents: 410
diff changeset
70
230
24757b771651 Renamed Markup to Genshi in repository.
cmlenz
parents: 182
diff changeset
71 from genshi.core import Attrs, Namespace, QName, Stream, START, END, TEXT
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
72
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
73 __all__ = ['Fragment', 'Element', 'tag']
425
5b248708bbed Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents: 410
diff changeset
74 __docformat__ = 'restructuredtext en'
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
75
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
76
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
77 class Fragment(object):
28
15e59a9cb362 More docstrings for the builder module.
cmlenz
parents: 27
diff changeset
78 """Represents a markup fragment, which is basically just a list of element
15e59a9cb362 More docstrings for the builder module.
cmlenz
parents: 27
diff changeset
79 or text nodes.
15e59a9cb362 More docstrings for the builder module.
cmlenz
parents: 27
diff changeset
80 """
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
81 __slots__ = ['children']
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
82
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
83 def __init__(self):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
84 self.children = []
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
85
65
5c024cf58ecb Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 34
diff changeset
86 def __add__(self, other):
5c024cf58ecb Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 34
diff changeset
87 return Fragment()(self, other)
5c024cf58ecb Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 34
diff changeset
88
5c024cf58ecb Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 34
diff changeset
89 def __call__(self, *args):
133
b9a0031d4bbb Minor cleanup and performance improvement for the builder module.
cmlenz
parents: 119
diff changeset
90 map(self.append, args)
65
5c024cf58ecb Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 34
diff changeset
91 return self
5c024cf58ecb Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 34
diff changeset
92
5c024cf58ecb Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 34
diff changeset
93 def __iter__(self):
98
bc73d3ab823f Bugfix in `builder` module: attribute values need to be converted to strings when generating streams.
cmlenz
parents: 94
diff changeset
94 return self._generate()
65
5c024cf58ecb Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 34
diff changeset
95
5c024cf58ecb Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 34
diff changeset
96 def __repr__(self):
5c024cf58ecb Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 34
diff changeset
97 return '<%s>' % self.__class__.__name__
5c024cf58ecb Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 34
diff changeset
98
5c024cf58ecb Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 34
diff changeset
99 def __str__(self):
5c024cf58ecb Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 34
diff changeset
100 return str(self.generate())
5c024cf58ecb Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 34
diff changeset
101
5c024cf58ecb Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 34
diff changeset
102 def __unicode__(self):
5c024cf58ecb Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 34
diff changeset
103 return unicode(self.generate())
5c024cf58ecb Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 34
diff changeset
104
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
105 def append(self, node):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
106 """Append an element or string as child node."""
379
d8c5045b547a The builder API now accepts streams as children of elements and fragments.
cmlenz
parents: 345
diff changeset
107 if isinstance(node, (Stream, Element, basestring, int, float, long)):
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
108 # For objects of a known/primitive type, we avoid the check for
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
109 # whether it is iterable for better performance
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
110 self.children.append(node)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
111 elif isinstance(node, Fragment):
133
b9a0031d4bbb Minor cleanup and performance improvement for the builder module.
cmlenz
parents: 119
diff changeset
112 self.children.extend(node.children)
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
113 elif node is not None:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
114 try:
133
b9a0031d4bbb Minor cleanup and performance improvement for the builder module.
cmlenz
parents: 119
diff changeset
115 map(self.append, iter(node))
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
116 except TypeError:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
117 self.children.append(node)
94
80c72e936e72 Some bugfixes and minor performance improvements for the builder module.
cmlenz
parents: 91
diff changeset
118
80c72e936e72 Some bugfixes and minor performance improvements for the builder module.
cmlenz
parents: 91
diff changeset
119 def _generate(self):
80c72e936e72 Some bugfixes and minor performance improvements for the builder module.
cmlenz
parents: 91
diff changeset
120 for child in self.children:
80c72e936e72 Some bugfixes and minor performance improvements for the builder module.
cmlenz
parents: 91
diff changeset
121 if isinstance(child, Fragment):
80c72e936e72 Some bugfixes and minor performance improvements for the builder module.
cmlenz
parents: 91
diff changeset
122 for event in child._generate():
80c72e936e72 Some bugfixes and minor performance improvements for the builder module.
cmlenz
parents: 91
diff changeset
123 yield event
379
d8c5045b547a The builder API now accepts streams as children of elements and fragments.
cmlenz
parents: 345
diff changeset
124 elif isinstance(child, Stream):
d8c5045b547a The builder API now accepts streams as children of elements and fragments.
cmlenz
parents: 345
diff changeset
125 for event in child:
d8c5045b547a The builder API now accepts streams as children of elements and fragments.
cmlenz
parents: 345
diff changeset
126 yield event
94
80c72e936e72 Some bugfixes and minor performance improvements for the builder module.
cmlenz
parents: 91
diff changeset
127 else:
80c72e936e72 Some bugfixes and minor performance improvements for the builder module.
cmlenz
parents: 91
diff changeset
128 if not isinstance(child, basestring):
80c72e936e72 Some bugfixes and minor performance improvements for the builder module.
cmlenz
parents: 91
diff changeset
129 child = unicode(child)
133
b9a0031d4bbb Minor cleanup and performance improvement for the builder module.
cmlenz
parents: 119
diff changeset
130 yield TEXT, child, (None, -1, -1)
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
131
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
132 def generate(self):
28
15e59a9cb362 More docstrings for the builder module.
cmlenz
parents: 27
diff changeset
133 """Return a markup event stream for the fragment."""
94
80c72e936e72 Some bugfixes and minor performance improvements for the builder module.
cmlenz
parents: 91
diff changeset
134 return Stream(self._generate())
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
135
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
136
345
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 230
diff changeset
137 def _value_to_unicode(value):
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 230
diff changeset
138 if isinstance(value, unicode):
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 230
diff changeset
139 return value
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 230
diff changeset
140 return unicode(value)
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 230
diff changeset
141
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 230
diff changeset
142 def _kwargs_to_attrs(kwargs):
403
32b283e1d310 Remove some magic/overhead from `Attrs` creation and manipulation by not automatically wrapping attribute names in `QName`.
cmlenz
parents: 379
diff changeset
143 return [(QName(k.rstrip('_').replace('_', '-')), _value_to_unicode(v))
345
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 230
diff changeset
144 for k, v in kwargs.items() if v is not None]
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 230
diff changeset
145
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 230
diff changeset
146
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
147 class Element(Fragment):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
148 """Simple XML output generator based on the builder pattern.
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
149
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
150 Construct XML elements by passing the tag name to the constructor:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
151
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
152 >>> print Element('strong')
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
153 <strong/>
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
154
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
155 Attributes can be specified using keyword arguments. The values of the
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
156 arguments will be converted to strings and any special XML characters
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
157 escaped:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
158
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
159 >>> print Element('textarea', rows=10, cols=60)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
160 <textarea rows="10" cols="60"/>
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
161 >>> print Element('span', title='1 < 2')
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
162 <span title="1 &lt; 2"/>
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
163 >>> print Element('span', title='"baz"')
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
164 <span title="&#34;baz&#34;"/>
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
165
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
166 The " character is escaped using a numerical entity.
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
167 The order in which attributes are rendered is undefined.
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
168
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
169 If an attribute value evaluates to `None`, that attribute is not included
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
170 in the output:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
171
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
172 >>> print Element('a', name=None)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
173 <a/>
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
174
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
175 Attribute names that conflict with Python keywords can be specified by
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
176 appending an underscore:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
177
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
178 >>> print Element('div', class_='warning')
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
179 <div class="warning"/>
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
180
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
181 Nested elements can be added to an element using item access notation.
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
182 The call notation can also be used for this and for adding attributes
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
183 using keyword arguments, as one would do in the constructor.
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
184
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
185 >>> print Element('ul')(Element('li'), Element('li'))
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
186 <ul><li/><li/></ul>
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
187 >>> print Element('a')('Label')
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
188 <a>Label</a>
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
189 >>> print Element('a')('Label', href="target")
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
190 <a href="target">Label</a>
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
191
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
192 Text nodes can be nested in an element by adding strings instead of
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
193 elements. Any special characters in the strings are escaped automatically:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
194
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
195 >>> print Element('em')('Hello world')
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
196 <em>Hello world</em>
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
197 >>> print Element('em')(42)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
198 <em>42</em>
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
199 >>> print Element('em')('1 < 2')
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
200 <em>1 &lt; 2</em>
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
201
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
202 This technique also allows mixed content:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
203
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
204 >>> print Element('p')('Hello ', Element('b')('world'))
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
205 <p>Hello <b>world</b></p>
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
206
34
ae293292cbb1 quotes should not be escaped inside text nodes
mgood
parents: 28
diff changeset
207 Quotes are not escaped inside text nodes:
ae293292cbb1 quotes should not be escaped inside text nodes
mgood
parents: 28
diff changeset
208 >>> print Element('p')('"Hello"')
ae293292cbb1 quotes should not be escaped inside text nodes
mgood
parents: 28
diff changeset
209 <p>"Hello"</p>
ae293292cbb1 quotes should not be escaped inside text nodes
mgood
parents: 28
diff changeset
210
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
211 Elements can also be combined with other elements or strings using the
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
212 addition operator, which results in a `Fragment` object that contains the
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
213 operands:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
214
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
215 >>> print Element('br') + 'some text' + Element('br')
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
216 <br/>some text<br/>
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
217
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
218 Elements with a namespace can be generated using the `Namespace` and/or
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
219 `QName` classes:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
220
230
24757b771651 Renamed Markup to Genshi in repository.
cmlenz
parents: 182
diff changeset
221 >>> from genshi.core import Namespace
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
222 >>> xhtml = Namespace('http://www.w3.org/1999/xhtml')
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
223 >>> print Element(xhtml.html, lang='en')
410
3460b04daeac Improve the handling of namespaces in serialization.
cmlenz
parents: 408
diff changeset
224 <html xmlns="http://www.w3.org/1999/xhtml" lang="en"/>
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
225 """
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
226 __slots__ = ['tag', 'attrib']
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
227
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
228 def __init__(self, tag_, **attrib):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
229 Fragment.__init__(self)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
230 self.tag = QName(tag_)
345
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 230
diff changeset
231 self.attrib = Attrs(_kwargs_to_attrs(attrib))
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
232
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
233 def __call__(self, *args, **kwargs):
345
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 230
diff changeset
234 self.attrib |= Attrs(_kwargs_to_attrs(kwargs))
98
bc73d3ab823f Bugfix in `builder` module: attribute values need to be converted to strings when generating streams.
cmlenz
parents: 94
diff changeset
235 Fragment.__call__(self, *args)
bc73d3ab823f Bugfix in `builder` module: attribute values need to be converted to strings when generating streams.
cmlenz
parents: 94
diff changeset
236 return self
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
237
65
5c024cf58ecb Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 34
diff changeset
238 def __repr__(self):
5c024cf58ecb Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 34
diff changeset
239 return '<%s "%s">' % (self.__class__.__name__, self.tag)
5c024cf58ecb Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 34
diff changeset
240
94
80c72e936e72 Some bugfixes and minor performance improvements for the builder module.
cmlenz
parents: 91
diff changeset
241 def _generate(self):
133
b9a0031d4bbb Minor cleanup and performance improvement for the builder module.
cmlenz
parents: 119
diff changeset
242 yield START, (self.tag, self.attrib), (None, -1, -1)
94
80c72e936e72 Some bugfixes and minor performance improvements for the builder module.
cmlenz
parents: 91
diff changeset
243 for kind, data, pos in Fragment._generate(self):
80c72e936e72 Some bugfixes and minor performance improvements for the builder module.
cmlenz
parents: 91
diff changeset
244 yield kind, data, pos
133
b9a0031d4bbb Minor cleanup and performance improvement for the builder module.
cmlenz
parents: 119
diff changeset
245 yield END, self.tag, (None, -1, -1)
94
80c72e936e72 Some bugfixes and minor performance improvements for the builder module.
cmlenz
parents: 91
diff changeset
246
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
247 def generate(self):
28
15e59a9cb362 More docstrings for the builder module.
cmlenz
parents: 27
diff changeset
248 """Return a markup event stream for the fragment."""
94
80c72e936e72 Some bugfixes and minor performance improvements for the builder module.
cmlenz
parents: 91
diff changeset
249 return Stream(self._generate())
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
250
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
251
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
252 class ElementFactory(object):
28
15e59a9cb362 More docstrings for the builder module.
cmlenz
parents: 27
diff changeset
253 """Factory for `Element` objects.
15e59a9cb362 More docstrings for the builder module.
cmlenz
parents: 27
diff changeset
254
15e59a9cb362 More docstrings for the builder module.
cmlenz
parents: 27
diff changeset
255 A new element is created simply by accessing a correspondingly named
15e59a9cb362 More docstrings for the builder module.
cmlenz
parents: 27
diff changeset
256 attribute of the factory object:
15e59a9cb362 More docstrings for the builder module.
cmlenz
parents: 27
diff changeset
257
15e59a9cb362 More docstrings for the builder module.
cmlenz
parents: 27
diff changeset
258 >>> factory = ElementFactory()
15e59a9cb362 More docstrings for the builder module.
cmlenz
parents: 27
diff changeset
259 >>> print factory.foo
15e59a9cb362 More docstrings for the builder module.
cmlenz
parents: 27
diff changeset
260 <foo/>
15e59a9cb362 More docstrings for the builder module.
cmlenz
parents: 27
diff changeset
261 >>> print factory.foo(id=2)
15e59a9cb362 More docstrings for the builder module.
cmlenz
parents: 27
diff changeset
262 <foo id="2"/>
15e59a9cb362 More docstrings for the builder module.
cmlenz
parents: 27
diff changeset
263
119
61b133ed95b2 Allow creating fragments from the `tag` object in `markup.builder`.
cmlenz
parents: 98
diff changeset
264 Markup fragments (lists of nodes without a parent element) can be created
61b133ed95b2 Allow creating fragments from the `tag` object in `markup.builder`.
cmlenz
parents: 98
diff changeset
265 by calling the factory:
61b133ed95b2 Allow creating fragments from the `tag` object in `markup.builder`.
cmlenz
parents: 98
diff changeset
266
61b133ed95b2 Allow creating fragments from the `tag` object in `markup.builder`.
cmlenz
parents: 98
diff changeset
267 >>> print factory('Hello, ', factory.em('world'), '!')
61b133ed95b2 Allow creating fragments from the `tag` object in `markup.builder`.
cmlenz
parents: 98
diff changeset
268 Hello, <em>world</em>!
61b133ed95b2 Allow creating fragments from the `tag` object in `markup.builder`.
cmlenz
parents: 98
diff changeset
269
28
15e59a9cb362 More docstrings for the builder module.
cmlenz
parents: 27
diff changeset
270 A factory can also be bound to a specific namespace:
15e59a9cb362 More docstrings for the builder module.
cmlenz
parents: 27
diff changeset
271
15e59a9cb362 More docstrings for the builder module.
cmlenz
parents: 27
diff changeset
272 >>> factory = ElementFactory('http://www.w3.org/1999/xhtml')
15e59a9cb362 More docstrings for the builder module.
cmlenz
parents: 27
diff changeset
273 >>> print factory.html(lang="en")
410
3460b04daeac Improve the handling of namespaces in serialization.
cmlenz
parents: 408
diff changeset
274 <html xmlns="http://www.w3.org/1999/xhtml" lang="en"/>
28
15e59a9cb362 More docstrings for the builder module.
cmlenz
parents: 27
diff changeset
275
15e59a9cb362 More docstrings for the builder module.
cmlenz
parents: 27
diff changeset
276 The namespace for a specific element can be altered on an existing factory
15e59a9cb362 More docstrings for the builder module.
cmlenz
parents: 27
diff changeset
277 by specifying the new namespace using item access:
15e59a9cb362 More docstrings for the builder module.
cmlenz
parents: 27
diff changeset
278
15e59a9cb362 More docstrings for the builder module.
cmlenz
parents: 27
diff changeset
279 >>> factory = ElementFactory()
15e59a9cb362 More docstrings for the builder module.
cmlenz
parents: 27
diff changeset
280 >>> print factory.html(factory['http://www.w3.org/2000/svg'].g(id=3))
410
3460b04daeac Improve the handling of namespaces in serialization.
cmlenz
parents: 408
diff changeset
281 <html><g xmlns="http://www.w3.org/2000/svg" id="3"/></html>
28
15e59a9cb362 More docstrings for the builder module.
cmlenz
parents: 27
diff changeset
282
15e59a9cb362 More docstrings for the builder module.
cmlenz
parents: 27
diff changeset
283 Usually, the `ElementFactory` class is not be used directly. Rather, the
15e59a9cb362 More docstrings for the builder module.
cmlenz
parents: 27
diff changeset
284 `tag` instance should be used to create elements.
15e59a9cb362 More docstrings for the builder module.
cmlenz
parents: 27
diff changeset
285 """
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
286
19
7b589453b797 Enable `ElementFactory` to create namespaced elements.
cmlenz
parents: 17
diff changeset
287 def __init__(self, namespace=None):
28
15e59a9cb362 More docstrings for the builder module.
cmlenz
parents: 27
diff changeset
288 """Create the factory, optionally bound to the given namespace.
15e59a9cb362 More docstrings for the builder module.
cmlenz
parents: 27
diff changeset
289
425
5b248708bbed Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents: 410
diff changeset
290 :param namespace: the namespace URI for any created elements, or `None`
5b248708bbed Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents: 410
diff changeset
291 for no namespace
28
15e59a9cb362 More docstrings for the builder module.
cmlenz
parents: 27
diff changeset
292 """
20
e3d3c1d8c98a Fix tests broken in [20].
cmlenz
parents: 19
diff changeset
293 if namespace and not isinstance(namespace, Namespace):
19
7b589453b797 Enable `ElementFactory` to create namespaced elements.
cmlenz
parents: 17
diff changeset
294 namespace = Namespace(namespace)
7b589453b797 Enable `ElementFactory` to create namespaced elements.
cmlenz
parents: 17
diff changeset
295 self.namespace = namespace
7b589453b797 Enable `ElementFactory` to create namespaced elements.
cmlenz
parents: 17
diff changeset
296
119
61b133ed95b2 Allow creating fragments from the `tag` object in `markup.builder`.
cmlenz
parents: 98
diff changeset
297 def __call__(self, *args):
61b133ed95b2 Allow creating fragments from the `tag` object in `markup.builder`.
cmlenz
parents: 98
diff changeset
298 return Fragment()(*args)
61b133ed95b2 Allow creating fragments from the `tag` object in `markup.builder`.
cmlenz
parents: 98
diff changeset
299
19
7b589453b797 Enable `ElementFactory` to create namespaced elements.
cmlenz
parents: 17
diff changeset
300 def __getitem__(self, namespace):
28
15e59a9cb362 More docstrings for the builder module.
cmlenz
parents: 27
diff changeset
301 """Return a new factory that is bound to the specified namespace."""
19
7b589453b797 Enable `ElementFactory` to create namespaced elements.
cmlenz
parents: 17
diff changeset
302 return ElementFactory(namespace)
7b589453b797 Enable `ElementFactory` to create namespaced elements.
cmlenz
parents: 17
diff changeset
303
7b589453b797 Enable `ElementFactory` to create namespaced elements.
cmlenz
parents: 17
diff changeset
304 def __getattr__(self, name):
28
15e59a9cb362 More docstrings for the builder module.
cmlenz
parents: 27
diff changeset
305 """Create an `Element` with the given name."""
19
7b589453b797 Enable `ElementFactory` to create namespaced elements.
cmlenz
parents: 17
diff changeset
306 return Element(self.namespace and self.namespace[name] or name)
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
307
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
308
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
309 tag = ElementFactory()
Copyright (C) 2012-2017 Edgewall Software