21
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 14s

This commit is contained in:
2026-04-02 21:32:26 +07:00
parent e7bd488551
commit 8fb6da84ae
4 changed files with 160 additions and 86 deletions

View File

@@ -1,5 +1,6 @@
from __future__ import annotations
import re
from html import escape
@@ -10,6 +11,8 @@ DEFAULT_STATUS_LABELS = {
"rest": "антракт",
}
ACTOR_PLACEHOLDER_RE = re.compile(r"\{\{\s*actor\s*:\s*([a-z0-9_\-]+)\s*\}\}", re.IGNORECASE)
def build_hidden_link(config: dict) -> str:
url = config.get("hidden_link_url", "").strip()
@@ -19,33 +22,55 @@ def build_hidden_link(config: dict) -> str:
return f'<a href="{escape(url, quote=True)}">{invisible}</a>'
def build_actor_lines(config: dict, state: dict) -> str:
actor_lines: list[str] = []
def build_actor_line(actor: dict, state: dict, config: dict) -> str:
actor_state = state.get("actors", {})
status_labels = {**DEFAULT_STATUS_LABELS, **config.get("status_labels", {})}
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)
display_name = actor.get("display_html", escape(actor["display_name"]))
meta = actor.get("meta_html", escape(actor["pronouns"]))
emoji = actor.get("emoji_html", escape(actor.get("emoji", "")))
line = (
f'{emoji} '
f'<a href="{escape(actor["link"], quote=True)}">{display_name}</a>'
f"{meta}{escape(label)}."
)
if phrase:
line = f"{line}\n {escape(phrase)}"
return line
def build_actor_lines(config: dict, state: dict, skip_keys: set[str] | None = None) -> str:
actor_lines: list[str] = []
skip_keys = skip_keys or set()
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)
display_name = actor.get("display_html", escape(actor["display_name"]))
meta = actor.get("meta_html", escape(actor["pronouns"]))
emoji = actor.get("emoji_html", escape(actor.get("emoji", "")))
line = (
f'{emoji} '
f'<a href="{escape(actor["link"], quote=True)}">{display_name}</a>'
f"{meta}{escape(label)}."
)
if phrase:
line = f"{line}\n {escape(phrase)}"
actor_lines.append(line)
if actor["key"] in skip_keys:
continue
actor_lines.append(build_actor_line(actor, state, config))
actor_lines.extend(config.get("static_actor_lines_html", []))
return "\n".join(actor_lines)
def replace_actor_placeholders(template: str, config: dict, state: dict) -> tuple[str, set[str]]:
used_keys: set[str] = set()
actors_by_key = {actor["key"].lower(): actor for actor in config["actors"]}
def repl(match: re.Match[str]) -> str:
actor_key = match.group(1).lower()
actor = actors_by_key.get(actor_key)
if actor is None:
return match.group(0)
used_keys.add(actor["key"])
return build_actor_line(actor, state, config)
return ACTOR_PLACEHOLDER_RE.sub(repl, template), used_keys
def build_default_template(config: dict) -> str:
blocks = []
hidden = build_hidden_link(config)
@@ -69,7 +94,8 @@ def build_default_template(config: dict) -> str:
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)
template, used_keys = replace_actor_placeholders(template, config, state)
actors_block = build_actor_lines(config, state, skip_keys=used_keys)
hidden_link = build_hidden_link(config)
text = template.replace("{{actors}}", actors_block)