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
96
97
98
99
100
101
102
103
104
105
|
import sublime
import sublime_plugin
import datetime
import os
import sys
from pathlib import Path
from typing import List, Optional, Tuple
class OrganizeNotesCommand(sublime_plugin.WindowCommand):
def run(self):
now = datetime.datetime.now()
project_data = self.window.project_data()
notes_path = None
active_sheet = self.window.active_sheet()
# Close the active note right away
active_sheet.close()
if project_data and "folders" in project_data:
for folder in project_data['folders']:
folder_path = folder['path']
print(folder_path)
if os.path.basename(folder_path).lower() == "notes":
notes_path = folder_path
break
if notes_path is not None:
notes = self.list_files(notes_path, ['.sublime-project', '.sublime-workspace'])
for note in notes:
year, month = self.parse_file_date(note.name)
dirs = os.path.join(notes_path, year, month)
os.makedirs(dirs, exist_ok=True)
new_note = os.path.join(notes_path, year, month, note.name)
os.rename(note.resolve(), new_note)
new_view = self.window.open_file(new_note)
else:
sublime.message_dialog("No 'Notes' directory found in the project. Please add a 'Notes' folder.")
def list_files(self, directory: str, ignore_extensions: Optional[List[str]] = None) -> List[Path]:
if ignore_extensions is None:
ignore_extensions = []
return [file for file in Path(directory).glob("*") if file.is_file() and file.suffix not in ignore_extensions]
def parse_file_date (self, file_name: str) -> Tuple[str, str]:
year = file_name[:4]
month = file_name[4:6]
return year, month
class NewNoteCommand(sublime_plugin.WindowCommand):
def run(self):
# Get the current date formatted as YYYYMMDD and YYYY-MM-DD
date_str_yyyymmdd = datetime.datetime.now().strftime("%Y%m%d")
date_str_display = datetime.datetime.now().strftime("%Y-%m-%d")
file_name = f"{date_str_yyyymmdd}.md"
# Create the underline based on the length of date_str_display
underline = '=' * len(date_str_display)
# Get the project directory and look for a "Notes" or "notes" folder
project_data = self.window.project_data()
notes_path = None
if project_data and 'folders' in project_data:
for folder in project_data['folders']:
folder_path = folder['path']
if os.path.basename(folder_path).lower() == "notes":
notes_path = folder_path
break
if notes_path:
file_path = os.path.join(notes_path, file_name)
# Check if file already exists
if os.path.exists(file_path):
sublime.message_dialog(f"The file '{file_name}' already exists.")
else:
# Create the new file and write the date string with a line underneath
with open(file_path, 'w') as file:
file.write(f"{date_str_display}\n{underline}\n\n\n")
# Open the file and set the cursor position
new_view = self.window.open_file(file_path)
self.set_cursor_position(new_view)
else:
sublime.message_dialog("No 'Notes' directory found in the project. Please add a 'Notes' folder.")
def set_cursor_position(self, view):
# Set the cursor position two lines beneath the underline
def on_load():
view.run_command("move_to", {"to": "bof"})
for _ in range(3):
view.run_command("move", {"by": "lines", "forward": True})
# Add an event listener for when the file is loaded
if view.is_loading():
sublime.set_timeout_async(lambda: self.set_cursor_position(view), 100)
else:
on_load()
|