Skip to content

Commit 78b2c86

Browse files
committed
refactor: added custom styling for syntax highlighting
1 parent da809bc commit 78b2c86

File tree

1 file changed

+57
-4
lines changed

1 file changed

+57
-4
lines changed

src/paste/main.py

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141

4242
BASE_DIR: Path = Path(__file__).resolve().parent
4343

44-
templates: Jinja2Templates = Jinja2Templates(directory=str(Path(BASE_DIR, "templates")))
44+
templates: Jinja2Templates = Jinja2Templates(
45+
directory=str(Path(BASE_DIR, "templates")))
4546

4647

4748
@app.post("/file")
@@ -85,7 +86,7 @@ async def get_paste_data(uuid: str, user_agent: Optional[str] = Header(None)) ->
8586
if not is_browser_request:
8687
# Return plain text response
8788
return PlainTextResponse(content)
88-
89+
8990
# Get file extension from the filename
9091
file_extension: str = Path(path).suffix[1:]
9192
if file_extension == "":
@@ -99,10 +100,62 @@ async def get_paste_data(uuid: str, user_agent: Optional[str] = Header(None)) ->
99100
lexer = get_lexer_by_name(
100101
"text", stripall=True) # Default lexer
101102
formatter = HtmlFormatter(
102-
style="colorful", full=True, linenos="inline")
103+
style="colorful", full=True, linenos="inline", cssclass='code')
103104
highlighted_code: str = highlight(content, lexer, formatter)
105+
106+
print(highlighted_code)
107+
custom_style = """
108+
.code pre span.linenos {
109+
color: #999;
110+
padding-right: 10px;
111+
-webkit-user-select: none;
112+
-webkit-touch-callout: none;
113+
-moz-user-select: none;
114+
-ms-user-select: none;
115+
user-select: none;
116+
}
117+
118+
span {
119+
font-size: 1.1em !important;
120+
}
121+
122+
pre {
123+
line-height: 1.4 !important;
124+
}
125+
126+
.code pre span.linenos::after {
127+
content: "";
128+
border-right: 1px solid #999;
129+
height: 100%;
130+
margin-left: 10px;
131+
}
132+
133+
.code {
134+
background-color: #fff;
135+
border: 1.5px solid #ddd;
136+
border-radius: 5px;
137+
margin-bottom: 20px;
138+
overflow: auto;
139+
}
140+
141+
pre {
142+
font-family: 'Consolas','Monaco','Andale Mono','Ubuntu Mono','monospace;' !important;
143+
}
144+
"""
145+
response_content: str = f"""
146+
<html>
147+
<head>
148+
<title>{uuid}</title>
149+
<style>{custom_style}</style>
150+
<style>{formatter.get_style_defs('.highlight')}</style>
151+
</head>
152+
<body>
153+
{highlighted_code}
154+
</body>
155+
</html>
156+
"""
104157
return HTMLResponse(
105-
content=highlighted_code
158+
content=response_content
106159
)
107160
except Exception as e:
108161
print(e)

0 commit comments

Comments
 (0)