Skip to content

Commit

Permalink
✨ Feature: Add support for rendering Telegram markdown citation format.
Browse files Browse the repository at this point in the history
  • Loading branch information
yym68686 committed Oct 25, 2024
1 parent 1a82b6d commit ea09f1b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

setup(
name="md2tgmd",
version="0.3.5",
version="0.3.6",
description="md2tgmd is a Markdown to Telegram-specific-markdown converter.",
long_description=Path("README.md").open(encoding="utf-8").read(),
long_description_content_type="text/markdown",
Expand Down
11 changes: 11 additions & 0 deletions src/latex2unicode.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ def __init__(self):
"\\textminus": "\u2212",

# Greek alphabet
"\\sin": "sin",
"\\lim": "lim",
"\\log": "log",
"\\ln": "ln",
"\\cos": "cos",
"\\tan": "tan",
"\\cot": "cot",
"\\sec": "sec",
"\\csc": "csc",
"\\alpha": "α",
"\\beta": "β",
"\\Gamma": "Γ",
Expand Down Expand Up @@ -774,6 +783,8 @@ def convert(self, latex):
print(result) # 预期输出: α + β = γ
result = latex2unicode.convert("\Delta v是速度的变化量")
print(result) # 预期输出: α + β = γ
result = latex2unicode.convert("\sin \lim")
print(result) # 预期输出: α + β = γ
result = latex2unicode.convert(r"a = \frac{27.8}{3.85} \approx 7.22")
print(result) # 预期输出: α + β = γ
result = latex2unicode.convert(r"a = \frac{27.8 \, \text{m/s} - 0 \, \text{m/s}}{3.85 \, \text{s}}")
Expand Down
34 changes: 33 additions & 1 deletion src/md2tgmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,42 @@ def escape(text, flag=0, italic=True):
text = re.sub(r"~", '\~', text)
text = re.sub(r"\@{3}(.*?)\@{3}", '~\\1~', text)

# text = re.sub(r"\n>\s", '\n@*@ ', text, count=1)
# matches = list(re.finditer(r"\n>\s", text))
# if len(matches) >= 6:
# # 获取第五个匹配项的位置
# fifth_match = matches[5]
# start, end = fifth_match.span()

# # 只替换第五个匹配项
# text = text[:start] + '\n@*@ ' + text[end:]

# # 获取最后一个匹配项的位置
# last_match = matches[-1]
# start, end = last_match.span()

# # 找到该行的结束位置
# line_end = text.find('\n', end)
# if line_end == -1: # 如果是最后一行
# line_end = len(text)

# # 在该行末尾添加 "||"
# text = text[:line_end] + '@!@' + text[line_end:]

text = re.sub(r"\n>\s", '\n@@@ ', text)
# print(text)
text = re.sub(r">", '\>', text)
# text = re.sub(r"\@\*\@", '**>', text)
text = re.sub(r"\@{3}", '>', text)

text = replace_all(text, r"(^#+\s.+?\n+)|```[\D\d\s]+?```", escapeshape)
text = re.sub(r"#", '\#', text)
text = replace_all(text, r"(\+)|\n[\s]*-\s|```[\D\d\s]+?```|`[\D\d\s]*?`", escapeplus)

# 数字序号
text = re.sub(r"\n{1,2}(\s*\d{1,2}\.\s)", '\n\n\\1', text)
# # 把 code block 以外的 - 替换掉

# 把 code block 以外的 - 替换掉
text = replace_all(text, r"```[\D\d\s]+?```|(-)", escapeminus2)
text = re.sub(r"-", '@<+@', text)
text = re.sub(r"\@\+\>\@", '-', text)
Expand All @@ -203,12 +233,14 @@ def escape(text, flag=0, italic=True):
text = re.sub(r"\@{3}([\D\d\s]+?)\@{3}", '```\\1```', text)
text = re.sub(r"=", '\=', text)
text = re.sub(r"\|", '\|', text)
# text = re.sub(r"\@\!\@", '||', text)
text = re.sub(r"{", '\{', text)
text = re.sub(r"}", '\}', text)
text = re.sub(r"\.", '\.', text)
text = re.sub(r"!", '\!', text)
text = find_lines_with_char(text, '`', 5)
text = replace_all(text, r"(\n+\x20*```[\D\d\s]+?```\n+)", dedent_space)
# print(text)
return text

text = r'''
Expand Down

0 comments on commit ea09f1b

Please sign in to comment.