feat(vault_writer): append entries with frontmatter and clarifications callout
This commit is contained in:
parent
b87462fb4c
commit
9703ae7c10
|
|
@ -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")
|
||||
|
|
@ -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
|
||||
Loading…
Reference in New Issue