import re
import sys
from pathlib import Path
def replace_code_blocks(readme_path="README.md"):
readme = Path(readme_path)
content = readme.read_text(encoding="utf-8")
pattern = re.compile(r"```(\w+) <!--([^\n\s]+)(\s+\d+)?\s*-->\n(.*?)```", re.DOTALL)
new_content = pattern.sub(replacer, content)
readme.write_text(new_content, encoding="utf-8")
print("[OK] README.md updated.")
def replacer(match):
lang, filepath, line_count, _ = match.groups()
path = Path(filepath.strip())
if not path.exists():
print(f"[WARN] File not found: {filepath}")
return match.group(0)
code = path.read_text(encoding="utf-8")
if line_count and line_count.strip():
code=clamp_text_lines(code, int(line_count.strip()))
return f"```{lang} <!--{filepath} {(line_count or "").strip()}-->\n{code}\n```"
def clamp_text_lines(text: str, linecount: int, *, sep_width: int = 40) -> str:
if linecount <= 0:
return ""
lines = text.splitlines()
n = len(lines)
if n <= linecount:
return text
keep_each = max((linecount - 1) // 2, 0) head = lines[:keep_each]
tail = lines[-keep_each:] if keep_each > 0 else []
omitted = n - (len(head) + len(tail))
bar = "─" * max(sep_width, 12)
sep = f"{bar} {omitted} lines omitted {bar}"
out: list[str] = []
out.extend(head)
out.append(sep)
out.extend(tail)
return "\n".join(out)
if __name__ == "__main__":
replace_code_blocks(sys.argv[1])