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

15 statements  

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

1from functools import cached_property 

2 

3from .literal import Literal 

4 

5 

6class Text(Literal): 

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

8 

9 Example: 

10 

11 ```html 

12 <span>Foxtrot</span> 

13 ``` 

14 

15 Yields: 

16 

17 ```javascript 

18 { 

19 type: 'element', 

20 tagName: 'span', 

21 properties: {}, 

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

23 } 

24 ``` 

25 """ 

26 

27 @cached_property 

28 def num_lines(self) -> int: 

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

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

31 

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

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

34 

35 Returns: 

36 str: Built html of text 

37 """ 

38 if self.parent is None or not any( 

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

40 ): 

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

42 for i, line in enumerate(lines): 

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

44 return "\n".join(lines) 

45 return self.value 

46 

47 def __repr__(self) -> str: 

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