|
| 1 | +# Pyrogram - Telegram MTProto API Client Library for Python |
| 2 | +# Copyright (C) 2017-2019 Dan Tès <https://github.com/delivrance> |
| 3 | +# |
| 4 | +# This file is part of Pyrogram. |
| 5 | +# |
| 6 | +# Pyrogram is free software: you can redistribute it and/or modify |
| 7 | +# it under the terms of the GNU Lesser General Public License as published |
| 8 | +# by the Free Software Foundation, either version 3 of the License, or |
| 9 | +# (at your option) any later version. |
| 10 | +# |
| 11 | +# Pyrogram is distributed in the hope that it will be useful, |
| 12 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +# GNU Lesser General Public License for more details. |
| 15 | +# |
| 16 | +# You should have received a copy of the GNU Lesser General Public License |
| 17 | +# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + |
| 19 | +from collections import OrderedDict |
| 20 | +from typing import Union |
| 21 | + |
| 22 | + |
| 23 | +import pyrogram |
| 24 | +from .html import HTML |
| 25 | +from .markdown import Markdown |
| 26 | + |
| 27 | + |
| 28 | +class Parser: |
| 29 | + def __init__(self, client: Union["pyrogram.BaseClient", None]): |
| 30 | + self.html = HTML(client) |
| 31 | + self.markdown = Markdown(client) |
| 32 | + |
| 33 | + def parse(self, text: str, mode: str = ""): |
| 34 | + if mode is None: |
| 35 | + return OrderedDict([ |
| 36 | + ("message", text), |
| 37 | + ("entities", []) |
| 38 | + ]) |
| 39 | + |
| 40 | + mode = mode.lower() |
| 41 | + |
| 42 | + if mode == "": |
| 43 | + return self.markdown.parse(text) |
| 44 | + |
| 45 | + if mode in ["markdown", "md"]: |
| 46 | + return self.markdown.parse(text, True) |
| 47 | + |
| 48 | + if mode == "html": |
| 49 | + return self.html.parse(text) |
| 50 | + |
| 51 | + @staticmethod |
| 52 | + def unparse(text: str, entities: list, is_html: bool): |
| 53 | + if is_html: |
| 54 | + return HTML.unparse(text, entities) |
| 55 | + else: |
| 56 | + return Markdown.unparse(text, entities) |
0 commit comments