Spaces:
Running
Running
feat: add HTML normalization and style fixing functions for improved preview rendering
Browse files
utils.py
CHANGED
|
@@ -1,9 +1,7 @@
|
|
| 1 |
-
import base64
|
| 2 |
-
|
| 3 |
-
|
| 4 |
import cairosvg
|
| 5 |
import io
|
| 6 |
import base64
|
|
|
|
| 7 |
from PIL import Image
|
| 8 |
|
| 9 |
|
|
@@ -46,3 +44,74 @@ def svg_to_png_base64(svg_content: str, width=800, height=800) -> tuple[str, str
|
|
| 46 |
except Exception as e:
|
| 47 |
print(f"Error converting SVG to PNG: {e}")
|
| 48 |
return "", ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import cairosvg
|
| 2 |
import io
|
| 3 |
import base64
|
| 4 |
+
import re
|
| 5 |
from PIL import Image
|
| 6 |
|
| 7 |
|
|
|
|
| 44 |
except Exception as e:
|
| 45 |
print(f"Error converting SVG to PNG: {e}")
|
| 46 |
return "", ""
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def normalize_html_content(html_content: str) -> str:
|
| 50 |
+
"""
|
| 51 |
+
Normalizes the body and svg styles in the HTML content to fixed values.
|
| 52 |
+
"""
|
| 53 |
+
fixed_body_style = """body {
|
| 54 |
+
margin: 0;
|
| 55 |
+
padding: 20px;
|
| 56 |
+
display: flex;
|
| 57 |
+
justify-content: center;
|
| 58 |
+
align-items: center;
|
| 59 |
+
min-height: 100%;
|
| 60 |
+
overflow: hidden;
|
| 61 |
+
}"""
|
| 62 |
+
|
| 63 |
+
fixed_svg_style = """svg {
|
| 64 |
+
max-width: 400px;
|
| 65 |
+
height: auto;
|
| 66 |
+
border-radius: 10px;
|
| 67 |
+
}"""
|
| 68 |
+
|
| 69 |
+
if '<meta name="viewport"' not in html_content:
|
| 70 |
+
html_content = html_content.replace(
|
| 71 |
+
"<head>",
|
| 72 |
+
'<head>\n<meta name="viewport" content="width=device-width, initial-scale=1.0">',
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
body_pattern = r"body\s*\{[^}]*\}"
|
| 76 |
+
html_content = re.sub(
|
| 77 |
+
body_pattern,
|
| 78 |
+
fixed_body_style,
|
| 79 |
+
html_content,
|
| 80 |
+
flags=re.MULTILINE | re.DOTALL | re.IGNORECASE,
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
# Match and replace svg style pattern
|
| 84 |
+
svg_pattern = r"svg\s*\{[^}]*\}"
|
| 85 |
+
html_content = re.sub(
|
| 86 |
+
svg_pattern,
|
| 87 |
+
fixed_svg_style,
|
| 88 |
+
html_content,
|
| 89 |
+
flags=re.MULTILINE | re.DOTALL | re.IGNORECASE,
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
return html_content
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
def fix_html_styles_for_preview(html_content: str) -> str:
|
| 96 |
+
"""
|
| 97 |
+
Main function to modify HTML styles for preview purposes.
|
| 98 |
+
"""
|
| 99 |
+
if not html_content or not html_content.strip():
|
| 100 |
+
return html_content
|
| 101 |
+
|
| 102 |
+
try:
|
| 103 |
+
normalized_html = normalize_html_content(html_content)
|
| 104 |
+
|
| 105 |
+
if "<!DOCTYPE html>" not in normalized_html:
|
| 106 |
+
normalized_html = "<!DOCTYPE html>\n" + normalized_html
|
| 107 |
+
|
| 108 |
+
if "<meta charset=" not in normalized_html:
|
| 109 |
+
normalized_html = normalized_html.replace(
|
| 110 |
+
"<head>", '<head>\n<meta charset="utf-8">'
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
return normalized_html
|
| 114 |
+
|
| 115 |
+
except Exception as e:
|
| 116 |
+
print(f"Error occurred while modifying HTML styles: {e}")
|
| 117 |
+
return html_content
|