The dom.indent module

Functionality to pretty-print a DOM document, with good default indentation.

class Indenter(indent_width=2, start_indent=0, max_align_indent=16)[source]

Bases: object

Prints the indented output of a node.

Indentation preferences can be given on instantiation or by setting the attributes of the same name.

The default indent_width can be given, and the additional start_indent which is prepended to every output line, both in number of spaces and defaulting to 0.

The max_align_indent argument determines the number of spaces used at most to align indenting lines with text on previous lines. If the number is exceeded, the default indent_width is used instead on such lines.

Call write() to get the indented text output of a node.


the default indent width


the number of spaces to prepend to every output line


the maximum number of spaces to indent to align a line with certain text on the previous line


Get the indented output of the node.

Called by Element.write_indented().

output_node(node, index=-1)[source]

(Internal.) Output one node and its children.

The index, if given, is the index of this node in its parent. This is used to get additional indenting hints for the node.


(Internal.) Add whitespace, which is combined as soon as text is printed out.


(Internal.) Enter a new indent level for the node.


(Internal.) Leave the younghest indent level.


(Internal.) Get the current indent (including start_indent) in nr of spaces.


(Internal.) Go to a new line.


(Internal.) Output whitespace. Newlines start a new output line.

output_head(text, index=-1, override=None)[source]

(Internal.) Output head text.

The index, if given, is the index of the node in its parent. This is used to get additional indenting hints for the node.

If override is not None, and this head text happens to be the first on a new line, this value is used as the indent depth for this line, in stead of the current indent.


(Internal.) Output the tail text.