social media crossposting tool. 3rd time's the charm
mastodon
misskey
crossposting
bluesky
1from typing import override
2import cross.fragments as f
3from util.html import HTMLToFragmentsParser
4
5
6class StatusParser(HTMLToFragmentsParser):
7 def __init__(self) -> None:
8 super().__init__()
9
10 @override
11 def handle_a_endtag(self):
12 current_end = len(self.text)
13 start, _attr = self._tag_stack.pop("a")
14
15 href = _attr.get('href')
16 if href and current_end > start:
17 cls = _attr.get('class', '')
18 if cls:
19 if 'hashtag' in cls:
20 tag = self.text[start:current_end]
21 tag = tag[1:] if tag.startswith('#') else tag
22
23 self.fragments.append(
24 f.TagFragment(start=start, end=current_end, tag=tag)
25 )
26 return
27 if 'mention' in cls: # TODO put the full acct in the fragment
28 mention = self.text[start:current_end]
29 mention = mention[1:] if mention.startswith('@') else mention
30 self.fragments.append(
31 f.MentionFragment(start=start, end=current_end, uri=mention)
32 )
33 return
34 self.fragments.append(
35 f.LinkFragment(start=start, end=current_end, url=href)
36 )