broadfield-dev commited on
Commit
f6cda11
·
verified ·
1 Parent(s): 4e4077d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +98 -0
app.py ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request, render_template, send_file
2
+ import markdown
3
+ from weasyprint import HTML
4
+ import os
5
+ from io import BytesIO
6
+
7
+ app = Flask(__name__)
8
+
9
+ # Ensure a temporary directory exists for saving files
10
+ os.makedirs("temp", exist_ok=True)
11
+
12
+ @app.route("/", methods=["GET", "POST"])
13
+ def index():
14
+ if request.method == "POST":
15
+ markdown_text = request.form.get("markdown_text")
16
+ if markdown_text:
17
+ # Convert Markdown to HTML
18
+ html_content = markdown.markdown(markdown_text, extensions=['fenced_code', 'tables'])
19
+
20
+ # Prepare HTML with basic styling
21
+ full_html = f"""
22
+ <!DOCTYPE html>
23
+ <html>
24
+ <head>
25
+ <style>
26
+ body {{ font-family: Arial, sans-serif; padding: 20px; }}
27
+ pre, code {{ background: #f4f4f4; padding: 10px; border-radius: 5px; }}
28
+ table {{ border-collapse: collapse; width: 100%; }}
29
+ th, td {{ border: 1px solid #ddd; padding: 8px; text-align: left; }}
30
+ th {{ background-color: #f2f2f2; }}
31
+ </style>
32
+ </head>
33
+ <body>
34
+ {html_content}
35
+ </body>
36
+ </html>
37
+ """
38
+
39
+ # Save HTML to a temporary file
40
+ html_path = "temp/output.html"
41
+ with open(html_path, "w") as f:
42
+ f.write(full_html)
43
+
44
+ # Convert HTML to PNG using WeasyPrint
45
+ png_io = BytesIO()
46
+ HTML(string=full_html).write_png(png_io)
47
+ png_io.seek(0)
48
+
49
+ # Save PNG to a temporary file
50
+ png_path = "temp/output.png"
51
+ with open(png_path, "wb") as f:
52
+ f.write(png_io.getvalue())
53
+
54
+ # Determine which file to download based on user selection
55
+ download_type = request.form.get("download_type")
56
+ if download_type == "html":
57
+ return send_file(
58
+ html_path,
59
+ as_attachment=True,
60
+ download_name="output.html",
61
+ mimetype="text/html"
62
+ )
63
+ else: # Default to PNG
64
+ return send_file(
65
+ png_path,
66
+ as_attachment=True,
67
+ download_name="output.png",
68
+ mimetype="image/png"
69
+ )
70
+
71
+ return render_template_string("""
72
+ <!DOCTYPE html>
73
+ <html>
74
+ <head>
75
+ <title>Markdown to PNG/HTML Converter</title>
76
+ <style>
77
+ body { font-family: Arial, sans-serif; max-width: 800px; margin: 0 auto; padding: 20px; }
78
+ textarea { width: 100%; height: 300px; margin-bottom: 10px; }
79
+ select, button { padding: 10px; margin: 5px; }
80
+ </style>
81
+ </head>
82
+ <body>
83
+ <h1>Markdown to PNG/HTML Converter</h1>
84
+ <form method="post">
85
+ <textarea name="markdown_text" placeholder="Paste your Markdown here..."></textarea><br>
86
+ <label for="download_type">Download as:</label>
87
+ <select name="download_type">
88
+ <option value="png">PNG</option>
89
+ <option value="html">HTML</option>
90
+ </select><br>
91
+ <button type="submit">Convert and Download</button>
92
+ </form>
93
+ </body>
94
+ </html>
95
+ """)
96
+
97
+ if __name__ == "__main__":
98
+ app.run(debug=True)