Add raw rendering with syntax highlight
This commit is contained in:
parent
ebf111942f
commit
ea9ddf3e41
1 changed files with 73 additions and 32 deletions
43
sfb.py
43
sfb.py
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue