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

9 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2022-12-08 13:16 -0600

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 import is_heading # pylint: disable=import-outside-toplevel 

16 

17 if is_heading(node): 

18 rank = match(r"h([1-6])", node.tag).group(1) 

19 return int(rank) 

20 

21 raise TypeError(f"Node must be a heading. Was a {node.type}.{node.tag}")