forked from alshedivat/al-folio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate_jsonld.py
More file actions
73 lines (60 loc) · 2.16 KB
/
Copy pathvalidate_jsonld.py
File metadata and controls
73 lines (60 loc) · 2.16 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
#!/usr/bin/env python3
"""
Script to validate JSON-LD structured data from Jekyll site
"""
import json
import re
import sys
from pathlib import Path
def extract_and_validate_jsonld(html_file):
"""Extract and validate JSON-LD from HTML file"""
try:
with open(html_file, 'r', encoding='utf-8') as f:
content = f.read()
# Find JSON-LD script tags
pattern = r'<script type="application/ld\+json">\s*(.*?)\s*</script>'
matches = re.findall(pattern, content, re.DOTALL)
if not matches:
print(f"No JSON-LD found in {html_file}")
return False
for i, json_str in enumerate(matches):
try:
# Parse JSON to validate
parsed = json.loads(json_str)
print(f"✅ JSON-LD #{i+1} in {html_file} is valid!")
print(f" Type: {parsed.get('@type', 'Unknown')}")
if 'name' in parsed:
print(f" Name: {parsed['name']}")
print()
except json.JSONDecodeError as e:
print(f"❌ JSON-LD #{i+1} in {html_file} is INVALID!")
print(f" Error: {e}")
print(f" JSON: {json_str[:200]}...")
return False
return True
except Exception as e:
print(f"Error processing {html_file}: {e}")
return False
def main():
site_dir = Path("/Users/psurukuc/Applications/psurukuc.github.io/_site")
# Check main pages
test_files = [
site_dir / "index.html",
site_dir / "group" / "index.html",
site_dir / "research" / "index.html",
site_dir / "news" / "index.html"
]
all_valid = True
for html_file in test_files:
if html_file.exists():
if not extract_and_validate_jsonld(html_file):
all_valid = False
else:
print(f"⚠️ File not found: {html_file}")
if all_valid:
print("🎉 All JSON-LD structured data is valid!")
else:
print("💥 Some JSON-LD is invalid!")
sys.exit(1)
if __name__ == "__main__":
main()