phml.utilities.misc.heading

Utility functions that do something with heading tags.

 1"""Utility functions that do something with heading tags."""
 2from re import match
 3
 4from phml.nodes import Element
 5
 6__all__ = ["heading_rank"]
 7
 8
 9def heading_rank(node: Element) -> int:
10    """Get the rank of the heading element.
11
12    Example:
13        `h2` yields `2`
14    """
15    from phml.utilities import is_heading  # pylint: disable=import-outside-toplevel
16
17    if is_heading(node):
18        rank = match(r"^h([1-6])$", node.tag)
19        if rank is not None:
20            return int(rank.group(1))
21
22    raise TypeError(f"Node must be a heading. Was a {node.type}.{node.tag}")
def heading_rank(node: phml.nodes.Element) -> int:
10def heading_rank(node: Element) -> int:
11    """Get the rank of the heading element.
12
13    Example:
14        `h2` yields `2`
15    """
16    from phml.utilities import is_heading  # pylint: disable=import-outside-toplevel
17
18    if is_heading(node):
19        rank = match(r"^h([1-6])$", node.tag)
20        if rank is not None:
21            return int(rank.group(1))
22
23    raise TypeError(f"Node must be a heading. Was a {node.type}.{node.tag}")

Get the rank of the heading element.

Example

h2 yields 2