chatgpt-md-converter 0.1.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.0/LICENSE +21 -0
- chatgpt_md_converter-0.1.0/PKG-INFO +59 -0
- chatgpt_md_converter-0.1.0/chatgpt_md_converter/__init__.py +0 -0
- chatgpt_md_converter-0.1.0/chatgpt_md_converter/converters.py +21 -0
- chatgpt_md_converter-0.1.0/chatgpt_md_converter/extractors.py +55 -0
- chatgpt_md_converter-0.1.0/chatgpt_md_converter/formatters.py +31 -0
- chatgpt_md_converter-0.1.0/chatgpt_md_converter/helpers.py +8 -0
- chatgpt_md_converter-0.1.0/chatgpt_md_converter/telegram_formatter.py +56 -0
- chatgpt_md_converter-0.1.0/chatgpt_md_converter.egg-info/PKG-INFO +59 -0
- chatgpt_md_converter-0.1.0/chatgpt_md_converter.egg-info/SOURCES.txt +13 -0
- chatgpt_md_converter-0.1.0/chatgpt_md_converter.egg-info/dependency_links.txt +1 -0
- chatgpt_md_converter-0.1.0/chatgpt_md_converter.egg-info/top_level.txt +1 -0
- chatgpt_md_converter-0.1.0/setup.cfg +4 -0
- chatgpt_md_converter-0.1.0/setup.py +18 -0
- chatgpt_md_converter-0.1.0/tests/test_parser.py +256 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Kostiantyn Kriuchkov
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: chatgpt_md_converter
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A package for converting markdown to HTML for chat Telegram bots
|
|
5
|
+
Home-page: https://github.com/Latand/formatter-chatgpt-telegram
|
|
6
|
+
Author: Kostiantyn Kriuchkov
|
|
7
|
+
Author-email: latand666@gmail.com
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Requires-Python: >=3.8
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
|
|
15
|
+
# ChatGPT Markdown to Telegram HTML Parser
|
|
16
|
+
|
|
17
|
+
## Overview
|
|
18
|
+
|
|
19
|
+
This project provides a solution for converting Markdown formatted text into HTML markup supported by the Telegram Bot API, specifically tailored for use in ChatGPT bots developed with the OpenAI API. It includes features for handling various Markdown elements and ensures proper tag closure, making it suitable for streaming mode applications.
|
|
20
|
+
|
|
21
|
+
## Features
|
|
22
|
+
|
|
23
|
+
- Converts Markdown syntax to Telegram-compatible HTML.
|
|
24
|
+
- Supports inline code, bold, italic, underline, and strikethrough formatting.
|
|
25
|
+
- Handles nested bold and italic formatting.
|
|
26
|
+
- Converts Markdown links and lists to their HTML equivalents.
|
|
27
|
+
- Processes code blocks with optional language specification, preserving formatting within `<pre><code>` tags.
|
|
28
|
+
- Automatically appends missing closing delimiters for code blocks.
|
|
29
|
+
- Escapes HTML special characters to prevent unwanted HTML rendering outside code blocks.
|
|
30
|
+
|
|
31
|
+
## Usage
|
|
32
|
+
|
|
33
|
+
To use the Markdown to Telegram HTML Parser in your ChatGPT bot, integrate the provided Python functions into your bot's processing pipeline. Here is a brief overview of how to incorporate the parser:
|
|
34
|
+
|
|
35
|
+
1. **Ensure Closing Delimiters**: Automatically appends missing closing delimiters for `` ` `` and ``` ``` ``` to ensure proper parsing.
|
|
36
|
+
|
|
37
|
+
2. **Extract and Convert Code Blocks**: Extracts Markdown code blocks, converts them to HTML `<pre><code>` format, and replaces them with placeholders to prevent formatting within code blocks.
|
|
38
|
+
|
|
39
|
+
3. **Markdown to HTML Conversion**: Applies various regex substitutions and custom logic to convert supported Markdown formatting to Telegram-compatible HTML tags.
|
|
40
|
+
|
|
41
|
+
4. **Reinsert Code Blocks**: Reinserts the previously extracted and converted code blocks back into the main text, replacing placeholders with the appropriate HTML content.
|
|
42
|
+
|
|
43
|
+
Simply call the `telegram_format(text: str) -> str` function with your Markdown-formatted text as input to receive the converted HTML output ready for use with the Telegram Bot API.
|
|
44
|
+
|
|
45
|
+
## Example
|
|
46
|
+
|
|
47
|
+
```python
|
|
48
|
+
formatted_text = telegram_format("Here is some **bold**, __underline__, and `inline code`.\n```python\nprint('Hello, world!')\n```")
|
|
49
|
+
print(formatted_text)
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Requirements
|
|
53
|
+
|
|
54
|
+
- Python 3.x
|
|
55
|
+
- No external libraries required (uses built-in `re` module for regex operations)
|
|
56
|
+
|
|
57
|
+
## Contribution
|
|
58
|
+
|
|
59
|
+
Feel free to contribute to this project by submitting pull requests or opening issues for bugs, feature requests, or improvements.
|
|
File without changes
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import re
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def convert_html_chars(text: str) -> str:
|
|
5
|
+
"""
|
|
6
|
+
Converts HTML reserved symbols to their respective character references.
|
|
7
|
+
"""
|
|
8
|
+
text = text.replace("&", "&")
|
|
9
|
+
text = text.replace("<", "<")
|
|
10
|
+
text = text.replace(">", ">")
|
|
11
|
+
return text
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def split_by_tag(out_text: str, md_tag: str, html_tag: str) -> str:
|
|
15
|
+
"""
|
|
16
|
+
Splits the text by markdown tag and replaces it with the specified HTML tag.
|
|
17
|
+
"""
|
|
18
|
+
tag_pattern = re.compile(
|
|
19
|
+
r"{}(.*?){}".format(re.escape(md_tag), re.escape(md_tag)), re.DOTALL
|
|
20
|
+
)
|
|
21
|
+
return tag_pattern.sub(r"<{}>\1</{}>".format(html_tag, html_tag), out_text)
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import re
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def ensure_closing_delimiters(text: str) -> str:
|
|
5
|
+
"""
|
|
6
|
+
Ensures that if an opening ` or ``` is found without a matching closing delimiter,
|
|
7
|
+
the missing delimiter is appended to the end of the text.
|
|
8
|
+
"""
|
|
9
|
+
# For triple backticks
|
|
10
|
+
if text.count("```") % 2 != 0:
|
|
11
|
+
text += "```"
|
|
12
|
+
# For single backticks
|
|
13
|
+
if text.count("`") % 2 != 0:
|
|
14
|
+
text += "`"
|
|
15
|
+
return text
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def extract_and_convert_code_blocks(text: str):
|
|
19
|
+
"""
|
|
20
|
+
Extracts code blocks from the text, converting them to HTML <pre><code> format,
|
|
21
|
+
and replaces them with placeholders. Also ensures closing delimiters for unmatched blocks.
|
|
22
|
+
"""
|
|
23
|
+
text = ensure_closing_delimiters(text)
|
|
24
|
+
placeholders = []
|
|
25
|
+
code_blocks = {}
|
|
26
|
+
|
|
27
|
+
def replacer(match):
|
|
28
|
+
language = match.group(1) if match.group(1) else ""
|
|
29
|
+
code_content = match.group(3)
|
|
30
|
+
placeholder = f"CODEBLOCKPLACEHOLDER{len(placeholders)}"
|
|
31
|
+
placeholders.append(placeholder)
|
|
32
|
+
if not language:
|
|
33
|
+
html_code_block = f"<pre><code>{code_content}</code></pre>"
|
|
34
|
+
else:
|
|
35
|
+
html_code_block = (
|
|
36
|
+
f'<pre><code class="language-{language}">{code_content}</code></pre>'
|
|
37
|
+
)
|
|
38
|
+
return (placeholder, html_code_block)
|
|
39
|
+
|
|
40
|
+
modified_text = text
|
|
41
|
+
for match in re.finditer(r"```(\w*)?(\n)?(.*?)```", text, flags=re.DOTALL):
|
|
42
|
+
placeholder, html_code_block = replacer(match)
|
|
43
|
+
code_blocks[placeholder] = html_code_block
|
|
44
|
+
modified_text = modified_text.replace(match.group(0), placeholder, 1)
|
|
45
|
+
|
|
46
|
+
return modified_text, code_blocks
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def reinsert_code_blocks(text: str, code_blocks: dict) -> str:
|
|
50
|
+
"""
|
|
51
|
+
Reinserts HTML code blocks into the text, replacing their placeholders.
|
|
52
|
+
"""
|
|
53
|
+
for placeholder, html_code_block in code_blocks.items():
|
|
54
|
+
text = text.replace(placeholder, html_code_block, 1)
|
|
55
|
+
return text
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import re
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def combine_blockquotes(text: str) -> str:
|
|
5
|
+
"""
|
|
6
|
+
Combines multiline blockquotes into a single blockquote.
|
|
7
|
+
"""
|
|
8
|
+
lines = text.split("\n")
|
|
9
|
+
combined_lines = []
|
|
10
|
+
blockquote_lines = []
|
|
11
|
+
in_blockquote = False
|
|
12
|
+
|
|
13
|
+
for line in lines:
|
|
14
|
+
if line.startswith(">"):
|
|
15
|
+
in_blockquote = True
|
|
16
|
+
blockquote_lines.append(line[1:].strip())
|
|
17
|
+
else:
|
|
18
|
+
if in_blockquote:
|
|
19
|
+
combined_lines.append(
|
|
20
|
+
"<blockquote>" + " ".join(blockquote_lines) + "</blockquote>"
|
|
21
|
+
)
|
|
22
|
+
blockquote_lines = []
|
|
23
|
+
in_blockquote = False
|
|
24
|
+
combined_lines.append(line)
|
|
25
|
+
|
|
26
|
+
if in_blockquote:
|
|
27
|
+
combined_lines.append(
|
|
28
|
+
"<blockquote>" + " ".join(blockquote_lines) + "</blockquote>"
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
return "\n".join(combined_lines)
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import re
|
|
2
|
+
from .converters import convert_html_chars, split_by_tag
|
|
3
|
+
from .extractors import extract_and_convert_code_blocks, reinsert_code_blocks
|
|
4
|
+
from .formatters import combine_blockquotes
|
|
5
|
+
from .helpers import remove_blockquote_escaping
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def telegram_format(text: str) -> str:
|
|
9
|
+
"""
|
|
10
|
+
Converts markdown in the provided text to HTML supported by Telegram.
|
|
11
|
+
"""
|
|
12
|
+
# Step 0: Combine blockquotes
|
|
13
|
+
text = combine_blockquotes(text)
|
|
14
|
+
|
|
15
|
+
# Step 1: Convert HTML reserved symbols
|
|
16
|
+
text = convert_html_chars(text)
|
|
17
|
+
|
|
18
|
+
# Step 2: Extract and convert code blocks first
|
|
19
|
+
output, code_blocks = extract_and_convert_code_blocks(text)
|
|
20
|
+
|
|
21
|
+
# Step 3: Escape HTML special characters in the output text
|
|
22
|
+
output = output.replace("<", "<").replace(">", ">")
|
|
23
|
+
|
|
24
|
+
# Inline code
|
|
25
|
+
output = re.sub(r"`(.*?)`", r"<code>\1</code>", output)
|
|
26
|
+
|
|
27
|
+
# Nested Bold and Italic
|
|
28
|
+
output = re.sub(r"\*\*\*(.*?)\*\*\*", r"<b><i>\1</i></b>", output)
|
|
29
|
+
|
|
30
|
+
# Process markdown formatting tags (bold, underline, italic, strikethrough)
|
|
31
|
+
# and convert them to their respective HTML tags
|
|
32
|
+
output = split_by_tag(output, "**", "b")
|
|
33
|
+
output = split_by_tag(output, "__", "u")
|
|
34
|
+
output = split_by_tag(output, "_", "i")
|
|
35
|
+
output = split_by_tag(output, "*", "i")
|
|
36
|
+
output = split_by_tag(output, "~~", "s")
|
|
37
|
+
|
|
38
|
+
# Remove storage links
|
|
39
|
+
output = re.sub(r"【[^】]+】", "", output)
|
|
40
|
+
|
|
41
|
+
# Convert links
|
|
42
|
+
output = re.sub(r"\[(.*?)\]\((.*?)\)", r'<a href="\2">\1</a>', output)
|
|
43
|
+
|
|
44
|
+
# Convert lists
|
|
45
|
+
output = re.sub(r"^\s*[\-\*] (.+)", r"• \1", output, flags=re.MULTILINE)
|
|
46
|
+
|
|
47
|
+
# Convert headings
|
|
48
|
+
output = re.sub(r"^\s*#+ (.+)", r"<b>\1</b>", output, flags=re.MULTILINE)
|
|
49
|
+
|
|
50
|
+
# Step 4: Reinsert the converted HTML code blocks
|
|
51
|
+
output = reinsert_code_blocks(output, code_blocks)
|
|
52
|
+
|
|
53
|
+
# Step 5: Remove blockquote escaping
|
|
54
|
+
output = remove_blockquote_escaping(output)
|
|
55
|
+
|
|
56
|
+
return output
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: chatgpt_md_converter
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A package for converting markdown to HTML for chat Telegram bots
|
|
5
|
+
Home-page: https://github.com/Latand/formatter-chatgpt-telegram
|
|
6
|
+
Author: Kostiantyn Kriuchkov
|
|
7
|
+
Author-email: latand666@gmail.com
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Requires-Python: >=3.8
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
|
|
15
|
+
# ChatGPT Markdown to Telegram HTML Parser
|
|
16
|
+
|
|
17
|
+
## Overview
|
|
18
|
+
|
|
19
|
+
This project provides a solution for converting Markdown formatted text into HTML markup supported by the Telegram Bot API, specifically tailored for use in ChatGPT bots developed with the OpenAI API. It includes features for handling various Markdown elements and ensures proper tag closure, making it suitable for streaming mode applications.
|
|
20
|
+
|
|
21
|
+
## Features
|
|
22
|
+
|
|
23
|
+
- Converts Markdown syntax to Telegram-compatible HTML.
|
|
24
|
+
- Supports inline code, bold, italic, underline, and strikethrough formatting.
|
|
25
|
+
- Handles nested bold and italic formatting.
|
|
26
|
+
- Converts Markdown links and lists to their HTML equivalents.
|
|
27
|
+
- Processes code blocks with optional language specification, preserving formatting within `<pre><code>` tags.
|
|
28
|
+
- Automatically appends missing closing delimiters for code blocks.
|
|
29
|
+
- Escapes HTML special characters to prevent unwanted HTML rendering outside code blocks.
|
|
30
|
+
|
|
31
|
+
## Usage
|
|
32
|
+
|
|
33
|
+
To use the Markdown to Telegram HTML Parser in your ChatGPT bot, integrate the provided Python functions into your bot's processing pipeline. Here is a brief overview of how to incorporate the parser:
|
|
34
|
+
|
|
35
|
+
1. **Ensure Closing Delimiters**: Automatically appends missing closing delimiters for `` ` `` and ``` ``` ``` to ensure proper parsing.
|
|
36
|
+
|
|
37
|
+
2. **Extract and Convert Code Blocks**: Extracts Markdown code blocks, converts them to HTML `<pre><code>` format, and replaces them with placeholders to prevent formatting within code blocks.
|
|
38
|
+
|
|
39
|
+
3. **Markdown to HTML Conversion**: Applies various regex substitutions and custom logic to convert supported Markdown formatting to Telegram-compatible HTML tags.
|
|
40
|
+
|
|
41
|
+
4. **Reinsert Code Blocks**: Reinserts the previously extracted and converted code blocks back into the main text, replacing placeholders with the appropriate HTML content.
|
|
42
|
+
|
|
43
|
+
Simply call the `telegram_format(text: str) -> str` function with your Markdown-formatted text as input to receive the converted HTML output ready for use with the Telegram Bot API.
|
|
44
|
+
|
|
45
|
+
## Example
|
|
46
|
+
|
|
47
|
+
```python
|
|
48
|
+
formatted_text = telegram_format("Here is some **bold**, __underline__, and `inline code`.\n```python\nprint('Hello, world!')\n```")
|
|
49
|
+
print(formatted_text)
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Requirements
|
|
53
|
+
|
|
54
|
+
- Python 3.x
|
|
55
|
+
- No external libraries required (uses built-in `re` module for regex operations)
|
|
56
|
+
|
|
57
|
+
## Contribution
|
|
58
|
+
|
|
59
|
+
Feel free to contribute to this project by submitting pull requests or opening issues for bugs, feature requests, or improvements.
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
setup.py
|
|
3
|
+
chatgpt_md_converter/__init__.py
|
|
4
|
+
chatgpt_md_converter/converters.py
|
|
5
|
+
chatgpt_md_converter/extractors.py
|
|
6
|
+
chatgpt_md_converter/formatters.py
|
|
7
|
+
chatgpt_md_converter/helpers.py
|
|
8
|
+
chatgpt_md_converter/telegram_formatter.py
|
|
9
|
+
chatgpt_md_converter.egg-info/PKG-INFO
|
|
10
|
+
chatgpt_md_converter.egg-info/SOURCES.txt
|
|
11
|
+
chatgpt_md_converter.egg-info/dependency_links.txt
|
|
12
|
+
chatgpt_md_converter.egg-info/top_level.txt
|
|
13
|
+
tests/test_parser.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
chatgpt_md_converter
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
from setuptools import setup
|
|
2
|
+
|
|
3
|
+
setup(
|
|
4
|
+
name="chatgpt_md_converter",
|
|
5
|
+
version="0.1.0",
|
|
6
|
+
author="Kostiantyn Kriuchkov",
|
|
7
|
+
author_email="latand666@gmail.com",
|
|
8
|
+
description="A package for converting markdown to HTML for chat Telegram bots",
|
|
9
|
+
long_description=open("README.MD").read(),
|
|
10
|
+
long_description_content_type="text/markdown",
|
|
11
|
+
url="https://github.com/Latand/formatter-chatgpt-telegram",
|
|
12
|
+
classifiers=[
|
|
13
|
+
"Programming Language :: Python :: 3",
|
|
14
|
+
"License :: OSI Approved :: MIT License",
|
|
15
|
+
"Operating System :: OS Independent",
|
|
16
|
+
],
|
|
17
|
+
python_requires=">=3.8",
|
|
18
|
+
)
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
from chatgpt_md_converter.telegram_formatter import telegram_format
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def test_split_by_tag_bold():
|
|
5
|
+
text = "This is **bold** text"
|
|
6
|
+
assert telegram_format(text) == "This is <b>bold</b> text"
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def test_telegram_format_italic():
|
|
10
|
+
text = "This is _italic_ text"
|
|
11
|
+
output = telegram_format(text)
|
|
12
|
+
assert output == "This is <i>italic</i> text"
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def test_telegram_format_italic_star():
|
|
16
|
+
text = "This is *italic* text"
|
|
17
|
+
output = telegram_format(text)
|
|
18
|
+
assert output == "This is <i>italic</i> text"
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def test_triple_backticks_with_language():
|
|
22
|
+
input_text = "```python\nprint('Hello, world!')\n```"
|
|
23
|
+
expected_output = (
|
|
24
|
+
"<pre><code class=\"language-python\">print('Hello, world!')\n</code></pre>"
|
|
25
|
+
)
|
|
26
|
+
output = telegram_format(input_text)
|
|
27
|
+
assert (
|
|
28
|
+
output == expected_output
|
|
29
|
+
), "Failed converting triple backticks with language to <pre><code> tags"
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def test_bold_and_underline_conversion():
|
|
33
|
+
input_text = "This is **bold** and this is __underline__."
|
|
34
|
+
expected_output = "This is <b>bold</b> and this is <u>underline</u>."
|
|
35
|
+
output = telegram_format(input_text)
|
|
36
|
+
assert output == expected_output, "Failed converting ** and __ to <b> and <u> tags"
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def test_escaping_special_characters():
|
|
40
|
+
input_text = "Avoid using < or > in your HTML."
|
|
41
|
+
expected_output = "Avoid using < or > in your HTML."
|
|
42
|
+
output = telegram_format(input_text)
|
|
43
|
+
assert output == expected_output, "Failed escaping < and > characters"
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def test_nested_markdown_syntax():
|
|
47
|
+
input_text = "This is **bold and _italic_** text."
|
|
48
|
+
expected_output = "This is <b>bold and <i>italic</i></b> text."
|
|
49
|
+
output = telegram_format(input_text)
|
|
50
|
+
assert output == expected_output, "Failed handling nested markdown syntax"
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def test_combination_of_markdown_elements():
|
|
54
|
+
input_text = """
|
|
55
|
+
# Heading
|
|
56
|
+
This is a test of **bold**, __underline__, and `inline code`.
|
|
57
|
+
- Item 1
|
|
58
|
+
* Item 2
|
|
59
|
+
|
|
60
|
+
```python
|
|
61
|
+
for i in range(3):
|
|
62
|
+
print(i)
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
[Link](http://example.com)
|
|
66
|
+
"""
|
|
67
|
+
expected_output = """<b>Heading</b>\nThis is a test of <b>bold</b>, <u>underline</u>, and <code>inline code</code>.\n• Item 1\n• Item 2\n\n<pre><code class="language-python">for i in range(3):\n print(i)\n</code></pre>\n\n<a href="http://example.com">Link</a>\n"""
|
|
68
|
+
output = telegram_format(input_text)
|
|
69
|
+
assert (
|
|
70
|
+
output.strip() == expected_output.strip()
|
|
71
|
+
), "Failed combining multiple markdown elements into HTML"
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def test_nested_bold_within_italic():
|
|
75
|
+
input_text = "This is *__bold within italic__* text."
|
|
76
|
+
expected_output = "This is <i><u>bold within italic</u></i> text."
|
|
77
|
+
output = telegram_format(input_text)
|
|
78
|
+
assert (
|
|
79
|
+
output == expected_output
|
|
80
|
+
), "Failed converting nested bold within italic markdown to HTML"
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def test_italic_within_bold():
|
|
84
|
+
input_text = "This is **bold and _italic_ together**."
|
|
85
|
+
expected_output = "This is <b>bold and <i>italic</i> together</b>."
|
|
86
|
+
output = telegram_format(input_text)
|
|
87
|
+
assert (
|
|
88
|
+
output == expected_output
|
|
89
|
+
), "Failed converting italic within bold markdown to HTML"
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def test_inline_code_within_bold_text():
|
|
93
|
+
input_text = "This is **bold and `inline code` together**."
|
|
94
|
+
expected_output = "This is <b>bold and <code>inline code</code> together</b>."
|
|
95
|
+
output = telegram_format(input_text)
|
|
96
|
+
assert output == expected_output, "Failed handling inline code within bold text"
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def test_mixed_formatting_tags_with_lists_and_links():
|
|
100
|
+
input_text = """
|
|
101
|
+
- This is a list item with **bold**, __underline__, and [a link](http://example.com)
|
|
102
|
+
- Another item with ***bold and italic*** text
|
|
103
|
+
"""
|
|
104
|
+
expected_output = """
|
|
105
|
+
• This is a list item with <b>bold</b>, <u>underline</u>, and <a href="http://example.com">a link</a>
|
|
106
|
+
• Another item with <b><i>bold and italic</i></b> text
|
|
107
|
+
"""
|
|
108
|
+
output = telegram_format(input_text)
|
|
109
|
+
assert (
|
|
110
|
+
output.strip() == expected_output.strip()
|
|
111
|
+
), "Failed handling mixed formatting tags with lists and links"
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def test_special_characters_within_code_blocks():
|
|
115
|
+
input_text = "Here is a code block: ```<script>alert('Hello')</script>```"
|
|
116
|
+
expected_output = "Here is a code block: <pre><code><script>alert('Hello')</script></code></pre>"
|
|
117
|
+
output = telegram_format(input_text)
|
|
118
|
+
assert (
|
|
119
|
+
output == expected_output
|
|
120
|
+
), "Failed escaping special characters within code blocks"
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def test_code_block_within_bold_text():
|
|
124
|
+
input_text = "This is **bold with a `code block` inside**."
|
|
125
|
+
expected_output = "This is <b>bold with a <code>code block</code> inside</b>."
|
|
126
|
+
output = telegram_format(input_text)
|
|
127
|
+
assert output == expected_output, "Failed handling code block within bold text"
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def test_triple_backticks_with_nested_markdown():
|
|
131
|
+
input_text = "```python\n**bold text** and __underline__ in code block```"
|
|
132
|
+
# Expecting the markdown syntax to be ignored within the code block
|
|
133
|
+
expected_output = '<pre><code class="language-python">**bold text** and __underline__ in code block</code></pre>'
|
|
134
|
+
output = telegram_format(input_text)
|
|
135
|
+
assert (
|
|
136
|
+
output == expected_output
|
|
137
|
+
), "Failed handling markdown within triple backtick code blocks"
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def test_unmatched_code_delimiters():
|
|
141
|
+
input_text = "This has an `unmatched code delimiter."
|
|
142
|
+
# Expecting original input as output due to the unmatched delimiter
|
|
143
|
+
expected_output = "This has an <code>unmatched code delimiter.</code>"
|
|
144
|
+
output = telegram_format(input_text)
|
|
145
|
+
assert output == expected_output, "Failed handling unmatched code delimiters"
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
def test_preformatted_block_with_unusual_language_specification():
|
|
149
|
+
input_text = "```weirdLang\nSome weirdLang code\n```"
|
|
150
|
+
expected_output = (
|
|
151
|
+
'<pre><code class="language-weirdLang">Some weirdLang code\n</code></pre>'
|
|
152
|
+
)
|
|
153
|
+
output = telegram_format(input_text)
|
|
154
|
+
assert (
|
|
155
|
+
output == expected_output
|
|
156
|
+
), "Failed handling preformatted block with unusual language specification"
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
def test_inline_code_within_lists():
|
|
160
|
+
input_text = """
|
|
161
|
+
- List item with `code`
|
|
162
|
+
* Another `code` item
|
|
163
|
+
"""
|
|
164
|
+
expected_output = """
|
|
165
|
+
• List item with <code>code</code>
|
|
166
|
+
• Another <code>code</code> item
|
|
167
|
+
"""
|
|
168
|
+
output = telegram_format(input_text)
|
|
169
|
+
assert (
|
|
170
|
+
output.strip() == expected_output.strip()
|
|
171
|
+
), "Failed handling inline code within lists"
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
def test_vector_storage_links_trim():
|
|
175
|
+
input_text = """
|
|
176
|
+
- List item with `code`
|
|
177
|
+
* Another `code` item【4:0†source】
|
|
178
|
+
"""
|
|
179
|
+
expected_output = """
|
|
180
|
+
• List item with <code>code</code>
|
|
181
|
+
• Another <code>code</code> item
|
|
182
|
+
"""
|
|
183
|
+
output = telegram_format(input_text)
|
|
184
|
+
assert output.strip() == expected_output.strip(), "Failed trim storage links"
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
def test_strikethrough_conversion():
|
|
188
|
+
input_text = "This is ~~strikethrough~~ text."
|
|
189
|
+
expected_output = "This is <s>strikethrough</s> text."
|
|
190
|
+
output = telegram_format(input_text)
|
|
191
|
+
assert output == expected_output, "Failed converting ~~ to <s> tags"
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
def test_blockquote_conversion():
|
|
195
|
+
input_text = "> This is a blockquote."
|
|
196
|
+
expected_output = "<blockquote>This is a blockquote.</blockquote>"
|
|
197
|
+
output = telegram_format(input_text)
|
|
198
|
+
assert output == expected_output, "Failed converting > to <blockquote> tags"
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
def test_inline_url_conversion():
|
|
202
|
+
input_text = "[example](http://example.com)"
|
|
203
|
+
expected_output = '<a href="http://example.com">example</a>'
|
|
204
|
+
output = telegram_format(input_text)
|
|
205
|
+
assert output == expected_output, "Failed converting [text](URL) to <a> tags"
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
def test_inline_mention_conversion():
|
|
209
|
+
input_text = "[User](tg://user?id=123456789)"
|
|
210
|
+
expected_output = '<a href="tg://user?id=123456789">User</a>'
|
|
211
|
+
output = telegram_format(input_text)
|
|
212
|
+
assert (
|
|
213
|
+
output == expected_output
|
|
214
|
+
), "Failed converting [text](tg://user?id=ID) to <a> tags"
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
def test_escaping_ampersand():
|
|
218
|
+
input_text = "Use & in your HTML."
|
|
219
|
+
expected_output = "Use & in your HTML."
|
|
220
|
+
output = telegram_format(input_text)
|
|
221
|
+
assert output == expected_output, "Failed escaping & character"
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
def test_pre_and_code_tags_with_html_entities():
|
|
225
|
+
input_text = "```html\n<div>Content</div>\n```"
|
|
226
|
+
expected_output = (
|
|
227
|
+
'<pre><code class="language-html"><div>Content</div>\n</code></pre>'
|
|
228
|
+
)
|
|
229
|
+
output = telegram_format(input_text)
|
|
230
|
+
assert (
|
|
231
|
+
output == expected_output
|
|
232
|
+
), "Failed handling pre and code tags with HTML entities"
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
def test_code_with_multiple_lines():
|
|
236
|
+
input_text = "```\ndef example():\n return 'example'\n```"
|
|
237
|
+
expected_output = "<pre><code>def example():\n return 'example'\n</code></pre>"
|
|
238
|
+
output = telegram_format(input_text)
|
|
239
|
+
assert output == expected_output, "Failed handling code with multiple lines"
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
def test_combined_formatting_with_lists():
|
|
243
|
+
input_text = """
|
|
244
|
+
- **Bold** list item
|
|
245
|
+
- _Italic_ list item
|
|
246
|
+
- `Code` list item
|
|
247
|
+
"""
|
|
248
|
+
expected_output = """
|
|
249
|
+
• <b>Bold</b> list item
|
|
250
|
+
• <i>Italic</i> list item
|
|
251
|
+
• <code>Code</code> list item
|
|
252
|
+
"""
|
|
253
|
+
output = telegram_format(input_text)
|
|
254
|
+
assert (
|
|
255
|
+
output.strip() == expected_output.strip()
|
|
256
|
+
), "Failed handling combined formatting with lists"
|