broken-link-checker-cli 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Haider Ali
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,124 @@
1
+ Metadata-Version: 2.4
2
+ Name: broken-link-checker-cli
3
+ Version: 0.1.0
4
+ Summary: Check links in Markdown and HTML docs against real pages — and tell page failures apart from anchor failures.
5
+ Author: Haider Ali
6
+ License: MIT
7
+ Project-URL: Homepage, https://github.com/haiderstudio/broken-link-checker-cli
8
+ Project-URL: Repository, https://github.com/haiderstudio/broken-link-checker-cli
9
+ Project-URL: Issues, https://github.com/haiderstudio/broken-link-checker-cli/issues
10
+ Keywords: links,markdown,html,docs,cli,developer-tools
11
+ Classifier: Development Status :: 4 - Beta
12
+ Classifier: Environment :: Console
13
+ Classifier: Intended Audience :: Developers
14
+ Classifier: License :: OSI Approved :: MIT License
15
+ Classifier: Programming Language :: Python :: 3
16
+ Classifier: Topic :: Documentation
17
+ Classifier: Topic :: Software Development :: Quality Assurance
18
+ Requires-Python: >=3.9
19
+ Description-Content-Type: text/markdown
20
+ License-File: LICENSE
21
+ Dynamic: license-file
22
+
23
+ # broken-link-checker-cli
24
+
25
+ Check links in Markdown and HTML docs against real pages — and tell page failures apart from anchor failures.
26
+
27
+ [![CI](https://github.com/haiderstudio/broken-link-checker-cli/actions/workflows/ci.yml/badge.svg)](https://github.com/haiderstudio/broken-link-checker-cli/actions/workflows/ci.yml) [![Licence: MIT](https://img.shields.io/badge/Licence-MIT-yellow.svg)](LICENSE)
28
+
29
+ ## What it does
30
+
31
+ `broken-link-checker` scans `.md`, `.markdown`, and `.html` files for links and checks each one for real:
32
+
33
+ - Local links: the target file must exist. If the link has an anchor, the target Markdown heading (GitHub-style slug) or HTML `id`/`name` must exist too.
34
+ - Remote links: the URL must answer (HEAD, falling back to GET where a server refuses HEAD). If the link has an anchor, the fetched page must carry that `id` or `name`.
35
+ - Every result is one of three statuses, with the broken kind named: `BROKEN (page)` for a missing page, `BROKEN (anchor)` for a page that exists without that anchor. A link is never reported OK unless its check actually ran.
36
+ - Links inside fenced code blocks and inline code spans are ignored, so code samples never produce false failures.
37
+ - Exit code 0 means no broken links, 1 means at least one broken link, 2 means a usage error such as a missing path.
38
+
39
+ ## What it does NOT do
40
+
41
+ - It does not guess. If an anchor cannot be checked (for example, an anchor on a PDF), the link is reported `SKIPPED` with the reason, never OK.
42
+ - It does not check `mailto:`, `tel:`, or other non-web schemes — those are reported `SKIPPED`, not broken.
43
+ - It does not follow JavaScript-rendered pages, log in to sites, or rewrite any file. It only reads and reports.
44
+ - It does not send your docs anywhere. Remote checks are plain requests to the linked URL itself; everything else runs locally.
45
+
46
+ ## Who it is for
47
+
48
+ Anyone who maintains docs, a README collection, or a static site and wants a broken link caught before a reader finds it — in CI, or as a one-off check before publishing.
49
+
50
+ ## Install
51
+
52
+ Python standard library only — no packages to download. Clone the repo and run it in place:
53
+
54
+ ```sh
55
+ git clone https://github.com/haiderstudio/broken-link-checker-cli.git
56
+ cd broken-link-checker-cli
57
+ ```
58
+
59
+ ## Usage
60
+
61
+ Check a directory (scanned recursively) or individual files:
62
+
63
+ ```sh
64
+ python3 src/broken_link_checker.py docs/
65
+ python3 src/broken_link_checker.py README.md docs/guide.md
66
+ ```
67
+
68
+ Real output from the bundled examples (one link is deliberately broken):
69
+
70
+ ```text
71
+ examples/sample-docs.md:3: other-page.md -> OK
72
+ examples/sample-docs.md:5: #sample-docs -> OK
73
+ examples/sample-docs.md:8: missing-page.md -> BROKEN (page) (file does not exist)
74
+ 2 ok, 1 broken, 0 skipped
75
+ ```
76
+
77
+ Useful flags:
78
+
79
+ ```sh
80
+ # Skip remote checks entirely (local files only)
81
+ python3 src/broken_link_checker.py --local-only docs/
82
+
83
+ # Machine-readable output
84
+ python3 src/broken_link_checker.py --format json docs/
85
+
86
+ # Skip URLs matching a pattern (repeatable)
87
+ python3 src/broken_link_checker.py --exclude 'example\.com' docs/
88
+ ```
89
+
90
+ ## Examples
91
+
92
+ Check only local links in a docs folder, so the run works offline:
93
+
94
+ ```sh
95
+ python3 src/broken_link_checker.py --local-only docs/
96
+ ```
97
+
98
+ Check one file and get JSON for a CI step:
99
+
100
+ ```sh
101
+ python3 src/broken_link_checker.py --format json README.md
102
+ ```
103
+
104
+ Run the bundled example (expect 2 ok, 1 broken, exit code 1):
105
+
106
+ ```sh
107
+ sh examples/run-example.sh
108
+ ```
109
+
110
+ ## How it is tested
111
+
112
+ ```sh
113
+ python3 tests/test_broken_link_checker.py
114
+ ```
115
+
116
+ The tests use only the standard library's `unittest`. Remote checks are exercised against a real HTTP server started by the tests on localhost — never against the public internet — covering a good page, a 404 page, a good anchor, and a missing anchor, plus local files, same-page anchors, skipped schemes, CLI exit codes, and JSON counts.
117
+
118
+ ## Contributing
119
+
120
+ Small, focused improvements are welcome. Open an issue first for anything larger than a typo, keep the standard-library-only rule, and run the tests before opening a pull request. See [CONTRIBUTING.md](CONTRIBUTING.md).
121
+
122
+ ## Licence
123
+
124
+ MIT — see [LICENSE](LICENSE). This is original work, built fresh for this repo.
@@ -0,0 +1,102 @@
1
+ # broken-link-checker-cli
2
+
3
+ Check links in Markdown and HTML docs against real pages — and tell page failures apart from anchor failures.
4
+
5
+ [![CI](https://github.com/haiderstudio/broken-link-checker-cli/actions/workflows/ci.yml/badge.svg)](https://github.com/haiderstudio/broken-link-checker-cli/actions/workflows/ci.yml) [![Licence: MIT](https://img.shields.io/badge/Licence-MIT-yellow.svg)](LICENSE)
6
+
7
+ ## What it does
8
+
9
+ `broken-link-checker` scans `.md`, `.markdown`, and `.html` files for links and checks each one for real:
10
+
11
+ - Local links: the target file must exist. If the link has an anchor, the target Markdown heading (GitHub-style slug) or HTML `id`/`name` must exist too.
12
+ - Remote links: the URL must answer (HEAD, falling back to GET where a server refuses HEAD). If the link has an anchor, the fetched page must carry that `id` or `name`.
13
+ - Every result is one of three statuses, with the broken kind named: `BROKEN (page)` for a missing page, `BROKEN (anchor)` for a page that exists without that anchor. A link is never reported OK unless its check actually ran.
14
+ - Links inside fenced code blocks and inline code spans are ignored, so code samples never produce false failures.
15
+ - Exit code 0 means no broken links, 1 means at least one broken link, 2 means a usage error such as a missing path.
16
+
17
+ ## What it does NOT do
18
+
19
+ - It does not guess. If an anchor cannot be checked (for example, an anchor on a PDF), the link is reported `SKIPPED` with the reason, never OK.
20
+ - It does not check `mailto:`, `tel:`, or other non-web schemes — those are reported `SKIPPED`, not broken.
21
+ - It does not follow JavaScript-rendered pages, log in to sites, or rewrite any file. It only reads and reports.
22
+ - It does not send your docs anywhere. Remote checks are plain requests to the linked URL itself; everything else runs locally.
23
+
24
+ ## Who it is for
25
+
26
+ Anyone who maintains docs, a README collection, or a static site and wants a broken link caught before a reader finds it — in CI, or as a one-off check before publishing.
27
+
28
+ ## Install
29
+
30
+ Python standard library only — no packages to download. Clone the repo and run it in place:
31
+
32
+ ```sh
33
+ git clone https://github.com/haiderstudio/broken-link-checker-cli.git
34
+ cd broken-link-checker-cli
35
+ ```
36
+
37
+ ## Usage
38
+
39
+ Check a directory (scanned recursively) or individual files:
40
+
41
+ ```sh
42
+ python3 src/broken_link_checker.py docs/
43
+ python3 src/broken_link_checker.py README.md docs/guide.md
44
+ ```
45
+
46
+ Real output from the bundled examples (one link is deliberately broken):
47
+
48
+ ```text
49
+ examples/sample-docs.md:3: other-page.md -> OK
50
+ examples/sample-docs.md:5: #sample-docs -> OK
51
+ examples/sample-docs.md:8: missing-page.md -> BROKEN (page) (file does not exist)
52
+ 2 ok, 1 broken, 0 skipped
53
+ ```
54
+
55
+ Useful flags:
56
+
57
+ ```sh
58
+ # Skip remote checks entirely (local files only)
59
+ python3 src/broken_link_checker.py --local-only docs/
60
+
61
+ # Machine-readable output
62
+ python3 src/broken_link_checker.py --format json docs/
63
+
64
+ # Skip URLs matching a pattern (repeatable)
65
+ python3 src/broken_link_checker.py --exclude 'example\.com' docs/
66
+ ```
67
+
68
+ ## Examples
69
+
70
+ Check only local links in a docs folder, so the run works offline:
71
+
72
+ ```sh
73
+ python3 src/broken_link_checker.py --local-only docs/
74
+ ```
75
+
76
+ Check one file and get JSON for a CI step:
77
+
78
+ ```sh
79
+ python3 src/broken_link_checker.py --format json README.md
80
+ ```
81
+
82
+ Run the bundled example (expect 2 ok, 1 broken, exit code 1):
83
+
84
+ ```sh
85
+ sh examples/run-example.sh
86
+ ```
87
+
88
+ ## How it is tested
89
+
90
+ ```sh
91
+ python3 tests/test_broken_link_checker.py
92
+ ```
93
+
94
+ The tests use only the standard library's `unittest`. Remote checks are exercised against a real HTTP server started by the tests on localhost — never against the public internet — covering a good page, a 404 page, a good anchor, and a missing anchor, plus local files, same-page anchors, skipped schemes, CLI exit codes, and JSON counts.
95
+
96
+ ## Contributing
97
+
98
+ Small, focused improvements are welcome. Open an issue first for anything larger than a typo, keep the standard-library-only rule, and run the tests before opening a pull request. See [CONTRIBUTING.md](CONTRIBUTING.md).
99
+
100
+ ## Licence
101
+
102
+ MIT — see [LICENSE](LICENSE). This is original work, built fresh for this repo.
@@ -0,0 +1,32 @@
1
+ [build-system]
2
+ requires = ["setuptools>=61"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "broken-link-checker-cli"
7
+ version = "0.1.0"
8
+ description = "Check links in Markdown and HTML docs against real pages — and tell page failures apart from anchor failures."
9
+ readme = { file = "README.md", content-type = "text/markdown" }
10
+ requires-python = ">=3.9"
11
+ license = { text = "MIT" }
12
+ authors = [{ name = "Haider Ali" }]
13
+ keywords = ["links", "markdown", "html", "docs", "cli", "developer-tools"]
14
+ classifiers = [
15
+ "Development Status :: 4 - Beta",
16
+ "Environment :: Console",
17
+ "Intended Audience :: Developers",
18
+ "License :: OSI Approved :: MIT License",
19
+ "Programming Language :: Python :: 3",
20
+ "Topic :: Documentation",
21
+ "Topic :: Software Development :: Quality Assurance",
22
+ ]
23
+ urls = { Homepage = "https://github.com/haiderstudio/broken-link-checker-cli", Repository = "https://github.com/haiderstudio/broken-link-checker-cli", Issues = "https://github.com/haiderstudio/broken-link-checker-cli/issues" }
24
+
25
+ [project.scripts]
26
+ broken-link-checker = "broken_link_checker:main"
27
+
28
+ [tool.setuptools.package-dir]
29
+ "" = "src"
30
+
31
+ [tool.setuptools]
32
+ py-modules = ["broken_link_checker"]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,282 @@
1
+ #!/usr/bin/env python3
2
+ """Check links in Markdown and HTML docs.
3
+
4
+ Usage:
5
+ python3 src/broken_link_checker.py docs/
6
+ python3 src/broken_link_checker.py README.md docs/guide.md
7
+ python3 src/broken_link_checker.py --local-only docs/
8
+ python3 src/broken_link_checker.py --format json docs/
9
+
10
+ For every link it reports one of OK, BROKEN, or SKIPPED, and it
11
+ distinguishes the two broken kinds: a page that does not exist
12
+ (page) versus a page that exists but has no such anchor (anchor).
13
+ A link is never reported OK unless the check for it actually ran.
14
+
15
+ Python standard library only. Nothing is uploaded anywhere; remote
16
+ checks are plain HEAD/GET requests to the linked URL itself.
17
+ """
18
+
19
+ import argparse
20
+ import json
21
+ import re
22
+ import sys
23
+ import urllib.error
24
+ import urllib.parse
25
+ import urllib.request
26
+ from pathlib import Path
27
+
28
+ LINK_RE = re.compile(r"!?\[([^\]]*)\]\(([^)\s]+)(?:\s+\"[^\"]*\")?\)")
29
+ AUTOLINK_RE = re.compile(r"<(https?://[^>\s]+)>")
30
+ HTML_LINK_RE = re.compile(
31
+ r"""(?:href|src)\s*=\s*["']([^"']+)["']""", re.IGNORECASE
32
+ )
33
+ FENCE_RE = re.compile(r"^\s*(```|~~~)")
34
+ HEADING_RE = re.compile(r"^#{1,6}\s+(.+?)\s*#*\s*$")
35
+ SKIP_SCHEMES = {"mailto", "tel", "data", "javascript"}
36
+ CHECKABLE_SUFFIXES = {".md", ".markdown", ".html", ".htm"}
37
+
38
+
39
+ def slugify(text):
40
+ """GitHub-style heading anchor: lowercase, punctuation stripped."""
41
+ text = re.sub(r"<[^>]+>", "", text).strip().lower()
42
+ text = re.sub(r"[^a-z0-9 _-]", "", text)
43
+ return re.sub(r"\s+", "-", text).strip("-")
44
+
45
+
46
+ def strip_code(text):
47
+ """Remove fenced code blocks and inline code spans before scanning."""
48
+ out = []
49
+ in_fence = False
50
+ fence = ""
51
+ for line in text.splitlines():
52
+ match = FENCE_RE.match(line)
53
+ if match:
54
+ marker = match.group(1)
55
+ if not in_fence:
56
+ in_fence, fence = True, marker
57
+ elif marker == fence:
58
+ in_fence = False
59
+ # Keep the line count identical so reported line numbers match
60
+ # the original document.
61
+ out.append("")
62
+ continue
63
+ if in_fence:
64
+ out.append("")
65
+ else:
66
+ out.append(re.sub(r"`[^`]*`", "", line))
67
+ return "\n".join(out)
68
+
69
+
70
+ def extract_links(text, is_html=False):
71
+ """Return [(line_number, target)] in document order."""
72
+ found = []
73
+ if is_html:
74
+ for lineno, line in enumerate(text.splitlines(), 1):
75
+ for match in HTML_LINK_RE.finditer(line):
76
+ found.append((lineno, match.group(1)))
77
+ return found
78
+ clean = strip_code(text)
79
+ for lineno, line in enumerate(clean.splitlines(), 1):
80
+ for match in LINK_RE.finditer(line):
81
+ found.append((lineno, match.group(2)))
82
+ for match in AUTOLINK_RE.finditer(line):
83
+ found.append((lineno, match.group(1)))
84
+ return found
85
+
86
+
87
+ def headings_of_markdown(text):
88
+ anchors = set()
89
+ for line in text.splitlines():
90
+ match = HEADING_RE.match(line)
91
+ if match:
92
+ anchors.add(slugify(match.group(1)))
93
+ return anchors
94
+
95
+
96
+ def anchors_of_html(text):
97
+ ids = set(re.findall(r"""id\s*=\s*["']([^"']+)["']""", text, re.IGNORECASE))
98
+ ids |= set(re.findall(r"""name\s*=\s*["']([^"']+)["']""", text, re.IGNORECASE))
99
+ return ids
100
+
101
+
102
+ def fetch(url, timeout, method="HEAD"):
103
+ request = urllib.request.Request(url, method=method)
104
+ request.add_header("User-Agent", "broken-link-checker-cli/1.0")
105
+ with urllib.request.urlopen(request, timeout=timeout) as response:
106
+ return response.status, response.read()
107
+
108
+
109
+ def check_remote(url, fragment, timeout):
110
+ try:
111
+ status, _ = fetch(url, timeout, "HEAD")
112
+ except urllib.error.HTTPError as exc:
113
+ if exc.code in (400, 401, 403, 404, 405, 410, 501):
114
+ # Some servers refuse HEAD; a GET is the real verdict.
115
+ try:
116
+ status, body = fetch(url, timeout, "GET")
117
+ except urllib.error.HTTPError as err:
118
+ return "BROKEN", "page", f"HTTP {err.code}"
119
+ except Exception as err: # network failure, not a page verdict
120
+ return "BROKEN", "page", str(err)
121
+ else:
122
+ return "BROKEN", "page", f"HTTP {exc.code}"
123
+ except Exception as err:
124
+ return "BROKEN", "page", str(err)
125
+ if status >= 400:
126
+ return "BROKEN", "page", f"HTTP {status}"
127
+ if not fragment:
128
+ return "OK", "", ""
129
+ try:
130
+ _, body = fetch(url, timeout, "GET")
131
+ except Exception as err:
132
+ return "BROKEN", "page", str(err)
133
+ text = body.decode("utf-8", "ignore")
134
+ if fragment in anchors_of_html(text):
135
+ return "OK", "", ""
136
+ return "BROKEN", "anchor", f"no id or name '{fragment}' on page"
137
+
138
+
139
+ def check_local(source_file, target, fragment, timeout):
140
+ base = source_file.parent
141
+ path = (base / urllib.parse.unquote(target)).resolve()
142
+ if not path.exists():
143
+ return "BROKEN", "page", "file does not exist"
144
+ if not fragment:
145
+ return "OK", "", ""
146
+ if path.suffix.lower() not in CHECKABLE_SUFFIXES:
147
+ return "SKIPPED", "", "anchor cannot be checked in this file type"
148
+ text = path.read_text(encoding="utf-8", errors="ignore")
149
+ anchors = (
150
+ headings_of_markdown(text)
151
+ if path.suffix.lower() in (".md", ".markdown")
152
+ else anchors_of_html(text)
153
+ )
154
+ if fragment in anchors:
155
+ return "OK", "", ""
156
+ return "BROKEN", "anchor", f"no heading or id '{fragment}' in file"
157
+
158
+
159
+ def check_link(source_file, target, timeout, local_only=False):
160
+ """Return (status, kind, detail) for one link target."""
161
+ parsed = urllib.parse.urlparse(target)
162
+ if parsed.scheme in SKIP_SCHEMES:
163
+ return "SKIPPED", "", f"{parsed.scheme} link, not checked"
164
+ if not parsed.scheme and not parsed.path and parsed.fragment:
165
+ # Same-page anchor: check it against the source file itself.
166
+ return check_local(source_file, source_file.name, parsed.fragment, timeout)
167
+ if parsed.scheme in ("http", "https"):
168
+ if local_only:
169
+ return "SKIPPED", "", "remote check skipped with --local-only"
170
+ url = urllib.parse.urlunparse(parsed._replace(fragment=""))
171
+ return check_remote(url, parsed.fragment, timeout)
172
+ if parsed.scheme:
173
+ return "SKIPPED", "", f"unsupported scheme '{parsed.scheme}'"
174
+ return check_local(source_file, parsed.path, parsed.fragment, timeout)
175
+
176
+
177
+ def collect_files(paths):
178
+ files = []
179
+ for raw in paths:
180
+ path = Path(raw)
181
+ if path.is_dir():
182
+ files.extend(
183
+ sorted(
184
+ p
185
+ for p in path.rglob("*")
186
+ if p.is_file()
187
+ and p.suffix.lower() in CHECKABLE_SUFFIXES
188
+ )
189
+ )
190
+ elif path.is_file():
191
+ files.append(path)
192
+ else:
193
+ raise FileNotFoundError(f"no such file or directory: {raw}")
194
+ return files
195
+
196
+
197
+ def check_files(files, timeout=10, local_only=False, exclude=()):
198
+ results = []
199
+ for source in files:
200
+ text = source.read_text(encoding="utf-8", errors="ignore")
201
+ is_html = source.suffix.lower() in (".html", ".htm")
202
+ for lineno, target in extract_links(text, is_html=is_html):
203
+ if any(re.search(pattern, target) for pattern in exclude):
204
+ results.append(
205
+ {
206
+ "file": str(source),
207
+ "line": lineno,
208
+ "url": target,
209
+ "status": "SKIPPED",
210
+ "kind": "",
211
+ "detail": "excluded",
212
+ }
213
+ )
214
+ continue
215
+ status, kind, detail = check_link(
216
+ source, target, timeout, local_only=local_only
217
+ )
218
+ results.append(
219
+ {
220
+ "file": str(source),
221
+ "line": lineno,
222
+ "url": target,
223
+ "status": status,
224
+ "kind": kind,
225
+ "detail": detail,
226
+ }
227
+ )
228
+ return results
229
+
230
+
231
+ def summarize(results):
232
+ counts = {"OK": 0, "BROKEN": 0, "SKIPPED": 0}
233
+ for item in results:
234
+ counts[item["status"]] += 1
235
+ return counts
236
+
237
+
238
+ def main(argv=None):
239
+ parser = argparse.ArgumentParser(
240
+ description="Check links in Markdown and HTML docs."
241
+ )
242
+ parser.add_argument("paths", nargs="+", help="files or directories to check")
243
+ parser.add_argument("--timeout", type=float, default=10)
244
+ parser.add_argument("--format", choices=["text", "json"], default="text")
245
+ parser.add_argument(
246
+ "--local-only", action="store_true", help="skip http(s) links"
247
+ )
248
+ parser.add_argument(
249
+ "--exclude", action="append", default=[], help="regex: skip matching URLs"
250
+ )
251
+ args = parser.parse_args(argv)
252
+ try:
253
+ files = collect_files(args.paths)
254
+ except FileNotFoundError as exc:
255
+ print(str(exc), file=sys.stderr)
256
+ return 2
257
+ results = check_files(
258
+ files,
259
+ timeout=args.timeout,
260
+ local_only=args.local_only,
261
+ exclude=args.exclude,
262
+ )
263
+ counts = summarize(results)
264
+ if args.format == "json":
265
+ print(json.dumps({"counts": counts, "results": results}, indent=2))
266
+ else:
267
+ for item in results:
268
+ line = f"{item['file']}:{item['line']}: {item['url']} -> {item['status']}"
269
+ if item["kind"]:
270
+ line += f" ({item['kind']})"
271
+ if item["detail"]:
272
+ line += f" ({item['detail']})"
273
+ print(line)
274
+ print(
275
+ f"{counts['OK']} ok, {counts['BROKEN']} broken, "
276
+ f"{counts['SKIPPED']} skipped"
277
+ )
278
+ return 1 if counts["BROKEN"] else 0
279
+
280
+
281
+ if __name__ == "__main__":
282
+ sys.exit(main())
@@ -0,0 +1,124 @@
1
+ Metadata-Version: 2.4
2
+ Name: broken-link-checker-cli
3
+ Version: 0.1.0
4
+ Summary: Check links in Markdown and HTML docs against real pages — and tell page failures apart from anchor failures.
5
+ Author: Haider Ali
6
+ License: MIT
7
+ Project-URL: Homepage, https://github.com/haiderstudio/broken-link-checker-cli
8
+ Project-URL: Repository, https://github.com/haiderstudio/broken-link-checker-cli
9
+ Project-URL: Issues, https://github.com/haiderstudio/broken-link-checker-cli/issues
10
+ Keywords: links,markdown,html,docs,cli,developer-tools
11
+ Classifier: Development Status :: 4 - Beta
12
+ Classifier: Environment :: Console
13
+ Classifier: Intended Audience :: Developers
14
+ Classifier: License :: OSI Approved :: MIT License
15
+ Classifier: Programming Language :: Python :: 3
16
+ Classifier: Topic :: Documentation
17
+ Classifier: Topic :: Software Development :: Quality Assurance
18
+ Requires-Python: >=3.9
19
+ Description-Content-Type: text/markdown
20
+ License-File: LICENSE
21
+ Dynamic: license-file
22
+
23
+ # broken-link-checker-cli
24
+
25
+ Check links in Markdown and HTML docs against real pages — and tell page failures apart from anchor failures.
26
+
27
+ [![CI](https://github.com/haiderstudio/broken-link-checker-cli/actions/workflows/ci.yml/badge.svg)](https://github.com/haiderstudio/broken-link-checker-cli/actions/workflows/ci.yml) [![Licence: MIT](https://img.shields.io/badge/Licence-MIT-yellow.svg)](LICENSE)
28
+
29
+ ## What it does
30
+
31
+ `broken-link-checker` scans `.md`, `.markdown`, and `.html` files for links and checks each one for real:
32
+
33
+ - Local links: the target file must exist. If the link has an anchor, the target Markdown heading (GitHub-style slug) or HTML `id`/`name` must exist too.
34
+ - Remote links: the URL must answer (HEAD, falling back to GET where a server refuses HEAD). If the link has an anchor, the fetched page must carry that `id` or `name`.
35
+ - Every result is one of three statuses, with the broken kind named: `BROKEN (page)` for a missing page, `BROKEN (anchor)` for a page that exists without that anchor. A link is never reported OK unless its check actually ran.
36
+ - Links inside fenced code blocks and inline code spans are ignored, so code samples never produce false failures.
37
+ - Exit code 0 means no broken links, 1 means at least one broken link, 2 means a usage error such as a missing path.
38
+
39
+ ## What it does NOT do
40
+
41
+ - It does not guess. If an anchor cannot be checked (for example, an anchor on a PDF), the link is reported `SKIPPED` with the reason, never OK.
42
+ - It does not check `mailto:`, `tel:`, or other non-web schemes — those are reported `SKIPPED`, not broken.
43
+ - It does not follow JavaScript-rendered pages, log in to sites, or rewrite any file. It only reads and reports.
44
+ - It does not send your docs anywhere. Remote checks are plain requests to the linked URL itself; everything else runs locally.
45
+
46
+ ## Who it is for
47
+
48
+ Anyone who maintains docs, a README collection, or a static site and wants a broken link caught before a reader finds it — in CI, or as a one-off check before publishing.
49
+
50
+ ## Install
51
+
52
+ Python standard library only — no packages to download. Clone the repo and run it in place:
53
+
54
+ ```sh
55
+ git clone https://github.com/haiderstudio/broken-link-checker-cli.git
56
+ cd broken-link-checker-cli
57
+ ```
58
+
59
+ ## Usage
60
+
61
+ Check a directory (scanned recursively) or individual files:
62
+
63
+ ```sh
64
+ python3 src/broken_link_checker.py docs/
65
+ python3 src/broken_link_checker.py README.md docs/guide.md
66
+ ```
67
+
68
+ Real output from the bundled examples (one link is deliberately broken):
69
+
70
+ ```text
71
+ examples/sample-docs.md:3: other-page.md -> OK
72
+ examples/sample-docs.md:5: #sample-docs -> OK
73
+ examples/sample-docs.md:8: missing-page.md -> BROKEN (page) (file does not exist)
74
+ 2 ok, 1 broken, 0 skipped
75
+ ```
76
+
77
+ Useful flags:
78
+
79
+ ```sh
80
+ # Skip remote checks entirely (local files only)
81
+ python3 src/broken_link_checker.py --local-only docs/
82
+
83
+ # Machine-readable output
84
+ python3 src/broken_link_checker.py --format json docs/
85
+
86
+ # Skip URLs matching a pattern (repeatable)
87
+ python3 src/broken_link_checker.py --exclude 'example\.com' docs/
88
+ ```
89
+
90
+ ## Examples
91
+
92
+ Check only local links in a docs folder, so the run works offline:
93
+
94
+ ```sh
95
+ python3 src/broken_link_checker.py --local-only docs/
96
+ ```
97
+
98
+ Check one file and get JSON for a CI step:
99
+
100
+ ```sh
101
+ python3 src/broken_link_checker.py --format json README.md
102
+ ```
103
+
104
+ Run the bundled example (expect 2 ok, 1 broken, exit code 1):
105
+
106
+ ```sh
107
+ sh examples/run-example.sh
108
+ ```
109
+
110
+ ## How it is tested
111
+
112
+ ```sh
113
+ python3 tests/test_broken_link_checker.py
114
+ ```
115
+
116
+ The tests use only the standard library's `unittest`. Remote checks are exercised against a real HTTP server started by the tests on localhost — never against the public internet — covering a good page, a 404 page, a good anchor, and a missing anchor, plus local files, same-page anchors, skipped schemes, CLI exit codes, and JSON counts.
117
+
118
+ ## Contributing
119
+
120
+ Small, focused improvements are welcome. Open an issue first for anything larger than a typo, keep the standard-library-only rule, and run the tests before opening a pull request. See [CONTRIBUTING.md](CONTRIBUTING.md).
121
+
122
+ ## Licence
123
+
124
+ MIT — see [LICENSE](LICENSE). This is original work, built fresh for this repo.
@@ -0,0 +1,10 @@
1
+ LICENSE
2
+ README.md
3
+ pyproject.toml
4
+ src/broken_link_checker.py
5
+ src/broken_link_checker_cli.egg-info/PKG-INFO
6
+ src/broken_link_checker_cli.egg-info/SOURCES.txt
7
+ src/broken_link_checker_cli.egg-info/dependency_links.txt
8
+ src/broken_link_checker_cli.egg-info/entry_points.txt
9
+ src/broken_link_checker_cli.egg-info/top_level.txt
10
+ tests/test_broken_link_checker.py
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ broken-link-checker = broken_link_checker:main
@@ -0,0 +1,181 @@
1
+ #!/usr/bin/env python3
2
+ """Tests for broken_link_checker. Run: python3 tests/test_broken_link_checker.py
3
+
4
+ No third-party dependencies. Remote checks run against a real local
5
+ HTTP server started by the tests, never against the public internet.
6
+ """
7
+
8
+ import json
9
+ import subprocess
10
+ import sys
11
+ import tempfile
12
+ import threading
13
+ import unittest
14
+ from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
15
+ from pathlib import Path
16
+
17
+ sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src"))
18
+ from broken_link_checker import ( # noqa: E402
19
+ check_files,
20
+ check_link,
21
+ extract_links,
22
+ main,
23
+ )
24
+
25
+ ROOT = Path(__file__).resolve().parents[1]
26
+ CLI = ROOT / "src" / "broken_link_checker.py"
27
+
28
+
29
+ class FixtureHandler(BaseHTTPRequestHandler):
30
+ def _send(self, code, body=b""):
31
+ self.send_response(code)
32
+ self.send_header("Content-Type", "text/html")
33
+ self.send_header("Content-Length", str(len(body)))
34
+ self.end_headers()
35
+ if self.command != "HEAD":
36
+ self.wfile.write(body)
37
+
38
+ def do_HEAD(self):
39
+ self._send(200 if self.path.split("#")[0] in ("/ok", "/page") else 404)
40
+
41
+ def do_GET(self):
42
+ if self.path == "/ok":
43
+ self._send(200, b"ok")
44
+ elif self.path == "/page":
45
+ self._send(200, b'<h2 id="intro">Intro</h2>')
46
+ else:
47
+ self._send(404, b"missing")
48
+
49
+ def log_message(self, *args):
50
+ pass
51
+
52
+
53
+ class ServerMixin(unittest.TestCase):
54
+ @classmethod
55
+ def setUpClass(cls):
56
+ cls.server = ThreadingHTTPServer(("127.0.0.1", 0), FixtureHandler)
57
+ cls.port = cls.server.server_address[1]
58
+ cls.thread = threading.Thread(
59
+ target=cls.server.serve_forever, daemon=True
60
+ )
61
+ cls.thread.start()
62
+
63
+ @classmethod
64
+ def tearDownClass(cls):
65
+ cls.server.shutdown()
66
+ cls.server.server_close()
67
+
68
+ def base(self):
69
+ return f"http://127.0.0.1:{self.port}"
70
+
71
+
72
+ class TestExtract(unittest.TestCase):
73
+ def test_code_blocks_and_inline_code_are_ignored(self):
74
+ text = (
75
+ "```\n[hidden](gone.md)\n```\n"
76
+ "Real [link](here.md) and `code [fake](gone.md)`\n"
77
+ )
78
+ self.assertEqual(extract_links(text), [(4, "here.md")])
79
+
80
+ def test_autolink_found(self):
81
+ self.assertEqual(
82
+ extract_links("see <https://example.com/x> now"),
83
+ [(1, "https://example.com/x")],
84
+ )
85
+
86
+
87
+ class TestLocal(ServerMixin):
88
+ def test_good_file_good_anchor_missing_file_missing_anchor(self):
89
+ with tempfile.TemporaryDirectory() as tmp:
90
+ root = Path(tmp)
91
+ (root / "target.md").write_text("# Getting Started\n\nBody.\n")
92
+ (root / "doc.md").write_text(
93
+ "[ok](target.md) [anchor](target.md#getting-started) "
94
+ "[gone](nope.md) [bad anchor](target.md#nope)\n"
95
+ )
96
+ results = check_files([root / "doc.md"])
97
+ by_url = {r["url"]: r for r in results}
98
+ self.assertEqual(by_url["target.md"]["status"], "OK")
99
+ self.assertEqual(
100
+ by_url["target.md#getting-started"]["status"], "OK"
101
+ )
102
+ self.assertEqual(by_url["nope.md"]["status"], "BROKEN")
103
+ self.assertEqual(by_url["nope.md"]["kind"], "page")
104
+ self.assertEqual(by_url["target.md#nope"]["status"], "BROKEN")
105
+ self.assertEqual(by_url["target.md#nope"]["kind"], "anchor")
106
+
107
+ def test_same_page_anchor_and_mailto_skipped(self):
108
+ with tempfile.TemporaryDirectory() as tmp:
109
+ root = Path(tmp)
110
+ source = root / "doc.md"
111
+ source.write_text("# Top\n\n[here](#top) [mail](mailto:a@b.c)\n")
112
+ results = check_files([source])
113
+ by_url = {r["url"]: r for r in results}
114
+ self.assertEqual(by_url["#top"]["status"], "OK")
115
+ self.assertEqual(by_url["mailto:a@b.c"]["status"], "SKIPPED")
116
+
117
+
118
+ class TestRemote(ServerMixin):
119
+ def test_page_ok_page_broken_anchor_ok_anchor_broken(self):
120
+ with tempfile.TemporaryDirectory() as tmp:
121
+ source = Path(tmp) / "doc.md"
122
+ source.write_text("x\n")
123
+ base = self.base()
124
+ self.assertEqual(
125
+ check_link(source, f"{base}/ok", 5)[0], "OK"
126
+ )
127
+ status, kind, _ = check_link(source, f"{base}/missing", 5)
128
+ self.assertEqual((status, kind), ("BROKEN", "page"))
129
+ self.assertEqual(
130
+ check_link(source, f"{base}/page#intro", 5)[0], "OK"
131
+ )
132
+ status, kind, _ = check_link(source, f"{base}/page#gone", 5)
133
+ self.assertEqual((status, kind), ("BROKEN", "anchor"))
134
+
135
+ def test_local_only_skips_remote(self):
136
+ with tempfile.TemporaryDirectory() as tmp:
137
+ source = Path(tmp) / "doc.md"
138
+ source.write_text("x\n")
139
+ status, _, _ = check_link(
140
+ source, f"{self.base()}/missing", 5, local_only=True
141
+ )
142
+ self.assertEqual(status, "SKIPPED")
143
+
144
+
145
+ class TestCli(unittest.TestCase):
146
+ def test_exit_zero_when_clean_exit_one_when_broken_json_counts(self):
147
+ with tempfile.TemporaryDirectory() as tmp:
148
+ root = Path(tmp)
149
+ (root / "good.md").write_text("[ok](good.md)\n")
150
+ (root / "bad.md").write_text("[gone](nope.md)\n")
151
+ clean = subprocess.run(
152
+ [sys.executable, str(CLI), str(root / "good.md")],
153
+ capture_output=True,
154
+ text=True,
155
+ )
156
+ self.assertEqual(clean.returncode, 0)
157
+ self.assertIn("1 ok, 0 broken", clean.stdout)
158
+ broken = subprocess.run(
159
+ [
160
+ sys.executable,
161
+ str(CLI),
162
+ "--format",
163
+ "json",
164
+ str(root / "bad.md"),
165
+ ],
166
+ capture_output=True,
167
+ text=True,
168
+ )
169
+ self.assertEqual(broken.returncode, 1)
170
+ data = json.loads(broken.stdout)
171
+ self.assertEqual(data["counts"], {"OK": 0, "BROKEN": 1, "SKIPPED": 0})
172
+ missing = subprocess.run(
173
+ [sys.executable, str(CLI), str(root / "absent")],
174
+ capture_output=True,
175
+ text=True,
176
+ )
177
+ self.assertEqual(missing.returncode, 2)
178
+
179
+
180
+ if __name__ == "__main__":
181
+ unittest.main()