Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

refactor(partners): unify markdown table row splitting across channels

Bingxi Zhao (Frank)1667d070ec56
diff --git a/deeptutor/partners/channels/feishu.py b/deeptutor/partners/channels/feishu.py
index eed5d8aab..149f0ffd2 100644
--- a/deeptutor/partners/channels/feishu.py
+++ b/deeptutor/partners/channels/feishu.py
@@ -20,6 +20,7 @@ from deeptutor.partners.bus.queue import MessageBus
from deeptutor.partners.channels.base import BaseChannel
from deeptutor.partners.config.paths import get_media_dir
from deeptutor.partners.config.schema import DeliveryOverrides, StreamingSupport
+from deeptutor.partners.helpers import split_markdown_table_row
FEISHU_AVAILABLE = importlib.util.find_spec("lark_oapi") is not None
@@ -484,17 +485,8 @@ class FeishuChannel(BaseChannel):
if len(lines) < 3:
return None
- def split(_line: str) -> list[str]:
- # strip("|") collapses leading/trailing empty cells (||col|| → one cell).
- line = _line.strip()
- if line.startswith("|"):
- line = line[1:]
- if line.endswith("|"):
- line = line[:-1]
- return [c.strip() for c in line.split("|")]
-
- headers = split(lines[0])
- rows = [split(_line) for _line in lines[2:]]
+ headers = split_markdown_table_row(lines[0])
+ rows = [split_markdown_table_row(_line) for _line in lines[2:]]
columns = [
{"tag": "column", "name": f"c{i}", "display_name": h, "width": "auto"}
for i, h in enumerate(headers)
diff --git a/deeptutor/partners/channels/slack.py b/deeptutor/partners/channels/slack.py
index 4d438bd64..59aef84a9 100644
--- a/deeptutor/partners/channels/slack.py
+++ b/deeptutor/partners/channels/slack.py
@@ -15,8 +15,8 @@ from slackify_markdown import slackify_markdown
from deeptutor.partners.bus.events import OutboundMessage
from deeptutor.partners.bus.queue import MessageBus
from deeptutor.partners.channels.base import BaseChannel
-from deeptutor.partners.helpers import convert_markdown_table_to_labeled_rows
from deeptutor.partners.config.schema import Base, DeliveryOverrides
+from deeptutor.partners.helpers import convert_markdown_table_to_labeled_rows
# Slack's WSS endpoint can be blocked while HTTPS still works (auth_test
# succeeds, Socket Mode hangs) — bound the handshake so the failure is loud.
diff --git a/deeptutor/partners/channels/telegram.py b/deeptutor/partners/channels/telegram.py
index 237d0cedf..5625533d4 100644
--- a/deeptutor/partners/channels/telegram.py
+++ b/deeptutor/partners/channels/telegram.py
@@ -21,7 +21,11 @@ from deeptutor.partners.bus.queue import MessageBus
from deeptutor.partners.channels.base import BaseChannel
from deeptutor.partners.config.paths import get_media_dir
from deeptutor.partners.config.schema import DeliveryOverrides, StreamingSupport
-from deeptutor.partners.helpers import is_markdown_table_separator_row, split_message
+from deeptutor.partners.helpers import (
+ is_markdown_table_separator_row,
+ split_markdown_table_row,
+ split_message,
+)
from deeptutor.services.partners.commands import build_partner_help_text, partner_command_palette
TELEGRAM_MAX_MESSAGE_LEN = 4000 # Telegram message character limit
@@ -80,7 +84,7 @@ def _render_table_box(table_lines: list[str]) -> str:
rows: list[list[str]] = []
has_sep = False
for line in table_lines:
- cells = [_strip_md(c) for c in line.strip().strip("|").split("|")]
+ cells = [_strip_md(c) for c in split_markdown_table_row(line)]
if is_markdown_table_separator_row(cells):
has_sep = True
continue
diff --git a/deeptutor/partners/helpers.py b/deeptutor/partners/helpers.py
index 6d5631a84..67cfa47a7 100644
--- a/deeptutor/partners/helpers.py
+++ b/deeptutor/partners/helpers.py
@@ -74,6 +74,29 @@ def split_message(content: str, max_len: int = 2000) -> list[str]:
return chunks
+def split_markdown_table_row(line: str) -> list[str]:
+ """Split one Markdown pipe-table row into stripped cells.
+
+ Strips exactly one leading and one trailing pipe so leading/trailing empty
+ cells (``|| a |`` / ``| a ||``) survive — ``str.strip("|")`` would collapse
+ them and shift every column.
+ """
+ line = line.strip()
+ if line.startswith("|"):
+ line = line[1:]
+ if line.endswith("|"):
+ line = line[:-1]
+ return [cell.strip() for cell in line.split("|")]
+
+
+def is_markdown_table_separator_row(cells: list[str]) -> bool:
+ """True if *cells* look like a markdown table separator row.
+
+ An all-empty row is not a separator (`all([])` would otherwise be True).
+ """
+ return bool(any(c for c in cells)) and all(re.match(r"^:?-+:?$", c) for c in cells if c)
+
+
def convert_markdown_table_to_labeled_rows(table_text: str) -> str:
"""Convert a Markdown pipe table to labeled rows (for Slack-style text).
@@ -82,21 +105,13 @@ def convert_markdown_table_to_labeled_rows(table_text: str) -> str:
lines = [ln.strip() for ln in table_text.strip().splitlines() if ln.strip()]
if len(lines) < 2:
return table_text
- headers = [h.strip() for h in lines[0].strip("|").split("|")]
- start = 2 if re.fullmatch(r"[|\s:\-]+", lines[1]) else 1
+ headers = split_markdown_table_row(lines[0])
+ start = 2 if is_markdown_table_separator_row(split_markdown_table_row(lines[1])) else 1
rows: list[str] = []
for line in lines[start:]:
- cells = [c.strip() for c in line.strip("|").split("|")]
+ cells = split_markdown_table_row(line)
cells = (cells + [""] * len(headers))[: len(headers)]
parts = [f"**{headers[i]}**: {cells[i]}" for i in range(len(headers))]
if parts:
rows.append(" · ".join(parts))
return "\n".join(rows)
-
-
-def is_markdown_table_separator_row(cells: list[str]) -> bool:
- """True if *cells* look like a markdown table separator row.
-
- An all-empty row is not a separator (`all([])` would otherwise be True).
- """
- return bool(any(c for c in cells)) and all(re.match(r"^:?-+:?$", c) for c in cells if c)
diff --git a/tests/partners/test_feishu_md_table_empty_edge_cells.py b/tests/partners/test_feishu_md_table_empty_edge_cells.py
index 1a379f32f..0fdcd373a 100644
--- a/tests/partners/test_feishu_md_table_empty_edge_cells.py
+++ b/tests/partners/test_feishu_md_table_empty_edge_cells.py
@@ -6,12 +6,7 @@ from deeptutor.partners.channels.feishu import FeishuChannel
def test_leading_empty_header_keeps_column_alignment() -> None:
- table = (
- "|| Name | Age |\n"
- "| --- | --- | --- |\n"
- "| id1 | Alice | 30 |\n"
- "| id2 | Bob | 25 |\n"
- )
+ table = "|| Name | Age |\n| --- | --- | --- |\n| id1 | Alice | 30 |\n| id2 | Bob | 25 |\n"
parsed = FeishuChannel._parse_md_table(table)
assert parsed is not None
assert [c["display_name"] for c in parsed["columns"]] == ["", "Name", "Age"]
@@ -22,12 +17,7 @@ def test_leading_empty_header_keeps_column_alignment() -> None:
def test_trailing_empty_header_keeps_row_values() -> None:
- table = (
- "| Name | Age ||\n"
- "| --- | --- | --- |\n"
- "| Alice | 30 | VIP |\n"
- "| Bob | 25 | |\n"
- )
+ table = "| Name | Age ||\n| --- | --- | --- |\n| Alice | 30 | VIP |\n| Bob | 25 | |\n"
parsed = FeishuChannel._parse_md_table(table)
assert parsed is not None
assert [c["display_name"] for c in parsed["columns"]] == ["Name", "Age", ""]
diff --git a/tests/partners/test_markdown_table_empty_cells.py b/tests/partners/test_markdown_table_empty_cells.py
index 994eebd5c..7410ab04d 100644
--- a/tests/partners/test_markdown_table_empty_cells.py
+++ b/tests/partners/test_markdown_table_empty_cells.py
@@ -6,12 +6,7 @@ from deeptutor.partners.helpers import convert_markdown_table_to_labeled_rows
def test_empty_cells_kept_in_converted_table() -> None:
- table = (
- "| Name | Score | Note |\n"
- "| --- | --- | --- |\n"
- "| Alice | | ok |\n"
- "| Bob | 10 | |\n"
- )
+ table = "| Name | Score | Note |\n| --- | --- | --- |\n| Alice | | ok |\n| Bob | 10 | |\n"
out = convert_markdown_table_to_labeled_rows(table)
assert out.count("**Name**:") == 2
assert out.count("**Score**:") == 2
diff --git a/tests/partners/test_split_markdown_table_row.py b/tests/partners/test_split_markdown_table_row.py
new file mode 100644
index 000000000..1e9772a7c
--- /dev/null
+++ b/tests/partners/test_split_markdown_table_row.py
@@ -0,0 +1,43 @@
+"""The shared markdown pipe-table row splitter keeps leading/trailing empty cells.
+
+Regression guard for the class of bug fixed per-channel in #679/#682/#683: the
+old ``str.strip("|")`` collapsed leading/trailing empty cells and shifted every
+column. All three partner channels now split rows through this one primitive.
+"""
+
+from __future__ import annotations
+
+from deeptutor.partners.helpers import (
+ convert_markdown_table_to_labeled_rows,
+ split_markdown_table_row,
+)
+
+
+def test_standard_row() -> None:
+ assert split_markdown_table_row("| A | B | C |") == ["A", "B", "C"]
+
+
+def test_leading_empty_cell_kept() -> None:
+ assert split_markdown_table_row("|| B | C |") == ["", "B", "C"]
+
+
+def test_trailing_empty_cell_kept() -> None:
+ assert split_markdown_table_row("| A | B ||") == ["A", "B", ""]
+
+
+def test_middle_empty_cell_kept() -> None:
+ assert split_markdown_table_row("| A | | C |") == ["A", "", "C"]
+
+
+def test_row_without_outer_pipes() -> None:
+ assert split_markdown_table_row("A | B") == ["A", "B"]
+
+
+def test_slack_conversion_no_longer_shifts_columns() -> None:
+ # A leading empty header column used to be dropped by strip("|"), shifting
+ # every value one column to the left in the Slack labeled-row output.
+ table = "|| Name | Age |\n| --- | --- | --- |\n| id1 | Alice | 30 |\n"
+ out = convert_markdown_table_to_labeled_rows(table)
+ assert "**Name**: Alice" in out # Alice correctly under Name, not "id1"
+ assert "**Age**: 30" in out
+ assert "**Name**: id1" not in out