From 4ed711004813b773d3c764f4345822311077d71e Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Tue, 5 Dec 2017 12:42:57 +0100 Subject: [PATCH] Add extensions package --- pyrogram/extensions/__init__.py | 19 +++++ pyrogram/extensions/markdown.py | 120 ++++++++++++++++++++++++++++++++ 2 files changed, 139 insertions(+) create mode 100644 pyrogram/extensions/__init__.py create mode 100644 pyrogram/extensions/markdown.py diff --git a/pyrogram/extensions/__init__.py b/pyrogram/extensions/__init__.py new file mode 100644 index 00000000..a8741ddf --- /dev/null +++ b/pyrogram/extensions/__init__.py @@ -0,0 +1,19 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017 Dan Tès +# +# This file is part of Pyrogram. +# +# Pyrogram is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Pyrogram is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with Pyrogram. If not, see . + +from .markdown import Markdown diff --git a/pyrogram/extensions/markdown.py b/pyrogram/extensions/markdown.py new file mode 100644 index 00000000..5ae51702 --- /dev/null +++ b/pyrogram/extensions/markdown.py @@ -0,0 +1,120 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017 Dan Tès +# +# This file is part of Pyrogram. +# +# Pyrogram is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Pyrogram is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with Pyrogram. If not, see . + +import re +from struct import unpack + +from pyrogram.api.types import ( + MessageEntityBold as Bold, + MessageEntityItalic as Italic, + MessageEntityCode as Code, + MessageEntityTextUrl as Url, + MessageEntityPre as Pre, +) + + +class Markdown: + INLINE_DELIMITERS = { + "**": Bold, + "__": Italic, + "`": Code + } + + # SMP = Supplementary Multilingual Plane: https://en.wikipedia.org/wiki/Plane_(Unicode)#Overview + SMP_RE = re.compile(r"[\U00010000-\U0010FFFF]") + + # ``` python + # for i in range(10): + # print(i) + # ``` + PRE_RE = r"(?P
```(?P.*)\n(?P(.|\n)*)\n```)"
+
+    # [url](github.com)
+    URL_RE = r"(?P(\[(?P.+?)\]\((?P.+?)\)))"
+
+    # **bold**
+    # __italic__
+    # `code`
+    INLINE_RE = r"(?P(?P{d})(?P.+?)(?P{d}))".format(
+        d="|".join(
+            ["".join(i) for i in [
+                ["\{}".format(j) for j in i]
+                for i in sorted(  # Sort delimiters by length
+                    INLINE_DELIMITERS.keys(),
+                    key=lambda k: len(k),  # Or: key=len
+                    reverse=True
+                )
+            ]]
+        )
+    )
+
+    MARKDOWN_RE = re.compile("|".join([PRE_RE, URL_RE, INLINE_RE]))
+
+    @classmethod
+    def add_surrogates(cls, text):
+        # Replace each SMP code point with a surrogate pair
+        return cls.SMP_RE.sub(
+            lambda match:  # Split SMP in two surrogates
+            "".join(chr(i) for i in unpack("