423
This commit is contained in:
@@ -110,8 +110,10 @@ def extract_template_text(message: Message) -> str | None:
|
|||||||
return message.text.split(maxsplit=1)[1]
|
return message.text.split(maxsplit=1)[1]
|
||||||
|
|
||||||
source = message.reply_to_message or message
|
source = message.reply_to_message or message
|
||||||
|
if source.html_text:
|
||||||
|
return source.html_text
|
||||||
if source.text:
|
if source.text:
|
||||||
return source.md_text
|
return source.text
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
@@ -179,7 +181,7 @@ async def update_channel_post(bot: Bot, app_config: dict, state_storage: JsonSta
|
|||||||
chat_id=settings.channel_id,
|
chat_id=settings.channel_id,
|
||||||
message_id=settings.channel_message_id,
|
message_id=settings.channel_message_id,
|
||||||
text=text,
|
text=text,
|
||||||
parse_mode=ParseMode.MARKDOWN_V2,
|
parse_mode=ParseMode.HTML,
|
||||||
disable_web_page_preview=False,
|
disable_web_page_preview=False,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from html import escape
|
||||||
|
|
||||||
|
|
||||||
DEFAULT_STATUS_LABELS = {
|
DEFAULT_STATUS_LABELS = {
|
||||||
"open": "исполняет роль",
|
"open": "исполняет роль",
|
||||||
@@ -8,23 +10,13 @@ DEFAULT_STATUS_LABELS = {
|
|||||||
"rest": "антракт",
|
"rest": "антракт",
|
||||||
}
|
}
|
||||||
|
|
||||||
MARKDOWN_V2_SPECIALS = r"_*[]()~`>#+-=|{}.!"
|
|
||||||
|
|
||||||
|
|
||||||
def escape_markdown_v2(value: str) -> str:
|
|
||||||
return "".join(f"\\{char}" if char in MARKDOWN_V2_SPECIALS else char for char in value)
|
|
||||||
|
|
||||||
|
|
||||||
def escape_markdown_v2_url(value: str) -> str:
|
|
||||||
return value.replace("\\", "\\\\").replace(")", "\\)")
|
|
||||||
|
|
||||||
|
|
||||||
def build_hidden_link(config: dict) -> str:
|
def build_hidden_link(config: dict) -> str:
|
||||||
url = config.get("hidden_link_url", "").strip()
|
url = config.get("hidden_link_url", "").strip()
|
||||||
if not url:
|
if not url:
|
||||||
return ""
|
return ""
|
||||||
invisible = config.get("hidden_link_char", "\u2063")
|
invisible = config.get("hidden_link_char", "​")
|
||||||
return f"[{invisible}]({escape_markdown_v2_url(url)})"
|
return f'<a href="{escape(url, quote=True)}">{invisible}</a>'
|
||||||
|
|
||||||
|
|
||||||
def build_actor_lines(config: dict, state: dict) -> str:
|
def build_actor_lines(config: dict, state: dict) -> str:
|
||||||
@@ -37,21 +29,20 @@ def build_actor_lines(config: dict, state: dict) -> str:
|
|||||||
status = current.get("status", actor.get("default_status", "backstage"))
|
status = current.get("status", actor.get("default_status", "backstage"))
|
||||||
phrase = current.get("phrase", actor.get("phrases", {}).get(status, ""))
|
phrase = current.get("phrase", actor.get("phrases", {}).get(status, ""))
|
||||||
label = status_labels.get(status, status)
|
label = status_labels.get(status, status)
|
||||||
display_name = actor.get("display_md", actor["display_name"])
|
display_name = actor.get("display_html", escape(actor["display_name"]))
|
||||||
meta = actor.get("meta_md", actor["pronouns"])
|
meta = actor.get("meta_html", escape(actor["pronouns"]))
|
||||||
emoji = actor.get("emoji_md", actor.get("emoji", ""))
|
emoji = actor.get("emoji_html", escape(actor.get("emoji", "")))
|
||||||
|
|
||||||
line = (
|
line = (
|
||||||
f"{emoji} "
|
f'{emoji} '
|
||||||
f"[{escape_markdown_v2(display_name)}]({escape_markdown_v2_url(actor['link'])}) "
|
f'<a href="{escape(actor["link"], quote=True)}">{display_name}</a>'
|
||||||
f"{escape_markdown_v2(meta)} "
|
f"{meta}{escape(label)}."
|
||||||
f"{escape_markdown_v2(label)}\\."
|
|
||||||
)
|
)
|
||||||
if phrase:
|
if phrase:
|
||||||
line = f"{line}\n {escape_markdown_v2(phrase)}"
|
line = f"{line}\n {escape(phrase)}"
|
||||||
actor_lines.append(line)
|
actor_lines.append(line)
|
||||||
|
|
||||||
actor_lines.extend(config.get("static_actor_lines_md", []))
|
actor_lines.extend(config.get("static_actor_lines_html", []))
|
||||||
return "\n".join(actor_lines)
|
return "\n".join(actor_lines)
|
||||||
|
|
||||||
|
|
||||||
@@ -61,17 +52,17 @@ def build_default_template(config: dict) -> str:
|
|||||||
if hidden:
|
if hidden:
|
||||||
blocks.append(hidden)
|
blocks.append(hidden)
|
||||||
|
|
||||||
for key in ("header", "intro_links", "projects_block", "actors_title"):
|
for key in ("header_html", "intro_links_html", "projects_block_html", "actors_title_html"):
|
||||||
value = config.get(key, "").strip()
|
value = config.get(key, "").strip()
|
||||||
if value:
|
if value:
|
||||||
blocks.append(escape_markdown_v2(value))
|
blocks.append(value)
|
||||||
|
|
||||||
blocks.append("{{actors}}")
|
blocks.append("{{actors}}")
|
||||||
|
|
||||||
for key in ("legend", "footer"):
|
for key in ("legend_html", "footer_html"):
|
||||||
value = config.get(key, "").strip()
|
value = config.get(key, "").strip()
|
||||||
if value:
|
if value:
|
||||||
blocks.append(escape_markdown_v2(value))
|
blocks.append(value)
|
||||||
|
|
||||||
return "\n\n".join(blocks)
|
return "\n\n".join(blocks)
|
||||||
|
|
||||||
|
|||||||
@@ -9,8 +9,10 @@ def test_build_channel_text_includes_phrase_and_status() -> None:
|
|||||||
{
|
{
|
||||||
"key": "astat",
|
"key": "astat",
|
||||||
"display_name": "ASTAT",
|
"display_name": "ASTAT",
|
||||||
|
"display_html": "<b>ASTAT</b>",
|
||||||
"link": "https://t.me/example",
|
"link": "https://t.me/example",
|
||||||
"pronouns": "he/him",
|
"pronouns": "he/him",
|
||||||
|
"meta_html": " he/him ",
|
||||||
"emoji": "🌟",
|
"emoji": "🌟",
|
||||||
"default_status": "backstage",
|
"default_status": "backstage",
|
||||||
"phrases": {"open": "принимает тейки"},
|
"phrases": {"open": "принимает тейки"},
|
||||||
@@ -22,7 +24,7 @@ def test_build_channel_text_includes_phrase_and_status() -> None:
|
|||||||
|
|
||||||
text = build_channel_text(config, state)
|
text = build_channel_text(config, state)
|
||||||
|
|
||||||
assert "[](https://example.com/image.png)" in text
|
assert '<a href="https://example.com/image.png">' in text
|
||||||
assert "[ASTAT](https://t.me/example)" in text
|
assert '<a href="https://t.me/example"><b>ASTAT</b></a>' in text
|
||||||
assert "исполняет роль" in text
|
assert "исполняет роль" in text
|
||||||
assert "готов к игре" in text
|
assert "готов к игре" in text
|
||||||
|
|||||||
Reference in New Issue
Block a user