broadfield-dev commited on
Commit
ff9cf4e
·
verified ·
1 Parent(s): d47aaad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -13
app.py CHANGED
@@ -77,14 +77,14 @@ def parse_endpoint():
77
  except Exception as e:
78
  return jsonify({'error': f'Failed to parse: {e}'}), 500
79
 
80
- # --- HTML & PNG BUILDER (Unchanged) ---
81
  def build_full_html(markdown_text, styles, include_fontawesome):
82
  wrapper_id = "#output-wrapper"
83
  font_family = styles.get('font_family', "'Arial', sans-serif")
84
- google_font_name = font_family.split(',')[0].strip("'\"")
 
85
  google_font_link = ""
86
- if " " in google_font_name and google_font_name not in ["Times New Roman", "Courier New"]:
87
- google_font_link = f'<link href="https://fonts.googleapis.com/css2?family={google_font_name.replace(" ", "+")}:wght@400;700&display=swap" rel="stylesheet">'
88
 
89
  highlight_theme = styles.get('highlight_theme', 'default')
90
  pygments_css = ""
@@ -111,8 +111,6 @@ def build_full_html(markdown_text, styles, include_fontawesome):
111
  html_content = markdown.markdown(markdown_text, extensions=md_extensions, extension_configs={'codehilite': {'css_class': 'codehilite'}})
112
  final_html_body = f'<div id="output-wrapper">{html_content}</div>'
113
 
114
- fontawesome_link = '<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css">' if include_fontawesome else ""
115
-
116
  full_html = f"""<!DOCTYPE html>
117
  <html><head><meta charset="UTF-8">{google_font_link}{fontawesome_link}<style>
118
  #ouput-wrapper {{ background-color: {styles.get('background_color', '#fff')}; padding: 25px; display: inline-block;}}
@@ -121,7 +119,7 @@ def build_full_html(markdown_text, styles, include_fontawesome):
121
 
122
  return full_html
123
 
124
- # --- API ENDPOINT for Conversion (CHANGED) ---
125
  @app.route('/convert', methods=['POST'])
126
  def convert_endpoint():
127
  data = request.json
@@ -133,14 +131,11 @@ def convert_endpoint():
133
  include_fontawesome=data.get('include_fontawesome', False)
134
  )
135
 
136
- # *** THIS IS THE FINAL FIX ***
137
- # This tells wkhtmltoimage to never make external network calls, fixing the ProtocolUnknownError.
138
- # It also uses xvfb for stable headless rendering.
139
  options = {
140
  "quiet": "",
141
  'encoding': "UTF-8",
142
- 'xvfb': '',
143
- 'disable-external-links': ''
144
  }
145
 
146
  temp_html_path = os.path.join(TEMP_DIR, f"{uuid.uuid4()}.html")
@@ -315,4 +310,4 @@ def index():
315
  """, highlight_styles=highlight_styles)
316
 
317
  if __name__ == "__main__":
318
- app.run(host="0.0.0.0", port=int(os.environ.get("PORT", 7860)))
 
77
  except Exception as e:
78
  return jsonify({'error': f'Failed to parse: {e}'}), 500
79
 
80
+ # --- HTML & PNG BUILDER (CHANGED) ---
81
  def build_full_html(markdown_text, styles, include_fontawesome):
82
  wrapper_id = "#output-wrapper"
83
  font_family = styles.get('font_family', "'Arial', sans-serif")
84
+
85
+ # *** THIS IS THE FIX: Forcefully disable ALL external network calls ***
86
  google_font_link = ""
87
+ fontawesome_link = ""
 
88
 
89
  highlight_theme = styles.get('highlight_theme', 'default')
90
  pygments_css = ""
 
111
  html_content = markdown.markdown(markdown_text, extensions=md_extensions, extension_configs={'codehilite': {'css_class': 'codehilite'}})
112
  final_html_body = f'<div id="output-wrapper">{html_content}</div>'
113
 
 
 
114
  full_html = f"""<!DOCTYPE html>
115
  <html><head><meta charset="UTF-8">{google_font_link}{fontawesome_link}<style>
116
  #ouput-wrapper {{ background-color: {styles.get('background_color', '#fff')}; padding: 25px; display: inline-block;}}
 
119
 
120
  return full_html
121
 
122
+ # --- API ENDPOINT for Conversion (Unchanged) ---
123
  @app.route('/convert', methods=['POST'])
124
  def convert_endpoint():
125
  data = request.json
 
131
  include_fontawesome=data.get('include_fontawesome', False)
132
  )
133
 
134
+ # We need xvfb because wkhtmltoimage is a headless browser
 
 
135
  options = {
136
  "quiet": "",
137
  'encoding': "UTF-8",
138
+ 'xvfb': ''
 
139
  }
140
 
141
  temp_html_path = os.path.join(TEMP_DIR, f"{uuid.uuid4()}.html")
 
310
  """, highlight_styles=highlight_styles)
311
 
312
  if __name__ == "__main__":
313
+ app.run(host="0.0.0.0", port=int(os.environ.get("PORT", 7860)))```