Skip to content

Commit

Permalink
Support more cases in "get_mastodon_text.py"
Browse files Browse the repository at this point in the history
  • Loading branch information
alexwlchan committed Dec 10, 2023
1 parent 16dea51 commit 2b65a4c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
9 changes: 8 additions & 1 deletion textexpander/get_mastodon_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,18 @@ def download(url):


def normalise_text(text: str) -> str:
text = text.replace("</p><p>", "\n\n")
text = text.replace("<p>", "").replace("</p>", "")
text = re.sub(
r'<a href="[^"]+" class="mention hashtag" rel="tag">#<span>(?P<hashtag>[^<]+)</span></a>',
r"\\#\g<hashtag>",
text,
)
text = re.sub(
r'<a href="(?P<url>[^"]+)" rel="nofollow noopener noreferrer" translate="no" target="_blank"><span class="invisible">[^<]+</span><span class="ellipsis">[^<]+</span><span class="invisible">[^<]+</span></a>',
r"\g<url>",
text,
)
return text


Expand Down Expand Up @@ -75,7 +81,8 @@ def normalise_text(text: str) -> str:

print(f'[{author}]({post_url}) ({created_at.strftime("%-d %B %Y")}):')
print("")
print("> " + normalise_text(post_data["content"]))
for line in normalise_text(post_data["content"]).splitlines():
print(f"> {line}".strip())

if post_data["media_attachments"]:
print(">\n> ", end="")
Expand Down
6 changes: 5 additions & 1 deletion textexpander/test_get_mastodon_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
'hashtag" rel="tag">#<span>ArtAdventCalendar</span></a> '
"contribution</p>",
"A variation on the previous system for todays \\#ArtAdventCalendar contribution",
)
),
(
'<p>my rules for git rebase</p><p>permalink: <a href="https://wizardzines.com/comics/rules-for-rebasing/" rel="nofollow noopener noreferrer" translate="no" target="_blank"><span class="invisible">https://</span><span class="ellipsis">wizardzines.com/comics/rules-f</span><span class="invisible">or-rebasing/</span></a></p>',
"my rules for git rebase\n\npermalink: https://wizardzines.com/comics/rules-for-rebasing/",
),
],
)
def test_normalise_text(input, output):
Expand Down

0 comments on commit 2b65a4c

Please sign in to comment.