signal_format.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. """Shared Signal formatting helpers: Markdown → Signal native formatting lives here so both the live
  2. adapter and the standalone send paths emit the same bodyRanges."""
  3. from __future__ import annotations
  4. import re
  5. from agent.markdown_tables import is_table_divider, realign_markdown_tables
  6. # Signal has no fixed-width client area; this budgets for a phone screen in the app's
  7. # monospace face. Tables wider than this fall back to realign_markdown_tables()'s
  8. # vertical Key: value rendering rather than soft-wrapping mid-cell.
  9. _TABLE_WIDTH = 40
  10. _CODE_BLOCK_RE = re.compile(r"```[a-zA-Z0-9_+-]*\n?(.*?)```", re.DOTALL)
  11. _HEADING_RE = re.compile(r"^#{1,6}\s+", re.MULTILINE)
  12. _INLINE_PATTERNS = [
  13. (re.compile(r"\*\*(.+?)\*\*", re.DOTALL), "BOLD"),
  14. (re.compile(r"__(.+?)__", re.DOTALL), "BOLD"),
  15. (re.compile(r"~~(.+?)~~", re.DOTALL), "STRIKETHROUGH"),
  16. (re.compile(r"`(.+?)`"), "MONOSPACE"),
  17. (re.compile(r"(?<!\*)\*(?!\*| )(.+?)(?<!\*)\*(?!\*)"), "ITALIC"),
  18. (re.compile(r"(?<!\w)_(?!_)(.+?)(?<!_)_(?!\w)"), "ITALIC")]
  19. def _utf16_len(s: str) -> int:
  20. """Length of *s* in UTF-16 code units."""
  21. return len(s.encode("utf-16-le")) // 2
  22. def _normalize_bullet_markers(source: str) -> str:
  23. """Replace Markdown bullet markers with plain Unicode bullets (Signal renders ``- item`` literally).
  24. Fenced code blocks are kept byte-for-byte: list-looking lines inside code are code, not bullets."""
  25. parts = re.split(r"(```.*?```)", source, flags=re.DOTALL)
  26. return "".join(re.sub(r"(?m)^([ \t]{0,3})[-*+]\s+", r"\1• ", part) if idx % 2 == 0 else part
  27. for idx, part in enumerate(parts))
  28. def _fence_tables(source: str) -> str:
  29. """Re-align every GFM table outside a code fence and wrap it in a fence, so the code-block pass
  30. in markdown_to_signal() records one MONOSPACE range over the aligned block and shifts every
  31. later range for it. Fenced regions are left alone: a pipe table inside a code block is code."""
  32. parts = re.split(r"(```.*?```)", source, flags=re.DOTALL)
  33. return "".join(part if idx % 2 else _fence_unfenced_tables(part) for idx, part in enumerate(parts))
  34. def _fence_unfenced_tables(text: str) -> str:
  35. if "|" not in text:
  36. return text
  37. lines, out = text.split("\n"), []
  38. i, n = 0, len(lines)
  39. while i < n:
  40. # Same block rule as realign_markdown_tables(): header row, divider, contiguous pipe rows.
  41. if "|" in lines[i] and i + 1 < n and is_table_divider(lines[i + 1]):
  42. j = i + 2
  43. while j < n and "|" in lines[j] and lines[j].strip():
  44. j += 1
  45. out += ["```", realign_markdown_tables("\n".join(lines[i:j]), _TABLE_WIDTH), "```"]
  46. i = j
  47. continue
  48. out.append(lines[i])
  49. i += 1
  50. return "\n".join(out)
  51. def markdown_to_signal(text: str) -> tuple[str, list[str]]:
  52. """Convert markdown to plain text + Signal textStyles list. Signal uses ``bodyRanges`` (signal-cli
  53. ``textStyle`` / ``textStyles`` params) as ``start:length:STYLE`` with positions in UTF-16 code units.
  54. Supported styles: BOLD, ITALIC, STRIKETHROUGH, MONOSPACE."""
  55. text = _fence_tables(_normalize_bullet_markers(re.sub(r"\n{3,}", "\n\n", text).strip()))
  56. styles: list[tuple[int, int, str]] = []
  57. while match := _CODE_BLOCK_RE.search(text):
  58. inner = match.group(1).rstrip("\n")
  59. styles.append((match.start(), len(inner), "MONOSPACE"))
  60. text = text[: match.start()] + inner + text[match.end() :]
  61. new_text, last_end = "", 0
  62. for match in _HEADING_RE.finditer(text):
  63. new_text += text[last_end : match.start()]
  64. eol = text.find("\n", match.end())
  65. if eol == -1:
  66. eol = len(text)
  67. heading_text = text[match.end() : eol]
  68. styles.append((len(new_text), len(heading_text), "BOLD"))
  69. new_text += heading_text
  70. last_end = eol
  71. text = new_text + text[last_end:]
  72. # Inline markers: first pattern to claim a span wins; later overlapping matches are dropped.
  73. all_matches: list[tuple[int, int, int, int, str]] = []
  74. occupied: list[tuple[int, int]] = []
  75. for pattern, style in _INLINE_PATTERNS:
  76. for match in pattern.finditer(text):
  77. ms, me = match.start(), match.end()
  78. if not any(ms < oe and me > os for os, oe in occupied):
  79. all_matches.append((ms, me, match.start(1), match.end(1), style))
  80. occupied.append((ms, me))
  81. all_matches.sort()
  82. # Strip the markers, recording (pos, len) removals so earlier block/heading ranges can be
  83. # shifted, and capturing inline ranges in the stripped text.
  84. result, last_end = "", 0
  85. removals: list[tuple[int, int]] = []
  86. inline_styles: list[tuple[int, int, str]] = []
  87. for ms, me, g1s, g1e, style in all_matches:
  88. if g1s > ms:
  89. removals.append((ms, g1s - ms))
  90. if me > g1e:
  91. removals.append((g1e, me - g1e))
  92. result += text[last_end:ms]
  93. inner = text[g1s:g1e]
  94. inline_styles.append((len(result), len(inner), style))
  95. result += inner
  96. last_end = me
  97. removals.sort()
  98. def _adjust(pos: int) -> int:
  99. shift = 0
  100. for remove_pos, remove_len in removals:
  101. if remove_pos >= pos:
  102. break
  103. shift += min(remove_len, pos - remove_pos)
  104. return pos - shift
  105. adjusted_prior = [(_adjust(start), _adjust(start + length) - _adjust(start), style)
  106. for start, length, style in styles if _adjust(start + length) > _adjust(start)]
  107. text = result + text[last_end:]
  108. style_strings: list[str] = []
  109. for cp_start, cp_len, style_type in sorted(adjusted_prior + inline_styles):
  110. if 0 <= cp_start and cp_start + cp_len <= len(text):
  111. u16_start, u16_len = _utf16_len(text[:cp_start]), _utf16_len(text[cp_start : cp_start + cp_len])
  112. style_strings.append(f"{u16_start}:{u16_len}:{style_type}")
  113. return text, style_strings