()
| 238 | |
| 239 | |
| 240 | def main() -> int: |
| 241 | ap = argparse.ArgumentParser() |
| 242 | ap.add_argument("--blog-dir", default="docs/website/content/blog") |
| 243 | ap.add_argument("--max-files", type=int, default=0) |
| 244 | ap.add_argument("--start-index", type=int, default=0, help="Start file index for batch processing") |
| 245 | ap.add_argument("--apply", action="store_true", help="Apply backtick fixes for reported tokens") |
| 246 | args = ap.parse_args() |
| 247 | |
| 248 | blog_dir = Path(args.blog_dir) |
| 249 | files = sorted(blog_dir.rglob("*.md")) |
| 250 | if args.start_index > 0: |
| 251 | files = files[args.start_index :] |
| 252 | if args.max_files > 0: |
| 253 | files = files[: args.max_files] |
| 254 | |
| 255 | findings = 0 |
| 256 | files_with_findings = 0 |
| 257 | for path in files: |
| 258 | rows = list(scan_file(path)) |
| 259 | if not rows: |
| 260 | continue |
| 261 | files_with_findings += 1 |
| 262 | print(f"\n## {path}") |
| 263 | if args.apply: |
| 264 | original = path.read_text(encoding="utf-8").splitlines() |
| 265 | updated = original[:] |
| 266 | for line_no, line, found in rows: |
| 267 | # Wrap from right-to-left to preserve offsets. |
| 268 | safe = [(k, tok, s, e) for k, tok, s, e in found if k in PATTERN_NAMES] |
| 269 | repls = sorted(((s, e, tok) for _, tok, s, e in safe), key=lambda x: x[0], reverse=True) |
| 270 | new_line = updated[line_no - 1] |
| 271 | for s, e, tok in repls: |
| 272 | if s > 0 and new_line[s - 1] == "`": |
| 273 | continue |
| 274 | if e < len(new_line) and new_line[e] == "`": |
| 275 | continue |
| 276 | new_line = new_line[:s] + f"`{tok}`" + new_line[e:] |
| 277 | updated[line_no - 1] = new_line |
| 278 | path.write_text("\n".join(updated) + "\n", encoding="utf-8") |
| 279 | |
| 280 | for line_no, line, found in rows: |
| 281 | findings += len(found) |
| 282 | kinds = ", ".join(f"{k}:{t}" for k, t, _, _ in found) |
| 283 | print(f"{line_no}: {kinds}") |
| 284 | print(f" {line}") |
| 285 | |
| 286 | mode = "Applied" if args.apply else "Reported" |
| 287 | print(f"\n{mode} scan complete. Scanned {len(files)} files. Files with findings: {files_with_findings}. Tokens flagged: {findings}.") |
| 288 | return 0 |
| 289 | |
| 290 | |
| 291 | if __name__ == "__main__": |
no test coverage detected