Coverage for src / codeaudit / htmlhelpfunctions.py: 11%

44 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-04-09 09:33 +0200

1""" 

2License GPLv3 or higher. 

3 

4(C) 2025 Created by Maikel Mardjan - https://nocomplexity.com/ 

5 

6This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. 

7 

8This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. 

9 

10You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>. 

11 

12HTML helper functions for codeaudit 

13""" 

14 

15import json 

16from html import escape 

17 

18 

19def dict_to_html(data): 

20 """Creates simple HTML from a dict with values that are list: 

21 Example {'core_modules': ['os', 'hashlib', 'socket', 'logging.config', 'tarfile'], 

22 'imported_modules': ['linkaudit', 'pandas']} 

23 """ 

24 html_output = "" 

25 

26 if not isinstance(data, dict): 

27 html_output += "<p>None</p>\n" 

28 return html_output 

29 

30 for key, items in data.items(): 

31 # Check if items are missing, empty, or not iterable 

32 if not items or not isinstance(items, (list, tuple)): 

33 html_output += f"<h3>{key.capitalize()}</h3>\n - not found<ul>\n" 

34 html_output += "</ul>\n" 

35 continue 

36 

37 html_output += f"<h3>{key.capitalize()}</h3>\n<ul>\n" 

38 try: 

39 for item in items: 

40 html_output += f" <li>{item}</li>\n" 

41 except Exception: 

42 html_output += " <li>None</li>\n" 

43 html_output += "</ul>\n" 

44 

45 return html_output 

46 

47 

48def json_to_html(json_input): 

49 """ 

50 Takes a Python dictionary or JSON string and returns an HTML page 

51 that displays the JSON in a formatted and readable way. 

52 """ 

53 # Parse JSON string if needed 

54 # Parse if input is a JSON string 

55 if isinstance(json_input, str): 

56 json_data = json.loads(json_input) 

57 else: 

58 json_data = json_input 

59 

60 # Pretty-printed JSON string 

61 pretty_json = json.dumps(json_data, indent=2) 

62 

63 # Escape HTML characters for safe embedding inside <code> 

64 escaped_json = escape(pretty_json) 

65 

66 html_output = f'<div class="json-display">{escaped_json}</div>' 

67 return html_output 

68 

69 

70def dict_list_to_html_table(data): 

71 """ 

72 Converts a list of dictionaries to an HTML table string. 

73 :param data: List[Dict] - List of dicts with the same keys. 

74 :return: str - HTML table as string. 

75 """ 

76 if not data: 

77 return "<p><em>No data available</em></p>" 

78 

79 # Get column headers from the first dictionary 

80 headers = data[0].keys() 

81 

82 # Start the HTML table 

83 html = '<table border="1" cellpadding="5" cellspacing="0">\n' 

84 html += " <thead>\n <tr>\n" 

85 for header in headers: 

86 html += f" <th>{header}</th>\n" 

87 html += " </tr>\n </thead>\n <tbody>\n" 

88 

89 # Add rows 

90 for row in data: 

91 html += " <tr>\n" 

92 for header in headers: 

93 html += f' <td>{row.get(header, "")}</td>\n' 

94 html += " </tr>\n" 

95 

96 html += " </tbody>\n</table>" 

97 

98 return html