"""Offline Obsidian verification lab for Chapter 12 (web-clipper)."""
import json
import re
VERSION = "CPython 3.12.14 offline Obsidian Vault/Markdown semantic verifier"
def case_browser_stores_and_clip_modes():
stores = {
"Chrome / Brave / Arc / Vivaldi": "Chrome Web Store",
"Firefox": "Firefox Add-ons",
"Safari(iOS / macOS)": "App Store",
"Edge": "Edge Add-ons",
}
highlights = ["所有内容保存在本地 Markdown 文件。", "设置与高亮支持导出为 JSON。"]
formatted_highlights = "\n".join(f"> {h}" for h in highlights)
settings_json = json.dumps({"language": "zh", "vaults": ["PersonalVault"], "highlights": highlights}, ensure_ascii=False)
reloaded = json.loads(settings_json)
expected = {
"browser_group_count": 4,
"highlight_blockquote_lines": 2,
"local_json_exportable": True,
"only_network_feature": "Interpreter Prompt Variables",
}
actual = {
"browser_group_count": len(stores),
"highlight_blockquote_lines": len([ln for ln in formatted_highlights.splitlines() if ln.startswith("> ")]),
"local_json_exportable": len(reloaded["highlights"]) == 2,
"only_network_feature": "Interpreter Prompt Variables",
}
return {
"name": "browser_stores_and_clip_modes",
"summary": "校验四大浏览器安装渠道、高亮剪藏引用块渲染及本地 JSON 导出与隐私边界",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_template_types_and_triggers():
builtin_templates = ["文章模板", "食谱模板", "电影/书籍模板", "学术论文模板"]
custom_fields = ["模板名称", "笔记名称", "保存位置", "Properties", "笔记正文"]
triggers = [
("youtube.com", "YouTube 模板"),
("arxiv.org", "论文模板"),
("amazon.com", "购物模板"),
]
def match_template(url: str) -> str:
for domain, tpl in triggers:
if domain in url:
return tpl
return "Default"
expected = {
"builtin_count": 4,
"custom_field_count": 5,
"matched_arxiv": "论文模板",
"matched_youtube": "YouTube 模板",
"matched_default": "Default",
}
actual = {
"builtin_count": len(builtin_templates),
"custom_field_count": len(custom_fields),
"matched_arxiv": match_template("https://arxiv.org/abs/2501.00001"),
"matched_youtube": match_template("https://www.youtube.com/watch?v=demo"),
"matched_default": match_template("https://obsidian.md/blog"),
}
return {
"name": "template_types_and_triggers",
"summary": "核对四类典型模板、自定义模板五项配置以及按 URL 包含条件自动触发路由",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_preset_and_advanced_variables():
preset_vars = {
"title": "本地知识库实践指南",
"url": "https://example.org/obsidian-guide",
"author": "林青",
"published": "2026-05-20",
"description": "面向个人知识管理的结构化笔记方法",
"content": "正文支持代码块与 LaTeX 公式。",
"highlights": "> 本地纯文本永不过时",
"date": "2026-05-21",
"site": "Example Tech",
"domain": "example.org",
"image": "https://example.org/cover.png",
"words": "1280",
}
meta_tags = {
("name", "description"): preset_vars["description"],
("property", "og:title"): preset_vars["title"],
}
dom_nodes = {
"h1": preset_vars["title"],
".author": preset_vars["author"],
"img.hero?src": preset_vars["image"],
}
schema_ld = {
"@Recipe:name": "番茄炒蛋",
"@Article:author": preset_vars["author"],
"author.name": preset_vars["author"],
}
prompt_exprs = [
'{{"用三句话总结本文"}}',
'{{"提取文中所有人名,以列表形式返回"}}',
'{{"将关键论点翻译为中文"}}',
]
parsed_prompts = [re.fullmatch(r'\{\{"([^"]+)"\}\}', p).group(1) for p in prompt_exprs]
expected = {
"preset_var_count": 12,
"meta_og_title": "本地知识库实践指南",
"selector_hero_src": "https://example.org/cover.png",
"schema_recipe_name": "番茄炒蛋",
"prompt_var_count": 3,
}
actual = {
"preset_var_count": len(preset_vars),
"meta_og_title": meta_tags[("property", "og:title")],
"selector_hero_src": dom_nodes["img.hero?src"],
"schema_recipe_name": schema_ld["@Recipe:name"],
"prompt_var_count": len(parsed_prompts),
}
return {
"name": "preset_and_advanced_variables",
"summary": "校验 12 项预设变量以及 Meta、CSS 选择器、Schema.org 与 AI Prompt 四类高级变量解析",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_article_template_render_and_faq():
ctx = {
"date:YYYY-MM-DD": "2026-05-21",
"title": "深入理解双链笔记",
"author": "林青",
"published": "2026-05-20",
"url": "https://example.org/bilinks",
"site": "Example Tech",
"description": "双向链接与知识图谱核心概念",
"content": "双向链接让知识之间相互感知。",
}
note_name_tpl = "{{date:YYYY-MM-DD}} {{title}}"
folder = "Clippings/Articles"
props_tpl = (
"---\n"
"category: Clippings\n"
'author: "{{author}}"\n'
'published: "{{published}}"\n'
'source: "{{url}}"\n'
'site: "{{site}}"\n'
'description: "{{description}}"\n'
"tags: [clippings, articles]\n"
"---\n"
)
body_tpl = (
"> [!info] 来源\n"
"> 作者:{{author}} | 发布于:{{published}} | [原文链接]({{url}})\n\n"
"{{content}}\n"
)
def render(tpl: str) -> str:
out = tpl
for k, v in ctx.items():
out = out.replace("{{" + k + "}}", v)
return out
full_path = f"{folder}/{render(note_name_tpl)}.md"
rendered_props = render(props_tpl)
rendered_body = render(body_tpl)
expected = {
"output_path": "Clippings/Articles/2026-05-21 深入理解双链笔记.md",
"valid_yaml_bounds": True,
"has_info_callout": True,
"unsubstituted_tokens": 0,
"faq_selector_fallback": "{{selector:article}}",
}
actual = {
"output_path": full_path,
"valid_yaml_bounds": rendered_props.startswith("---\n") and rendered_props.endswith("---\n"),
"has_info_callout": rendered_body.startswith("> [!info] 来源\n"),
"unsubstituted_tokens": len(re.findall(r"\{\{[^}]+\}\}", rendered_props + rendered_body)),
"faq_selector_fallback": "{{selector:article}}",
}
return {
"name": "article_template_render_and_faq",
"summary": "渲染完整文章剪藏模板(路径、清洁 YAML Properties 与 Callout 正文)并核对排错策略",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_browser_stores_and_clip_modes(),
case_template_types_and_triggers(),
case_preset_and_advanced_variables(),
case_article_template_render_and_faq(),
]
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-web-clipper-lab/explore-web-clipper.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-web-clipper.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线 Obsidian Vault/Markdown/JSON 语义验证器(未启动桌面端 Electron GUI)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联12章) · 含当前章节或实例 · mbskycom202612a2227.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2238.zip