From bc917ec9f53ff3ee64e7e1ddf7afbf0a1b402136 Mon Sep 17 00:00:00 2001 From: Alex Root Junior Date: Sat, 22 Jul 2017 20:16:51 +0300 Subject: [PATCH] Rewrite markdown util --- aiogram/utils/markdown.py | 42 ++++++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/aiogram/utils/markdown.py b/aiogram/utils/markdown.py index c3e83c1c..26976054 100644 --- a/aiogram/utils/markdown.py +++ b/aiogram/utils/markdown.py @@ -1,37 +1,51 @@ -MD_SYMBOLS = '*_`' +LIST_MD_SYMBOLS = '*_`[' + +MD_SYMBOLS = ( + (LIST_MD_SYMBOLS[0], LIST_MD_SYMBOLS[0]), + (LIST_MD_SYMBOLS[1], LIST_MD_SYMBOLS[1]), + (LIST_MD_SYMBOLS[2], LIST_MD_SYMBOLS[2]), + (LIST_MD_SYMBOLS[2] * 3 + '\n', '\n' + LIST_MD_SYMBOLS[2] * 3) +) -def _rst(symbol, *content, sep=' '): - start, end = (symbol, symbol) if isinstance(symbol, str) else (symbol[0], symbol[1]) - return start + sep.join(map(str, content)) + end +def _join(*content, sep=' '): + return sep.join(map(str, content)) + + +def _escape(s, symbols=LIST_MD_SYMBOLS): + for symbol in symbols: + s = s.replace(symbol, '\\' + symbol) + return s + + +def _md(string, symbols=('', '')): + start, end = symbols + return start + string + end def text(*content, sep=' '): - return _rst('', *content, sep=sep) + return _md('', _join(*content, sep=sep)) def bold(*content, sep=' '): - return _rst(MD_SYMBOLS[0], *content, sep=sep) + return _md(_join(*content, sep=sep), symbols=MD_SYMBOLS[0]) def italic(*content, sep=' '): - return _rst(MD_SYMBOLS[1], *content, sep=sep) + return _md(_join(*content, sep=sep), symbols=MD_SYMBOLS[1]) def code(*content, sep=' '): - return _rst(MD_SYMBOLS[2], *content, sep=sep) + return _md(_join(*content, sep=sep), symbols=MD_SYMBOLS[2]) def pre(*content, sep='\n'): - return _rst(('```\n', '\n```'), *content, sep=sep) + return _md(_join(*content, sep=sep), symbols=MD_SYMBOLS[3]) def link(title, url): - return f"[{title}]({url})" + return f"[{_escape(title)}]({url})" def escape_md(*content, sep=' '): - result = text(*content, sep=sep) - for symbol in MD_SYMBOLS + '[': - result = result.replace(symbol, '\\' + symbol) - return result + return _escape(_join(*content, sep=sep))