"""Приведение XML-док к блочному виду (действующий код-стайл «Дейл»). Правило: открывающий и закрывающий — каждый на своей строке. Однострочная запись `/// текст` не допускается. Режимы: python scripts/fix_summary_blocks.py --check # только посчитать, что изменится python scripts/fix_summary_blocks.py --apply # применить """ import os import re import sys ROOT = r"C:\telbase\src" EXC = ("bin", "obj", "node_modules", ".git") LINE_RE = re.compile(r"^(\s*)(///)([ \t]?)(.*)$") def transform(body: str): m = LINE_RE.match(body) if not m: return [body] indent, _, _, content = m.groups() if "" not in content and "" not in content: return [body] base = indent + "///" def tag(text=""): return base + (" " + text if text else "") if "" in content and "" in content: pre, rest = content.split("", 1) between, post = rest.split("", 1) res = [] if pre.strip(): res.append(tag(pre.strip())) res.append(tag("")) if between.strip(): res.append(tag(between.strip())) res.append(tag("")) if post.strip(): res.append(tag(post.strip())) return res if "" in content: pre, after = content.split("", 1) res = [] if pre.strip(): res.append(tag(pre.strip())) res.append(tag("")) if after.strip(): res.append(tag(after.strip())) return res if "" in content: pre, after = content.split("", 1) res = [] if pre.strip(): res.append(tag(pre.strip())) res.append(tag("")) if after.strip(): res.append(tag(after.strip())) return res return [body] def iter_cs(): for dp, dn, fn in os.walk(ROOT): dn[:] = [d for d in dn if d not in EXC] for f in fn: if f.endswith(".cs"): yield os.path.join(dp, f) def process(path, apply): raw = open(path, "rb").read().decode("utf-8") lines = raw.splitlines(keepends=True) out = [] changed = False for raw_line in lines: if raw_line.endswith("\r\n"): body, ending = raw_line[:-2], "\r\n" elif raw_line.endswith("\n"): body, ending = raw_line[:-1], "\n" else: body, ending = raw_line, "" new_bodies = transform(body) if new_bodies != [body]: changed = True out.extend(b + ending for b in new_bodies) if changed and apply: open(path, "w", encoding="utf-8", newline="").write("".join(out)) return changed def main(): apply = "--apply" in sys.argv total = 0 files = 0 for path in iter_cs(): if process(path, apply): files += 1 print(("APPLIED" if apply else "DRY-RUN"), "files changed:", files) if __name__ == "__main__": main()