42 lines
1.3 KiB
Python
42 lines
1.3 KiB
Python
# SPDX-FileCopyrightText: 2023-present Plane Software, Inc.
|
|
# SPDX-License-Identifier: LicenseRef-Plane-Commercial
|
|
#
|
|
# Licensed under the Plane Commercial License (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
# https://plane.so/legals/eula
|
|
#
|
|
# DO NOT remove or modify this notice.
|
|
# NOTICE: Proprietary and confidential. Unauthorized use or distribution is prohibited.
|
|
|
|
# Python imports
|
|
import re
|
|
|
|
# Django imports
|
|
from django.utils.html import strip_tags
|
|
|
|
|
|
def generate_plain_text_from_html(html_content):
|
|
"""
|
|
Generate clean plain text from HTML email template.
|
|
Removes all HTML tags, CSS styles, and excessive whitespace.
|
|
|
|
Args:
|
|
html_content (str): The HTML content to convert to plain text
|
|
|
|
Returns:
|
|
str: Clean plain text without HTML tags, styles, or excessive whitespace
|
|
"""
|
|
# Remove style tags and their content
|
|
html_content = re.sub(r"<style[^>]*>.*?</style>", "", html_content, flags=re.DOTALL | re.IGNORECASE)
|
|
|
|
# Strip HTML tags
|
|
text_content = strip_tags(html_content)
|
|
|
|
# Remove excessive empty lines
|
|
text_content = re.sub(r"\n\s*\n\s*\n+", "\n\n", text_content)
|
|
|
|
# Ensure there's a leading and trailing whitespace
|
|
text_content = "\n\n" + text_content.lstrip().rstrip() + "\n\n"
|
|
|
|
return text_content
|