Coverage for phml\utilities\misc\heading.py: 100%

10 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-04-06 16:35 -0500

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}")