Add raw rendering with syntax highlight

This commit is contained in:
Night Kaly 2025-01-16 10:54:39 +00:00
parent ebf111942f
commit ea9ddf3e41
Signed by: night0721
SSH key fingerprint: SHA256:B/hgVwUoBpx5vdNsXl9w8XwZljA9766uk6T4ubZp5HM

43
sfb.py
View file

@ -75,5 +75,46 @@ def get_file(id):
except Exception as e:
return jsonify({"error": str(e)}), 500
if __name__ == "__main__":
HTML_TEMPLATE = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{ id }}</title>
<style>
{{ style }}
</style>
</head>
<body>
<h1>File: {{ id }}</h1>
<pre>{{ content|safe }}</pre>
</body>
</html>
"""
@app.route("/<id>/raw", methods=["GET"])
def get_file_raw(id):
file_path = os.path.join(OUTPUT_DIR, id)
if not os.path.isfile(file_path):
return jsonify({"error": f"File '{id}' not found"}), 404
try:
with open(file_path, 'r') as file:
content = file.read()
lexer = guess_lexer_for_filename(file_path, content)
formatter = HtmlFormatter()
highlighted_content = highlight(content, lexer, formatter)
style = formatter.get_style_defs()
return render_template_string(
HTML_TEMPLATE,
id=id,
content=highlighted_content,
style=style,
)
except Exception as e:
return jsonify({"error": str(e)}), 500
app.run(host="0.0.0.0", port=PORT, debug=False)