"""Offline Obsidian verification lab for Chapter 07 (file-organization)."""
import json
import re
import tempfile
from pathlib import Path
VERSION = "CPython 3.12.14 offline Obsidian Vault/Markdown semantic verifier"
def case_para_and_zettelkasten_layout():
starter_dirs = ["00-日记", "10-项目", "20-领域", "30-资源", "40-归档", "Templates", "attachments"]
with tempfile.TemporaryDirectory() as tmp:
vault = Path(tmp) / "Vault"
for d in starter_dirs:
(vault / d).mkdir(parents=True)
(vault / "主页.md").write_text("# 主页\n", encoding="utf-8")
sorted_dirs = sorted(p.name for p in vault.iterdir() if p.is_dir())
numbered_order = [d for d in sorted_dirs if d[:2].isdigit()]
expected = {
"numbered_order": ["00-日记", "10-项目", "20-领域", "30-资源", "40-归档"],
"has_templates_dir": True,
"has_home_note": True,
"para_buckets": 4,
}
actual = {
"numbered_order": numbered_order,
"has_templates_dir": "Templates" in sorted_dirs,
"has_home_note": True,
"para_buckets": len([d for d in numbered_order if d != "00-日记"]),
}
return {
"name": "para_and_zettelkasten_layout",
"summary": "在临时目录构建带数字前缀的入门 PARA 混合仓库结构并验证确定性目录排序",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_nested_tags_and_folder_roles():
raw_tags = ["#python", "#obsidian", "#笔记方法", "#编程/Python", "#编程/JavaScript", "#编程/Go", "#读书/技术类", "#状态/草稿"]
tag_tree = {}
for t in raw_tags:
parts = t.lstrip("#").split("/")
cur = tag_tree
for p in parts:
cur = cur.setdefault(p, {})
expected = {
"total_tags": 8,
"programming_children": ["Go", "JavaScript", "Python"],
"status_children": ["草稿"],
"multi_tag_per_note": True,
}
actual = {
"total_tags": len(raw_tags),
"programming_children": sorted(tag_tree["编程"].keys()),
"status_children": sorted(tag_tree["状态"].keys()),
"multi_tag_per_note": True,
}
return {
"name": "nested_tags_and_folder_roles",
"summary": "解析平级与嵌套斜杠标签并构建层级标签树,核对文件夹唯一归属与标签多维标注分工",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_aliases_and_core_templates():
aliases = ["OOP", "面向对象", "Object-Oriented Programming", "对象模型"]
quick_switcher = {a.lower(): "面向对象编程.md" for a in aliases}
template = (
"---\n"
"tags:\n"
" - 概念\n"
"created: {{date}}\n"
"---\n\n"
"# {{title}}\n\n"
"## 定义\n\n一句话概括:是什么。\n"
)
rendered = template.replace("{{date}}", "2026-05-21").replace("{{title}}", "闭包")
expected = {
"alias_hit": "面向对象编程.md",
"rendered_date_line": "created: 2026-05-21",
"rendered_h1": "# 闭包",
"unsubstituted_tokens": 0,
}
actual = {
"alias_hit": quick_switcher.get("oop", ""),
"rendered_date_line": [ln for ln in rendered.splitlines() if ln.startswith("created:")][0],
"rendered_h1": [ln for ln in rendered.splitlines() if ln.startswith("# ")][0],
"unsubstituted_tokens": len(re.findall(r"\{\{[^}]+\}\}", rendered)),
}
return {
"name": "aliases_and_core_templates",
"summary": "验证 Front Matter 别名快速切换命中及核心 Templates 插件对 {{title}} 与 {{date}} 的变量替换",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_search_operators_and_bookmarks():
notes = [
{"file": "Python 装饰器", "path": "编程/Python 装饰器.md", "tags": ["#编程/Python", "#状态/进行中"], "body": "## 原理\n装饰器依赖闭包\nimport os"},
{"file": "Docker 安装", "path": "运维/Docker 安装.md", "tags": ["#运维/Docker", "#状态/已完成"], "body": "## 安装\n配置容器环境"},
{"file": "Python 列表", "path": "编程/Python 列表.md", "tags": ["#编程/Python", "#状态/已完成"], "body": "## 切片\n基础用法"},
]
def run_query(path_prefix=None, tag_prefix=None, keyword=None, section=None, line_text=None):
hits = []
for n in notes:
if path_prefix and not n["path"].startswith(path_prefix):
continue
if tag_prefix and not any(t.startswith(tag_prefix) for t in n["tags"]):
continue
if keyword and keyword not in n["body"] and keyword not in n["file"]:
continue
if section and f"## {section}" not in n["body"]:
continue
if line_text and not any(line_text in ln for ln in n["body"].splitlines()):
continue
hits.append(n["file"])
return sorted(hits)
combo_hits = run_query(path_prefix="编程/", tag_prefix="#编程/Python", keyword="闭包")
bookmark_hits = run_query(tag_prefix="#状态/进行中")
section_hits = run_query(section="安装")
line_hits = run_query(line_text="import os")
expected = {
"combo_hits": ["Python 装饰器"],
"bookmark_hits": ["Python 装饰器"],
"section_hits": ["Docker 安装"],
"line_hits": ["Python 装饰器"],
}
actual = {
"combo_hits": combo_hits,
"bookmark_hits": bookmark_hits,
"section_hits": section_hits,
"line_hits": line_hits,
}
return {
"name": "search_operators_and_bookmarks",
"summary": "离线求值 path:/tag:/section:/line: 组合搜索表达式与动态搜索书签结果",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_para_and_zettelkasten_layout(),
case_nested_tags_and_folder_roles(),
case_aliases_and_core_templates(),
case_search_operators_and_bookmarks(),
]
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14 离线 Obsidian Vault/Markdown/JSON 语义验证器(未启动桌面端 Electron GUI)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14 离线 Obsidian Vault/Markdown/JSON 语义验证器(未启动桌面端 Electron GUI)。
入口文件:projects/obsidian-file-organization-lab/explore-file-organization.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-file-organization.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线 Obsidian Vault/Markdown/JSON 语义验证器(未启动桌面端 Electron GUI)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联12章) · 含当前章节或实例 · mbskycom202612a2227.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2231.zip