Spaces:
Sleeping
Sleeping
dhruv575 commited on
Commit ·
2970179
1
Parent(s): 214db35
In the post
Browse files- app/email_new_converter.py +1 -0
- app/post_process.py +101 -1
app/email_new_converter.py
CHANGED
|
@@ -874,6 +874,7 @@ class EmailNewConverter:
|
|
| 874 |
return f"{parts[0]}/upload/{transformation}"
|
| 875 |
else:
|
| 876 |
# No transformations, insert them before the path
|
|
|
|
| 877 |
return f"{parts[0]}/upload/{transformation}/{path_part}"
|
| 878 |
|
| 879 |
return cloudinary_url
|
|
|
|
| 874 |
return f"{parts[0]}/upload/{transformation}"
|
| 875 |
else:
|
| 876 |
# No transformations, insert them before the path
|
| 877 |
+
# This handles both versioned URLs (v123/...) and non-versioned URLs
|
| 878 |
return f"{parts[0]}/upload/{transformation}/{path_part}"
|
| 879 |
|
| 880 |
return cloudinary_url
|
app/post_process.py
CHANGED
|
@@ -256,6 +256,103 @@ def fix_unsubscribe_link(html_content: str) -> str:
|
|
| 256 |
return html_content
|
| 257 |
|
| 258 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 259 |
def add_link_spacing(html_content: str) -> str:
|
| 260 |
"""Add spaces before specific links to ensure proper spacing in email clients.
|
| 261 |
|
|
@@ -336,7 +433,10 @@ def minify_html(html_content: str) -> str:
|
|
| 336 |
# Step 5: Fix unsubscribe link href (update from # to {% manage_subscription_preferences_url %})
|
| 337 |
html_content = fix_unsubscribe_link(html_content)
|
| 338 |
|
| 339 |
-
# Step 6:
|
|
|
|
|
|
|
|
|
|
| 340 |
html_content = add_link_spacing(html_content)
|
| 341 |
|
| 342 |
final_size = len(html_content)
|
|
|
|
| 256 |
return html_content
|
| 257 |
|
| 258 |
|
| 259 |
+
def fix_cloudinary_image_transformations(html_content: str) -> str:
|
| 260 |
+
"""Add Cloudinary transformations to market images that are missing them.
|
| 261 |
+
|
| 262 |
+
This ensures all market images (48px square images) have proper transformations
|
| 263 |
+
for cropping, while preserving aspect ratio for footer and top news images.
|
| 264 |
+
"""
|
| 265 |
+
modified = False
|
| 266 |
+
fixed_count = 0
|
| 267 |
+
|
| 268 |
+
# Pattern to match img tags with Cloudinary URLs
|
| 269 |
+
# Matches: <img ... src="https://res.cloudinary.com/.../upload/v123/..." ...>
|
| 270 |
+
pattern = r'(<img[^>]*src=["\'])(https://res\.cloudinary\.com/[^"\']+)(["\'][^>]*>)'
|
| 271 |
+
|
| 272 |
+
def add_transformations(match):
|
| 273 |
+
nonlocal modified, fixed_count
|
| 274 |
+
prefix = match.group(1)
|
| 275 |
+
url = match.group(2)
|
| 276 |
+
suffix = match.group(3)
|
| 277 |
+
full_tag = match.group(0)
|
| 278 |
+
|
| 279 |
+
# Check if URL already has transformations
|
| 280 |
+
has_transformations = "/upload/w_" in url or "/upload/c_" in url or "/upload/h_" in url
|
| 281 |
+
|
| 282 |
+
if has_transformations:
|
| 283 |
+
return full_tag # Already has transformations, skip
|
| 284 |
+
|
| 285 |
+
# Check if this is a market image (48px width) vs footer/top news
|
| 286 |
+
# Market images have width="48" or style containing "width:48px"
|
| 287 |
+
is_market_image = False
|
| 288 |
+
is_footer_or_top_news = False
|
| 289 |
+
|
| 290 |
+
# Check for width="48" attribute
|
| 291 |
+
if 'width="48"' in full_tag or "width='48'" in full_tag:
|
| 292 |
+
is_market_image = True
|
| 293 |
+
|
| 294 |
+
# Check for style with width:48px
|
| 295 |
+
if re.search(r'width\s*:\s*48px', full_tag, re.IGNORECASE):
|
| 296 |
+
is_market_image = True
|
| 297 |
+
|
| 298 |
+
# Check if it's a footer image (alt="Polymarket" or in footer section)
|
| 299 |
+
if 'alt="Polymarket"' in full_tag or 'alt=\'Polymarket\'' in full_tag:
|
| 300 |
+
is_footer_or_top_news = True
|
| 301 |
+
|
| 302 |
+
# Check if it's a top news image (inside top_news_box or alt="Top news image")
|
| 303 |
+
if 'alt="Top news image"' in full_tag or 'alt=\'Top news image\'' in full_tag:
|
| 304 |
+
is_footer_or_top_news = True
|
| 305 |
+
else:
|
| 306 |
+
# Check if it's inside a top_news_box by looking for the class in nearby HTML
|
| 307 |
+
img_pos = html_content.find(full_tag)
|
| 308 |
+
if img_pos != -1:
|
| 309 |
+
# Check if there's a top_news_box before this img tag (within 1000 chars)
|
| 310 |
+
before_img = html_content[max(0, img_pos - 1000):img_pos]
|
| 311 |
+
# Look for top_news_box class (handle both quoted and unquoted, with/without spaces)
|
| 312 |
+
if re.search(r'class\s*=\s*["\']?[^"\'>]*top_news_box', before_img, re.IGNORECASE):
|
| 313 |
+
is_footer_or_top_news = True
|
| 314 |
+
|
| 315 |
+
# Only add transformations to market images (48px square images)
|
| 316 |
+
if is_market_image and not is_footer_or_top_news:
|
| 317 |
+
# Extract target size from width attribute or style
|
| 318 |
+
target_size = 48 # Default for market images
|
| 319 |
+
|
| 320 |
+
# Try to extract from width attribute
|
| 321 |
+
width_match = re.search(r'width=["\'](\d+)["\']', full_tag, re.IGNORECASE)
|
| 322 |
+
if width_match:
|
| 323 |
+
target_size = int(width_match.group(1))
|
| 324 |
+
else:
|
| 325 |
+
# Try to extract from style
|
| 326 |
+
style_match = re.search(r'width\s*:\s*(\d+)px', full_tag, re.IGNORECASE)
|
| 327 |
+
if style_match:
|
| 328 |
+
target_size = int(style_match.group(1))
|
| 329 |
+
|
| 330 |
+
# Add transformations to the URL
|
| 331 |
+
transformation = f"w_{target_size},h_{target_size},c_fill,g_center,q_auto,f_auto"
|
| 332 |
+
|
| 333 |
+
# Insert transformations into the URL
|
| 334 |
+
if "/upload/" in url:
|
| 335 |
+
parts = url.split("/upload/", 1)
|
| 336 |
+
if len(parts) == 2:
|
| 337 |
+
path_part = parts[1]
|
| 338 |
+
new_url = f"{parts[0]}/upload/{transformation}/{path_part}"
|
| 339 |
+
modified = True
|
| 340 |
+
fixed_count += 1
|
| 341 |
+
return f"{prefix}{new_url}{suffix}"
|
| 342 |
+
|
| 343 |
+
return full_tag # No changes needed
|
| 344 |
+
|
| 345 |
+
# Process all img tags
|
| 346 |
+
html_content = re.sub(pattern, add_transformations, html_content, flags=re.IGNORECASE)
|
| 347 |
+
|
| 348 |
+
if modified:
|
| 349 |
+
print(f" Fixed {fixed_count} Cloudinary image URL(s) by adding transformations")
|
| 350 |
+
else:
|
| 351 |
+
print(f" No Cloudinary images found that need transformation fixes")
|
| 352 |
+
|
| 353 |
+
return html_content
|
| 354 |
+
|
| 355 |
+
|
| 356 |
def add_link_spacing(html_content: str) -> str:
|
| 357 |
"""Add spaces before specific links to ensure proper spacing in email clients.
|
| 358 |
|
|
|
|
| 433 |
# Step 5: Fix unsubscribe link href (update from # to {% manage_subscription_preferences_url %})
|
| 434 |
html_content = fix_unsubscribe_link(html_content)
|
| 435 |
|
| 436 |
+
# Step 6: Fix Cloudinary image URLs (add transformations to market images)
|
| 437 |
+
html_content = fix_cloudinary_image_transformations(html_content)
|
| 438 |
+
|
| 439 |
+
# Step 7: Add spaces before links (ensures proper spacing in email clients)
|
| 440 |
html_content = add_link_spacing(html_content)
|
| 441 |
|
| 442 |
final_size = len(html_content)
|