free-claude-code/messaging/rendering/markdown_tables.py
Alishahryar1 0e3b2c24b4 refactor: remove OpenRouter rollback, shims, and redundant layers
- OpenRouter: native Anthropic only; remove chat_request and OPENROUTER_TRANSPORT
- Drop OpenAICompatibleProvider alias, api.request_utils, voice_pipeline facade
- Simplify OpenRouter SSE, generic reasoning in conversion, messaging dispatch
- Shared markdown table helpers; API optimization response helper; contract guards
- Restore PLAN.md; update docs and tests
2026-04-24 21:08:38 -07:00

49 lines
1.4 KiB
Python

"""Shared Markdown table pre-normalization for platform renderers."""
from __future__ import annotations
import re
_TABLE_SEP_RE = re.compile(r"^\s*\|?\s*:?-{3,}:?\s*(\|\s*:?-{3,}:?\s*)+\|?\s*$")
_FENCE_RE = re.compile(r"^\s*```")
def _is_gfm_table_header_line(line: str) -> bool:
"""Return whether a line looks like a GFM table header."""
if "|" not in line:
return False
if _TABLE_SEP_RE.match(line):
return False
parts = [part.strip() for part in line.strip().strip("|").split("|")]
return len([part for part in parts if part]) >= 2
def normalize_gfm_tables(text: str) -> str:
"""Insert blank lines before detected tables outside fenced code blocks."""
lines = text.splitlines()
if len(lines) < 2:
return text
out_lines: list[str] = []
in_fence = False
for idx, line in enumerate(lines):
if _FENCE_RE.match(line):
in_fence = not in_fence
out_lines.append(line)
continue
if (
not in_fence
and idx + 1 < len(lines)
and _is_gfm_table_header_line(line)
and _TABLE_SEP_RE.match(lines[idx + 1])
and out_lines
and out_lines[-1].strip() != ""
):
indent_match = re.match(r"^(\s*)", line)
out_lines.append(indent_match.group(1) if indent_match else "")
out_lines.append(line)
return "\n".join(out_lines)