41
41
42
42
BASE_DIR : Path = Path (__file__ ).resolve ().parent
43
43
44
- templates : Jinja2Templates = Jinja2Templates (directory = str (Path (BASE_DIR , "templates" )))
44
+ templates : Jinja2Templates = Jinja2Templates (
45
+ directory = str (Path (BASE_DIR , "templates" )))
45
46
46
47
47
48
@app .post ("/file" )
@@ -85,7 +86,7 @@ async def get_paste_data(uuid: str, user_agent: Optional[str] = Header(None)) ->
85
86
if not is_browser_request :
86
87
# Return plain text response
87
88
return PlainTextResponse (content )
88
-
89
+
89
90
# Get file extension from the filename
90
91
file_extension : str = Path (path ).suffix [1 :]
91
92
if file_extension == "" :
@@ -99,10 +100,62 @@ async def get_paste_data(uuid: str, user_agent: Optional[str] = Header(None)) ->
99
100
lexer = get_lexer_by_name (
100
101
"text" , stripall = True ) # Default lexer
101
102
formatter = HtmlFormatter (
102
- style = "colorful" , full = True , linenos = "inline" )
103
+ style = "colorful" , full = True , linenos = "inline" , cssclass = 'code' )
103
104
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
+ """
104
157
return HTMLResponse (
105
- content = highlighted_code
158
+ content = response_content
106
159
)
107
160
except Exception as e :
108
161
print (e )
0 commit comments