60 lines
1.6 KiB
Python
Executable File
60 lines
1.6 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""Obsidian Import Utility
|
|
|
|
Import files into an Obsidian vault:
|
|
- .md -> copy contents to a new note (filename as title)
|
|
- .pdf -> convert to markdown, extract images, link original PDF
|
|
|
|
Usage:
|
|
python main.py /path/to/file.pdf
|
|
python main.py --vault ~/Obsidian/MyVault /path/to/file.md
|
|
"""
|
|
|
|
import argparse
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
from handlers.md_handler import handle_markdown
|
|
from handlers.pdf_handler import handle_pdf
|
|
from utils.config import get_vault_path
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description="Import files into an Obsidian vault.")
|
|
parser.add_argument("file", help="Path to the file to import.")
|
|
parser.add_argument(
|
|
"--vault",
|
|
dest="vault",
|
|
default=None,
|
|
help="Path to the Obsidian vault. Overrides OBSIDIAN_VAULT env var.",
|
|
)
|
|
args = parser.parse_args()
|
|
|
|
file_path = Path(args.file).expanduser().resolve()
|
|
if not file_path.exists():
|
|
print(f"Error: file not found: {file_path}", file=sys.stderr)
|
|
sys.exit(1)
|
|
|
|
try:
|
|
vault_path = get_vault_path(args.vault)
|
|
except Exception as e:
|
|
print(f"Error: {e}", file=sys.stderr)
|
|
sys.exit(1)
|
|
|
|
suffix = file_path.suffix.lower()
|
|
|
|
if suffix == ".md":
|
|
handle_markdown(file_path, vault_path)
|
|
elif suffix == ".pdf":
|
|
handle_pdf(file_path, vault_path)
|
|
else:
|
|
print(
|
|
f"Error: unsupported file type '{suffix}'. Supported: .md, .pdf",
|
|
file=sys.stderr,
|
|
)
|
|
sys.exit(1)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|