Coverage for phml\nodes\text.py: 44%

16 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2022-11-30 09:38 -0600

1from functools import cached_property 

2from typing import Iterator 

3 

4from .literal import Literal 

5 

6 

7class Text(Literal): 

8 """Text (Literal) represents a Text ([DOM]). 

9 

10 Example: 

11 

12 ```html 

13 <span>Foxtrot</span> 

14 ``` 

15 

16 Yields: 

17 

18 ```javascript 

19 { 

20 type: 'element', 

21 tagName: 'span', 

22 properties: {}, 

23 children: [{type: 'text', value: 'Foxtrot'}] 

24 } 

25 ``` 

26 """ 

27 

28 @cached_property 

29 def num_lines(self) -> int: 

30 """Determine the number of lines the text has.""" 

31 return len([line for line in self.value.split("\n") if line.strip() != ""]) 

32 

33 def stringify(self, indent: int = 0) -> str: 

34 """Build indented html string of html text. 

35 

36 Returns: 

37 str: Built html of text 

38 """ 

39 if self.parent is None or not any( 

40 tag in self.get_ancestry() for tag in ["pre", "python", "script", "style"] 

41 ): 

42 lines = [line.lstrip() for line in self.value.split("\n") if line.strip() != ""] 

43 for i, line in enumerate(lines): 

44 lines[i] = (' ' * indent) + line 

45 return "\n".join(lines) 

46 return self.value 

47 

48 def __repr__(self) -> str: 

49 return f"literal.text('{self.value}')"