colored demo

This commit is contained in:
Yaossg 2025-03-10 00:46:24 +08:00
parent 1bcff515b7
commit e425464aab
5 changed files with 183 additions and 35 deletions

114
transcript.py Normal file
View file

@ -0,0 +1,114 @@
import sys
import html
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("# string table: "):
asm += line
break
else:
asm += decorate(line)
flip = True
for line in sys.stdin:
asm += line
title = 'RVBTCC Code Gen Demo'
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.replace("<%title%>", title).replace("<%src%>", src).replace("<%asm%>", asm))