mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-02-05 10:53:28 -05:00
more
This commit is contained in:
@@ -1,36 +1,68 @@
|
||||
import mistune
|
||||
from notifications_utils import MAGIC_SEQUENCE, magic_sequence_regex
|
||||
from notifications_utils.formatters import create_sanitised_html_for_url
|
||||
import re
|
||||
from itertools import count
|
||||
import html
|
||||
|
||||
LINK_STYLE = "word-wrap: break-word; color: #1D70B8;"
|
||||
|
||||
|
||||
def escape_plus_lists(markdown_text):
|
||||
return re.sub(r"(?m)^(\+)(?=\s)", r"\\\1", markdown_text)
|
||||
|
||||
|
||||
def autolinkify(text):
|
||||
# url_pattern = re.compile(r"""(?<!\]\()(?<!["'])\b(https?://[^\s<>()]+)""")
|
||||
url_pattern = re.compile(
|
||||
r"""(?<!\]\()
|
||||
(?<!href=["'])
|
||||
\b(https?://[^\s<>"')\]]+)""",
|
||||
re.VERBOSE,
|
||||
)
|
||||
|
||||
def replacer(match):
|
||||
url = match.group(0)
|
||||
return f"[{url}]({url})"
|
||||
|
||||
return url_pattern.sub(replacer, text)
|
||||
|
||||
|
||||
class EmailRenderer(mistune.HTMLRenderer):
|
||||
|
||||
def table(self, header, body):
|
||||
return ""
|
||||
|
||||
def table_row(self, content):
|
||||
return ""
|
||||
|
||||
def table_cell(self, content, **kwargs):
|
||||
return ""
|
||||
|
||||
def heading(self, text, level):
|
||||
if level == 1:
|
||||
return (
|
||||
'<h2 style="Margin: 0 0 20px 0; padding: 0; '
|
||||
'font-size: 27px; line-heigh: 35px; font-weight: bold; color: #0B0C0C;">'
|
||||
'font-size: 27px; line-height: 35px; font-weight: bold; color: #0B0C0C;">'
|
||||
f"{text}</h2>"
|
||||
)
|
||||
return self.paragraph(text)
|
||||
|
||||
def paragraph(self, text):
|
||||
if text.strip():
|
||||
text = html.unescape(text)
|
||||
return (
|
||||
'<p style="Margin: 0 0 20px 0; font-size: 19px; '
|
||||
'line-height: 25px; color: #0B0C0C;">' + text + '</p>'
|
||||
'line-height: 25px; color: #0B0C0C;">' + text + "</p>"
|
||||
)
|
||||
|
||||
def emphasis(self, text):
|
||||
return f"*{text}*"
|
||||
|
||||
def strong(self, text):
|
||||
return f"**{text}**"
|
||||
|
||||
def block_code(self, code, info=None):
|
||||
return code.strip()
|
||||
|
||||
def block_quote(self, text):
|
||||
return (
|
||||
'<blockquote style="Margin: 0 0 20px 0; border-left: 10px solid #B1B4B6; '
|
||||
@@ -41,33 +73,36 @@ class EmailRenderer(mistune.HTMLRenderer):
|
||||
def thematic_break(self):
|
||||
return '<hr style="border: 0; height: 1px; background: #B1B4B6; Margin: 30px 0 30px 0;">'
|
||||
|
||||
|
||||
def codespan(self, text):
|
||||
return (
|
||||
f"`{text}`"
|
||||
)
|
||||
return f"`{text}`"
|
||||
|
||||
def linebreak(self):
|
||||
return "<br />"
|
||||
|
||||
def list(self, text, ordered, level=None, start=None, **kwargs):
|
||||
def newline(self):
|
||||
return self.linebreak()
|
||||
|
||||
def list(self, text, ordered, level=None, **kwargs):
|
||||
tag = "ol" if ordered else "ul"
|
||||
style = (
|
||||
'list-style-type: decimal;' if ordered else 'list-style-type: disc;'
|
||||
)
|
||||
style = "list-style-type: decimal;" if ordered else "list-style-type: disc;"
|
||||
return (
|
||||
'<table role="presentation" style="padding 0 0 20px 0;"><tr<td style="font-family: Helvetica, Arial, sans-serif;">'
|
||||
'<table role="presentation" style="padding 0 0 20px 0;">'
|
||||
'<tr><td style="font-family: Helvetica, Arial, sans-serif;">'
|
||||
f'<{tag} style="Margin: 0 0 0 20px; padding: 0; {style}">{text}</{tag}>'
|
||||
'</td></tr></table'
|
||||
"</td></tr></table>"
|
||||
)
|
||||
|
||||
def list_item(self, text, level=None):
|
||||
return (
|
||||
'<li style="Margin: 5px 0 5px; padding: 0 0 0 5px; font-size: 19px;'
|
||||
'line-height: 25px; color: #0B0C0C;">' + text.strip() + '</li>'
|
||||
'line-height: 25px; color: #0B0C0C;">' + text.strip() + "</li>"
|
||||
)
|
||||
|
||||
def link(self, link=None, text=None, title=None, url=None, **kwargs):
|
||||
href = url or (link if link and link.startswith("http://", "https://") else "")
|
||||
|
||||
href = html.escape(
|
||||
url or (link if link and link.startswith("http://", "https://") else "")
|
||||
)
|
||||
display_text = text or link or href or ""
|
||||
title_attr = f' title="{title}"' if title else ""
|
||||
return f'<a style="{LINK_STYLE}" href="{href}"{title_attr}>{display_text}</a>'
|
||||
@@ -82,12 +117,22 @@ class EmailRenderer(mistune.HTMLRenderer):
|
||||
return (
|
||||
'<p style="Margin: 0 0 20px 0; font-size: 19px; line-height: 25px; color: #0B0C0C;">'
|
||||
f"~~{text}~~"
|
||||
'</p>'
|
||||
"</p>"
|
||||
)
|
||||
|
||||
|
||||
class PlainTextRenderer(mistune.HTMLRenderer):
|
||||
COLUMN_WIDTH = 65
|
||||
|
||||
def table(self, header, body):
|
||||
return ""
|
||||
|
||||
def table_row(self, content):
|
||||
return ""
|
||||
|
||||
def table_cell(self, content, **kwargs):
|
||||
return ""
|
||||
|
||||
def heading(self, text, level):
|
||||
if level == 1:
|
||||
return f"\n\n\n{text}\n{'-' * self.COLUMN_WIDTH}"
|
||||
@@ -99,17 +144,14 @@ class PlainTextRenderer(mistune.HTMLRenderer):
|
||||
return ""
|
||||
|
||||
def thematic_break(self):
|
||||
return f"\n\n{'=' * self.COLUMN_WIDTH}\n"
|
||||
|
||||
def heading(self, text, level):
|
||||
print(f"TEXT {text} LEVEL {level}")
|
||||
if level == 1:
|
||||
return f"\n\n\n{text}\n{'-' * self.COLUMN_WIDTH}"
|
||||
return self.paragraph(text)
|
||||
return f"\n\n{'=' * self.COLUMN_WIDTH}"
|
||||
|
||||
def block_quote(self, text):
|
||||
return text
|
||||
|
||||
def block_code(self, code, info=None):
|
||||
return code.strip()
|
||||
|
||||
def linebreak(self):
|
||||
return "\n"
|
||||
|
||||
@@ -147,6 +189,7 @@ class PlainTextRenderer(mistune.HTMLRenderer):
|
||||
def strikethrough(self, text):
|
||||
return f"~~{text}~~"
|
||||
|
||||
|
||||
class PreheaderRenderer(PlainTextRenderer):
|
||||
def heading(self, text, level):
|
||||
return self.paragraph(text)
|
||||
@@ -157,7 +200,6 @@ class PreheaderRenderer(PlainTextRenderer):
|
||||
def link(self, link, text=None, title=None):
|
||||
return text or link
|
||||
|
||||
|
||||
def image(self, src, alt="", title=None, url=None):
|
||||
return ""
|
||||
|
||||
@@ -173,14 +215,13 @@ class LetterPreviewRenderer(mistune.HTMLRenderer):
|
||||
return f"<p>{text}</p>"
|
||||
return ""
|
||||
|
||||
|
||||
def block_code(self, code, info=None):
|
||||
return code.strip()
|
||||
|
||||
def link(self, link, text=None, title=None, url=None):
|
||||
href = url
|
||||
display_text = text or link
|
||||
print(f"LINKE {link} URL {url} HREF {href}")
|
||||
return f"{display_text}: <strong>{href.replace('http://', '').replace('https://', '')}</strong>"
|
||||
#return f"{text}: {link}"
|
||||
|
||||
def autolink(self, link, is_email=False):
|
||||
return f"<strong>{link.replace('http://', '')}.replace(https://', '')</strong>"
|
||||
@@ -213,8 +254,22 @@ class LetterPreviewRenderer(mistune.HTMLRenderer):
|
||||
return "<br>"
|
||||
|
||||
|
||||
|
||||
notify_email_markdown = mistune.create_markdown(renderer=EmailRenderer())
|
||||
notify_letter_preview_markdown = mistune.create_markdown(renderer=LetterPreviewRenderer())
|
||||
_notify_email_markdown = mistune.create_markdown(
|
||||
renderer=EmailRenderer(), hard_wrap=True
|
||||
)
|
||||
notify_letter_preview_markdown = mistune.create_markdown(
|
||||
renderer=LetterPreviewRenderer()
|
||||
)
|
||||
notify_email_preheader_markdown = mistune.create_markdown(renderer=PreheaderRenderer())
|
||||
notify_plain_text_email_markdown=mistune.create_markdown(renderer=PlainTextRenderer())
|
||||
_notify_plain_text_email_markdown = mistune.create_markdown(
|
||||
renderer=PlainTextRenderer()
|
||||
)
|
||||
|
||||
|
||||
def notify_email_markdown(text):
|
||||
return _notify_email_markdown(autolinkify(text))
|
||||
|
||||
|
||||
def notify_plain_text_email_markdown(text):
|
||||
text = escape_plus_lists(text)
|
||||
return _notify_plain_text_email_markdown(text)
|
||||
|
||||
Reference in New Issue
Block a user