diff options
author | (quasar) nebula <qznebula@protonmail.com> | 2025-06-24 18:33:02 -0300 |
---|---|---|
committer | (quasar) nebula <qznebula@protonmail.com> | 2025-06-24 18:33:02 -0300 |
commit | 1bcd517874c8f0bf424f31380073a99437a499a1 (patch) | |
tree | 1d9626fe2b29f9641f2bcd8d65c1a5a604d54cc1 | |
parent | 7cf907a6b7193eb5dc9e008968367a51807e88a6 (diff) |
st4: hsmusic: replace urls with wiki links
-rw-r--r-- | apps/Sublime Text 4/Packages/User/HSMusic Editing.py | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/apps/Sublime Text 4/Packages/User/HSMusic Editing.py b/apps/Sublime Text 4/Packages/User/HSMusic Editing.py index a65c883..5d20322 100644 --- a/apps/Sublime Text 4/Packages/User/HSMusic Editing.py +++ b/apps/Sublime Text 4/Packages/User/HSMusic Editing.py @@ -244,6 +244,34 @@ def _consider_reference_cycle(view, region, cycle): else: return ('insert', 'right', next_ref + ':', 0) +def _consider_transform_url(view, region): + region = _normalize_region(region) + selected_text = view.substr(region) + + start_through_selection = sublime.Region(view.line(region).a, region.b) + match = re.search(r'https?://\S+$', view.substr(start_through_selection)) + + if not match: + return None + + # We don't have a convenient way to just replace a selection in one move, + # so just move to the end of the selection and try again next time... + if selected_text: + return ('collapse_move', len(selected_text)) + + url = match.group(0) + + hsm = r'(?:https://(?:preview\.|staging\.)?hsmusic\.wiki/|http://localhost:\d+/)' + + match = re.search(hsm + r'(album|track|artist|group|tag|flash)/(.+?)/', url) + if match: + link = f'[[{match.group(1)}:{match.group(2)}]]' + + if not link: + return None + + return ('replace_left', len(url), link, 0, 'right') + def _guess_directory(name): directory = name directory = "-".join(directory.split(" ")) @@ -276,6 +304,10 @@ class ExitWikiTagCommand(_CursorAdaptiveCommand): if rest_of_tag: return ('collapse_move', len(rest_of_tag)) + transform_url_action = _consider_transform_url(self.view, region) + if transform_url_action: + return transform_url_action + region = _normalize_region(region) to_start = sublime.Region(self.view.line(region).a, region.a) to_end = sublime.Region(region.a, self.view.line(region).b) |