diff --git a/lib/plugins/bluesky.py b/lib/plugins/bluesky.py index 974796d..6c7e1b4 100644 --- a/lib/plugins/bluesky.py +++ b/lib/plugins/bluesky.py @@ -158,6 +158,12 @@ def handle_url_card( ) return embed_external + def content_in_chunks(self, content, max_chunk_length): + paragraphs = content.split("\n\n\n") + for p in paragraphs: + for chunk in textwrap.wrap(p.strip("\n"), max_chunk_length, replace_whitespace=False): + yield chunk + def wrap_text_with_index(self, content): if len(content) <= self.max_content_length: return [content] @@ -165,8 +171,8 @@ def wrap_text_with_index(self, content): placeholder_content = re.sub( r"https?://\S+", lambda m: "~" * len(m.group()), content ) - wrapped_lines = textwrap.wrap( - placeholder_content, self.max_content_length - 8, replace_whitespace=False + wrapped_lines = list( + self.content_in_chunks(placeholder_content, self.max_content_length - 8) ) final_lines = [] url_index = 0 diff --git a/lib/plugins/markdown.py b/lib/plugins/markdown.py index 7c24bc5..44b8dc3 100644 --- a/lib/plugins/markdown.py +++ b/lib/plugins/markdown.py @@ -11,6 +11,7 @@ def __init__(self, **kwargs): ) def format_content(self, content, mentions, hashtags, images, **kwargs): + content = content.replace("\n" * 3, "\n" * 2) _images = "\n".join( [f'![{image.get("alt_text", "")}]({image["url"]})' for image in images] ) diff --git a/lib/plugins/mastodon.py b/lib/plugins/mastodon.py index 3af2196..71fb0f4 100644 --- a/lib/plugins/mastodon.py +++ b/lib/plugins/mastodon.py @@ -14,21 +14,28 @@ def __init__(self, **kwargs): ) self.max_content_length = kwargs.get("max_content_length", 500) + def content_in_chunks(self, content, max_chunk_length): + paragraphs = content.split("\n\n\n") + for p in paragraphs: + for chunk in textwrap.wrap(p.strip("\n"), max_chunk_length, replace_whitespace=False): + yield chunk + def wrap_text_with_index(self, content): if len(content) <= self.max_content_length: return [content] + # urls always count as 23 chars on mastodon + placeholder = "~" * 23 urls = re.findall(r"https?://\S+", content) placeholder_content = re.sub( - r"https?://\S+", lambda m: "~" * len(m.group()), content + r"https?://\S+", placeholder, content ) - wrapped_lines = textwrap.wrap( - placeholder_content, self.max_content_length - 8, replace_whitespace=False + wrapped_lines = list( + self.content_in_chunks(placeholder_content, self.max_content_length - 8) ) final_lines = [] url_index = 0 for i, line in enumerate(wrapped_lines, 1): - while "~~~~~~~~~~" in line and url_index < len(urls): - placeholder = "~" * len(urls[url_index]) + while placeholder in line and url_index < len(urls): line = line.replace(placeholder, urls[url_index], 1) url_index += 1 final_lines.append(f"{line} ({i}/{len(wrapped_lines)})") diff --git a/lib/plugins/matrix.py b/lib/plugins/matrix.py index 498e79b..d26c8b1 100644 --- a/lib/plugins/matrix.py +++ b/lib/plugins/matrix.py @@ -120,8 +120,9 @@ async def async_create_post(self, content): await self.client.close() return True, event_link - def format_content(self, *args, **kwargs): - result = self.runner.run(self.async_format_content(*args, **kwargs)) + def format_content(self, content, *args, **kwargs): + content = content.replace("\n" * 3, "\n" * 2) + result = self.runner.run(self.async_format_content(content, *args, **kwargs)) return result def create_post(self, content, **kwargs): diff --git a/lib/plugins/slack.py b/lib/plugins/slack.py index cf2f2ec..87984ba 100644 --- a/lib/plugins/slack.py +++ b/lib/plugins/slack.py @@ -11,6 +11,12 @@ def __init__(self, **kwargs): self.channel_id = kwargs.get("channel_id") self.max_content_length = kwargs.get("max_content_length", 40000) + def content_in_chunks(self, content, max_chunk_length): + paragraphs = content.split("\n\n\n") + for p in paragraphs: + for chunk in textwrap.wrap(p.strip("\n"), max_chunk_length, replace_whitespace=False): + yield chunk + def wrap_text_with_index(self, content): if len(content) <= self.max_content_length: return [content] @@ -18,8 +24,8 @@ def wrap_text_with_index(self, content): placeholder_content = re.sub( r"https?://\S+", lambda m: "~" * len(m.group()), content ) - wrapped_lines = textwrap.wrap( - placeholder_content, self.max_content_length - 8, replace_whitespace=False + wrapped_lines = list( + self.content_in_chunks(placeholder_content, self.max_content_length - 8) ) final_lines = [] url_index = 0