Module Reference

html5builder

Simple HTML5 document builder.

class html5builder.HTML5Builder[source]

Bases: object

Simple 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 a HTML5Element. The pseudo-method name specifies the name of a generating element. The argument children and the keyword arguments are passed to the HTML5Element initializer as children and attrs arguments, respectively. In the keyword arguments, cls can be used instead of class.

>>> 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: object

Simple 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 children is a string or non-sequence, the argument is the child of the instance. If children is 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
__getitem__(key)[source]

Syntax sugar for self.attr[key].

__setitem__(key, value)[source]

Syntax sugar for self.attr[key]=.

__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>'