-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevnotes.py
More file actions
95 lines (79 loc) · 3.06 KB
/
devnotes.py
File metadata and controls
95 lines (79 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import sublime
import sublime_plugin
import os
import re
def settings():
return sublime.load_settings('devnotes.sublime-settings')
def get_root():
project_settings = sublime.active_window().active_view().settings().get('devnotes')
if project_settings:
root = os.path.normpath(os.path.expanduser(project_settings.get('root', settings().get("root"))))
else:
root = os.path.normpath(os.path.expanduser(settings().get("root")))
return root
def expand_to_braces(string, region_begin, region_end):
regex = re.compile("(\[\[)([^\[\]]+)(\]\])")
for match in regex.finditer(string):
brace_begin = match.start()
brace_end = match.end()
if brace_end < region_end:
continue
if brace_begin > region_end:
return None
if brace_begin < region_begin:
return match.group(2)
return None
class OpenNoteCommand(sublime_plugin.ApplicationCommand):
def run(self):
self.notes_dir = get_root()
self.window = sublime.active_window()
view = self.window.active_view()
for region in view.sel():
line = view.line(region)
region_begin = region.begin() - line.begin()
region_end = region.end() - line.begin()
link = expand_to_braces(view.substr(line), region_begin, region_end)
if link:
self.open(link)
def open(self, link):
ext = "." + settings().get("note_save_extension")
file = os.path.join(self.notes_dir, link + ext)
if not os.path.exists(file):
open(file, 'w+').close()
view = self.window.open_file(file)
class NewNoteCommand(sublime_plugin.ApplicationCommand):
def run(self, title=None, linked=False):
self.notes_dir = get_root()
self.window = sublime.active_window()
self.linked = linked
if title is None:
self.window.show_input_panel("Title:", "", self.create_note, None, None)
else:
self.create_note(title)
def create_note(self, title):
fullname = title
filename = title.split("/")
if len(filename) > 1:
title = filename[-1]
folder = "\\"
folder = folder.join(filename[:-1])
directory = os.path.join(self.notes_dir, folder)
tag = filename[0]
else:
title = filename[0]
directory = self.notes_dir
tag = ""
if self.linked:
view = self.window.active_view()
view.run_command("insert_note_link", {"title": fullname})
if not os.path.exists(directory):
os.makedirs(directory)
ext = "." + settings().get("note_save_extension")
file = os.path.join(directory, title + ext)
if not os.path.exists(file):
open(file, 'w+').close()
view = sublime.active_window().open_file(file)
class InsertNoteLinkCommand(sublime_plugin.TextCommand):
def run(self, edit, title):
for region in self.view.sel():
self.view.insert(edit, region.begin(), "[["+title+"]]")