RVBTCC/transcript.py
2025-04-01 00:12:15 +08:00

111 lines
No EOL
2.5 KiB
Python

import sys
import html
from string import Template
src = ''
asm = ''
style = 0
flip = False
def decorate(line):
return f'<span class="style{style}">{html.escape(line)}</span>'
for line in sys.stdin:
if line.startswith("#@"):
line = line.removeprefix("#@")
src += decorate(line) if flip else line
style += 1
style %= 5
flip = False
elif line.startswith(".L") or line.startswith(".data") or line.startswith(".text"):
asm += line
else:
asm += decorate(line)
flip = True
title = 'RVBTCC Code Gen Demo'
template = Template('''
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>${title}</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
display: flex;
flex-direction: column;
align-items: center;
}
header {
background-color: #4CAF50;
color: white;
padding: 1em;
text-align: center;
width: 100%;
}
.container {
display: flex;
width: 80%;
margin: 2em 0;
}
.column {
flex: 1;
margin: 0 1em;
}
pre {
background-color: #f4f4f4;
padding: 1em;
border: 1px solid #ddd;
overflow-x: auto;
}
code {
font-family: 'Consolas', Consolas, monospace;
font-size: 20px;
}
.style0 {
background-color: rgba(255,129,130,0.4);
}
.style1 {
background-color: rgba(212,167,44,0.4);
}
.style2 {
background-color: rgba(74,194,107,0.4);
}
.style3 {
background-color: rgba(84,174,255,0.4);
}
.style4 {
background-color: rgba(194,151,255,0.4);
}
</style>
</head>
<body>
<header>
<h1>${title}</h1>
</header>
<div class="container">
<div class="column">
<h2>Source Code</h2>
<pre>
<code>${src}</code>
</pre>
</div>
<div class="column">
<h2>Assembly</h2>
<pre>
<code>${asm}</code>
</pre>
</div>
</div>
</body>
</html>
''')
print(template.substitute(title=title, src=src, asm=asm))