58 lines
1.8 KiB
Python
58 lines
1.8 KiB
Python
|
|
#!/usr/bin/env python3
|
||
|
|
"""Add **Status:** Active Documentation to docs that have **Last Updated:** but no **Status:** in first 20 lines."""
|
||
|
|
import re
|
||
|
|
import sys
|
||
|
|
|
||
|
|
STATUS_LINE = "**Status:** Active Documentation\n"
|
||
|
|
|
||
|
|
|
||
|
|
def add_status(path: str) -> bool:
|
||
|
|
try:
|
||
|
|
with open(path, "r", encoding="utf-8", errors="replace") as f:
|
||
|
|
lines = f.readlines()
|
||
|
|
except Exception as e:
|
||
|
|
print(f"Read error {path}: {e}", file=sys.stderr)
|
||
|
|
return False
|
||
|
|
if not lines:
|
||
|
|
return False
|
||
|
|
first_20 = "".join(lines[:20])
|
||
|
|
if "**Status:**" in first_20 or "Status:" in first_20:
|
||
|
|
return False
|
||
|
|
if "**Last Updated:**" not in first_20:
|
||
|
|
return False
|
||
|
|
# Find the line index of **Last Updated:** and insert Status after it (or after Document Version if present)
|
||
|
|
insert_after = None
|
||
|
|
for i, line in enumerate(lines[:20]):
|
||
|
|
if "**Last Updated:**" in line:
|
||
|
|
insert_after = i
|
||
|
|
break
|
||
|
|
if insert_after is None:
|
||
|
|
return False
|
||
|
|
# If next line is **Document Version:**, insert after that
|
||
|
|
if insert_after + 1 < len(lines) and "**Document Version:**" in lines[insert_after + 1]:
|
||
|
|
insert_after += 1
|
||
|
|
# Insert Status line after insert_after
|
||
|
|
new_lines = lines[: insert_after + 1] + [STATUS_LINE] + lines[insert_after + 1 :]
|
||
|
|
try:
|
||
|
|
with open(path, "w", encoding="utf-8", newline="\n") as f:
|
||
|
|
f.writelines(new_lines)
|
||
|
|
except Exception as e:
|
||
|
|
print(f"Write error {path}: {e}", file=sys.stderr)
|
||
|
|
return False
|
||
|
|
return True
|
||
|
|
|
||
|
|
|
||
|
|
def main():
|
||
|
|
paths = [p.strip() for p in sys.stdin if p.strip()]
|
||
|
|
count = 0
|
||
|
|
for path in paths:
|
||
|
|
if add_status(path):
|
||
|
|
count += 1
|
||
|
|
print(path)
|
||
|
|
print(f"Added Status to {count} files.", file=sys.stderr)
|
||
|
|
return 0
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
sys.exit(main())
|