File: lint_prose.py

package info (click to toggle)
pytermgui 7.7.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 19,888 kB
  • sloc: python: 12,931; makefile: 40; sh: 37
file content (50 lines) | stat: -rw-r--r-- 1,549 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import re
from pathlib import Path

import proselint
from proselint.config import default as DEFAULT_CONFIG

from pytermgui import break_line, terminal, tim

RE_ADMONITION_HEADER = re.compile(r'([\!\?]{3}) \w+(?: ".*?")?')
RE_QUOTE = re.compile(r'"(.*?)"')
RE_CODEBLOCK = re.compile(r"(```.*?\n[\s\S]*?\n?```)")

docs = Path("docs")

ignored = [
    "leonard.exclamation.30ppm",
    "leonard.exclamation.multiple",
    "cliches.write_good",
]

for path in docs.glob("**/*.md"):
    with open(path, "r") as f:
        content = f.read()

    # Remove non-regex compatible escapes (mostly ANSI SGR)
    content = content.replace(r"\x", "").replace("...", "…")
    # Remove admonition headers
    content = RE_ADMONITION_HEADER.sub("---------------", content)
    # Remove codeblocks completely
    content = RE_CODEBLOCK.sub(lambda m: m[1].count("\n") * "-+-+-+-+-\n", content)
    # Change quote characters
    content = RE_QUOTE.sub(lambda m: f"“{m[1]}”", content)

    result = proselint.tools.lint(content, config=DEFAULT_CONFIG)
    result = list(filter(lambda item: item[0] not in ignored, result))

    if result != []:
        tim.print(f"[primary+2]{path}:")

        for check, message, row, col, *_ in result:
            row += 1

            tim.print(
                f"  [~file://{path.absolute()}]{path.name}[/]:"
                + f"{row}:{col} [warning]{check}[/]:"
            )
            for line in break_line(message, terminal.width * 2 / 3):
                tim.print("    " + f"[surface+2]{line}")

            print()