forked from SublimeText-Markdown/MarkdownEditing
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Dave Ellis
committed
Oct 2, 2017
1 parent
62e8e92
commit f8985ae
Showing
7 changed files
with
89 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import sublime, sublime_plugin | ||
import os, string | ||
import re | ||
import mmap | ||
|
||
class ListBackLinks(sublime_plugin.TextCommand): | ||
def run(self, edit): | ||
print("Listing backlinks") | ||
self.backlinks = self.find_backlinks() | ||
self.select_backlink() | ||
|
||
|
||
def find_backlinks(self): | ||
current_file = self.view.file_name() | ||
current_dir, current_base = os.path.split(current_file) | ||
current_name, current_extension = os.path.splitext(current_base) | ||
|
||
results = [] | ||
for dirname, _, files in self.list_dir_tree(current_dir): | ||
for file in files: | ||
filename = os.path.join(dirname, file) | ||
if self.contains_wikilink(filename, current_name): | ||
results.append(filename) | ||
|
||
return results | ||
|
||
|
||
def contains_wikilink(self, filename, current_name): | ||
link_text = "[[" + current_name + "]]" | ||
print("Searching %s for: %s" % (filename, current_name)) | ||
|
||
try: | ||
if link_text in open(filename).read(): | ||
return True | ||
except: | ||
pass | ||
|
||
return False | ||
|
||
def select_backlink(self): | ||
self.view.window().show_quick_panel(self.backlinks, self.open_file) | ||
|
||
|
||
def open_file(self, selected_index): | ||
if selected_index != -1: | ||
file = self.backlinks[selected_index] | ||
|
||
print("Opening file '%s'" % (file)) | ||
self.view.window().open_file(file) | ||
|
||
|
||
def list_dir_tree(self, directory): | ||
for dir, dirnames, files in os.walk(directory): | ||
dirnames[:] = [dirname for dirname in dirnames] | ||
yield dir, dirnames, files | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters