Debian Codegraph @

utils/lint_prose.py

raw symbols SCIP
1 import re
2 from pathlib import Path
3
4 import proselint
5 from proselint.config import default as DEFAULT_CONFIG
6
7 from pytermgui import break_line, terminal, tim
8
9 RE_ADMONITION_HEADER = re.compile(r'([\!\?]{3}) \w+(?: ".*?")?')
10 RE_QUOTE = re.compile(r'"(.*?)"')
11 RE_CODEBLOCK = re.compile(r"(```.*?\n[\s\S]*?\n?```)")
12
13 docs = Path("docs")
14
15 ignored = [
16 "leonard.exclamation.30ppm",
17 "leonard.exclamation.multiple",
18 "cliches.write_good",
19 ]
20
21 for path in docs.glob("**/*.md"):
22 with open(path, "r") as f:
23 content = f.read()
24
25 # Remove non-regex compatible escapes (mostly ANSI SGR)
26 content = content.replace(r"\x", "").replace("...", "…")
27 # Remove admonition headers
28 content = RE_ADMONITION_HEADER.sub("---------------", content)
29 # Remove codeblocks completely
30 content = RE_CODEBLOCK.sub(lambda m: m[1].count("\n") * "-+-+-+-+-\n", content)
31 # Change quote characters
32 content = RE_QUOTE.sub(lambda m: f"“{m[1]}”", content)
33
34 result = proselint.tools.lint(content, config=DEFAULT_CONFIG)
35 result = list(filter(lambda item: item[0] not in ignored, result))
36
37 if result != []:
38 tim.print(f"[primary+2]{path}:")
39
40 for check, message, row, col, *_ in result:
41 row += 1
42
43 tim.print(
44 f" [~file://{path.absolute()}]{path.name}[/]:"
45 + f"{row}:{col} [warning]{check}[/]:"
46 )
47 for line in break_line(message, terminal.width * 2 / 3):
48 tim.print(" " + f"[surface+2]{line}")
49
50 print()