Coverage for lintro / utils / output / helpers.py: 100%

9 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2026-04-03 18:53 +0000

1"""Helper functions for output formatting. 

2 

3This module contains escape and sanitization helpers used by 

4output writing classes and functions. 

5""" 

6 

7import html 

8 

9 

10def markdown_escape(text: str) -> str: 

11 """Escape text for Markdown formatting. 

12 

13 Args: 

14 text: str: Text to escape. 

15 

16 Returns: 

17 str: Escaped text safe for Markdown. 

18 """ 

19 return text.replace("|", r"\|").replace("\n", " ") 

20 

21 

22def html_escape(text: str) -> str: 

23 """Escape text for HTML formatting. 

24 

25 Args: 

26 text: str: Text to escape. 

27 

28 Returns: 

29 str: Escaped text safe for HTML. 

30 """ 

31 return html.escape(text) 

32 

33 

34def sanitize_csv_value(value: str) -> str: 

35 """Sanitize CSV cell value to prevent formula injection. 

36 

37 Prefixes values starting with '=', '+', '-', or '@' with a single quote 

38 to prevent spreadsheet applications from interpreting them as formulas. 

39 

40 Args: 

41 value: str: The value to sanitize. 

42 

43 Returns: 

44 str: Sanitized value with leading quote if needed. 

45 """ 

46 if value and value.startswith(("=", "+", "-", "@")): 

47 return "'" + value 

48 return value