74 lines
2.3 KiB
Python
74 lines
2.3 KiB
Python
from __future__ import annotations
|
|
|
|
from html import escape
|
|
|
|
|
|
DEFAULT_STATUS_LABELS = {
|
|
"open": "исполняет роль",
|
|
"backstage": "в закулисье",
|
|
"delay": "задержки",
|
|
"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 ""
|
|
|
|
|
|
def build_channel_text(config: dict, state: dict) -> str:
|
|
parts: list[str] = []
|
|
|
|
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)
|
|
|
|
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'])} "
|
|
|
|
line = (
|
|
f'{emoji} <a href="{escape(actor["link"], quote=True)}"><b>{name_html}</b></a>'
|
|
f"{meta_html}{escape(label)}."
|
|
)
|
|
if phrase:
|
|
line = f"{line}\n {escape(phrase)}"
|
|
actor_lines.append(line)
|
|
|
|
actor_lines.extend(config.get("static_actor_lines_html", []))
|
|
parts.append("\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)
|