From dae96590c3ad1c8688033d2dda58a207ea96b43c Mon Sep 17 00:00:00 2001 From: Alex Root Junior Date: Tue, 31 Dec 2019 17:56:12 +0200 Subject: [PATCH] Update text decorations. --- aiogram/utils/markdown.py | 36 +++++--- aiogram/utils/text_decorations.py | 26 ++++-- tests/test_utils/test_markdown.py | 6 +- tests/test_utils/test_text_decorations.py | 106 ++++++++++++---------- 4 files changed, 97 insertions(+), 77 deletions(-) diff --git a/aiogram/utils/markdown.py b/aiogram/utils/markdown.py index e934fe14..7b217b4f 100644 --- a/aiogram/utils/markdown.py +++ b/aiogram/utils/markdown.py @@ -1,4 +1,4 @@ -from .text_decorations import html, markdown +from .text_decorations import html_decoration, markdown_decoration LIST_MD_SYMBOLS = "*_`[" @@ -41,7 +41,7 @@ def bold(*content, sep=" "): :param sep: :return: """ - return markdown.bold.format(value=html.quote(_join(*content, sep=sep))) + return markdown_decoration.bold.format(value=html_decoration.quote(_join(*content, sep=sep))) def hbold(*content, sep=" "): @@ -52,7 +52,7 @@ def hbold(*content, sep=" "): :param sep: :return: """ - return html.bold.format(value=html.quote(_join(*content, sep=sep))) + return html_decoration.bold.format(value=html_decoration.quote(_join(*content, sep=sep))) def italic(*content, sep=" "): @@ -63,7 +63,7 @@ def italic(*content, sep=" "): :param sep: :return: """ - return markdown.italic.format(value=html.quote(_join(*content, sep=sep))) + return markdown_decoration.italic.format(value=html_decoration.quote(_join(*content, sep=sep))) def hitalic(*content, sep=" "): @@ -74,7 +74,7 @@ def hitalic(*content, sep=" "): :param sep: :return: """ - return html.italic.format(value=html.quote(_join(*content, sep=sep))) + return html_decoration.italic.format(value=html_decoration.quote(_join(*content, sep=sep))) def code(*content, sep=" "): @@ -85,7 +85,7 @@ def code(*content, sep=" "): :param sep: :return: """ - return markdown.code.format(value=html.quote(_join(*content, sep=sep))) + return markdown_decoration.code.format(value=html_decoration.quote(_join(*content, sep=sep))) def hcode(*content, sep=" "): @@ -96,7 +96,7 @@ def hcode(*content, sep=" "): :param sep: :return: """ - return html.code.format(value=html.quote(_join(*content, sep=sep))) + return html_decoration.code.format(value=html_decoration.quote(_join(*content, sep=sep))) def pre(*content, sep="\n"): @@ -107,7 +107,7 @@ def pre(*content, sep="\n"): :param sep: :return: """ - return markdown.pre.format(value=html.quote(_join(*content, sep=sep))) + return markdown_decoration.pre.format(value=html_decoration.quote(_join(*content, sep=sep))) def hpre(*content, sep="\n"): @@ -118,7 +118,7 @@ def hpre(*content, sep="\n"): :param sep: :return: """ - return html.pre.format(value=html.quote(_join(*content, sep=sep))) + return html_decoration.pre.format(value=html_decoration.quote(_join(*content, sep=sep))) def underline(*content, sep=" "): @@ -129,7 +129,9 @@ def underline(*content, sep=" "): :param sep: :return: """ - return markdown.underline.format(value=markdown.quote(_join(*content, sep=sep))) + return markdown_decoration.underline.format( + value=markdown_decoration.quote(_join(*content, sep=sep)) + ) def hunderline(*content, sep=" "): @@ -140,7 +142,7 @@ def hunderline(*content, sep=" "): :param sep: :return: """ - return html.underline.format(value=html.quote(_join(*content, sep=sep))) + return html_decoration.underline.format(value=html_decoration.quote(_join(*content, sep=sep))) def strikethrough(*content, sep=" "): @@ -151,7 +153,9 @@ def strikethrough(*content, sep=" "): :param sep: :return: """ - return markdown.strikethrough.format(value=markdown.quote(_join(*content, sep=sep))) + return markdown_decoration.strikethrough.format( + value=markdown_decoration.quote(_join(*content, sep=sep)) + ) def hstrikethrough(*content, sep=" "): @@ -162,7 +166,9 @@ def hstrikethrough(*content, sep=" "): :param sep: :return: """ - return html.strikethrough.format(value=html.quote(_join(*content, sep=sep))) + return html_decoration.strikethrough.format( + value=html_decoration.quote(_join(*content, sep=sep)) + ) def link(title: str, url: str) -> str: @@ -173,7 +179,7 @@ def link(title: str, url: str) -> str: :param url: :return: """ - return markdown.link.format(value=html.quote(title), link=url) + return markdown_decoration.link.format(value=html_decoration.quote(title), link=url) def hlink(title: str, url: str) -> str: @@ -184,7 +190,7 @@ def hlink(title: str, url: str) -> str: :param url: :return: """ - return html.link.format(value=html.quote(title), link=url) + return html_decoration.link.format(value=html_decoration.quote(title), link=url) def hide_link(url: str) -> str: diff --git a/aiogram/utils/text_decorations.py b/aiogram/utils/text_decorations.py index 3ca73bc9..5b9502b7 100644 --- a/aiogram/utils/text_decorations.py +++ b/aiogram/utils/text_decorations.py @@ -6,7 +6,13 @@ from typing import AnyStr, Callable, Generator, Iterable, List, Optional from aiogram.api.types import MessageEntity -__all__ = ("TextDecoration", "html", "markdown", "add_surrogate", "remove_surrogate") +__all__ = ( + "TextDecoration", + "html_decoration", + "markdown_decoration", + "add_surrogate", + "remove_surrogate", +) @dataclass @@ -82,7 +88,7 @@ class TextDecoration: yield self.quote(text[offset:length]) -html = TextDecoration( +html_decoration = TextDecoration( link='{value}', bold="{value}", italic="{value}", @@ -93,18 +99,18 @@ html = TextDecoration( quote=html.escape, ) -markdown = TextDecoration( +MARKDOWN_QUOTE_PATTERN = re.compile(r"([_*\[\]()~`>#+\-|{}.!])") + +markdown_decoration = TextDecoration( link="[{value}]({link})", bold="*{value}*", - italic="_{value}_", + italic="_{value}_\r", code="`{value}`", pre="```{value}```", - underline="--{value}--", # Is not supported - strikethrough="~~{value}~~", # Is not supported - quote=lambda text: re.sub( - pattern=r"([*_`\[])", repl=r"\\\1", string=text - ), # Is not always helpful -) # Markdown is not recommended for usage. Use HTML instead + underline="__{value}__", + strikethrough="~{value}~", + quote=lambda text: re.sub(pattern=MARKDOWN_QUOTE_PATTERN, repl=r"\\\1", string=text), +) def add_surrogate(text: str) -> str: diff --git a/tests/test_utils/test_markdown.py b/tests/test_utils/test_markdown.py index 79b22c97..b9da8f46 100644 --- a/tests/test_utils/test_markdown.py +++ b/tests/test_utils/test_markdown.py @@ -14,15 +14,15 @@ class TestMarkdown: [markdown.text, ("test", "test"), None, "test test"], [markdown.bold, ("test", "test"), " ", "*test test*"], [markdown.hbold, ("test", "test"), " ", "test test"], - [markdown.italic, ("test", "test"), " ", "_test test_"], + [markdown.italic, ("test", "test"), " ", "_test test_\r"], [markdown.hitalic, ("test", "test"), " ", "test test"], [markdown.code, ("test", "test"), " ", "`test test`"], [markdown.hcode, ("test", "test"), " ", "test test"], [markdown.pre, ("test", "test"), " ", "```test test```"], [markdown.hpre, ("test", "test"), " ", "
test test
"], - [markdown.underline, ("test", "test"), " ", "--test test--"], + [markdown.underline, ("test", "test"), " ", "__test test__"], [markdown.hunderline, ("test", "test"), " ", "test test"], - [markdown.strikethrough, ("test", "test"), " ", "~~test test~~"], + [markdown.strikethrough, ("test", "test"), " ", "~test test~"], [markdown.hstrikethrough, ("test", "test"), " ", "test test"], [markdown.link, ("test", "https://aiogram.dev"), None, "[test](https://aiogram.dev)"], [ diff --git a/tests/test_utils/test_text_decorations.py b/tests/test_utils/test_text_decorations.py index 40694a0a..babe0a58 100644 --- a/tests/test_utils/test_text_decorations.py +++ b/tests/test_utils/test_text_decorations.py @@ -3,32 +3,36 @@ from typing import List, Optional import pytest from aiogram.api.types import MessageEntity, User -from aiogram.utils.text_decorations import TextDecoration, html, markdown +from aiogram.utils.text_decorations import TextDecoration, html_decoration, markdown_decoration class TestTextDecoration: @pytest.mark.parametrize( "decorator,entity,result", [ - [html, MessageEntity(type="url", offset=0, length=5), "test"], + [html_decoration, MessageEntity(type="url", offset=0, length=5), "test"], [ - html, + html_decoration, MessageEntity(type="text_link", offset=0, length=5, url="https://aiogram.dev"), 'test', ], - [html, MessageEntity(type="bold", offset=0, length=5), "test"], - [html, MessageEntity(type="italic", offset=0, length=5), "test"], - [html, MessageEntity(type="code", offset=0, length=5), "test"], - [html, MessageEntity(type="pre", offset=0, length=5), "
test
"], - [html, MessageEntity(type="underline", offset=0, length=5), "test"], - [html, MessageEntity(type="strikethrough", offset=0, length=5), "test"], - [html, MessageEntity(type="hashtag", offset=0, length=5), "test"], - [html, MessageEntity(type="cashtag", offset=0, length=5), "test"], - [html, MessageEntity(type="bot_command", offset=0, length=5), "test"], - [html, MessageEntity(type="email", offset=0, length=5), "test"], - [html, MessageEntity(type="phone_number", offset=0, length=5), "test"], + [html_decoration, MessageEntity(type="bold", offset=0, length=5), "test"], + [html_decoration, MessageEntity(type="italic", offset=0, length=5), "test"], + [html_decoration, MessageEntity(type="code", offset=0, length=5), "test"], + [html_decoration, MessageEntity(type="pre", offset=0, length=5), "
test
"], + [html_decoration, MessageEntity(type="underline", offset=0, length=5), "test"], [ - html, + html_decoration, + MessageEntity(type="strikethrough", offset=0, length=5), + "test", + ], + [html_decoration, MessageEntity(type="hashtag", offset=0, length=5), "test"], + [html_decoration, MessageEntity(type="cashtag", offset=0, length=5), "test"], + [html_decoration, MessageEntity(type="bot_command", offset=0, length=5), "test"], + [html_decoration, MessageEntity(type="email", offset=0, length=5), "test"], + [html_decoration, MessageEntity(type="phone_number", offset=0, length=5), "test"], + [ + html_decoration, MessageEntity( type="text_mention", offset=0, @@ -37,25 +41,29 @@ class TestTextDecoration: ), 'test', ], - [html, MessageEntity(type="url", offset=0, length=5), "test"], + [html_decoration, MessageEntity(type="url", offset=0, length=5), "test"], [ - html, + html_decoration, MessageEntity(type="text_link", offset=0, length=5, url="https://aiogram.dev"), 'test', ], - [markdown, MessageEntity(type="bold", offset=0, length=5), "*test*"], - [markdown, MessageEntity(type="italic", offset=0, length=5), "_test_"], - [markdown, MessageEntity(type="code", offset=0, length=5), "`test`"], - [markdown, MessageEntity(type="pre", offset=0, length=5), "```test```"], - [markdown, MessageEntity(type="underline", offset=0, length=5), "--test--"], - [markdown, MessageEntity(type="strikethrough", offset=0, length=5), "~~test~~"], - [markdown, MessageEntity(type="hashtag", offset=0, length=5), "test"], - [markdown, MessageEntity(type="cashtag", offset=0, length=5), "test"], - [markdown, MessageEntity(type="bot_command", offset=0, length=5), "test"], - [markdown, MessageEntity(type="email", offset=0, length=5), "test"], - [markdown, MessageEntity(type="phone_number", offset=0, length=5), "test"], + [markdown_decoration, MessageEntity(type="bold", offset=0, length=5), "*test*"], + [markdown_decoration, MessageEntity(type="italic", offset=0, length=5), "_test_\r"], + [markdown_decoration, MessageEntity(type="code", offset=0, length=5), "`test`"], + [markdown_decoration, MessageEntity(type="pre", offset=0, length=5), "```test```"], + [markdown_decoration, MessageEntity(type="underline", offset=0, length=5), "__test__"], [ - markdown, + markdown_decoration, + MessageEntity(type="strikethrough", offset=0, length=5), + "~test~", + ], + [markdown_decoration, MessageEntity(type="hashtag", offset=0, length=5), "test"], + [markdown_decoration, MessageEntity(type="cashtag", offset=0, length=5), "test"], + [markdown_decoration, MessageEntity(type="bot_command", offset=0, length=5), "test"], + [markdown_decoration, MessageEntity(type="email", offset=0, length=5), "test"], + [markdown_decoration, MessageEntity(type="phone_number", offset=0, length=5), "test"], + [ + markdown_decoration, MessageEntity( type="text_mention", offset=0, @@ -74,16 +82,16 @@ class TestTextDecoration: @pytest.mark.parametrize( "decorator,before,after", [ - [html, "test", "test"], - [html, "test < test", "test < test"], - [html, "test > test", "test > test"], - [html, "test & test", "test & test"], - [html, "test @ test", "test @ test"], - [markdown, "test", "test"], - [markdown, "[test]", "\\[test]"], - [markdown, "test ` test", "test \\` test"], - [markdown, "test * test", "test \\* test"], - [markdown, "test _ test", "test \\_ test"], + [html_decoration, "test", "test"], + [html_decoration, "test < test", "test < test"], + [html_decoration, "test > test", "test > test"], + [html_decoration, "test & test", "test & test"], + [html_decoration, "test @ test", "test @ test"], + [markdown_decoration, "test", "test"], + [markdown_decoration, "[test]", "\\[test\\]"], + [markdown_decoration, "test ` test", "test \\` test"], + [markdown_decoration, "test * test", "test \\* test"], + [markdown_decoration, "test _ test", "test \\_ test"], ], ) def test_quote(self, decorator: TextDecoration, before: str, after: str): @@ -92,10 +100,10 @@ class TestTextDecoration: @pytest.mark.parametrize( "decorator,text,entities,result", [ - [html, "test", None, "test"], - [html, "test", [], "test"], + [html_decoration, "test", None, "test"], + [html_decoration, "test", [], "test"], [ - html, + html_decoration, "test1 test2 test3 test4 test5 test6 test7", [ MessageEntity(type="bold", offset=6, length=29), @@ -105,7 +113,7 @@ class TestTextDecoration: "test1 test2 test3 test4 test5 test6 test7", ], [ - html, + html_decoration, "test1 test2 test3 test4 test5", [ MessageEntity(type="bold", offset=6, length=17), @@ -114,7 +122,7 @@ class TestTextDecoration: "test1 test2 test3 test4 test5", ], [ - html, + html_decoration, "test1 test2 test3 test4", [ MessageEntity(type="bold", offset=6, length=11), @@ -123,19 +131,19 @@ class TestTextDecoration: "test1 test2 test3 test4", ], [ - html, + html_decoration, "test1 test2 test3", [MessageEntity(type="bold", offset=6, length=5)], "test1 test2 test3", ], [ - html, + html_decoration, "test1 test2", [MessageEntity(type="bold", offset=0, length=5)], "test1 test2", ], [ - html, + html_decoration, "strike bold", [ MessageEntity(type="strikethrough", offset=0, length=6), @@ -144,7 +152,7 @@ class TestTextDecoration: "strike bold", ], [ - html, + html_decoration, "test", [ MessageEntity(type="strikethrough", offset=0, length=5), @@ -153,7 +161,7 @@ class TestTextDecoration: "test", ], [ - html, + html_decoration, "strikeboldunder", [ MessageEntity(type="strikethrough", offset=0, length=15),