-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck-links.py
More file actions
159 lines (137 loc) · 5.53 KB
/
Copy pathcheck-links.py
File metadata and controls
159 lines (137 loc) · 5.53 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
#!/usr/bin/env python3
"""Check for broken internal links and unused images in the docs."""
import os
import re
import sys
from pathlib import Path
from collections import defaultdict
DOCS_DIR = Path(__file__).parent / "Docs"
# Image extensions to track
IMG_EXTS = {".png", ".jpg", ".jpeg", ".gif", ".svg", ".webp", ".ico"}
def find_all_images(root: Path) -> set[Path]:
"""Return all image files under root."""
images = set()
for ext in IMG_EXTS:
for f in root.rglob(f"*{ext}"):
images.add(f.resolve())
return images
def find_all_yml_files(root: Path) -> list[Path]:
"""Return all .yml/.yaml files under root."""
return list(root.rglob("*.yml")) + list(root.rglob("*.yaml"))
def extract_yml_image_refs(yml_path: Path, base: Path = None) -> set[str]:
"""Extract image paths referenced in yml config files (e.g. logo, favicon)."""
if base is None:
base = yml_path.parent
text = yml_path.read_text(encoding="utf-8")
images = set()
for m in re.finditer(r'(?:logo|favicon|icon|image|src)\s*:\s*(.+)', text):
val = m.group(1).strip().strip('"').strip("'")
if val and not val.startswith(("http://", "https://")):
target = (base / val).resolve()
images.add(str(target))
return images
def find_all_md_files(root: Path) -> list[Path]:
"""Return all .md files under root."""
return list(root.rglob("*.md"))
def extract_references(md_path: Path) -> tuple[set[str], set[str]]:
"""
Parse a markdown file and return:
- local_links: set of local paths referenced in []() or []() links
- local_images: set of local image paths referenced in ![]() or <img src="">
Excludes external URLs (http/https).
"""
text = md_path.read_text(encoding="utf-8")
base = md_path.parent
local_links = set()
local_images = set()
# Pattern 1: [text](url) and 
for m in re.finditer(r'!?\[.*?\]\(([^\)]+)\)', text):
url = m.group(1).strip()
# Skip external URLs
if url.startswith(("http://", "https://", "mailto:", "#")):
continue
# Strip anchor and query
url = url.split("#")[0].split("?")[0]
if not url:
continue
target = (base / url).resolve()
if m.group(0).startswith("!"):
local_images.add(str(target))
else:
local_links.add(str(target))
# Pattern 2: <img src="...">
for m in re.finditer(r'<img[^>]+src=["\']([^"\']+)["\']', text, re.IGNORECASE):
url = m.group(1).strip()
if url.startswith(("http://", "https://")):
continue
url = url.split("?")[0]
if not url:
continue
target = (base / url).resolve()
local_images.add(str(target))
return local_links, local_images
def main():
md_files = find_all_md_files(DOCS_DIR)
all_images_on_disk = find_all_images(DOCS_DIR)
all_refd_links: dict[str, list[str]] = defaultdict(list) # target → [source files]
all_refd_images: dict[str, list[str]] = defaultdict(list)
print(f"Scanning {len(md_files)} .md files and {len(all_images_on_disk)} images...\n")
# Scan .md files
for md in md_files:
local_links, local_images = extract_references(md)
rel_md = str(md.relative_to(DOCS_DIR))
for link in local_links:
all_refd_links[link].append(rel_md)
for img in local_images:
all_refd_images[img].append(rel_md)
# Scan .yml config files (for logo, favicon, etc.)
# Include both Docs/ subdir and repo root (properdocs.yml lives there)
yml_files = find_all_yml_files(DOCS_DIR) + find_all_yml_files(DOCS_DIR.parent)
for yml in yml_files:
# For yml at repo root, paths are relative to DOCS_DIR (mkdocs docs_dir)
base = DOCS_DIR if yml.parent == DOCS_DIR.parent else yml.parent
imgs = extract_yml_image_refs(yml, base)
rel = str(yml.relative_to(DOCS_DIR.parent)) if DOCS_DIR.parent in yml.parents else str(yml.relative_to(DOCS_DIR))
for img in imgs:
all_refd_images[img].append(rel)
# --- Broken internal links ---
broken = 0
print("=" * 60)
print("BROKEN INTERNAL LINKS")
print("=" * 60)
for target, sources in sorted(all_refd_links.items()):
tpath = Path(target)
# Check if the target file exists
if not tpath.exists():
# Could be a .md file missing the extension — try appending .md
if not Path(target + ".md").exists():
print(f"\n ✗ {Path(target).name}")
print(f" Referenced by:")
for s in sources:
print(f" - {s}")
broken += 1
if broken == 0:
print(" ✓ No broken internal links found.")
else:
print(f"\n Total: {broken} broken link(s)")
# --- Unused images ---
unused = 0
print("\n" + "=" * 60)
print("UNUSED IMAGES")
print("=" * 60)
all_refd_image_paths = {Path(p) for p in all_refd_images}
for img in sorted(all_images_on_disk):
if img not in all_refd_image_paths:
rel = img.relative_to(DOCS_DIR)
print(f" ✗ {rel} ({img.stat().st_size / 1024:.1f} KB)")
unused += 1
if unused == 0:
print(" ✓ No unused images found.")
else:
print(f"\n Total: {unused} unused image(s)")
# --- Links that reference directories (ambiguous) ---
# (Skipping for brevity — mkdocs resolves /index.md, etc.)
print()
return 0 if (broken + unused) == 0 else 1
if __name__ == "__main__":
sys.exit(main())