phml.utilities.transform
utilities.transform
A collection of utilities focused around transforming an ast or a specific nodes.
1"""utilities.transform 2 3A collection of utilities focused around transforming an 4ast or a specific nodes. 5""" 6 7from .extract import * 8from .sanitize import * 9from .transform import * 10 11# __all__ = [ 12# "filter_nodes", 13# "remove_nodes", 14# "map_nodes", 15# "find_and_replace", 16# "shift_heading", 17# ] 18 19def normalize_indent(content: str, indent: int = 0) -> str: 20 """Normalize the indent between all lines. 21 22 Args: 23 content (str): The content to normalize the indent for 24 indent (bool): The amount of offset to add to each line after normalization. 25 26 Returns: 27 str: The normalized string 28 """ 29 from phml.core.formats.parse import strip_blank_lines # pylint: disable=import-outside-toplevel 30 from phml.utilities.misc import offset as spaces # pylint: disable=import-outside-toplevel 31 32 content = str(content).split("\n") 33 offset = len(content[0]) - len(content[0].lstrip()) 34 lines = [] 35 for line in content: 36 if len(line) > 0 and spaces(line) >= offset: 37 lines.append(" " * indent + line[offset:]) 38 else: 39 lines.append(line) 40 return "\n".join(strip_blank_lines(lines))
def
normalize_indent(content: str, indent: int = 0) -> str:
20def normalize_indent(content: str, indent: int = 0) -> str: 21 """Normalize the indent between all lines. 22 23 Args: 24 content (str): The content to normalize the indent for 25 indent (bool): The amount of offset to add to each line after normalization. 26 27 Returns: 28 str: The normalized string 29 """ 30 from phml.core.formats.parse import strip_blank_lines # pylint: disable=import-outside-toplevel 31 from phml.utilities.misc import offset as spaces # pylint: disable=import-outside-toplevel 32 33 content = str(content).split("\n") 34 offset = len(content[0]) - len(content[0].lstrip()) 35 lines = [] 36 for line in content: 37 if len(line) > 0 and spaces(line) >= offset: 38 lines.append(" " * indent + line[offset:]) 39 else: 40 lines.append(line) 41 return "\n".join(strip_blank_lines(lines))
Normalize the indent between all lines.
Args
- content (str): The content to normalize the indent for
- indent (bool): The amount of offset to add to each line after normalization.
Returns
str: The normalized string