The dom.util module¶
Some utility functions.
-
whitespace_key(text)[source]¶ Return a key to determine the importance of the whitespace.
This is used by e.g. the
collapse_whitespace()function. A two-tuple is returned:(newlines, spaces), where the first value is the number of newlines in the text, and the second value the number of spaces.
-
collapse_whitespace(whitespaces)[source]¶ Return the “most important” whitespace of the specified strings.
This is used to combine whitespace requirements. For example, newlines are preferred over single spaces, and a single space is preferred over an empty string. For example:
>>> collapse_whitespace(['\n', ' ']) '\n' >>> collapse_whitespace([' ', '']) ' '
-
combine_text(fragments)[source]¶ Concatenate text fragments collapsing whitespace before and after the fragments.
fragmentsis an iterable of (before,text,after) tuples, wherebeforeandafterare whitespace. If atextis empty, the whitespace before and after are collapsed into the other surrounding whitespace. Returns a tree-tuple (before,text,after) containing the firstbeforevalue, the combinedtext, and the lastaftervalue.
-
add_newlines(node, text, block_separator='\n', max_blank_lines=10)[source]¶ Set whitespace properties of the node and all its descendents according to the original text.
Only nodes with an origin are affected. When a node appears on a new line, the
space_beforeproperty is set; when the tail part of a node appears on a new line, thespace_before_tailproperty is set.It is possible to set the
block_separator(by default a single newline); and the maximum amount of consecutive blank lines, usingmax_blank_lines.This can be useful before re-indenting or reformatting a document completely, to retain some aspects of the original formatting.
-
replace_unknown(tree, text)[source]¶ Replace all
Unknownnodes withTextnodes containing the respective text.The
textshould be the text the DOMtreewas generated from. Do this before writing out a node usingwrite()orwrite_indented()or related methods.