utils.py: escape_markdown handles urls and all reserved characters

pull/2/head
Michael DM Dryden 2020-08-08 02:04:22 -04:00
rodzic b8cfa98638
commit 70411cb4e0
2 zmienionych plików z 12 dodań i 1 usunięć

Wyświetl plik

@ -9,6 +9,10 @@ and this project adheres to `Semantic Versioning <https://semver.org/spec/v2.0.0
-------------
`Unreleased`_
-------------
Fixed
-----
- Correctly escape all reserved markdown characters and markdown links
---------------------
`0.2.0`_ - 2020-06-16
---------------------

Wyświetl plik

@ -21,6 +21,13 @@
import re
md_match = re.compile(r"(\[[^][]*]\(http[^()]*\))|([_*[\]()~>#+-=|{}.!\\])")
def escape_markdown(string: str) -> str:
return re.sub(r'([\\_*\[\]()`])', r'\\\g<1>', string)
def url_match(match: re.Match):
if match.group(1):
return f'{match.group(1)}'
return f'\\{match.group(2)}'
return re.sub(md_match, url_match, string)