Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -103,7 +103,6 @@ def build_full_html(markdown_text, styles, include_fontawesome):
|
|
103 |
{wrapper_id} th {{ background-color: #f2f2f2; }}
|
104 |
{wrapper_id} img {{ max-width: 100%; height: auto; }}
|
105 |
{wrapper_id} pre {{ padding: {styles.get('code_padding', '15')}px; border-radius: 5px; white-space: pre-wrap; word-wrap: break-word; }}
|
106 |
-
|
107 |
{wrapper_id} h1, {wrapper_id} h2, {wrapper_id} h3 {{ border-bottom: 1px solid #eee; padding-bottom: 5px; margin-top: 1.5em; }}
|
108 |
{wrapper_id} :not(pre) > code {{ font-family: 'Courier New', monospace; background-color: #eef; padding: .2em .4em; border-radius: 3px; }}
|
109 |
{pygments_css} {styles.get('custom_css', '')}
|
@@ -123,7 +122,7 @@ def build_full_html(markdown_text, styles, include_fontawesome):
|
|
123 |
|
124 |
return full_html
|
125 |
|
126 |
-
# --- API ENDPOINT for Conversion (
|
127 |
@app.route('/convert', methods=['POST'])
|
128 |
def convert_endpoint():
|
129 |
data = request.json
|
@@ -133,15 +132,18 @@ def convert_endpoint():
|
|
133 |
styles=data.get('styles', {}),
|
134 |
include_fontawesome=data.get('include_fontawesome', False)
|
135 |
)
|
|
|
|
|
|
|
136 |
if data.get('download', False):
|
137 |
download_type = data.get('download_type', 'png')
|
138 |
if download_type == 'html':
|
139 |
return send_file(BytesIO(full_html.encode("utf-8")), as_attachment=True, download_name="output.html", mimetype="text/html")
|
140 |
else:
|
141 |
-
png_bytes = imgkit.from_string(full_html, False, options=
|
142 |
return send_file(BytesIO(png_bytes), as_attachment=True, download_name="output.png", mimetype="image/png")
|
143 |
else:
|
144 |
-
png_bytes = imgkit.from_string(full_html, False, options=
|
145 |
png_base64 = base64.b64encode(png_bytes).decode('utf-8')
|
146 |
return jsonify({'preview_html': full_html, 'preview_png_base64': png_base64})
|
147 |
except Exception as e:
|
@@ -209,7 +211,6 @@ def index():
|
|
209 |
</div>
|
210 |
<div id="png-preview-container" class="preview-container"></div>
|
211 |
</div>
|
212 |
-
|
213 |
<script>
|
214 |
// --- All JavaScript is unchanged from the previous correct version ---
|
215 |
// It correctly gathers style info without modifying the parent page.
|
@@ -220,7 +221,6 @@ def index():
|
|
220 |
previewSection = document.getElementById('preview-section'), htmlPreviewContainer = document.getElementById('html-preview-container'),
|
221 |
pngPreviewContainer = document.getElementById('png-preview-container'), errorBox = document.getElementById('error-box'),
|
222 |
infoBox = document.getElementById('info-box');
|
223 |
-
|
224 |
function toggleAllComponents(checked) { componentsContainer.querySelectorAll('.component-checkbox').forEach(cb => cb.checked = checked); }
|
225 |
function displayError(message) { errorBox.textContent = message; errorBox.style.display = 'block'; previewSection.style.display = 'none'; }
|
226 |
function buildPayload() {
|
|
|
103 |
{wrapper_id} th {{ background-color: #f2f2f2; }}
|
104 |
{wrapper_id} img {{ max-width: 100%; height: auto; }}
|
105 |
{wrapper_id} pre {{ padding: {styles.get('code_padding', '15')}px; border-radius: 5px; white-space: pre-wrap; word-wrap: break-word; }}
|
|
|
106 |
{wrapper_id} h1, {wrapper_id} h2, {wrapper_id} h3 {{ border-bottom: 1px solid #eee; padding-bottom: 5px; margin-top: 1.5em; }}
|
107 |
{wrapper_id} :not(pre) > code {{ font-family: 'Courier New', monospace; background-color: #eef; padding: .2em .4em; border-radius: 3px; }}
|
108 |
{pygments_css} {styles.get('custom_css', '')}
|
|
|
122 |
|
123 |
return full_html
|
124 |
|
125 |
+
# --- API ENDPOINT for Conversion (CHANGED) ---
|
126 |
@app.route('/convert', methods=['POST'])
|
127 |
def convert_endpoint():
|
128 |
data = request.json
|
|
|
132 |
styles=data.get('styles', {}),
|
133 |
include_fontawesome=data.get('include_fontawesome', False)
|
134 |
)
|
135 |
+
# Define options here to avoid repetition and add the required fix
|
136 |
+
options = {"quiet": "", 'encoding': "UTF-8", "--no-cache": ""}
|
137 |
+
|
138 |
if data.get('download', False):
|
139 |
download_type = data.get('download_type', 'png')
|
140 |
if download_type == 'html':
|
141 |
return send_file(BytesIO(full_html.encode("utf-8")), as_attachment=True, download_name="output.html", mimetype="text/html")
|
142 |
else:
|
143 |
+
png_bytes = imgkit.from_string(full_html, False, options=options)
|
144 |
return send_file(BytesIO(png_bytes), as_attachment=True, download_name="output.png", mimetype="image/png")
|
145 |
else:
|
146 |
+
png_bytes = imgkit.from_string(full_html, False, options=options)
|
147 |
png_base64 = base64.b64encode(png_bytes).decode('utf-8')
|
148 |
return jsonify({'preview_html': full_html, 'preview_png_base64': png_base64})
|
149 |
except Exception as e:
|
|
|
211 |
</div>
|
212 |
<div id="png-preview-container" class="preview-container"></div>
|
213 |
</div>
|
|
|
214 |
<script>
|
215 |
// --- All JavaScript is unchanged from the previous correct version ---
|
216 |
// It correctly gathers style info without modifying the parent page.
|
|
|
221 |
previewSection = document.getElementById('preview-section'), htmlPreviewContainer = document.getElementById('html-preview-container'),
|
222 |
pngPreviewContainer = document.getElementById('png-preview-container'), errorBox = document.getElementById('error-box'),
|
223 |
infoBox = document.getElementById('info-box');
|
|
|
224 |
function toggleAllComponents(checked) { componentsContainer.querySelectorAll('.component-checkbox').forEach(cb => cb.checked = checked); }
|
225 |
function displayError(message) { errorBox.textContent = message; errorBox.style.display = 'block'; previewSection.style.display = 'none'; }
|
226 |
function buildPayload() {
|