diff --git a/dzen-assistant/app/routes.py b/dzen-assistant/app/routes.py index 61435f5..d71221d 100644 --- a/dzen-assistant/app/routes.py +++ b/dzen-assistant/app/routes.py @@ -382,10 +382,12 @@ def strip_article_cover_html(html: str) -> str: def editable_article_html(article: Article) -> str: + lead_html, lead_question_html = split_audience_question_html(article.lead) body_html, audience_question_html = split_audience_question_html(article.body_html) + audience_question_html = audience_question_html or lead_question_html if article.video_id and VIDEO_PLACEHOLDER not in body_html: body_html = "\n".join(part for part in [body_html, f"

{VIDEO_PLACEHOLDER}

"] if part) - html = "\n".join(part for part in [article.lead, body_html, audience_question_html, article.cta_html] if part).strip() + html = "\n".join(part for part in [lead_html, body_html, audience_question_html, article.cta_html] if part).strip() html = strip_article_cover_html(html) return strip_leading_duplicate_title(html, article.title) @@ -938,7 +940,9 @@ def update_article(article_id: int, payload: ArticleUpdateIn, session: Session = body = body.replace(signature_html, "").rstrip() else: signature_html = "" - _, prior_question = split_audience_question_html(article.body_html) + _, prior_lead_question = split_audience_question_html(article.lead) + _, prior_body_question = split_audience_question_html(article.body_html) + prior_question = prior_body_question or prior_lead_question body, updated_question = split_audience_question_html(body) audience_question_html = updated_question or prior_question if article.video_id: diff --git a/dzen-assistant/app/rss.py b/dzen-assistant/app/rss.py index 67e576e..7feed7c 100644 --- a/dzen-assistant/app/rss.py +++ b/dzen-assistant/app/rss.py @@ -1,5 +1,5 @@ from datetime import datetime, timezone -from html import escape +from html import escape, unescape import re from urllib.parse import urlencode @@ -162,36 +162,54 @@ def sanitize_rss_html(html: str) -> str: def _is_audience_question_paragraph(paragraph_html: str) -> bool: text = re.sub(r"<[^>]+>", " ", paragraph_html, flags=re.S) - text = re.sub(r"\s+", " ", text).strip().lower() + text = re.sub(r"\s+", " ", unescape(text)).strip().lower() return text.startswith("ваше мнение:") and "комментар" in text +def _audience_question_text(paragraph_html: str) -> str: + text = re.sub(r"<[^>]+>", " ", paragraph_html, flags=re.S) + text = re.sub(r"\s+", " ", unescape(text)).strip() + text = re.sub(r"^Ваше мнение\s*:\s*", "", text, flags=re.I) + text = re.sub(r"\bвы\b", "Вы", text) + return text + + +def _canonical_audience_question(paragraph_html: str) -> str: + question = escape(_audience_question_text(paragraph_html)) + return f"

Ваше мнение

{question}

" + + def split_audience_question_html(html: str) -> tuple[str, str]: """Extract one audience question and remove accidental duplicate copies.""" source = html or "" - paragraphs = list(re.finditer(r"]*>.*?

", source, flags=re.I | re.S)) - question_match = next( - ( - match - for match in paragraphs - if "audience-question" in match.group(0).lower() - or _is_audience_question_paragraph(match.group(0)) - ), - None, + candidates: list[tuple[int, int, str]] = [] + canonical_blocks = list( + re.finditer( + r"]*>\s*Ваше мнение\s*\s*(]*>.*?

)", + source, + flags=re.I | re.S, + ) ) - if not question_match: + for match in canonical_blocks: + candidates.append((match.start(), match.end(), match.group(1))) + + paragraphs = list(re.finditer(r"]*>.*?

", source, flags=re.I | re.S)) + for match in paragraphs: + if any(match.start() >= start and match.end() <= end for start, end, _ in candidates): + continue + if "audience-question" in match.group(0).lower() or _is_audience_question_paragraph(match.group(0)): + candidates.append((match.start(), match.end(), match.group(0))) + + if not candidates: return source, "" - question_html = question_match.group(0).strip() + candidates.sort(key=lambda item: item[0]) + question_html = _canonical_audience_question(candidates[0][2]) body_parts: list[str] = [] cursor = 0 - for match in paragraphs: - if ( - "audience-question" in match.group(0).lower() - or _is_audience_question_paragraph(match.group(0)) - ): - body_parts.append(source[cursor:match.start()]) - cursor = match.end() + for start, end, _ in candidates: + body_parts.append(source[cursor:start]) + cursor = end body_parts.append(source[cursor:]) return "".join(body_parts).strip(), question_html @@ -206,11 +224,13 @@ def media_player_xml(article: Article) -> str: def render_article_html(article: Article, include_video_embed: bool = True, rss_safe: bool = False) -> str: + lead_html, lead_question_html = split_audience_question_html(article.lead) body_html = strip_article_cover_html(article.body_html) body_html, audience_question_html = split_audience_question_html(body_html) + audience_question_html = audience_question_html or lead_question_html body_html, has_video_placeholder = render_video_placeholder(article, body_html, rss_safe=rss_safe) body_html = sanitize_rss_html(body_html) if rss_safe else body_html - parts = [article.lead, cover_html(article, rss_safe=rss_safe), body_html] + parts = [lead_html, cover_html(article, rss_safe=rss_safe), body_html] if include_video_embed and not has_video_placeholder: parts.append(video_embed_html(article, rss_safe=rss_safe)) if audience_question_html: diff --git a/dzen-assistant/app/worker.py b/dzen-assistant/app/worker.py index 83ca7e0..929ca5d 100644 --- a/dzen-assistant/app/worker.py +++ b/dzen-assistant/app/worker.py @@ -1259,9 +1259,11 @@ def audience_question_html(item: dict[str, Any], title: str) -> str: question = clean_generated_text(str(item.get("audience_question") or "")).strip() if not question: question = f"Как вы оцениваете основные выводы статьи «{title}»?" + question = re.sub(r"^Ваше мнение\s*:\s*", "", question, flags=re.I) + question = re.sub(r"\bвы\b", "Вы", question) if not question.endswith(("?", "?")): question = f"{question.rstrip('.!')}?" - return f'

Ваше мнение: {escape(question)} Пишите своё мнение в комментариях.

' + return f"

Ваше мнение

{escape(question)} Пишите своё мнение в комментариях.

" def frame_figure_html(frame: Frame, title: str, caption: str | None = None) -> str: