fix: ensure blank line break after bold title when stripping separator lines
This commit is contained in:
@@ -44,16 +44,25 @@ def build_publication_text(
|
|||||||
base = strip_trailing_hashtag_line(text)
|
base = strip_trailing_hashtag_line(text)
|
||||||
lines = base.splitlines()
|
lines = base.splitlines()
|
||||||
sep_pattern = re.compile(r"^\s*[━—─\-=\*\#_]{2,}\s*$")
|
sep_pattern = re.compile(r"^\s*[━—─\-=\*\#_]{2,}\s*$")
|
||||||
filtered_lines = [line for line in lines if not sep_pattern.match(line)]
|
|
||||||
if not filtered_lines:
|
cleaned_lines = []
|
||||||
return publication_hashtags(category_tag, source_tag)
|
for line in lines:
|
||||||
|
if sep_pattern.match(line):
|
||||||
|
cleaned_lines.append("")
|
||||||
|
else:
|
||||||
|
cleaned_lines.append(line)
|
||||||
|
|
||||||
title_idx = None
|
title_idx = None
|
||||||
for idx, line in enumerate(filtered_lines):
|
for idx, line in enumerate(cleaned_lines):
|
||||||
if line.strip():
|
if line.strip():
|
||||||
title_idx = idx
|
title_idx = idx
|
||||||
break
|
break
|
||||||
|
|
||||||
|
if title_idx is None:
|
||||||
|
return publication_hashtags(category_tag, source_tag)
|
||||||
|
|
||||||
formatted_lines = []
|
formatted_lines = []
|
||||||
for idx, line in enumerate(filtered_lines):
|
for idx, line in enumerate(cleaned_lines):
|
||||||
if idx == title_idx and format_title:
|
if idx == title_idx and format_title:
|
||||||
if parse_mode == "html":
|
if parse_mode == "html":
|
||||||
escaped_title = html.escape(line.strip())
|
escaped_title = html.escape(line.strip())
|
||||||
@@ -62,16 +71,23 @@ def build_publication_text(
|
|||||||
formatted_lines.append(f"**{line.strip()}**")
|
formatted_lines.append(f"**{line.strip()}**")
|
||||||
else:
|
else:
|
||||||
formatted_lines.append(line.strip())
|
formatted_lines.append(line.strip())
|
||||||
|
|
||||||
|
# Ensure a newline break (blank line) after the bold title
|
||||||
|
if idx + 1 < len(cleaned_lines) and cleaned_lines[idx + 1].strip() != "":
|
||||||
|
formatted_lines.append("")
|
||||||
else:
|
else:
|
||||||
if parse_mode == "html":
|
if parse_mode == "html":
|
||||||
formatted_lines.append(html.escape(line))
|
formatted_lines.append(html.escape(line))
|
||||||
else:
|
else:
|
||||||
formatted_lines.append(line)
|
formatted_lines.append(line)
|
||||||
body = "\n".join(formatted_lines).strip()
|
|
||||||
|
raw_body = "\n".join(formatted_lines)
|
||||||
|
# Collapse multiple consecutive blank lines to at most two newlines (\n\n)
|
||||||
|
normalized_body = re.sub(r"\n{3,}", "\n\n", raw_body).strip()
|
||||||
hashtags = publication_hashtags(category_tag, source_tag)
|
hashtags = publication_hashtags(category_tag, source_tag)
|
||||||
if hashtags:
|
if hashtags:
|
||||||
return f"{body}\n\n{hashtags}" if body else hashtags
|
return f"{normalized_body}\n\n{hashtags}" if normalized_body else hashtags
|
||||||
return body
|
return normalized_body
|
||||||
|
|
||||||
|
|
||||||
def parse_categories(value: object) -> list[str]:
|
def parse_categories(value: object) -> list[str]:
|
||||||
|
|||||||
Reference in New Issue
Block a user