From 06f46c5e6a620ad3fd66ecff7abb10fe06c656a7 Mon Sep 17 00:00:00 2001 From: Alex Root Junior Date: Fri, 19 May 2017 22:55:29 +0300 Subject: [PATCH] Add markdown util. --- aiogram/utils/__init__.py | 0 aiogram/utils/markdown.py | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 aiogram/utils/__init__.py create mode 100644 aiogram/utils/markdown.py diff --git a/aiogram/utils/__init__.py b/aiogram/utils/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/aiogram/utils/markdown.py b/aiogram/utils/markdown.py new file mode 100644 index 00000000..c3e83c1c --- /dev/null +++ b/aiogram/utils/markdown.py @@ -0,0 +1,37 @@ +MD_SYMBOLS = '*_`' + + +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 text(*content, sep=' '): + return _rst('', *content, sep=sep) + + +def bold(*content, sep=' '): + return _rst(MD_SYMBOLS[0], *content, sep=sep) + + +def italic(*content, sep=' '): + return _rst(MD_SYMBOLS[1], *content, sep=sep) + + +def code(*content, sep=' '): + return _rst(MD_SYMBOLS[2], *content, sep=sep) + + +def pre(*content, sep='\n'): + return _rst(('```\n', '\n```'), *content, sep=sep) + + +def link(title, url): + return f"[{title}]({url})" + + +def escape_md(*content, sep=' '): + result = text(*content, sep=sep) + for symbol in MD_SYMBOLS + '[': + result = result.replace(symbol, '\\' + symbol) + return result