diff --git a/src/journal_bot/vault_writer.py b/src/journal_bot/vault_writer.py new file mode 100644 index 0000000..e479d86 --- /dev/null +++ b/src/journal_bot/vault_writer.py @@ -0,0 +1,34 @@ +from datetime import date +from pathlib import Path + + +class VaultWriter: + """Appends entries to daily notes; creates files with frontmatter when missing.""" + + def __init__(self, vault_path: Path): + self.vault_path = vault_path + + def append( + self, + relative_path: str, + entry_markdown: str, + clarifications: list[str] | None = None, + ) -> None: + target = self.vault_path / relative_path + target.parent.mkdir(parents=True, exist_ok=True) + + block = entry_markdown.rstrip() + if clarifications: + lines = "\n".join(f"> - {c}" for c in clarifications) + block += f"\n\n> [!warning] Klärung\n{lines}" + + if not target.exists(): + iso = target.stem + frontmatter = f"---\ntags: [daily]\ndate: {iso}\n---\n\n" + target.write_text(frontmatter + block + "\n", encoding="utf-8") + return + + existing = target.read_text(encoding="utf-8") + if not existing.endswith("\n"): + existing += "\n" + target.write_text(existing + "\n" + block + "\n", encoding="utf-8") diff --git a/tests/test_vault_writer.py b/tests/test_vault_writer.py new file mode 100644 index 0000000..2ad0780 --- /dev/null +++ b/tests/test_vault_writer.py @@ -0,0 +1,42 @@ +from pathlib import Path +from journal_bot.vault_writer import VaultWriter + + +def test_creates_file_with_frontmatter(tmp_path): + vault = tmp_path + (vault / "05 Daily Notes").mkdir() + w = VaultWriter(vault) + w.append("05 Daily Notes/2026-06-14.md", "## 14:32\nHallo") + content = (vault / "05 Daily Notes" / "2026-06-14.md").read_text(encoding="utf-8") + assert content.startswith("---\n") + assert "tags: [daily]" in content + assert "date: 2026-06-14" in content + assert "## 14:32\nHallo" in content + + +def test_appends_to_existing_file(tmp_path): + vault = tmp_path + daily = vault / "05 Daily Notes" + daily.mkdir() + existing = daily / "2026-06-14.md" + existing.write_text("---\ntags: [daily]\ndate: 2026-06-14\n---\n\n## 09:00\nMorgen\n", encoding="utf-8") + w = VaultWriter(vault) + w.append("05 Daily Notes/2026-06-14.md", "## 14:32\nNachmittag") + content = existing.read_text(encoding="utf-8") + assert "## 09:00\nMorgen" in content + assert "## 14:32\nNachmittag" in content + assert content.count("---\ntags: [daily]") == 1 # frontmatter not duplicated + + +def test_appends_clarifications_callout(tmp_path): + vault = tmp_path + (vault / "05 Daily Notes").mkdir() + w = VaultWriter(vault) + w.append( + "05 Daily Notes/2026-06-14.md", + "## 14:32\nTreffen mit Steffen", + clarifications=["Welcher Steffen?"], + ) + content = (vault / "05 Daily Notes" / "2026-06-14.md").read_text(encoding="utf-8") + assert "> [!warning] Klärung" in content + assert "> - Welcher Steffen?" in content