Skip to content

Instantly share code, notes, and snippets.

@dansheffler
Last active June 27, 2016 08:42
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 dansheffler/46fd6d6c71d5d9d4fe6a to your computer and use it in GitHub Desktop.
Save dansheffler/46fd6d6c71d5d9d4fe6a to your computer and use it in GitHub Desktop.
[
{ "keys": ["ctrl+super+f"], "command": "make_footnote"}
]
import sublime, sublime_plugin
# Encode and Decode functions stolen from:
# http://stackoverflow.com/questions/1119722/base-62-conversion-in-python
import string
BASE_LIST = string.digits + string.ascii_letters
BASE_DICT = dict((c, i) for i, c in enumerate(BASE_LIST))
def base_decode(string, reverse_base=BASE_DICT):
length = len(reverse_base)
ret = 0
for i, c in enumerate(string[::-1]):
ret += (length ** i) * reverse_base[c]
return ret
def base_encode(integer, base=BASE_LIST):
length = len(base)
ret = ''
while integer != 0:
ret = base[integer % length] + ret
integer /= length
integer = int(integer)
return ret
class MakeFootnoteCommand(sublime_plugin.TextCommand):
def run(self, edit):
# Get the last used footnote number from a permanent, global
# setting.
settings = sublime.load_settings('MyFootnote.sublime-settings')
footnoteLast = settings.get('footnote_last')
# Decode this number into an integer, iterate and re-encode
# it
integerFootnote = base_decode(footnoteLast)
integerFootnote += 1
footnoteNew = base_encode(integerFootnote)
# Update the settings file
settings.set('footnote_last', footnoteNew)
sublime.save_settings('MyFootnote.sublime-settings')
# Insert a markdown footnote with the new number at the
# current first cursor position.
pos = self.view.sel()[0].begin()
self.view.insert(edit, pos, "[^"+ footnoteNew + "]")
par = self.view.find('\n\n+[\w>#]', pos)
if par.begin() > 0:
self.view.insert(edit, par.begin(), "\n\n [^"+ footnoteNew + "]: ")
self.view.sel().clear()
pos = self.view.find(': ', par.begin())
self.view.sel().add(pos.end())
{
"footnote_last": "2W"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment