DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.
Python ai-agentsai-tutorclawbotcli-tooldeepresearchinteractive-learninglearningmulti-agent-systemsrag
fix(partners): render every fenced code block as code in Telegram HTML
Hui7fdadd76cf8b
diff --git a/deeptutor/partners/channels/telegram.py b/deeptutor/partners/channels/telegram.pyindex 1a8b582f9..ca1e7bddf 100644--- a/deeptutor/partners/channels/telegram.py+++ b/deeptutor/partners/channels/telegram.py@@ -6,7 +6,7 @@ import asynciofrom dataclasses import dataclassimport reimport time-from typing import Any, Literal+from typing import Any, Callable, Literalimport unicodedatafrom loguru import logger@@ -48,13 +48,49 @@ class _StreamBuf:stream_id: str | None = None+_FENCE_OPEN_RE = re.compile(r"^[ \t]*(`{3,}|~{3,})(.*)$")+_INLINE_FENCE_RE = re.compile(r"```[\w]*\n?([\s\S]*?)```")+++def _replace_fenced_code(text: str, replace: Callable[[str], str]) -> str:+ """Replace fenced code blocks with ``replace(code)``.++ A block opens on a ``` or ~~~ line (a backtick fence's info string cannot+ contain a backtick) and closes on a line holding only a run of the same+ character that is at least as long, so tilde fences work, an info string+ such as ``c++`` stays out of the code, and a ```` fence can show a ```+ example. What remains (one-line ```code``` and unclosed fences) keeps the+ previous regex handling.+ """+ lines = text.split("\n")+ out: list[str] = []+ i = 0+ while i < len(lines):+ match = _FENCE_OPEN_RE.match(lines[i])+ if match and not (match.group(1)[0] == "`" and "`" in match.group(2)):+ fence = match.group(1)+ for j in range(i + 1, len(lines)):+ closing = lines[j].strip()+ if closing == fence[0] * len(closing) and len(closing) >= len(fence):+ out.append(replace("".join(line + "\n" for line in lines[i + 1 : j])))+ i = j + 1+ break+ else:+ out.append(lines[i])+ i += 1+ continue+ out.append(lines[i])+ i += 1+ return _INLINE_FENCE_RE.sub(lambda m: replace(m.group(1)), "\n".join(out))++def _strip_md_block(text: str) -> str:"""Strip block-level and inline markdown for readable plain-text preview.Used during streaming mid-edits so users see clean text instead of rawmarkdown syntax while the response is still being generated."""- text = re.sub(r"```[\w]*\n?([\s\S]*?)```", r"\1", text)+ text = _replace_fenced_code(text, lambda code: code)text = re.sub(r"^#{1,6}\s+(.+)$", r"\1", text, flags=re.MULTILINE)text = re.sub(r"^>\s*(.*)$", r"\1", text, flags=re.MULTILINE)text = re.sub(r"\*\*(.+?)\*\*", r"\1", text)@@ -116,11 +152,11 @@ def _markdown_to_telegram_html(text: str) -> str:# 1. Extract and protect code blocks (preserve content from other processing)code_blocks: list[str] = []- def save_code_block(m: re.Match) -> str:- code_blocks.append(m.group(1))+ def save_code_block(code: str) -> str:+ code_blocks.append(code)return f"\x00CB{len(code_blocks) - 1}\x00"- text = re.sub(r"```[\w]*\n?([\s\S]*?)```", save_code_block, text)+ text = _replace_fenced_code(text, save_code_block)# 1.5. Convert markdown tables to box-drawing (reuse code_block placeholders)lines = text.split("\n")diff --git a/tests/services/partners/test_telegram_markdown_html.py b/tests/services/partners/test_telegram_markdown_html.pynew file mode 100644index 000000000..b629bb392--- /dev/null+++ b/tests/services/partners/test_telegram_markdown_html.py@@ -0,0 +1,54 @@+"""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,+)+++ ("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+++ ("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"