Check dead or broken links

This commit is contained in:
relikd
2020-11-14 20:19:05 +01:00
parent 265c865b52
commit ea40bda0c7
4 changed files with 36 additions and 0 deletions

View File

@@ -47,6 +47,10 @@ server:
build: build:
@cd '$(PROJDIR)' && \ @cd '$(PROJDIR)' && \
lektor build --output-path ../bin --buildstate-path ../build-state -f ENABLE_APPCACHE lektor build --output-path ../bin --buildstate-path ../build-state -f ENABLE_APPCACHE
@echo
@echo 'Checking dead links ...'
@python3 extras/find-dead-links.py
@echo
deploy: deploy:
@echo @echo

32
extras/find-dead-links.py Executable file
View File

@@ -0,0 +1,32 @@
#!/usr/bin/env python3
import os
import re
from contextlib import closing
from mmap import mmap, ACCESS_READ
rx_a = re.compile(br'[@(]\.\./([^/]*)')
def regex_file(file):
with open(file, 'r') as f:
with closing(mmap(f.fileno(), 0, access=ACCESS_READ)) as d:
for x in re.finditer(rx_a, d):
yield x.group(1).decode('utf-8')
base_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
dist_dir = os.path.join(base_dir, 'data', 'distribution')
all_ids = None
for path, dirs, files in os.walk(dist_dir):
if not all_ids:
all_ids = dirs
for lr_file in files:
if not lr_file.endswith('lr'):
continue
for link in regex_file(path + os.path.sep + lr_file):
if link not in all_ids:
short_name = os.path.basename(path) + os.path.sep + lr_file
print(f'dead-link: {short_name} ({link})')
print('done.')