File: pymarkdown.py

package info (click to toggle)
ansible-core 2.19.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 32,840 kB
  • sloc: python: 181,406; cs: 4,929; sh: 4,630; xml: 34; makefile: 21
file content (67 lines) | stat: -rw-r--r-- 1,585 bytes parent folder | download | duplicates (2)
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""Sanity test for Markdown files."""

from __future__ import annotations

import pathlib
import re
import subprocess
import sys

import typing as t


def main() -> None:
    paths = sys.argv[1:] or sys.stdin.read().splitlines()

    cmd = [
        sys.executable,
        '-m',
        'pymarkdown',
        '--config',
        pathlib.Path(__file__).parent / 'pymarkdown.config.json',
        '--strict-config',
        'scan',
    ] + paths

    process = subprocess.run(
        cmd,
        stdin=subprocess.DEVNULL,
        capture_output=True,
        check=False,
        text=True,
    )

    if process.stderr:
        print(process.stderr.strip(), file=sys.stderr)
        sys.exit(1)

    if not (stdout := process.stdout.strip()):
        return

    pattern = re.compile(r'^(?P<path_line_column>[^:]*:[0-9]+:[0-9]+): (?P<code>[^:]*): (?P<message>.*) \((?P<aliases>.*)\)$')
    matches = parse_to_list_of_dict(pattern, stdout)
    results = [f"{match['path_line_column']}: {match['aliases'].split(', ')[0]}: {match['message']}" for match in matches]

    print('\n'.join(results))


def parse_to_list_of_dict(pattern: re.Pattern, value: str) -> list[dict[str, t.Any]]:
    matched = []
    unmatched = []

    for line in value.splitlines():
        match = re.search(pattern, line)

        if match:
            matched.append(match.groupdict())
        else:
            unmatched.append(line)

    if unmatched:
        raise Exception(f'Pattern {pattern!r} did not match values:\n' + '\n'.join(unmatched))

    return matched


if __name__ == '__main__':
    main()