34 lines
1.2 KiB
Python
34 lines
1.2 KiB
Python
"""Разбор URL-кнопок из textarea: по одной в строке ``Текст | https://…``."""
|
||
|
||
from __future__ import annotations
|
||
|
||
MAX_BUTTONS = 8
|
||
|
||
|
||
def parse_buttons(raw: str) -> tuple[list[tuple[str, str]], list[str]]:
|
||
buttons: list[tuple[str, str]] = []
|
||
errors: list[str] = []
|
||
|
||
for line in raw.splitlines():
|
||
line = line.strip()
|
||
if not line:
|
||
continue
|
||
if "|" not in line:
|
||
errors.append(f"Кнопка без разделителя «|»: {line}")
|
||
continue
|
||
text, url = (part.strip() for part in line.split("|", 1))
|
||
if not text:
|
||
errors.append("У кнопки пустой текст")
|
||
continue
|
||
if not url.startswith(("https://", "http://")):
|
||
errors.append(f"Ссылка кнопки должна начинаться с http(s): {url}")
|
||
continue
|
||
if len(text) > 64:
|
||
errors.append("Текст кнопки длиннее 64 символов")
|
||
continue
|
||
buttons.append((text, url))
|
||
|
||
if len(buttons) > MAX_BUTTONS:
|
||
errors.append(f"Не больше {MAX_BUTTONS} кнопок на сообщение")
|
||
return buttons, errors
|