Skip to content

Commit

Permalink
Merge pull request #54 from sudoskys/dev
Browse files Browse the repository at this point in the history
fix: better logger | Downgrade pydantic to support pyqt6
  • Loading branch information
sudoskys authored Jan 3, 2025
2 parents 0cfd93e + ac8772a commit 235feb0
Show file tree
Hide file tree
Showing 10 changed files with 504 additions and 36 deletions.
449 changes: 440 additions & 9 deletions pdm.lock

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion playground/markdownify_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,13 @@
emoji_md = r"""
![👍](tg://emoji?id=5368324170671202286)
"""
url_exp= r"""
[Test](https://test.com)
"""

# export Markdown to Telegram MarkdownV2 style.
converted = telegramify_markdown.markdownify(
md,
url_exp,
max_line_length=None, # If you want to change the max line length for links, images, set it to the desired value.
normalize_whitespace=False,
latex_escape=True
Expand Down
8 changes: 5 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "telegramify-markdown"
version = "0.3.1"
version = "0.3.2"
description = "Makes it easy to send Markdown in Telegram MarkdownV2 style"
authors = [
{ name = "sudoskys", email = "[email protected]" },
Expand All @@ -10,10 +10,12 @@ dependencies = [
"pytelegrambotapi>=4.22.0",
"docutils>=0.20.1",
"Pillow>=10.4.0",
"pydantic>=2.10.3",
"pydantic>=2.6.1",
"aiohttp>=3.10.11",
"matplotlib>=3.9.4",
"loguru>=0.7.3",
]
requires-python = ">=3.8"
requires-python = ">=3.9"
readme = "README.md"
license = { text = "MIT" }

Expand Down
2 changes: 1 addition & 1 deletion src/telegramify_markdown/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from .interpreters import Text, File, Photo, BaseInterpreter, MermaidInterpreter
from .latex_escape.const import LATEX_SYMBOLS, NOT_MAP, LATEX_STYLES
from .latex_escape.helper import LatexToUnicodeHelper
from .logger import logger
from loguru import logger
from .mermaid import render_mermaid
from .mime import get_filename
from .render import TelegramMarkdownRenderer, escape_markdown
Expand Down
42 changes: 34 additions & 8 deletions src/telegramify_markdown/interpreters.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

import mistletoe

from telegramify_markdown.logger import logger
from loguru import logger
from telegramify_markdown.mermaid import render_mermaid
from telegramify_markdown.mime import get_filename
from telegramify_markdown.type import TaskType, File, Text, Photo, SentType
from telegramify_markdown.type import TaskType, File, Text, Photo, SentType, ContentTrace

if TYPE_CHECKING:
from aiohttp import ClientSession
Expand Down Expand Up @@ -67,11 +67,30 @@ async def render_task(self,
if isinstance(_escaped_code, mistletoe.block_token.CodeFence):
lang = _escaped_code.language
file_name = get_filename(line=render_block_func(token1_l), language=lang)
return [File(file_name=file_name, file_data=file_content.encode(), caption="")]
return [
File(
file_name=file_name,
file_data=file_content.encode(),
caption="",
content_trace=ContentTrace(source_type=self.name)
)
]
# 如果超过最大字数限制
return [File(file_name="letter.txt", file_data=render_block_func(token2_l).encode(), caption="")]
return [
File(
file_name="letter.txt",
file_data=render_block_func(token2_l).encode(),
caption="",
content_trace=ContentTrace(source_type=self.name)
)
]
# 没有超过最大字数限制
return [Text(content=render_block_func(token1_l))]
return [
Text(
content=render_block_func(token1_l),
content_trace=ContentTrace(source_type=self.name)
)
]


class MermaidInterpreter(BaseInterpreter):
Expand Down Expand Up @@ -168,17 +187,24 @@ async def render_task(self,
File(
file_name="invalid_mermaid.txt",
file_data=render_block_func(unescape_tokens).encode(),
caption=render_lines_func("invalid_mermaid")
caption=render_lines_func("invalid_mermaid"),
content_trace=ContentTrace(source_type=self.name)
)
]
else:
return [
Photo(
file_name="mermaid.png",
file_data=img_io.getvalue(),
caption=render_lines_func(message)
caption=render_lines_func(message),
content_trace=ContentTrace(source_type=self.name)
)
]
return [
File(file_name="mermaid_code.txt", file_data=render_block_func(unescape_tokens).encode(), caption="")
File(
file_name="mermaid_code.txt",
file_data=render_block_func(unescape_tokens).encode(),
caption="",
content_trace=ContentTrace(source_type=self.name)
)
]
5 changes: 0 additions & 5 deletions src/telegramify_markdown/logger.py

This file was deleted.

2 changes: 1 addition & 1 deletion src/telegramify_markdown/mermaid.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from PIL import Image
from aiohttp import ClientSession

from telegramify_markdown.logger import logger
from loguru import logger


@dataclasses.dataclass
Expand Down
2 changes: 1 addition & 1 deletion src/telegramify_markdown/mime.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from pathlib import Path
from typing import Optional

from telegramify_markdown.logger import logger
from loguru import logger

default_language_to_ext = {
"python": "py",
Expand Down
2 changes: 1 addition & 1 deletion src/telegramify_markdown/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from itertools import chain, tee
from typing import Iterable

from mistletoe import block_token, span_token
from mistletoe import span_token, block_token
from mistletoe.markdown_renderer import MarkdownRenderer, LinkReferenceDefinition, Fragment
from telebot import formatting

Expand Down
23 changes: 17 additions & 6 deletions src/telegramify_markdown/type.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from enum import Enum
from typing import Tuple, List, Any, Union

from pydantic import dataclasses
from pydantic import BaseModel, Field

TaskType = Tuple[str, List[Tuple[Any, Any]]]
SentType = List[Union["Text", "File", "Photo"]]
Expand All @@ -14,39 +14,50 @@ class ContentTypes(Enum):
PHOTO = "photo"


class RenderedContent(object, metaclass=ABCMeta):
class ContentTrace(BaseModel):
"""
The content trace.
- content: str
- content_type: ContentTypes
"""
source_type: str
extra: dict = Field(default_factory=dict)


class RenderedContent(BaseModel, metaclass=ABCMeta):
"""
The rendered content.
- content: str
- content_type: ContentTypes
"""
content_trace: ContentTrace
content_type: ContentTypes


@dataclasses.dataclass
class Text(RenderedContent):
content: str
content_trace: ContentTrace
content_type: ContentTypes = ContentTypes.TEXT


@dataclasses.dataclass
class File(RenderedContent):
file_name: str
file_data: bytes

caption: str = ""
"""Please use render_lines_func to render the content."""

content_trace: ContentTrace
content_type: ContentTypes = ContentTypes.FILE


@dataclasses.dataclass
class Photo(RenderedContent):
file_name: str
file_data: bytes

caption: str = ""
"""Please use render_lines_func to render the content."""

content_trace: ContentTrace
content_type: ContentTypes = ContentTypes.PHOTO

0 comments on commit 235feb0

Please sign in to comment.