forked from Ikalus1988/MisakaNet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate_lessons.py
More file actions
165 lines (138 loc) · 4.9 KB
/
Copy pathvalidate_lessons.py
File metadata and controls
165 lines (138 loc) · 4.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env python3
"""Validate all MisakaNet lessons against the lesson schema.
Usage:
python3 scripts/validate_lessons.py # validate all lessons
python3 scripts/validate_lessons.py <file> # validate a single file
"""
from __future__ import annotations
import json
import re
import sys
from pathlib import Path
REPO_ROOT = Path(__file__).resolve().parent.parent
SCHEMA_PATH = REPO_ROOT / "schemas" / "lesson.json"
LESSONS_DIR = REPO_ROOT / "lessons"
try:
import jsonschema
except ImportError:
print("ERROR: jsonschema not installed. Run: pip install jsonschema")
sys.exit(1)
def load_schema() -> dict:
with open(SCHEMA_PATH) as f:
return json.load(f)
def extract_frontmatter(path: Path) -> tuple[dict | None, str | None]:
"""Extract JSON frontmatter from a lesson markdown file."""
content = path.read_text(encoding="utf-8")
m = re.match(r"^---\s*\n(.*?)\n---", content, re.DOTALL)
if not m:
return None, "No frontmatter block found (must start with ---)"
raw = m.group(1).strip()
# Try JSON first, fall back to YAML-like
try:
fm = json.loads(raw)
return fm, None
except json.JSONDecodeError:
pass
# Simple YAML-like parser for common patterns
try:
fm = {}
for line in raw.split("\n"):
line = line.strip()
if not line:
continue
if ":" in line:
key, _, val = line.partition(":")
key = key.strip()
val = val.strip()
if val.startswith("[") and val.endswith("]"):
val = [v.strip().strip('"').strip("'") for v in val[1:-1].split(",")]
elif val.lower() == "true":
val = True
elif val.lower() == "false":
val = False
elif val.startswith('"') and val.endswith('"'):
val = val[1:-1]
elif val.startswith("'") and val.endswith("'"):
val = val[1:-1]
fm[key] = val
if fm:
return fm, None
except Exception:
pass
return None, "Frontmatter must be valid JSON"
def validate_body(path: Path) -> list[str]:
"""Check lesson body has required sections."""
content = path.read_text(encoding="utf-8")
# Strip frontmatter
body = re.sub(r"^---\s*\n.*?\n---\s*\n", "", content, count=1, flags=re.DOTALL)
errors = []
# Must have at least 3 sections
sections = re.findall(r"^##\s+(.+)", body, re.MULTILINE)
if len(sections) < 3:
errors.append(f"Body has only {len(sections)} sections (minimum 3 required: Background/Solution/Verify)")
# Check minimum content length
text_only = re.sub(r"```.*?```", "", body, flags=re.DOTALL)
text_only = re.sub(r"\s+", " ", text_only).strip()
if len(text_only) < 100:
errors.append(f"Body text too short ({len(text_only)} chars, minimum 100)")
# No placeholders
placeholders = ["TODO", "FIXME", "coming soon", "to be written"]
for ph in placeholders:
if ph.lower() in body.lower():
errors.append(f"Contains placeholder '{ph}'")
return errors
def validate_lesson(path: Path, schema: dict) -> tuple[int, list[str]]:
"""Validate a single lesson. Returns (exit_code, [errors])."""
errors = []
# 1. Frontmatter extraction
fm, fm_err = extract_frontmatter(path)
if fm_err:
errors.append(fm_err)
return 1, errors
# 2. JSON Schema validation
try:
jsonschema.validate(fm, schema)
except jsonschema.ValidationError as e:
errors.append(f"Schema violation: {e.message}")
if e.path:
errors.append(f" Path: {' -> '.join(str(p) for p in e.path)}")
return 1, errors
# 3. Body structure validation
body_errs = validate_body(path)
errors.extend(body_errs)
return 0 if not errors else 1, errors
def main():
schema = load_schema()
if len(sys.argv) > 1:
paths = [Path(sys.argv[1])]
else:
paths = sorted(LESSONS_DIR.glob("**/*.md"))
exit_code = 0
total = 0
passed = 0
failed = 0
for path in paths:
if path.name in ("index.md", "README.md"):
continue
if "_archive" in str(path):
continue
total += 1
is_core = "contrib" not in path.parts
code, errs = validate_lesson(path, schema)
if code == 0:
passed += 1
else:
failed += 1
rel = path.relative_to(REPO_ROOT)
if is_core:
print(f"❌ {rel}")
exit_code = 1
else:
print(f"⚠️ {rel} (contrib — legacy, not blocking)")
for e in errs:
print(f" - {e}")
print(f"\n{'='*40}")
print(f"Total: {total} Passed: {passed} Failed: {failed}")
return exit_code
if __name__ == "__main__":
sys.exit(main())