-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog2html.py
More file actions
52 lines (48 loc) · 1.62 KB
/
Copy pathlog2html.py
File metadata and controls
52 lines (48 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import re
# Define a function to replace ANSI codes with HTML
def ansi_to_html_converter(ansi_text):
# Dictionary to map ANSI codes to HTML
ansi_to_html = {
'31': 'red', # ANSI red
'32': 'green', # ANSI green
'33': 'yellow', # ANSI yellow
'34': 'blue', # ANSI blue
'35': 'magenta', # ANSI magenta
'36': 'cyan', # ANSI cyan
}
# Function to replace ANSI codes with HTML
def replace_ansi(match):
color_code = match.group(1)
color = ansi_to_html.get(color_code, 'black')
return f'<span style="color:{color}; font-weight: bold;">'
# Remove/reset ANSI codes at the end of color blocks
ansi_text = re.sub(r'\033\[0m', '</span>', ansi_text)
# Replace ANSI color codes with HTML spans
ansi_text = re.sub(r'\033\[([0-9]+)m', replace_ansi, ansi_text)
ansi_text = ansi_text.replace("\n", "\n<br>")
ansi_text = ansi_text.replace(" ", " ")
return f"""
<html>
<head>
<style>
body {{
font-family: monospace;
font-size: 20px;
background-color: #f4f4f4;
margin: 40px;
padding: 20px;
border: 1px solid #ccc;
border-radius: 5px;
line-height: 1.5;
text-align: left;
}}
strong {{
font-weight: bold;
}}
</style>
</head>
<body>
<strong>{ansi_text}</strong>
</body>
</html>
"""