|
8 | 8 |
|
9 | 9 | import markdown_it |
10 | 10 | import mdformat |
| 11 | +from mdformat._cli import print_paragraphs |
11 | 12 |
|
12 | 13 | TYPE_CHECKING = False |
13 | 14 | if TYPE_CHECKING: |
|
23 | 24 | import tomli as tomllib |
24 | 25 |
|
25 | 26 |
|
26 | | -@cache |
27 | | -def _find_pyproject_toml_path(search_path: Path) -> Path | None: |
28 | | - """Find the pyproject.toml file that applies to the search path. |
29 | | -
|
30 | | - The search is done ascending through the folders tree until a pyproject.toml |
31 | | - file is found in the same folder. If the root '/' is reached, None is returned. |
32 | | - """ |
33 | | - if search_path.is_file(): |
34 | | - search_path = search_path.parent |
35 | | - |
36 | | - for parent in (search_path, *search_path.parents): |
37 | | - candidate = parent / "pyproject.toml" |
38 | | - if candidate.is_file(): |
39 | | - return candidate |
40 | | - |
41 | | - return None |
42 | | - |
43 | | - |
44 | 27 | @cache |
45 | 28 | def _parse_pyproject(pyproject_path: Path) -> _ConfigOptions | None: |
46 | | - """Extract and validate the mdformat options from the pyproject.toml file. |
| 29 | + """Extract and validate the mdformat options from the ``pyproject.toml`` file. |
| 30 | +
|
| 31 | + The options are searched inside a ``[tool.mdformat]`` section within the TOML file, |
| 32 | + and they are validated using the default functions from ``mdformat._conf``. |
47 | 33 |
|
48 | | - The options are searched inside a [tool.mdformat] key within the toml file, |
49 | | - and they are validated using the default functions from `mdformat._conf`. |
| 34 | + If no ``[tool.mdformat]`` section is found, ``None`` is returned. |
50 | 35 | """ |
51 | 36 | with pyproject_path.open(mode="rb") as pyproject_file: |
52 | | - content = tomllib.load(pyproject_file) |
| 37 | + try: |
| 38 | + content = tomllib.load(pyproject_file) |
| 39 | + except tomllib.TOMLDecodeError as e: |
| 40 | + raise mdformat._conf.InvalidConfError(f"Invalid TOML syntax: {e}") |
53 | 41 |
|
54 | 42 | options = content.get("tool", {}).get("mdformat") |
55 | | - if options is not None: |
56 | | - mdformat._conf._validate_keys(options, pyproject_path) |
57 | | - mdformat._conf._validate_values(options, pyproject_path) |
| 43 | + if options is None: |
| 44 | + return None |
| 45 | + |
| 46 | + mdformat._conf._validate_keys(options, pyproject_path) |
| 47 | + mdformat._conf._validate_values(options, pyproject_path) |
58 | 48 |
|
59 | 49 | return options |
60 | 50 |
|
61 | 51 |
|
| 52 | +_orig_read_toml_opts = mdformat._conf.read_toml_opts |
| 53 | + |
| 54 | + |
62 | 55 | @cache |
63 | | -def read_toml_opts(conf_dir: Path) -> tuple[dict, Path | None]: |
64 | | - """Alternative read_toml_opts that reads from pyproject.toml instead of .mdformat.toml. |
| 56 | +def patched_read_toml_opts(search_path: Path) -> tuple[dict, Path | None]: |
| 57 | + """Patched version of ``mdformat._conf.read_toml_opts``. |
| 58 | +
|
| 59 | + Search the first ``.mdformat.toml`` or ``pyproject.toml`` file in the provided |
| 60 | + ``search_path`` folder or its parents. |
| 61 | +
|
| 62 | + The search is done ascending through the folders tree until a ``.mdformat.toml`` or a |
| 63 | + ``pyproject.toml`` file is found. If the root ``/`` is reached, ``None`` is returned. |
65 | 64 |
|
66 | | - Notice that if `.mdformat.toml` exists it is ignored. |
| 65 | + ``.mdformat.toml`` takes precedence over ``pyproject.toml`` if both are present in the |
| 66 | + same folder. In that case, a warning is logged to inform the user that the |
| 67 | + ``pyproject.toml`` file has been ignored. |
| 68 | +
|
| 69 | + ``pyproject.toml`` files without a ``[tool.mdformat]`` section are ignored. |
| 70 | +
|
| 71 | + This behavior mimics the one from Ruff, as described in `issues#17 |
| 72 | + <https://github.com/csala/mdformat-pyproject/issues/17>`_. |
67 | 73 | """ |
68 | | - pyproject_path = _find_pyproject_toml_path(conf_dir) |
69 | | - if pyproject_path: |
70 | | - pyproject_opts = _parse_pyproject(pyproject_path) |
71 | | - else: |
72 | | - pyproject_opts = {} |
| 74 | + if search_path.is_file(): |
| 75 | + search_path = search_path.parent |
73 | 76 |
|
74 | | - return pyproject_opts, pyproject_path |
| 77 | + for parent in (search_path, *search_path.parents): |
| 78 | + # Try to find a pyproject.toml file first, with a [tool.mdformat] section. |
| 79 | + pyproject_options = None |
| 80 | + pyproject_path = parent / "pyproject.toml" |
| 81 | + if pyproject_path.is_file(): |
| 82 | + pyproject_options = _parse_pyproject(pyproject_path) |
| 83 | + |
| 84 | + # Read options from .mdformat.toml if present. |
| 85 | + mdformat_path = parent / ".mdformat.toml" |
| 86 | + if mdformat_path.is_file(): |
| 87 | + # If both pyproject.toml and .mdformat.toml are present, the latter takes |
| 88 | + # precedence, but we warn the user that pyproject.toml is being ignored. |
| 89 | + if pyproject_options is not None: |
| 90 | + print_paragraphs( |
| 91 | + ( |
| 92 | + f"Warning: ignoring mdformat options from {pyproject_path} " |
| 93 | + f"since {mdformat_path} is present.", |
| 94 | + ) |
| 95 | + ) |
| 96 | + |
| 97 | + # Return options from .mdformat.toml using the original function, even if |
| 98 | + # they are empty. |
| 99 | + return _orig_read_toml_opts(mdformat_path.parent) |
| 100 | + |
| 101 | + # Return options from pyproject.toml if .mdformat.toml is not present. |
| 102 | + elif pyproject_options is not None: |
| 103 | + return pyproject_options, pyproject_path |
| 104 | + |
| 105 | + # No config file found, return empty options. |
| 106 | + return {}, None |
75 | 107 |
|
76 | 108 |
|
77 | 109 | def update_mdit(mdit: markdown_it.MarkdownIt) -> None: |
78 | | - """No-op, since this plugin only monkey patches and does not modify mdit.""" |
| 110 | + """No-op, since this plugin only monkey patches and does not modify ``mdit``.""" |
79 | 111 | pass |
80 | 112 |
|
81 | 113 |
|
82 | 114 | RENDERERS: dict[str, Render] = {} |
83 | 115 |
|
84 | 116 | # Monkey patch mdformat._conf to use our own read_toml_opts version |
85 | | -mdformat._conf.read_toml_opts = read_toml_opts |
| 117 | +mdformat._cli.read_toml_opts = patched_read_toml_opts |
0 commit comments