mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-02 03:00:52 +08:00
The Telegram converter protected code with ```[\w]*\n?([\s\S]*?)```,
so an info string such as c++ left "++" as the first code line, a ~~~
block was formatted as prose ("# setup" lost its hash, __init__ became
bold "init"), and a ```` fence showing a ``` example closed at the inner
fence, leaving an empty <pre> and the example leaking out as text.
Replace fenced blocks line by line first: open on ``` or ~~~ and close
on a bare run of the same character that is at least as long. One-line
```code``` and unclosed fences keep the previous regex handling. The
streaming preview strips fences through the same helper.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
55 lines
1.6 KiB
Python
55 lines
1.6 KiB
Python
"""Fenced code blocks in the Telegram markdown -> HTML converter."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import pytest
|
|
|
|
pytest.importorskip("telegram")
|
|
|
|
from deeptutor.partners.channels.telegram import ( # noqa: E402
|
|
_markdown_to_telegram_html,
|
|
_strip_md_block,
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("markdown", "expected"),
|
|
[
|
|
pytest.param(
|
|
"```c++\nint main() {}\n```",
|
|
"<pre><code>int main() {}\n</code></pre>",
|
|
id="info-string-with-symbols",
|
|
),
|
|
pytest.param(
|
|
"~~~python\n# setup\ndef __init__(self): pass\n~~~",
|
|
"<pre><code># setup\ndef __init__(self): pass\n</code></pre>",
|
|
id="tilde-fence",
|
|
),
|
|
pytest.param(
|
|
"````markdown\n```python\nx = 1\n```\n````\nAfter **bold**",
|
|
"<pre><code>```python\nx = 1\n```\n</code></pre>\nAfter <b>bold</b>",
|
|
id="longer-fence-around-example",
|
|
),
|
|
],
|
|
)
|
|
def test_fenced_code_is_rendered_as_code(markdown: str, expected: str) -> None:
|
|
assert _markdown_to_telegram_html(markdown) == expected
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("markdown", "expected"),
|
|
[
|
|
pytest.param(
|
|
"```python\nx = __y__\n```",
|
|
"<pre><code>x = __y__\n</code></pre>",
|
|
id="backtick-fence",
|
|
),
|
|
],
|
|
)
|
|
def test_backtick_fence_output_is_unchanged(markdown: str, expected: str) -> None:
|
|
assert _markdown_to_telegram_html(markdown) == expected
|
|
|
|
|
|
def test_streaming_preview_strips_tilde_fences() -> None:
|
|
assert _strip_md_block("~~~bash\nls\n~~~\nDone") == "ls\n\nDone"
|