Refactor text truncation logic in rss2bsky.py to remove ellipsis
- Updated the text truncation logic to avoid using "..." when shortening text. - Ensured that text is truncated to the maximum allowed length without additional trailing characters. - Improved handling of text variants to maintain clarity and avoid unnecessary truncation.
This commit is contained in:
22
rss2bsky.py
22
rss2bsky.py
@@ -359,7 +359,7 @@ def is_html(text: str) -> bool:
|
||||
def strip_trailing_url_punctuation(url: str) -> str:
|
||||
if not url:
|
||||
return url
|
||||
return re.sub(r"[\s…\.,;:!?)\]\"']+$", "", url.strip())
|
||||
return re.sub(r"[\s…,\.,;:!?)\]"']+$", "", url.strip())
|
||||
|
||||
|
||||
def canonicalize_url(url: str):
|
||||
@@ -401,6 +401,7 @@ def process_title(title: str) -> str:
|
||||
title_text = clean_whitespace(title_text)
|
||||
return title_text
|
||||
|
||||
|
||||
def build_post_text_variants(title_text: str, link: str, max_length: int = 300):
|
||||
title_text = clean_whitespace(title_text)
|
||||
link = canonicalize_url(link) or link or ""
|
||||
@@ -414,36 +415,35 @@ def build_post_text_variants(title_text: str, link: str, max_length: int = 300):
|
||||
seen.add(cleaned)
|
||||
variants.append(cleaned)
|
||||
|
||||
# Variant 1: títol + link (si cap sencer)
|
||||
# Variant 1: title + link (if fits completely)
|
||||
if title_text and link:
|
||||
full = f"{title_text}\n\n{link}"
|
||||
if len(full) <= max_length:
|
||||
add_variant(full)
|
||||
else:
|
||||
# Trunca el títol per fer-hi lloc al link
|
||||
# Reserva espai per "\n\n" + link
|
||||
# Truncate the title to make space for the link
|
||||
# Reserve space for "\n\n" + link
|
||||
reserve = len(link) + 2
|
||||
available = max_length - reserve
|
||||
if available > 20:
|
||||
# FIX: Use single char '…' and strip trailing dots/spaces
|
||||
truncated_title = title_text[:available - 1].rstrip(" .") + "…"
|
||||
truncated_title = title_text[:available].rstrip()
|
||||
add_variant(f"{truncated_title}\n\n{link}")
|
||||
|
||||
# Variant 2: només títol (truncat si cal)
|
||||
# Variant 2: title only (truncated if necessary)
|
||||
if title_text:
|
||||
if len(title_text) <= max_length:
|
||||
add_variant(title_text)
|
||||
else:
|
||||
# FIX: Use single char '…' and strip trailing dots/spaces
|
||||
truncated = title_text[:max_length - 1].rstrip(" .") + "…"
|
||||
truncated = title_text[:max_length].rstrip()
|
||||
add_variant(truncated)
|
||||
|
||||
# Variant 3: només link (si no hi ha títol)
|
||||
# Variant 3: link only (if no title)
|
||||
if link and not title_text:
|
||||
add_variant(link)
|
||||
|
||||
return variants
|
||||
|
||||
|
||||
def is_x_or_twitter_domain(url: str) -> bool:
|
||||
try:
|
||||
hostname = (urlparse(url).hostname or "").lower()
|
||||
@@ -500,7 +500,7 @@ def make_rich(content: str):
|
||||
text_builder.text(trailing)
|
||||
|
||||
elif cleaned_word.startswith("#") and len(cleaned_word) > 1:
|
||||
tag_name = cleaned_word[1:].rstrip(".,;:!?)'\"…")
|
||||
tag_name = cleaned_word[1:].rstrip(".,;:!?)'"…")
|
||||
if tag_name:
|
||||
text_builder.tag(cleaned_word, tag_name)
|
||||
trailing = word[len(cleaned_word):]
|
||||
|
||||
Reference in New Issue
Block a user