Skip to content

Commit

Permalink
squash 169 - action links partial
Browse files Browse the repository at this point in the history
  • Loading branch information
kalbfled committed Dec 12, 2024
1 parent de3ec97 commit 740fdce
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 36 deletions.
33 changes: 33 additions & 0 deletions notifications_utils/action_link.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""
A Mistune plugin to parse "action links" in markdown. An action link is specified like this:
>>[action](https://example.com/foo?a=b)
"""

from re import Match

from mistune.block_parser import BlockParser
from mistune.core import BlockState
from mistune.markdown import Markdown
from mistune.util import escape_url


ACTION_LINK_PATTERN = r'''>>\[(?P<text>[\w ]+)\]\((?P<url>\S+)\)'''
# ACTION_LINK_PATTERN = r'''\[action'''


def parse_action_link(block: BlockParser, m: Match[str], state: BlockState) -> int:
# print("PARSE ACTION LINK", m) # TODO - delete
text = m.group('text')
url = m.group('url')
state.append_token({
'type': 'action_link',
'attrs': {
'text': text,
'url': escape_url(url),
},
})
return m.end()


def action_link(md: Markdown):
md.block.register("block_action_link", ACTION_LINK_PATTERN, parse_action_link, before='block_text')
6 changes: 5 additions & 1 deletion notifications_utils/formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from mistune.renderers.html import HTMLRenderer
from mistune.renderers.markdown import MarkdownRenderer
from . import email_with_smart_quotes_regex
from notifications_utils.action_link import action_link
from notifications_utils.sanitise_text import SanitiseSMS

BLOCK_QUOTE_STYLE = 'background: #F1F1F1; ' \
Expand Down Expand Up @@ -414,6 +415,9 @@ def replace_symbols_with_placeholder_parens(value: str) -> str:


class NotifyHTMLRenderer(HTMLRenderer):
def action_link(self, text, url):
raise NotImplementedError('MADE IT HERE')

def block_quote(self, text):
value = super().block_quote(text)
return value[:11] + f' style="{BLOCK_QUOTE_STYLE}"' + value[11:]
Expand Down Expand Up @@ -544,7 +548,7 @@ def thematic_break(self, token, state):
notify_html_markdown = mistune.create_markdown(
hard_wrap=True,
renderer=NotifyHTMLRenderer(escape=False),
plugins=['strikethrough', 'table', 'url'],
plugins=[action_link, 'strikethrough', 'table', 'url'],
)

notify_markdown = mistune.create_markdown(
Expand Down
4 changes: 0 additions & 4 deletions notifications_utils/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
add_prefix,
add_trailing_newline,
autolink_sms, escape_html,
insert_action_link,
make_quotes_smart,
nl2br,
normalise_newlines,
Expand Down Expand Up @@ -46,11 +45,9 @@ def compose1(value, *fs):
Return the composition of functions applied to a single value.
"""

# print('COMPOSE', value) # TODO
return_value = value
for f in fs:
return_value = f(return_value)
# print('COMPOSE', return_value) # TODO
return return_value


Expand Down Expand Up @@ -480,7 +477,6 @@ def get_html_email_body(
# after converting to html link, replace !!foo## with ((foo))
replace_symbols_with_placeholder_parens,
do_nice_typography,
insert_action_link,
)


Expand Down
62 changes: 31 additions & 31 deletions tests/test_template_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,18 @@ def test_pass_through_renderer():
'line one\nline two with ((name))\n\nnew paragraph',
{'name': 'bob'},
(
f'<p style="{PARAGRAPH_STYLE}">line one<br />line two with bob</p>'
f'<p style="{PARAGRAPH_STYLE}">new paragraph</p>'
f'<p style="{PARAGRAPH_STYLE}">line one<br />\nline two with bob</p>\n'
f'<p style="{PARAGRAPH_STYLE}">new paragraph</p>\n'
),
),
(
'>>[action](https://example.com/foo?a=b)',
{},
(
f'<p style="{PARAGRAPH_STYLE}"><a style="{LINK_STYLE}" href="https://example.com/foo?a=b" '
'target="_blank">'
f'<p style="{PARAGRAPH_STYLE}"><a style="{LINK_STYLE}" target="_blank" '
'href="https://example.com/foo?a=b">'
'<img src="https://dev-va-gov-assets.s3-us-gov-west-1.amazonaws.com/img/vanotify-action-link.png" '
'alt="call to action img" style="vertical-align: middle;"> <b>action</b></a></p>'
'alt="call to action img" style="vertical-align: middle;"> <b>action</b></a></p>\n'
)
),
(
Expand All @@ -59,79 +59,79 @@ def test_pass_through_renderer():
'<h1 style="Margin: 0 0 20px 0; padding: 0; font-size: 32px; line-height: 35px; font-weight: bold; '
'color: #323A45;">foo</h1><h2 style="Margin: 0 0 15px 0; padding: 0; line-height: 26px; color: #323A45;'
'font-size: 24px; font-weight: bold; font-family: Helvetica, Arial, sans-serif;">Bar</h2>'
f'<p style="{PARAGRAPH_STYLE}">The quick brown fox<br /></p>'
f'<p style="{PARAGRAPH_STYLE}">The quick brown fox<br /></p>\n'
f'<p style="{PARAGRAPH_STYLE}"><a style="{LINK_STYLE}" href="https://example.com" target="_blank">'
'<img src="https://dev-va-gov-assets.s3-us-gov-west-1.amazonaws.com/img/vanotify-action-link.png" '
'alt="call to action img" style="vertical-align: middle;"> <b>the action_link-of doom</b></a></p>'
'alt="call to action img" style="vertical-align: middle;"> <b>the action_link-of doom</b></a></p>\n'
),
),
(
'text before link\n\n>>[great link](http://example.com)\n\ntext after link',
{},
(
f'<p style="{PARAGRAPH_STYLE}">text before link</p>'
f'<p style="{PARAGRAPH_STYLE}">text before link</p>\n'
f'<p style="{PARAGRAPH_STYLE}"><a style="{LINK_STYLE}" href="http://example.com" target="_blank">'
'<img src="https://dev-va-gov-assets.s3-us-gov-west-1.amazonaws.com/img/vanotify-action-link.png" '
'alt="call to action img" style="vertical-align: middle;"> <b>great link</b></a></p>'
f'<p style="{PARAGRAPH_STYLE}">text after link</p>'
'alt="call to action img" style="vertical-align: middle;"> <b>great link</b></a></p>\n'
f'<p style="{PARAGRAPH_STYLE}">text after link</p>\n'
)
),
(
'action link: &gt;&gt;[Example](http://example.com)\nanother link: [test](https://example2.com)',
{},
(
f'<p style="{PARAGRAPH_STYLE}">action link: </p>'
f'<p style="{PARAGRAPH_STYLE}">action link: </p>\n'
f'<p style="{PARAGRAPH_STYLE}"><a style="{LINK_STYLE}" href="http://example.com" target="_blank">'
'<img src="https://dev-va-gov-assets.s3-us-gov-west-1.amazonaws.com/img/vanotify-action-link.png" '
'alt="call to action img" style="vertical-align: middle;"> <b>Example</b></a></p>'
'alt="call to action img" style="vertical-align: middle;"> <b>Example</b></a></p>\n'
f'<p style="{PARAGRAPH_STYLE}"><br />'
f'another link: <a style="{LINK_STYLE}" href="https://example2.com" target="_blank">test</a></p>'
f'another link: <a style="{LINK_STYLE}" href="https://example2.com" target="_blank">test</a></p>\n'
)
),
(
'action link: &gt;&gt;[grin](http://example.com) another action link: >>[test](https://example2.com)',
{},
(
f'<p style="{PARAGRAPH_STYLE}">action link: </p>'
f'<p style="{PARAGRAPH_STYLE}">action link: </p>\n'
f'<p style="{PARAGRAPH_STYLE}"><a style="{LINK_STYLE}" href="http://example.com" target="_blank">'
'<img src="https://dev-va-gov-assets.s3-us-gov-west-1.amazonaws.com/img/vanotify-action-link.png" '
'alt="call to action img" style="vertical-align: middle;"> <b>grin</b></a></p>'
f'<p style="{PARAGRAPH_STYLE}"> another action link: </p>'
'alt="call to action img" style="vertical-align: middle;"> <b>grin</b></a></p>\n'
f'<p style="{PARAGRAPH_STYLE}"> another action link: </p>\n'
f'<p style="{PARAGRAPH_STYLE}"><a style="{LINK_STYLE}" href="https://example2.com" target="_blank">'
'<img src="https://dev-va-gov-assets.s3-us-gov-west-1.amazonaws.com/img/vanotify-action-link.png" '
'alt="call to action img" style="vertical-align: middle;"> <b>test</b></a></p>'
'alt="call to action img" style="vertical-align: middle;"> <b>test</b></a></p>\n'
)
),
(
'text before && link &gt;&gt;[Example](http://example.com) text after & link',
{},
(
f'<p style="{PARAGRAPH_STYLE}">text before &amp;&amp; link </p>'
f'<p style="{PARAGRAPH_STYLE}">text before &amp;&amp; link </p>\n'
f'<p style="{PARAGRAPH_STYLE}"><a style="{LINK_STYLE}" href="http://example.com" target="_blank">'
'<img src="https://dev-va-gov-assets.s3-us-gov-west-1.amazonaws.com/img/vanotify-action-link.png" '
'alt="call to action img" style="vertical-align: middle;"> <b>Example</b></a></p>'
f'<p style="{PARAGRAPH_STYLE}"> text after &amp; link</p>'
'alt="call to action img" style="vertical-align: middle;"> <b>Example</b></a></p>\n'
f'<p style="{PARAGRAPH_STYLE}"> text after &amp; link</p>\n'
)
),
(
'text before >> link &gt;&gt;[great action](http://example.com) text after >>link',
{},
(
f'<p style="{PARAGRAPH_STYLE}">text before &gt;&gt; link </p>'
f'<p style="{PARAGRAPH_STYLE}">text before &gt;&gt; link </p>\n'
f'<p style="{PARAGRAPH_STYLE}"><a style="{LINK_STYLE}" href="http://example.com" target="_blank">'
'<img src="https://dev-va-gov-assets.s3-us-gov-west-1.amazonaws.com/img/vanotify-action-link.png" '
'alt="call to action img" style="vertical-align: middle;"> <b>great action</b></a></p>'
f'<p style="{PARAGRAPH_STYLE}"> text after &gt;&gt;link</p>'
'alt="call to action img" style="vertical-align: middle;"> <b>great action</b></a></p>\n'
f'<p style="{PARAGRAPH_STYLE}"> text after &gt;&gt;link</p>\n'
)
),
(
'text >> then [item] and (things) then >>[action](link)',
{},
(
f'<p style="{PARAGRAPH_STYLE}">text &gt;&gt; then [item] and (things) then </p>'
f'<p style="{PARAGRAPH_STYLE}">text &gt;&gt; then [item] and (things) then </p>\n'
f'<p style="{PARAGRAPH_STYLE}"><a style="{LINK_STYLE}" href="link" target="_blank">'
'<img src="https://dev-va-gov-assets.s3-us-gov-west-1.amazonaws.com/img/vanotify-action-link.png" '
'alt="call to action img" style="vertical-align: middle;"> <b>action</b></a></p>'
'alt="call to action img" style="vertical-align: middle;"> <b>action</b></a></p>\n'
)
),
(
Expand All @@ -144,17 +144,17 @@ def test_pass_through_renderer():
(
f'<p style="{PARAGRAPH_STYLE}"><a style="{LINK_STYLE}" href="#" target="_blank">'
'<img src="https://dev-va-gov-assets.s3-us-gov-west-1.amazonaws.com/img/vanotify-action-link.png" '
'alt="call to action img" style="vertical-align: middle;"> <b>action link</b></a></p>'
f'<p style="{PARAGRAPH_STYLE}">testing the new </p>'
'alt="call to action img" style="vertical-align: middle;"> <b>action link</b></a></p>\n'
f'<p style="{PARAGRAPH_STYLE}">testing the new </p>\n'
f'<p style="{PARAGRAPH_STYLE}"><a style="{LINK_STYLE}" href="#" target="_blank">'
'<img src="https://dev-va-gov-assets.s3-us-gov-west-1.amazonaws.com/img/vanotify-action-link.png" '
'alt="call to action img" style="vertical-align: middle;"> <b>action link</b></a></p>'
f'<p style="{PARAGRAPH_STYLE}"> thingy...</p>'
'alt="call to action img" style="vertical-align: middle;"> <b>action link</b></a></p>\n'
f'<p style="{PARAGRAPH_STYLE}"> thingy...</p>\n'
f'<p style="{PARAGRAPH_STYLE}"><a style="{LINK_STYLE}" href="#" target="_blank">'
'<img src="https://dev-va-gov-assets.s3-us-gov-west-1.amazonaws.com/img/vanotify-action-link.png" '
'alt="call to action img" style="vertical-align: middle;"> <b>click me</b></a></p>'
'alt="call to action img" style="vertical-align: middle;"> <b>click me</b></a></p>\n'
f'<p style="{PARAGRAPH_STYLE}">! Text with a '
'<a style="word-wrap: break-word; color: #004795;" href="#" target="_blank">regular link</a></p>'
'<a style="word-wrap: break-word; color: #004795;" href="#" target="_blank">regular link</a></p>\n'
)
)
],
Expand Down

0 comments on commit 740fdce

Please sign in to comment.