|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +import sys |
| 5 | +from pathlib import Path |
| 6 | +import re |
| 7 | + |
| 8 | +OUTPUT_FILE = Path("inconsistent-filenames.txt") |
| 9 | + |
| 10 | +IGNORE_SET = { |
| 11 | + ">", |
| 12 | + "<", |
| 13 | + "<>", |
| 14 | + ":", |
| 15 | + "?", |
| 16 | + "|", |
| 17 | + "jc.json", |
| 18 | + "lid.libuser", |
| 19 | + "mc.cli", |
| 20 | + "mc.fm", |
| 21 | + "pacman d", |
| 22 | + "pacman f", |
| 23 | + "pacman q", |
| 24 | + "pacman r", |
| 25 | + "pacman s", |
| 26 | + "pacman t", |
| 27 | + "pacman u", |
| 28 | + "parted", |
| 29 | + "print.runmailcap", |
| 30 | + "print.win", |
| 31 | + "print.zsh", |
| 32 | + "python m json.tool", |
| 33 | + "rename", |
| 34 | + "snap.esa", |
| 35 | + "snap.pkg", |
| 36 | +} |
| 37 | + |
| 38 | + |
| 39 | +def normalize(text: str) -> str: |
| 40 | + """ |
| 41 | + Normalize a string: |
| 42 | + - replace '-' with spaces |
| 43 | + - lowercase |
| 44 | + - collapse multiple spaces into one |
| 45 | + - strip leading/trailing whitespace |
| 46 | + """ |
| 47 | + text = text.replace("-", " ").lower().strip() |
| 48 | + text = re.sub(r"\s+", " ", text) |
| 49 | + return text |
| 50 | + |
| 51 | + |
| 52 | +def check_file(path: Path) -> str | None: |
| 53 | + """Check a single markdown file for name/title consistency.""" |
| 54 | + filename = path.name |
| 55 | + |
| 56 | + # Remove known suffixes |
| 57 | + command_file = filename |
| 58 | + for suffix in (".md", ".fish", ".js", ".1", ".2", ".3"): |
| 59 | + if command_file.endswith(suffix): |
| 60 | + command_file = command_file[: -len(suffix)] |
| 61 | + command_file = normalize(command_file) |
| 62 | + |
| 63 | + try: |
| 64 | + with path.open("r", encoding="utf-8") as f: |
| 65 | + firstline = f.readline().strip() |
| 66 | + except Exception as exc: |
| 67 | + return f"Error reading {path}: {exc}" |
| 68 | + |
| 69 | + if not firstline.startswith("#"): |
| 70 | + return f"Inconsistency found in file: {path} has no title" |
| 71 | + |
| 72 | + command_page = normalize(firstline[2:]) |
| 73 | + |
| 74 | + # Skip if either filename or title is in the ignore set |
| 75 | + if command_file in IGNORE_SET or command_page in IGNORE_SET: |
| 76 | + return None |
| 77 | + |
| 78 | + if command_file != command_page: |
| 79 | + return ( |
| 80 | + f"Inconsistency found in file: {path}: " |
| 81 | + f"{command_page} should be {command_file}" |
| 82 | + ) |
| 83 | + return None |
| 84 | + |
| 85 | + |
| 86 | +def main() -> int: |
| 87 | + """Run the filename consistency check.""" |
| 88 | + base_dirs = [p for p in Path(".").glob("pages*") if p.is_dir()] |
| 89 | + files = [f for base in base_dirs for f in base.rglob("*.md")] |
| 90 | + |
| 91 | + # Ensure OUTPUT_FILE is always empty at the start |
| 92 | + OUTPUT_FILE.write_text("", encoding="utf-8") |
| 93 | + |
| 94 | + with OUTPUT_FILE.open("a", encoding="utf-8") as out: |
| 95 | + for path in files: |
| 96 | + result = check_file(path) |
| 97 | + if result: |
| 98 | + out.write(result + "\n") |
| 99 | + |
| 100 | + return 0 |
| 101 | + |
| 102 | + |
| 103 | +if __name__ == "__main__": |
| 104 | + sys.exit(main()) |
0 commit comments