|
|
from flask import Flask, request, render_template_string, send_file |
|
|
import markdown |
|
|
import imgkit |
|
|
import os |
|
|
from io import BytesIO |
|
|
import base64 |
|
|
|
|
|
app = Flask(__name__) |
|
|
|
|
|
|
|
|
os.makedirs("temp", exist_ok=True) |
|
|
|
|
|
@app.route("/", methods=["GET", "POST"]) |
|
|
def index(): |
|
|
preview_html = None |
|
|
download_available = False |
|
|
download_type = "png" |
|
|
file_path = None |
|
|
|
|
|
if request.method == "POST": |
|
|
markdown_text = request.form.get("markdown_text") |
|
|
download_type = request.form.get("download_type", "png") |
|
|
|
|
|
if markdown_text: |
|
|
|
|
|
html_content = markdown.markdown(markdown_text, extensions=['fenced_code', 'tables']) |
|
|
|
|
|
|
|
|
full_html = f""" |
|
|
<!DOCTYPE html> |
|
|
<html> |
|
|
<head> |
|
|
<style> |
|
|
body {{ font-family: Arial, sans-serif; padding: 20px; }} |
|
|
pre, code {{ background: #f4f4f4; padding: 10px; border-radius: 5px; }} |
|
|
table {{ border-collapse: collapse; width: 100%; }} |
|
|
th, td {{ border: 1px solid #ddd; padding: 8px; text-align: left; }} |
|
|
th {{ background-color: #f2f2f2; }} |
|
|
</style> |
|
|
</head> |
|
|
<body> |
|
|
{html_content} |
|
|
</body> |
|
|
</html> |
|
|
""" |
|
|
|
|
|
|
|
|
html_path = "temp/output.html" |
|
|
with open(html_path, "w") as f: |
|
|
f.write(full_html) |
|
|
|
|
|
|
|
|
preview_html = full_html |
|
|
download_available = True |
|
|
|
|
|
if download_type == "html": |
|
|
file_path = html_path |
|
|
else: |
|
|
|
|
|
png_path = "temp/output.png" |
|
|
imgkit.from_string(full_html, png_path, options={"quiet": ""}) |
|
|
file_path = png_path |
|
|
|
|
|
|
|
|
if "download" in request.form: |
|
|
return send_file( |
|
|
file_path, |
|
|
as_attachment=True, |
|
|
download_name=f"output.{download_type}", |
|
|
mimetype="text/html" if download_type == "html" else "image/png" |
|
|
) |
|
|
|
|
|
return render_template_string(""" |
|
|
<!DOCTYPE html> |
|
|
<html> |
|
|
<head> |
|
|
<title>Markdown to PNG/HTML Converter</title> |
|
|
<style> |
|
|
body { font-family: Arial, sans-serif; max-width: 800px; margin: 0 auto; padding: 20px; } |
|
|
textarea { width: 100%; height: 300px; margin-bottom: 10px; } |
|
|
select, button { padding: 10px; margin: 5px; } |
|
|
.preview { border: 1px solid #ddd; padding: 12px; margin-top: 20px; } |
|
|
.download-btn { background-color: #4CAF50; color: white; border: none; cursor: pointer; } |
|
|
.download-btn:hover { background-color: #45a049; } |
|
|
</style> |
|
|
</head> |
|
|
<body> |
|
|
<h1>Markdown to PNG/HTML Converter</h1> |
|
|
<form method="post"> |
|
|
<textarea name="markdown_text" placeholder="Paste your Markdown here...">{{ request.form.get('markdown_text', '') }}</textarea><br> |
|
|
<label for="download_type">Output format:</label> |
|
|
<select name="download_type"> |
|
|
<option value="png" {% if download_type == 'png' %}selected{% endif %}>PNG</option> |
|
|
<option value="html" {% if download_type == 'html' %}selected{% endif %}>HTML</option> |
|
|
</select><br> |
|
|
<button type="submit">Generate Preview</button> |
|
|
{% if download_available %} |
|
|
<button type="submit" name="download" value="true" class="download-btn">Download {{ download_type.upper() }}</button> |
|
|
{% endif %} |
|
|
</form> |
|
|
{% if preview_html %} |
|
|
<h2>Preview</h2> |
|
|
<div class="preview"> |
|
|
{{ preview_html | safe }} |
|
|
</div> |
|
|
{% endif %} |
|
|
</body> |
|
|
</html> |
|
|
""", preview_html=preview_html, download_available=download_available, download_type=download_type) |
|
|
|
|
|
if __name__ == "__main__": |
|
|
app.run(host="0.0.0.0", port=int(os.environ.get("PORT", 7860))) |