forked from mdesantis/MarkdownEditing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gather_missing_links.py
23 lines (18 loc) · 1.01 KB
/
gather_missing_links.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import sublime_plugin
class GatherMissingLinkMarkersCommand(sublime_plugin.TextCommand):
def run(self, edit):
markers = []
self.view.find_all("\]\[([^\]]+)\]", 0, "$1", markers)
self.view.find_all("\[([^\]]*)\]\[\]", 0, "$1", markers)
missinglinks = [link for link in set(markers) if not self.view.find_all("\n\s*\[%s\]:" % link)]
if len(missinglinks):
# Remove all whitespace at the end of the file
whitespace_at_end = self.view.find(r'\s*\z', 0)
self.view.replace(edit, whitespace_at_end, "\n")
# If there is not already a reference list at the and, insert a new line at the end
if not self.view.find(r'\n\s*\[[^\]]*\]:.*\s*\z', 0):
self.view.insert(edit, self.view.size(), "\n")
for link in missinglinks:
self.view.insert(edit, self.view.size(), '[%s]: \n' % link)
def is_enabled(self):
return bool(self.view.score_selector(self.view.sel()[0].a, "text.html.markdown"))