diff options
| author | zberwaldt <17715430+zberwaldt@users.noreply.github.com> | 2024-06-16 14:59:46 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-06-16 14:59:46 -0400 |
| commit | 9a2f8dbcb02f3a2c084f9bb92c765028ba7b2ef0 (patch) | |
| tree | 395b59397e0b3769c9f9955e6e53244754b26973 | |
| parent | 09923f0a79d6c567c5cda77c756a122a8651a246 (diff) | |
| parent | 65dea987ae4217606d0a682ec49128a7a2c18c25 (diff) | |
Merge pull request #1 from zberwaldt/develop
Version 1
| -rw-r--r-- | .gitignore | 4 | ||||
| -rw-r--r-- | .python-version | 1 | ||||
| -rw-r--r-- | Default (Linux).sublime-keymap | 10 | ||||
| -rw-r--r-- | Default (Windows).sublime-keymap | 10 | ||||
| -rw-r--r-- | main.py | 13 | ||||
| -rw-r--r-- | note_tools/__init__.py | 0 | ||||
| -rw-r--r-- | note_tools/commands.py | 98 |
7 files changed, 136 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7cad780 --- /dev/null +++ b/.gitignore | |||
| @@ -0,0 +1,4 @@ | |||
| 1 | *.pyc | ||
| 2 | *.cache | ||
| 3 | *.sublime-project | ||
| 4 | *.sublime-workspace | ||
diff --git a/.python-version b/.python-version new file mode 100644 index 0000000..cc1923a --- /dev/null +++ b/.python-version | |||
| @@ -0,0 +1 @@ | |||
| 3.8 | |||
diff --git a/Default (Linux).sublime-keymap b/Default (Linux).sublime-keymap new file mode 100644 index 0000000..52ebbf1 --- /dev/null +++ b/Default (Linux).sublime-keymap | |||
| @@ -0,0 +1,10 @@ | |||
| 1 | [ | ||
| 2 | { | ||
| 3 | "keys": ["alt+n", "o"], | ||
| 4 | "command": "organize_notes" | ||
| 5 | }, | ||
| 6 | { | ||
| 7 | "keys": ["alt+n", "n"], | ||
| 8 | "command": "new_note" | ||
| 9 | } | ||
| 10 | ] | ||
diff --git a/Default (Windows).sublime-keymap b/Default (Windows).sublime-keymap new file mode 100644 index 0000000..52ebbf1 --- /dev/null +++ b/Default (Windows).sublime-keymap | |||
| @@ -0,0 +1,10 @@ | |||
| 1 | [ | ||
| 2 | { | ||
| 3 | "keys": ["alt+n", "o"], | ||
| 4 | "command": "organize_notes" | ||
| 5 | }, | ||
| 6 | { | ||
| 7 | "keys": ["alt+n", "n"], | ||
| 8 | "command": "new_note" | ||
| 9 | } | ||
| 10 | ] | ||
| @@ -0,0 +1,13 @@ | |||
| 1 | from .note_tools.commands import ( | ||
| 2 | OrganizeNotesCommand, | ||
| 3 | NewNoteCommand | ||
| 4 | ) | ||
| 5 | |||
| 6 | __all__= [ | ||
| 7 | "NewNoteCommand", | ||
| 8 | "OrganizeNotesCommand" | ||
| 9 | ] | ||
| 10 | |||
| 11 | def plugin_loaded(): | ||
| 12 | print("Note tools have been loaded.") | ||
| 13 | \ No newline at end of file | ||
diff --git a/note_tools/__init__.py b/note_tools/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/note_tools/__init__.py | |||
diff --git a/note_tools/commands.py b/note_tools/commands.py new file mode 100644 index 0000000..3c26915 --- /dev/null +++ b/note_tools/commands.py | |||
| @@ -0,0 +1,98 @@ | |||
| 1 | import sublime | ||
| 2 | import sublime_plugin | ||
| 3 | import datetime | ||
| 4 | import os | ||
| 5 | from pathlib import Path | ||
| 6 | from typing import List, Optional, Tuple | ||
| 7 | |||
| 8 | |||
| 9 | class OrganizeNotesCommand(sublime_plugin.WindowCommand): | ||
| 10 | def run(self): | ||
| 11 | |||
| 12 | now = datetime.datetime.now() | ||
| 13 | |||
| 14 | project_data = self.window.project_data() | ||
| 15 | |||
| 16 | notes_path = None | ||
| 17 | |||
| 18 | if project_data and "folders" in project_data: | ||
| 19 | for folder in project_data['folders']: | ||
| 20 | folder_path = folder['path'] | ||
| 21 | print(folder_path) | ||
| 22 | if os.path.basename(folder_path).lower() == "notes": | ||
| 23 | notes_path = folder_path | ||
| 24 | break | ||
| 25 | |||
| 26 | if notes_path is not None: | ||
| 27 | notes = self.list_files(notes_path, ['.sublime-project', '.sublime-workspace']) | ||
| 28 | for note in notes: | ||
| 29 | year, month = self.parse_file_date(note.name) | ||
| 30 | dirs = os.path.join(notes_path, year, month) | ||
| 31 | os.makedirs(dirs, exist_ok=True) | ||
| 32 | new_note = os.path.join(notes_path, year, month, note.name) | ||
| 33 | os.rename(note.resolve(), new_note) | ||
| 34 | else: | ||
| 35 | sublime.message_dialog("No 'Notes' directory found in the project. Please add a 'Notes' folder.") | ||
| 36 | |||
| 37 | def list_files(self, directory: str, ignore_extensions: Optional[List[str]] = None) -> List[Path]: | ||
| 38 | if ignore_extensions is None: | ||
| 39 | ignore_extensions = [] | ||
| 40 | return [file for file in Path(directory).glob("*") if file.is_file() and file.suffix not in ignore_extensions] | ||
| 41 | |||
| 42 | def parse_file_date (self, file_name: str) -> Tuple[str, str]: | ||
| 43 | year = file_name[:4] | ||
| 44 | month = file_name[4:6] | ||
| 45 | return year, month | ||
| 46 | |||
| 47 | |||
| 48 | class NewNoteCommand(sublime_plugin.WindowCommand): | ||
| 49 | def run(self): | ||
| 50 | # Get the current date formatted as YYYYMMDD and YYYY-MM-DD | ||
| 51 | date_str_yyyymmdd = datetime.datetime.now().strftime("%Y%m%d") | ||
| 52 | date_str_display = datetime.datetime.now().strftime("%Y-%m-%d") | ||
| 53 | file_name = f"{date_str_yyyymmdd}.md" | ||
| 54 | |||
| 55 | # Create the underline based on the length of date_str_display | ||
| 56 | underline = '=' * len(date_str_display) | ||
| 57 | |||
| 58 | # Get the project directory and look for a "Notes" or "notes" folder | ||
| 59 | project_data = self.window.project_data() | ||
| 60 | notes_path = None | ||
| 61 | |||
| 62 | if project_data and 'folders' in project_data: | ||
| 63 | for folder in project_data['folders']: | ||
| 64 | folder_path = folder['path'] | ||
| 65 | if os.path.basename(folder_path).lower() == "notes": | ||
| 66 | notes_path = folder_path | ||
| 67 | break | ||
| 68 | |||
| 69 | if notes_path: | ||
| 70 | file_path = os.path.join(notes_path, file_name) | ||
| 71 | |||
| 72 | # Check if file already exists | ||
| 73 | if os.path.exists(file_path): | ||
| 74 | sublime.message_dialog(f"The file '{file_name}' already exists.") | ||
| 75 | else: | ||
| 76 | # Create the new file and write the date string with a line underneath | ||
| 77 | with open(file_path, 'w') as file: | ||
| 78 | file.write(f"{date_str_display}\n{underline}\n\n\n") | ||
| 79 | |||
| 80 | # Open the file and set the cursor position | ||
| 81 | new_view = self.window.open_file(file_path) | ||
| 82 | self.set_cursor_position(new_view) | ||
| 83 | |||
| 84 | else: | ||
| 85 | sublime.message_dialog("No 'Notes' directory found in the project. Please add a 'Notes' folder.") | ||
| 86 | |||
| 87 | def set_cursor_position(self, view): | ||
| 88 | # Set the cursor position two lines beneath the underline | ||
| 89 | def on_load(): | ||
| 90 | view.run_command("move_to", {"to": "bof"}) | ||
| 91 | for _ in range(3): | ||
| 92 | view.run_command("move", {"by": "lines", "forward": True}) | ||
| 93 | |||
| 94 | # Add an event listener for when the file is loaded | ||
| 95 | if view.is_loading(): | ||
| 96 | sublime.set_timeout_async(lambda: self.set_cursor_position(view), 100) | ||
| 97 | else: | ||
| 98 | on_load() | ||
