feat: Implement core Onyx CLI logic in src/onyx.py, remove various test scripts, and update README.md.
This commit is contained in:
1 parent
e536630b91
commit
e7304b840e
12 files changed
+55
-265
No files matched your search
+54
-3
@@ -146,13 +146,63 @@ class OnyxCLI:
|
||||
click.echo(f"Default vault: {self.default_vault}")
|
||||
click.echo(f"Path: {vault_path}")
|
||||
|
||||
def _get_template_folder(self, vault_path: Path) -> Optional[Path]:
|
||||
"""
|
||||
Get template folder path from Templater plugin (if enabled),
|
||||
falling back to core Templates plugin.
|
||||
"""
|
||||
# Try Templater plugin first
|
||||
templater_config = vault_path / ".obsidian" / "plugins" / "templater-obsidian" / "data.json"
|
||||
if templater_config.exists():
|
||||
try:
|
||||
config = json.loads(templater_config.read_text(encoding='utf-8-sig'))
|
||||
folder = config.get('templates_folder')
|
||||
if folder:
|
||||
return vault_path / folder
|
||||
except (json.JSONDecodeError, KeyError):
|
||||
pass
|
||||
|
||||
# Fall back to core Templates plugin
|
||||
templates_config = vault_path / ".obsidian" / "templates.json"
|
||||
if templates_config.exists():
|
||||
try:
|
||||
config = json.loads(templates_config.read_text(encoding='utf-8-sig'))
|
||||
folder = config.get('folder')
|
||||
if folder:
|
||||
return vault_path / folder
|
||||
except (json.JSONDecodeError, KeyError):
|
||||
pass
|
||||
|
||||
return None
|
||||
|
||||
def create_note(self, note_name: str, content: str = "", vault: str = None,
|
||||
overwrite: bool = False, append: bool = False, open_after: bool = False):
|
||||
overwrite: bool = False, append: bool = False, open_after: bool = False, template: str = None):
|
||||
vault_name, vault_path = self._get_vault_and_path(vault)
|
||||
note_path = vault_path / note_name
|
||||
if not str(note_path).endswith('.md'):
|
||||
note_path = Path(str(note_path) + '.md')
|
||||
|
||||
if template:
|
||||
template_folder = self._get_template_folder(vault_path)
|
||||
if template_folder:
|
||||
template_path = template_folder / template
|
||||
if not template_path.suffix:
|
||||
template_path = template_path.with_suffix('.md')
|
||||
|
||||
if template_path.exists():
|
||||
template_content = template_path.read_text(encoding='utf-8-sig')
|
||||
if content:
|
||||
content = template_content + '\n' + content
|
||||
else:
|
||||
content = template_content
|
||||
|
||||
# Open by default if template is used
|
||||
open_after = True
|
||||
else:
|
||||
click.echo(f"Warning: Template '{template}' not found at {template_path}")
|
||||
else:
|
||||
click.echo("Warning: Template folder not configured")
|
||||
|
||||
note_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
if note_path.exists():
|
||||
@@ -683,7 +733,8 @@ def yearly(vault):
|
||||
@click.option('--overwrite', is_flag=True, help="Overwrite existing note")
|
||||
@click.option('--append', is_flag=True, help="Append to existing note")
|
||||
@click.option('--open', 'open_after', is_flag=True, help="Open note after creation")
|
||||
def create(note_name, content, vault, overwrite, append, open_after):
|
||||
@click.option('--template', help="Template to use")
|
||||
def create(note_name, content, vault, overwrite, append, open_after, template):
|
||||
"""Create or modify a note in the vault
|
||||
|
||||
USAGE:
|
||||
@@ -692,7 +743,7 @@ def create(note_name, content, vault, overwrite, append, open_after):
|
||||
"""
|
||||
if content:
|
||||
content = content.replace('\\n', '\n')
|
||||
OnyxCLI().create_note(note_name, content, vault, overwrite, append, open_after)
|
||||
OnyxCLI().create_note(note_name, content, vault, overwrite, append, open_after, template)
|
||||
|
||||
@cli.command()
|
||||
@click.argument('note_name')
|
||||
|
||||
Reference in new issue
Block a user