Spaces:
Sleeping
Sleeping
dhruv575 commited on
Commit ·
288912f
1
Parent(s): 3125086
In the post
Browse files- app/post_process.py +59 -1
app/post_process.py
CHANGED
|
@@ -201,6 +201,61 @@ def fix_story_box_headlines(html_content: str) -> str:
|
|
| 201 |
return html_content
|
| 202 |
|
| 203 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 204 |
def add_link_spacing(html_content: str) -> str:
|
| 205 |
"""Add spaces before specific links to ensure proper spacing in email clients.
|
| 206 |
|
|
@@ -278,7 +333,10 @@ def minify_html(html_content: str) -> str:
|
|
| 278 |
if "margin-top:0" in html_content:
|
| 279 |
print(f" Fixed story box headlines (added margin-top:0)")
|
| 280 |
|
| 281 |
-
# Step 5:
|
|
|
|
|
|
|
|
|
|
| 282 |
html_content = add_link_spacing(html_content)
|
| 283 |
|
| 284 |
final_size = len(html_content)
|
|
|
|
| 201 |
return html_content
|
| 202 |
|
| 203 |
|
| 204 |
+
def fix_unsubscribe_link(html_content: str) -> str:
|
| 205 |
+
"""Fix unsubscribe link href to use Customer.io merge tag.
|
| 206 |
+
|
| 207 |
+
Finds links containing "unsubscribe here" text and updates their href
|
| 208 |
+
from "#" (or empty) to {% manage_subscription_preferences_url %}.
|
| 209 |
+
"""
|
| 210 |
+
modified = False
|
| 211 |
+
|
| 212 |
+
# Pattern to match <a> tag with href="#" followed by "unsubscribe here" text
|
| 213 |
+
# Matches: <a href="#" ...>...unsubscribe here...</a>
|
| 214 |
+
# This pattern captures the full link tag including attributes and closing tag
|
| 215 |
+
pattern = r'(<a[^>]*href=["\']#["\'][^>]*>.*?unsubscribe\s+here.*?</a>)'
|
| 216 |
+
|
| 217 |
+
def replace_href(match):
|
| 218 |
+
link_tag = match.group(0)
|
| 219 |
+
# Replace href="#" with href="{% manage_subscription_preferences_url %}"
|
| 220 |
+
updated_link = re.sub(
|
| 221 |
+
r'href=["\']#["\']',
|
| 222 |
+
'href="{% manage_subscription_preferences_url %}"',
|
| 223 |
+
link_tag,
|
| 224 |
+
flags=re.IGNORECASE
|
| 225 |
+
)
|
| 226 |
+
return updated_link
|
| 227 |
+
|
| 228 |
+
if re.search(pattern, html_content, re.IGNORECASE | re.DOTALL):
|
| 229 |
+
html_content = re.sub(pattern, replace_href, html_content, flags=re.IGNORECASE | re.DOTALL)
|
| 230 |
+
modified = True
|
| 231 |
+
print(f" Fixed unsubscribe link href to {% manage_subscription_preferences_url %}")
|
| 232 |
+
|
| 233 |
+
# Also handle case where just "here" is the link text (after "unsubscribe" text)
|
| 234 |
+
# Pattern: <a href="#">here</a> when preceded by "unsubscribe" text
|
| 235 |
+
pattern2 = r'(unsubscribe\s+)(<a[^>]*href=["\']#["\'][^>]*>.*?here.*?</a>)'
|
| 236 |
+
def replace_href_here_only(match):
|
| 237 |
+
before_text = match.group(1)
|
| 238 |
+
link_tag = match.group(2)
|
| 239 |
+
# Replace href="#" with href="{% manage_subscription_preferences_url %}"
|
| 240 |
+
updated_link = re.sub(
|
| 241 |
+
r'href=["\']#["\']',
|
| 242 |
+
'href="{% manage_subscription_preferences_url %}"',
|
| 243 |
+
link_tag,
|
| 244 |
+
flags=re.IGNORECASE
|
| 245 |
+
)
|
| 246 |
+
return before_text + updated_link
|
| 247 |
+
|
| 248 |
+
if re.search(pattern2, html_content, re.IGNORECASE | re.DOTALL):
|
| 249 |
+
html_content = re.sub(pattern2, replace_href_here_only, html_content, flags=re.IGNORECASE | re.DOTALL)
|
| 250 |
+
modified = True
|
| 251 |
+
print(f" Fixed unsubscribe 'here' link href to {% manage_subscription_preferences_url %}")
|
| 252 |
+
|
| 253 |
+
if not modified:
|
| 254 |
+
print(f" No unsubscribe links found that need href fixing")
|
| 255 |
+
|
| 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 |
|
|
|
|
| 333 |
if "margin-top:0" in html_content:
|
| 334 |
print(f" Fixed story box headlines (added margin-top:0)")
|
| 335 |
|
| 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: Add spaces before links (ensures proper spacing in email clients)
|
| 340 |
html_content = add_link_spacing(html_content)
|
| 341 |
|
| 342 |
final_size = len(html_content)
|