chatgpt-md-converter 0.1.2__tar.gz → 0.3.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {chatgpt_md_converter-0.1.2 → chatgpt_md_converter-0.3.0}/PKG-INFO +11 -2
- {chatgpt_md_converter-0.1.2 → chatgpt_md_converter-0.3.0}/chatgpt_md_converter/converters.py +5 -0
- chatgpt_md_converter-0.3.0/chatgpt_md_converter/formatters.py +68 -0
- chatgpt_md_converter-0.3.0/chatgpt_md_converter/helpers.py +27 -0
- chatgpt_md_converter-0.3.0/chatgpt_md_converter/telegram_formatter.py +102 -0
- {chatgpt_md_converter-0.1.2 → chatgpt_md_converter-0.3.0}/chatgpt_md_converter.egg-info/PKG-INFO +11 -2
- {chatgpt_md_converter-0.1.2 → chatgpt_md_converter-0.3.0}/setup.py +1 -1
- chatgpt_md_converter-0.3.0/tests/test_parser.py +722 -0
- chatgpt_md_converter-0.1.2/chatgpt_md_converter/formatters.py +0 -28
- chatgpt_md_converter-0.1.2/chatgpt_md_converter/helpers.py +0 -8
- chatgpt_md_converter-0.1.2/chatgpt_md_converter/telegram_formatter.py +0 -57
- chatgpt_md_converter-0.1.2/tests/test_parser.py +0 -398
- {chatgpt_md_converter-0.1.2 → chatgpt_md_converter-0.3.0}/LICENSE +0 -0
- {chatgpt_md_converter-0.1.2 → chatgpt_md_converter-0.3.0}/chatgpt_md_converter/__init__.py +0 -0
- {chatgpt_md_converter-0.1.2 → chatgpt_md_converter-0.3.0}/chatgpt_md_converter/extractors.py +0 -0
- {chatgpt_md_converter-0.1.2 → chatgpt_md_converter-0.3.0}/chatgpt_md_converter.egg-info/SOURCES.txt +0 -0
- {chatgpt_md_converter-0.1.2 → chatgpt_md_converter-0.3.0}/chatgpt_md_converter.egg-info/dependency_links.txt +0 -0
- {chatgpt_md_converter-0.1.2 → chatgpt_md_converter-0.3.0}/chatgpt_md_converter.egg-info/top_level.txt +0 -0
- {chatgpt_md_converter-0.1.2 → chatgpt_md_converter-0.3.0}/setup.cfg +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
2
|
Name: chatgpt_md_converter
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.3.0
|
|
4
4
|
Summary: A package for converting markdown to HTML for chat Telegram bots
|
|
5
5
|
Home-page: https://github.com/Latand/formatter-chatgpt-telegram
|
|
6
6
|
Author: Kostiantyn Kriuchkov
|
|
@@ -11,6 +11,15 @@ Classifier: Operating System :: OS Independent
|
|
|
11
11
|
Requires-Python: >=3.8
|
|
12
12
|
Description-Content-Type: text/markdown
|
|
13
13
|
License-File: LICENSE
|
|
14
|
+
Dynamic: author
|
|
15
|
+
Dynamic: author-email
|
|
16
|
+
Dynamic: classifier
|
|
17
|
+
Dynamic: description
|
|
18
|
+
Dynamic: description-content-type
|
|
19
|
+
Dynamic: home-page
|
|
20
|
+
Dynamic: license-file
|
|
21
|
+
Dynamic: requires-python
|
|
22
|
+
Dynamic: summary
|
|
14
23
|
|
|
15
24
|
# ChatGPT Markdown to Telegram HTML Parser
|
|
16
25
|
|
{chatgpt_md_converter-0.1.2 → chatgpt_md_converter-0.3.0}/chatgpt_md_converter/converters.py
RENAMED
|
@@ -19,4 +19,9 @@ def split_by_tag(out_text: str, md_tag: str, html_tag: str) -> str:
|
|
|
19
19
|
r"(?<!\w){}(.*?){}(?!\w)".format(re.escape(md_tag), re.escape(md_tag)),
|
|
20
20
|
re.DOTALL,
|
|
21
21
|
)
|
|
22
|
+
|
|
23
|
+
# Special handling for the tg-spoiler tag
|
|
24
|
+
if html_tag == 'span class="tg-spoiler"':
|
|
25
|
+
return tag_pattern.sub(r'<span class="tg-spoiler">\1</span>', out_text)
|
|
26
|
+
|
|
22
27
|
return tag_pattern.sub(r"<{}>\1</{}>".format(html_tag, html_tag), out_text)
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
def combine_blockquotes(text: str) -> str:
|
|
2
|
+
"""
|
|
3
|
+
Combines multiline blockquotes into a single blockquote while keeping the \n characters.
|
|
4
|
+
Supports both regular blockquotes (>) and expandable blockquotes (**>).
|
|
5
|
+
"""
|
|
6
|
+
lines = text.split("\n")
|
|
7
|
+
combined_lines = []
|
|
8
|
+
blockquote_lines = []
|
|
9
|
+
in_blockquote = False
|
|
10
|
+
is_expandable = False
|
|
11
|
+
|
|
12
|
+
for line in lines:
|
|
13
|
+
if line.startswith("**>"):
|
|
14
|
+
# Expandable blockquote
|
|
15
|
+
in_blockquote = True
|
|
16
|
+
is_expandable = True
|
|
17
|
+
blockquote_lines.append(line[3:].strip())
|
|
18
|
+
elif line.startswith(">"):
|
|
19
|
+
# Regular blockquote
|
|
20
|
+
if not in_blockquote:
|
|
21
|
+
# This is a new blockquote
|
|
22
|
+
in_blockquote = True
|
|
23
|
+
is_expandable = False
|
|
24
|
+
blockquote_lines.append(line[1:].strip())
|
|
25
|
+
else:
|
|
26
|
+
if in_blockquote:
|
|
27
|
+
# End of blockquote, combine the lines
|
|
28
|
+
if is_expandable:
|
|
29
|
+
combined_lines.append(
|
|
30
|
+
"<blockquote expandable>"
|
|
31
|
+
+ "\n".join(blockquote_lines)
|
|
32
|
+
+ "</blockquote>"
|
|
33
|
+
)
|
|
34
|
+
else:
|
|
35
|
+
combined_lines.append(
|
|
36
|
+
"<blockquote>" + "\n".join(blockquote_lines) + "</blockquote>"
|
|
37
|
+
)
|
|
38
|
+
blockquote_lines = []
|
|
39
|
+
in_blockquote = False
|
|
40
|
+
is_expandable = False
|
|
41
|
+
combined_lines.append(line)
|
|
42
|
+
|
|
43
|
+
if in_blockquote:
|
|
44
|
+
# Handle the case where the file ends with a blockquote
|
|
45
|
+
if is_expandable:
|
|
46
|
+
combined_lines.append(
|
|
47
|
+
"<blockquote expandable>"
|
|
48
|
+
+ "\n".join(blockquote_lines)
|
|
49
|
+
+ "</blockquote>"
|
|
50
|
+
)
|
|
51
|
+
else:
|
|
52
|
+
combined_lines.append(
|
|
53
|
+
"<blockquote>" + "\n".join(blockquote_lines) + "</blockquote>"
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
return "\n".join(combined_lines)
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def fix_asterisk_equations(text: str) -> str:
|
|
60
|
+
"""
|
|
61
|
+
Replaces numeric expressions with '*' in them with '×'
|
|
62
|
+
to avoid accidental italic formatting.
|
|
63
|
+
e.g. '6*8' -> '6×8', '6 * 8' -> '6×8'
|
|
64
|
+
"""
|
|
65
|
+
import re
|
|
66
|
+
|
|
67
|
+
eq_pattern = re.compile(r"(\d+)\s*\*\s*(\d+)")
|
|
68
|
+
return eq_pattern.sub(r"\1×\2", text)
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
def remove_blockquote_escaping(output: str) -> str:
|
|
2
|
+
"""
|
|
3
|
+
Removes the escaping from blockquote tags, including expandable blockquotes.
|
|
4
|
+
"""
|
|
5
|
+
# Regular blockquotes
|
|
6
|
+
output = output.replace("<blockquote>", "<blockquote>").replace(
|
|
7
|
+
"</blockquote>", "</blockquote>"
|
|
8
|
+
)
|
|
9
|
+
|
|
10
|
+
# Expandable blockquotes
|
|
11
|
+
output = output.replace(
|
|
12
|
+
"<blockquote expandable>", "<blockquote expandable>"
|
|
13
|
+
).replace("</blockquote>", "</blockquote>")
|
|
14
|
+
|
|
15
|
+
return output
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def remove_spoiler_escaping(output: str) -> str:
|
|
19
|
+
"""
|
|
20
|
+
Ensures spoiler tags are correctly formatted (rather than being escaped).
|
|
21
|
+
"""
|
|
22
|
+
# Fix any incorrectly escaped spoiler tags
|
|
23
|
+
output = output.replace(
|
|
24
|
+
'<span class="tg-spoiler">', '<span class="tg-spoiler">'
|
|
25
|
+
)
|
|
26
|
+
output = output.replace("</span>", "</span>")
|
|
27
|
+
return output
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import re
|
|
2
|
+
|
|
3
|
+
from .converters import convert_html_chars, split_by_tag
|
|
4
|
+
from .extractors import extract_and_convert_code_blocks, reinsert_code_blocks
|
|
5
|
+
from .formatters import combine_blockquotes
|
|
6
|
+
from .helpers import remove_blockquote_escaping, remove_spoiler_escaping
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def extract_inline_code_snippets(text: str):
|
|
10
|
+
"""
|
|
11
|
+
Extracts inline code (single-backtick content) from the text,
|
|
12
|
+
replacing it with placeholders, returning modified text and a dict of placeholders -> code text.
|
|
13
|
+
This ensures characters like '*' or '_' inside inline code won't be interpreted as Markdown.
|
|
14
|
+
"""
|
|
15
|
+
placeholders = []
|
|
16
|
+
code_snippets = {}
|
|
17
|
+
inline_code_pattern = re.compile(r"`([^`]+)`")
|
|
18
|
+
|
|
19
|
+
def replacer(match):
|
|
20
|
+
snippet = match.group(1)
|
|
21
|
+
placeholder = f"INLINECODEPLACEHOLDER{len(placeholders)}"
|
|
22
|
+
placeholders.append(placeholder)
|
|
23
|
+
code_snippets[placeholder] = snippet
|
|
24
|
+
return placeholder
|
|
25
|
+
|
|
26
|
+
new_text = inline_code_pattern.sub(replacer, text)
|
|
27
|
+
return new_text, code_snippets
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def telegram_format(text: str) -> str:
|
|
31
|
+
"""
|
|
32
|
+
Converts markdown in the provided text to HTML supported by Telegram.
|
|
33
|
+
"""
|
|
34
|
+
|
|
35
|
+
# Step 0: Combine blockquotes
|
|
36
|
+
text = combine_blockquotes(text)
|
|
37
|
+
|
|
38
|
+
# Step 1: Convert HTML reserved symbols
|
|
39
|
+
text = convert_html_chars(text)
|
|
40
|
+
|
|
41
|
+
# Step 2: Extract and convert triple-backtick code blocks first
|
|
42
|
+
output, triple_code_blocks = extract_and_convert_code_blocks(text)
|
|
43
|
+
|
|
44
|
+
# Step 2.5: Extract inline code snippets (single backticks) so they won't be parsed as italics, etc.
|
|
45
|
+
output, inline_code_snippets = extract_inline_code_snippets(output)
|
|
46
|
+
|
|
47
|
+
# Step 3: Escape HTML special characters in the output text (for non-code parts)
|
|
48
|
+
# We do NOT want to escape what's inside placeholders here, only what's outside code placeholders.
|
|
49
|
+
output = output.replace("<", "<").replace(">", ">")
|
|
50
|
+
|
|
51
|
+
# Convert headings (H1-H6)
|
|
52
|
+
output = re.sub(r"^(#{1,6})\s+(.+)$", r"<b>\2</b>", output, flags=re.MULTILINE)
|
|
53
|
+
|
|
54
|
+
# Convert unordered lists (do this before italic detection so that leading '*' is recognized as bullet)
|
|
55
|
+
output = re.sub(r"^(\s*)[\-\*]\s+(.+)$", r"\1• \2", output, flags=re.MULTILINE)
|
|
56
|
+
|
|
57
|
+
# Nested Bold and Italic
|
|
58
|
+
output = re.sub(r"\*\*\*(.*?)\*\*\*", r"<b><i>\1</i></b>", output)
|
|
59
|
+
output = re.sub(r"\_\_\_(.*?)\_\_\_", r"<u><i>\1</i></u>", output)
|
|
60
|
+
|
|
61
|
+
# Process markdown for bold (**), underline (__), strikethrough (~~), and spoiler (||)
|
|
62
|
+
output = split_by_tag(output, "**", "b")
|
|
63
|
+
output = split_by_tag(output, "__", "u")
|
|
64
|
+
output = split_by_tag(output, "~~", "s")
|
|
65
|
+
output = split_by_tag(output, "||", 'span class="tg-spoiler"')
|
|
66
|
+
|
|
67
|
+
# Custom approach for single-asterisk italic
|
|
68
|
+
italic_pattern = re.compile(
|
|
69
|
+
r"(?<![A-Za-z0-9])\*(?=[^\s])(.*?)(?<!\s)\*(?![A-Za-z0-9])", re.DOTALL
|
|
70
|
+
)
|
|
71
|
+
output = italic_pattern.sub(r"<i>\1</i>", output)
|
|
72
|
+
|
|
73
|
+
# Process single underscore-based italic
|
|
74
|
+
output = split_by_tag(output, "_", "i")
|
|
75
|
+
|
|
76
|
+
# Remove storage links (Vector storage placeholders like 【4:0†source】)
|
|
77
|
+
output = re.sub(r"【[^】]+】", "", output)
|
|
78
|
+
|
|
79
|
+
# Convert Markdown links/images to <a href="">…</a>
|
|
80
|
+
link_pattern = r"(?:!?)\[((?:[^\[\]]|\[.*?\])*)\]\(([^)]+)\)"
|
|
81
|
+
output = re.sub(link_pattern, r'<a href="\2">\1</a>', output)
|
|
82
|
+
|
|
83
|
+
# Step 3.5: Reinsert inline code snippets, escaping special chars in code content
|
|
84
|
+
for placeholder, snippet in inline_code_snippets.items():
|
|
85
|
+
escaped_snippet = (
|
|
86
|
+
snippet.replace("&", "&").replace("<", "<").replace(">", ">")
|
|
87
|
+
)
|
|
88
|
+
output = output.replace(placeholder, f"<code>{escaped_snippet}</code>")
|
|
89
|
+
|
|
90
|
+
# Step 4: Reinsert the converted triple-backtick code blocks
|
|
91
|
+
output = reinsert_code_blocks(output, triple_code_blocks)
|
|
92
|
+
|
|
93
|
+
# Step 5: Remove blockquote escaping
|
|
94
|
+
output = remove_blockquote_escaping(output)
|
|
95
|
+
|
|
96
|
+
# Step 6: Remove spoiler tag escaping
|
|
97
|
+
output = remove_spoiler_escaping(output)
|
|
98
|
+
|
|
99
|
+
# Clean up multiple consecutive newlines, but preserve intentional spacing
|
|
100
|
+
output = re.sub(r"\n{3,}", "\n\n", output)
|
|
101
|
+
|
|
102
|
+
return output.strip()
|
{chatgpt_md_converter-0.1.2 → chatgpt_md_converter-0.3.0}/chatgpt_md_converter.egg-info/PKG-INFO
RENAMED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
2
|
Name: chatgpt_md_converter
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.3.0
|
|
4
4
|
Summary: A package for converting markdown to HTML for chat Telegram bots
|
|
5
5
|
Home-page: https://github.com/Latand/formatter-chatgpt-telegram
|
|
6
6
|
Author: Kostiantyn Kriuchkov
|
|
@@ -11,6 +11,15 @@ Classifier: Operating System :: OS Independent
|
|
|
11
11
|
Requires-Python: >=3.8
|
|
12
12
|
Description-Content-Type: text/markdown
|
|
13
13
|
License-File: LICENSE
|
|
14
|
+
Dynamic: author
|
|
15
|
+
Dynamic: author-email
|
|
16
|
+
Dynamic: classifier
|
|
17
|
+
Dynamic: description
|
|
18
|
+
Dynamic: description-content-type
|
|
19
|
+
Dynamic: home-page
|
|
20
|
+
Dynamic: license-file
|
|
21
|
+
Dynamic: requires-python
|
|
22
|
+
Dynamic: summary
|
|
14
23
|
|
|
15
24
|
# ChatGPT Markdown to Telegram HTML Parser
|
|
16
25
|
|