Skip to content

Commit

Permalink
Merge pull request #57 from nj-vs-vh/main
Browse files Browse the repository at this point in the history
Dependency cleanup
  • Loading branch information
sudoskys authored Jan 13, 2025
2 parents 235feb0 + bf48379 commit cab67d0
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 1,341 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,4 @@ cython_debug/
/.pdm-python
.idea/
/src/telegramify_markdown/latex_escape/_deprecated_symbols.py
.DS_Store
1,299 changes: 10 additions & 1,289 deletions pdm.lock

Large diffs are not rendered by default.

25 changes: 13 additions & 12 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
[project]
name = "telegramify-markdown"
version = "0.3.2"
version = "0.4.0"
description = "Makes it easy to send Markdown in Telegram MarkdownV2 style"
authors = [
{ name = "sudoskys", email = "coldlando@hotmail.com" },
]
dependencies = [
"mistletoe==1.4.0",
"pytelegrambotapi>=4.22.0",
"docutils>=0.20.1",
"Pillow>=10.4.0",
"pydantic>=2.6.1",
"aiohttp>=3.10.11",
"matplotlib>=3.9.4",
"loguru>=0.7.3",
]
requires-python = ">=3.9"
readme = "README.md"
license = { text = "MIT" }

[project.optional-dependencies]
mermaid = [
"Pillow>=10.4.0",
"aiohttp>=3.10.11",
]

[dependency-groups]
dev = [
"pytelegrambotapi>=4.22.0",
"python-dotenv>=1.0.1",
]

[project.urls]
repository = "https://github.com/sudoskys/telegramify-markdown"

Expand All @@ -30,11 +36,6 @@ build-backend = "pdm.backend"
[tool.pdm]
distribution = true

[tool.pdm.dev-dependencies]
dev = [
"loguru>=0.7.2",
"python-dotenv>=1.0.1",
]

[tool.pdm.scripts]
test = "python -m unittest discover -s ./tests -p *_test.py"
19 changes: 19 additions & 0 deletions src/telegramify_markdown/markdown.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import re


def escape(body: str) -> str:
parse = re.sub(r"([_*\[\]()~`>\#\+\-=|\.!\{\}\\])", r"\\\1", body)
reparse = re.sub(r"\\\\([_*\[\]()~`>\#\+\-=|\.!\{\}\\])", r"\1", parse)
return reparse


def bold(body: str) -> str:
return f"*{body}*"


def code(body: str) -> str:
return f"```\n{body}\n```"


def link(body: str, href: str) -> str:
return f"[{escape(body)}]({href})"
26 changes: 13 additions & 13 deletions src/telegramify_markdown/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
import re
from itertools import chain, tee
from typing import Iterable
from telegramify_markdown import markdown

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

from .customize import markdown_symbol, strict_markdown, cite_expandable

Expand Down Expand Up @@ -157,7 +157,7 @@ def render_heading(
line += " " + text
if token.closing_sequence:
line += " " + token.closing_sequence
return [formatting.mbold(line, escape=False)]
return [markdown.bold(line)]

def render_fenced_code_block(
self, token: block_token.BlockCode, max_line_length: int
Expand Down Expand Up @@ -186,14 +186,14 @@ def render_block_code(
self, token: block_token.BlockCode,
max_line_length: int
) -> Iterable[str]:
return [formatting.mcode(token.content, escape=False)]
return [markdown.code(token.content)]

def render_setext_heading(
self, token: block_token.SetextHeading,
max_line_length: int
) -> Iterable[str]:
yield from self.span_to_lines(token.children, max_line_length=max_line_length)
yield formatting.escape_markdown("───────────────────")
yield markdown.escape("───────────────────")

def render_emphasis(self, token: span_token.Emphasis) -> Iterable[Fragment]:
return super().render_emphasis(token)
Expand Down Expand Up @@ -240,17 +240,17 @@ def render_list_item(
if token_origin.endswith("."):
if not token.leader.endswith(" "):
token.leader += " "
token.leader = formatting.escape_markdown(token.leader)
token.leader = markdown.escape(token.leader)
else:
token.leader = formatting.escape_markdown("⦁")
token.leader = markdown.escape("⦁")
return super().render_list_item(token, max_line_length)

def render_link_reference_definition(
self, token: LinkReferenceDefinition
) -> Iterable[Fragment]:
yield from (
Fragment(
markdown_symbol.link + formatting.mlink(
markdown_symbol.link + markdown.link(
content=token.title if token.title else token.label,
url=token.dest,
escape=True
Expand Down Expand Up @@ -280,24 +280,24 @@ def render_link_or_image(
)
else:
yield Fragment(
formatting.mlink(url=target, content=title, escape=True)
markdown.link(href=target, body=title)
)
elif token.dest_type == "full":
# "[" description "][" label "]"
yield from (
Fragment(formatting.escape_markdown("[")),
Fragment(markdown.escape("[")),
Fragment(token.label, wordwrap=True),
Fragment(formatting.escape_markdown("]")),
Fragment(markdown.escape("]")),
)
elif token.dest_type == "collapsed":
# "[" description "][]"
yield Fragment(formatting.escape_markdown("[]")),
yield Fragment(markdown.escape("[]")),
else:
# "[" description "]"
pass

def render_auto_link(self, token: span_token.AutoLink) -> Iterable[Fragment]:
yield Fragment(formatting.escape_markdown("<") + token.children[0].content + formatting.escape_markdown(">"))
yield Fragment(markdown.escape("<") + token.children[0].content + markdown.escape(">"))

def render_escape_sequence(
self, token: span_token.EscapeSequence
Expand All @@ -311,4 +311,4 @@ def render_table(
) -> Iterable[str]:
# note: column widths are not preserved; they are automatically adjusted to fit the contents.
fs = super().render_table(token, max_line_length)
return [formatting.mcode("\n".join(fs))]
return [markdown.code(markdown.escape("\n".join(fs)))]
39 changes: 12 additions & 27 deletions src/telegramify_markdown/type.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
from abc import ABCMeta
import dataclasses
from enum import Enum
from typing import Tuple, List, Any, Union

from pydantic import BaseModel, Field

TaskType = Tuple[str, List[Tuple[Any, Any]]]
SentType = List[Union["Text", "File", "Photo"]]

Expand All @@ -14,50 +12,37 @@ class ContentTypes(Enum):
PHOTO = "photo"


class ContentTrace(BaseModel):
"""
The content trace.
- content: str
- content_type: ContentTypes
"""
@dataclasses.dataclass
class ContentTrace:
source_type: str
extra: dict = Field(default_factory=dict)

extra: dict = dataclasses.field(default_factory=dict, kw_only=True)

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


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


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

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

content_trace: ContentTrace
caption: str = ""
content_type: ContentTypes = ContentTypes.FILE


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

caption: str = ""
"""Please use render_lines_func to render the content."""
content_trace: ContentTrace
caption: str = ""
content_type: ContentTypes = ContentTypes.PHOTO

0 comments on commit cab67d0

Please sign in to comment.