post message
Some checks failed
CI / Lint (ruff + mypy) (push) Failing after 32s
CI / Run tests (push) Has been skipped
CI / Docker build test (push) Successful in 10s

This commit is contained in:
2026-04-02 20:59:04 +07:00
parent c286039df7
commit d8231c13a4
5 changed files with 266 additions and 241 deletions

View File

@@ -1,7 +1,5 @@
from __future__ import annotations
from html import escape
DEFAULT_STATUS_LABELS = {
"open": "исполняет роль",
@@ -10,64 +8,83 @@ DEFAULT_STATUS_LABELS = {
"rest": "антракт",
}
def _get_block(config: dict, html_key: str, plain_key: str) -> str:
html_value = config.get(html_key, "").strip()
if html_value:
return html_value
plain_value = config.get(plain_key, "").strip()
if plain_value:
return escape(plain_value)
return ""
MARKDOWN_V2_SPECIALS = r"_*[]()~`>#+-=|{}.!"
def build_channel_text(config: dict, state: dict) -> str:
parts: list[str] = []
def escape_markdown_v2(value: str) -> str:
return "".join(f"\\{char}" if char in MARKDOWN_V2_SPECIALS else char for char in value)
for html_key, plain_key in (
("header_html", "header"),
("intro_links_html", "intro_links"),
("projects_block_html", "projects_block"),
("actors_title_html", "actors_title"),
):
block = _get_block(config, html_key, plain_key)
if block:
parts.append(block)
def escape_markdown_v2_url(value: str) -> str:
return value.replace("\\", "\\\\").replace(")", "\\)")
def build_hidden_link(config: dict) -> str:
url = config.get("hidden_link_url", "").strip()
if not url:
return ""
invisible = config.get("hidden_link_char", "\u2063")
return f"[{invisible}]({escape_markdown_v2_url(url)})"
def build_actor_lines(config: dict, state: dict) -> str:
actor_lines: list[str] = []
actor_state = state.get("actors", {})
status_labels = {**DEFAULT_STATUS_LABELS, **config.get("status_labels", {})}
for actor in config["actors"]:
current = actor_state.get(actor["key"], {})
status = current.get("status", actor.get("default_status", "backstage"))
phrase = current.get("phrase", actor.get("phrases", {}).get(status, ""))
label = status_labels.get(status, status)
emoji = actor.get("emoji_html") or escape(actor.get("emoji", ""))
name_html = actor.get("display_html") or escape(actor["display_name"])
meta_html = actor.get("meta_html")
if not meta_html:
meta_html = f" {escape(actor['pronouns'])} "
display_name = actor.get("display_md", actor["display_name"])
meta = actor.get("meta_md", actor["pronouns"])
emoji = actor.get("emoji_md", actor.get("emoji", ""))
line = (
f'{emoji} <a href="{escape(actor["link"], quote=True)}"><b>{name_html}</b></a>'
f"{meta_html}{escape(label)}."
f"{emoji} "
f"[{escape_markdown_v2(display_name)}]({escape_markdown_v2_url(actor['link'])}) "
f"{escape_markdown_v2(meta)} "
f"{escape_markdown_v2(label)}\\."
)
if phrase:
line = f"{line}\n {escape(phrase)}"
line = f"{line}\n {escape_markdown_v2(phrase)}"
actor_lines.append(line)
actor_lines.extend(config.get("static_actor_lines_html", []))
parts.append("\n".join(actor_lines))
actor_lines.extend(config.get("static_actor_lines_md", []))
return "\n".join(actor_lines)
for html_key, plain_key in (
("legend_html", "legend"),
("footer_html", "footer"),
):
block = _get_block(config, html_key, plain_key)
if block:
parts.append(block)
return "\n\n".join(part for part in parts if part)
def build_default_template(config: dict) -> str:
blocks = []
hidden = build_hidden_link(config)
if hidden:
blocks.append(hidden)
for key in ("header", "intro_links", "projects_block", "actors_title"):
value = config.get(key, "").strip()
if value:
blocks.append(escape_markdown_v2(value))
blocks.append("{{actors}}")
for key in ("legend", "footer"):
value = config.get(key, "").strip()
if value:
blocks.append(escape_markdown_v2(value))
return "\n\n".join(blocks)
def build_channel_text(config: dict, state: dict) -> str:
template = state.get("template", {}).get("text") or config.get("template_text") or build_default_template(config)
actors_block = build_actor_lines(config, state)
hidden_link = build_hidden_link(config)
text = template.replace("{{actors}}", actors_block)
text = text.replace("{{hidden_link}}", hidden_link)
if "{{hidden_link}}" not in template and hidden_link:
text = f"{hidden_link}\n{text}"
return text