Skip to content

Instantly share code, notes, and snippets.

@OdatNurd
Created August 26, 2023 06:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OdatNurd/cb79549c65872f58790abf9780c28c99 to your computer and use it in GitHub Desktop.
Save OdatNurd/cb79549c65872f58790abf9780c28c99 to your computer and use it in GitHub Desktop.
Simple plugin that lets you easily copy temporary passwords out of markdown files
import sublime
import sublime_plugin
from urllib.parse import quote, unquote
class SetUpButtonsCommand(sublime_plugin.TextCommand):
"""
When executed, this command checks the current file for all items that
match the regular expression and adds a phantom that alows for copying
text.
The regex looks for:
Some Random Text: password goes here [copy]
The password goes here part will be copied when the clipboard icon next to
the word [copy] is clicked.
This is triggered automatically by the event listener when a markdown file
is edited; however you could also trigger this manually, say via a key
binding or the command palette, if you add in new passwords after the file
has been opened.
"""
def run(self, edit):
# Create a new phantom set and attach it to the vierw
self.buttons = sublime.PhantomSet(self.view)
# Find all of the instances of the regex; the format string will pull
# the password out (based on the capture group in the regex) and put it
# into the password list; btns is the text regions of each match,
passwords = []
btns = self.view.find_all(r'^[^:]+: (.*) \[copy\]',0, '$1', passwords)
# Set up a phantom for every match; this zips together the regions and
# the passwords they contain for easy handling.
phantoms = []
for btn,password in zip(btns, passwords):
# Add in a phantom; the -5 moves the phantom 4 characters back from
# the end of the matched region, so that it ends up in the square
# brackets.
phantoms.append(sublime.Phantom(
sublime.Region(btn.b - 5),
f'<a href="copy#{quote(password)}">&#128203;</a>',
sublime.LAYOUT_INLINE,
self.click
))
# Add the regions to the buffer
self.buttons.update(phantoms)
def click(self, href):
"""
This gets invoked with the href attribute of one of clicked link in a
phantom; we pull the passwor dout and put it onto the clipboard.
"""
password = href.split('#')[-1]
sublime.set_clipboard(unquote(password))
self.view.window().status_message('secret password copied to the clipboard')
class MarkdownPasswordCopyListener(sublime_plugin.ViewEventListener):
"""
Simple listener that watches for a markdown file to open and then triggers
the command that looks for passwords.
"""
@classmethod
def is_applicable(cls, settings):
return "/Markdown/" in settings.get("syntax")
def on_load(self):
self.view.run_command("set_up_buttons")
@OdatNurd
Copy link
Author

OdatNurd commented Aug 26, 2023

This is a simple plugin created at the request of user Roilisi in my Twitch Stream.

When you open a markdown file with a sequence of text like:

Some Text: a password here [copy]

The plugin will inject a clipboard button inside of the square brackets which, when clicked, will copy the password to the clipboard.

If you're not sure how to use plugins in Sublime Text, this video shows how to set that up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment