File size: 5,055 Bytes
a074fa0
 
 
9bc382d
a074fa0
 
9bc382d
 
35151aa
a074fa0
 
35151aa
a074fa0
 
 
 
 
35151aa
a074fa0
9bc382d
a074fa0
 
 
 
 
35151aa
a074fa0
35151aa
a074fa0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35151aa
 
a074fa0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
from flask import Flask, request, render_template_string, send_file
import markdown
import imgkit
import os
import traceback
from io import BytesIO

app = Flask(__name__)

# Use a directory within the app's working directory to avoid permission issues
TEMP_DIR = os.path.join(os.getcwd(), "temp")

# Create temporary directory if it doesn't exist
try:
    os.makedirs(TEMP_DIR, exist_ok=True)
except Exception as e:
    print(f"Error creating temp directory: {e}")

@app.route("/", methods=["GET", "POST"])
def index():
    preview_html = None
    download_available = False
    download_type = "png"
    error_message = None
    markdown_text = request.form.get("markdown_text", "") if request.method == "POST" else ""

    if request.method == "POST" and markdown_text:
        try:
            # Convert Markdown to HTML
            html_content = markdown.markdown(markdown_text, extensions=['fenced_code', 'tables'])

            # Prepare HTML with basic styling
            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>
            """

            # Save HTML to a temporary file
            html_path = os.path.join(TEMP_DIR, "output.html")
            with open(html_path, "w", encoding="utf-8") as f:
                f.write(full_html)

            # Generate preview HTML
            preview_html = full_html
            download_available = True
            download_type = request.form.get("download_type", "png")

            if "download" in request.form:
                if download_type == "html":
                    return send_file(
                        html_path,
                        as_attachment=True,
                        download_name="output.html",
                        mimetype="text/html"
                    )
                else:  # PNG
                    # Convert HTML to PNG using imgkit
                    png_path = os.path.join(TEMP_DIR, "output.png")
                    imgkit.from_string(full_html, png_path, options={"quiet": ""})
                    return send_file(
                        png_path,
                        as_attachment=True,
                        download_name="output.png",
                        mimetype="image/png"
                    )

        except Exception as e:
            error_message = f"Error processing request: {str(e)}"
            print(f"Error: {traceback.format_exc()}")

    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: 15px; margin-top: 20px; }
            .download-btn { background-color: #4CAF50; color: white; border: none; cursor: pointer; }
            .download-btn:hover { background-color: #45a049; }
            .error { color: red; margin-top: 10px; }
        </style>
    </head>
    <body>
        <h1>Markdown to PNG/HTML Converter</h1>
        <form method="post">
            <textarea name="markdown_text" placeholder="Paste your Markdown here...">{{ 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 error_message %}
            <p class="error">{{ error_message }}</p>
        {% endif %}
        {% 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, error_message=error_message, markdown_text=markdown_text)

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=int(os.environ.get("PORT", 7860)))