Skip to content

Commit a856deb

Browse files
committed
Add check-links script
1 parent 8f7f2f7 commit a856deb

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
lines changed

bin/check-links

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
function show_usage()
6+
{
7+
cat <<EOF
8+
Usage: review-tools [TOOLS_BLACKLIST_FILE]
9+
EOF
10+
}
11+
12+
function show_help()
13+
{
14+
cat <<EOF
15+
NAME
16+
check-links – check links on given website
17+
18+
SYNOPSIS
19+
check-links [--clean] [--no-extern] [URL]
20+
21+
DESCRIPTION
22+
This is a wrapper for the LinkChecker [1] validator.
23+
24+
When called without arguments, the URL is set to http://localhost:1313, which is the default Hugo local URL.
25+
When checking external links is not deactivated, the number of requests per second is limited to 3 to avoid GitHub rate limiting.
26+
27+
On the first run, the script creates an virtual environment in 'venv-lc' at the repository root and installs all required dependencies.
28+
For later calls, only updates to 'requirements-lc.txt' are applied.
29+
30+
[1] See https://linkchecker.github.io/linkchecker/index.html
31+
32+
OPTIONS
33+
--clean
34+
Recreate the virtual environment before running the check.
35+
This installs the latest version of LinkChecker and updates requirements-lc.txt
36+
37+
--no-extern
38+
Do not check external URLs
39+
40+
-h, --help
41+
Show this help and exit
42+
EOF
43+
}
44+
45+
OPTIONS=()
46+
REPO_ROOT="$( git rev-parse --show-toplevel )"
47+
VENV_DIR="${REPO_ROOT}/venv-lc"
48+
REQUIREMENTS_TXT="${REPO_ROOT}/bin/requirements-lc.txt"
49+
50+
until [[ -z "${1-}" ]] ; do
51+
case "${1}" in
52+
--clean)
53+
DO_CLEANUP="yes"
54+
;;
55+
--no-extern)
56+
CHECK_EXTERN="no"
57+
;;
58+
--help)
59+
show_help
60+
exit 1
61+
;;
62+
--*)
63+
echo "Unknown long option '${1}'"
64+
show_usage
65+
exit 1
66+
;;
67+
-*)
68+
echo "Unknown short option '${1}'"
69+
show_usage
70+
exit 1
71+
;;
72+
*)
73+
URL="${1}"
74+
;;
75+
esac
76+
shift
77+
done
78+
79+
if [[ "${DO_CLEANUP-"no"}" = "yes" ]] ; then
80+
echo "Removing virtual environment..."
81+
rm -rf "${VENV_DIR}"
82+
rm -f "${REQUIREMENTS_TXT}"
83+
fi
84+
85+
if [[ ! -d "${VENV_DIR}" ]] ; then
86+
echo "Creating virtual environment..."
87+
python3 -m venv "${VENV_DIR}"
88+
"${VENV_DIR}/bin/pip" install --upgrade pip
89+
fi
90+
91+
if [[ ! -e "${REQUIREMENTS_TXT}" ]] ; then
92+
"${VENV_DIR}/bin/pip" install linkchecker
93+
"${VENV_DIR}/bin/pip" freeze > "${REQUIREMENTS_TXT}"
94+
else
95+
"${VENV_DIR}/bin/pip" install -r "${REQUIREMENTS_TXT}" >/dev/null
96+
fi
97+
98+
if [[ "${CHECK_EXTERN-"yes"}" == "yes" ]] ; then
99+
OPTIONS+=( "--check-extern" "--config" "${REPO_ROOT}/bin/linkcheckerrc" )
100+
fi
101+
102+
echo "Checking links on ${URL-"http://localhost:1313"}..."
103+
"${VENV_DIR}/bin/linkchecker" "${OPTIONS[@]}" \
104+
--o html \
105+
"${URL-"http://localhost:1313"}" > linkcheck.html

bin/linkcheckerrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[checking]
2+
maxrequestspersecond=1

bin/requirements-lc.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
beautifulsoup4==4.12.3
2+
certifi==2024.7.4
3+
charset-normalizer==3.3.2
4+
dnspython==2.6.1
5+
idna==3.7
6+
LinkChecker==10.4.0
7+
requests==2.32.3
8+
soupsieve==2.5
9+
urllib3==2.2.2

0 commit comments

Comments
 (0)