Module Reference¶
html5builder¶
Simple HTML5 document builder.
-
class
html5builder.HTML5Builder[source]¶ Bases:
objectSimple HTML5 generator class.
The following code generates a blank japanese HTML5 document.
>>> tag = HTML5Builder() >>> doc = tag.html([tag.head(''), tag.body('')], lang='ja') >>> str(tag.doctype + str(doc)) '<!DOCTYPE html><html lang="ja"><head></head><body></body></html>'
-
__getattr__(name)[source]¶ Return a callable element builder.
This special method is called through
self.name(children, **kwargs). This pseudo-method creates aHTML5Element. The pseudo-method name specifies the name of a generating element. The argumentchildrenand the keyword arguments are passed to theHTML5Elementinitializer aschildrenandattrsarguments, respectively. In the keyword arguments,clscan be used instead ofclass.>>> tag = HTML5Builder() >>> tag.a('anchor text', href='target.html') HTML5Element('a', ['anchor text'], {'href': 'target.html'}) >>> tag.img(src='image.png') HTML5Element('img', [], {'src': 'image.png'}) >>> tag.div('', cls='divclass') HTML5Element('div', [''], {'class': 'divclass'})
-
doctype= '<!DOCTYPE html>'¶ HTML5 Doctype string.
-
-
class
html5builder.HTML5Element(name, children, attrs)[source]¶ Bases:
objectSimple HTML5 element class.
An instance is usually created through
HTML5Builder.Parameters: - name (str) – The element name.
- children (sequence) – The sequence of element children.
- attrs (mapping) – The mapping of element attributes.
If
childrenis a string or non-sequence, the argument is the child of the instance. Ifchildrenis a sequence, the elements of the sequence are the children of the instance.-
name¶ The element name.
Type: str
-
child¶ The list of element children.
Type: list
-
attrs¶ The dictionary of element attributes.
Type: dict
-
__str__()[source]¶ Output a string of the outer HTML.
For each child,
str()is recursively called.>>> str(HTML5Element('span', 'inner', {})) '<span>inner</span>' >>> str(HTML5Element('div', '', {'class': 'divclass'})) '<div class="divclass"></div>' >>> str(HTML5Element('a', ... [HTML5Element('img', [], {'src': 'image.png'}), 'anchor text'], ... {'href': 'target.html'})) '<a href="target.html"><img src="image.png">anchor text</a>'