broken-link-checker-cli 0.1.0__py3-none-any.whl

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.
broken_link_checker.py ADDED
@@ -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,7 @@
1
+ broken_link_checker.py,sha256=QVI3f1OnbGo8yhS5S__xRu_8DPTSJONiwMp9j73Zq5Q,9784
2
+ broken_link_checker_cli-0.1.0.dist-info/licenses/LICENSE,sha256=mTjYlQM7htiV7thgBkBwT0woURqRpTJG1eWK1Logfl8,1067
3
+ broken_link_checker_cli-0.1.0.dist-info/METADATA,sha256=kCNGNdt95QB07dTlGuYXoX5XezXcKfjBSsJU9s5NHGo,5087
4
+ broken_link_checker_cli-0.1.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
5
+ broken_link_checker_cli-0.1.0.dist-info/entry_points.txt,sha256=CpDyS_KVAhLiA0dX8ixq917d6MkQ8VR6QmisCxamYo4,65
6
+ broken_link_checker_cli-0.1.0.dist-info/top_level.txt,sha256=Oh9VmlsyItxmwuSnVYR63BAFKMXrABn9RxtEWLXUaug,20
7
+ broken_link_checker_cli-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (84.2.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ broken-link-checker = broken_link_checker:main
@@ -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 @@
1
+ broken_link_checker