|
2 | 2 |
|
3 | 3 | import pathlib |
4 | 4 | import sys |
5 | | -from typing import TYPE_CHECKING, MutableMapping, Optional, Sequence, Union |
| 5 | +from typing import TYPE_CHECKING, MutableMapping, Optional, Sequence, Tuple, Union |
6 | 6 |
|
7 | 7 | import markdown_it |
8 | 8 | import mdformat |
|
27 | 27 |
|
28 | 28 |
|
29 | 29 | @cache |
30 | | -def _find_pyproject_toml_path(search_path: str) -> Optional[pathlib.Path]: |
31 | | - """Find the pyproject.toml file that corresponds to the search path. |
| 30 | +def _find_pyproject_toml_path(search_path: pathlib.Path) -> Optional[pathlib.Path]: |
| 31 | + """Find the pyproject.toml file that applies to the search path. |
32 | 32 |
|
33 | 33 | The search is done ascending through the folders tree until a pyproject.toml |
34 | 34 | file is found in the same folder. If the root '/' is reached, None is returned. |
35 | | -
|
36 | | - The special path "-" used for stdin inputs is replaced with the current working |
37 | | - directory. |
38 | 35 | """ |
39 | | - if search_path == "-": |
40 | | - search_path = pathlib.Path.cwd() |
41 | | - else: |
42 | | - search_path = pathlib.Path(search_path).resolve() |
| 36 | + if search_path.is_file(): |
| 37 | + search_path = search_path.parent |
43 | 38 |
|
44 | 39 | for parent in (search_path, *search_path.parents): |
45 | 40 | candidate = parent / "pyproject.toml" |
@@ -68,50 +63,26 @@ def _parse_pyproject(pyproject_path: pathlib.Path) -> Optional[_ConfigOptions]: |
68 | 63 |
|
69 | 64 |
|
70 | 65 | @cache |
71 | | -def _reload_cli_opts() -> _ConfigOptions: |
72 | | - """Re-parse the sys.argv array to deduce which arguments were used in the CLI. |
73 | | -
|
74 | | - If unknown arguments are found, we deduce that mdformat is being used as a |
75 | | - python library and therefore no mdformat command line arguments were passed. |
| 66 | +def read_toml_opts(conf_dir: pathlib.Path) -> Tuple[MutableMapping, Optional[pathlib.Path]]: |
| 67 | + """Alternative read_toml_opts that reads from pyproject.toml instead of .mdformat.toml. |
76 | 68 |
|
77 | | - Notice that the strategy above does not fully close the door to situations |
78 | | - with colliding arguments with different meanings, but the rarity of the |
79 | | - situation and the complexity of a possible solution makes the risk worth taking. |
| 69 | + Notice that if `.mdformat.toml` exists it is ignored. |
80 | 70 | """ |
81 | | - import mdformat._cli |
82 | | - |
83 | | - if hasattr(mdformat.plugins, "_PARSER_EXTENSION_DISTS"): |
84 | | - # New API, mdformat>=0.7.19 |
85 | | - arg_parser = mdformat._cli.make_arg_parser( |
86 | | - mdformat.plugins._PARSER_EXTENSION_DISTS, |
87 | | - mdformat.plugins._CODEFORMATTER_DISTS, |
88 | | - mdformat.plugins.PARSER_EXTENSIONS, |
89 | | - ) |
| 71 | + pyproject_path = _find_pyproject_toml_path(conf_dir) |
| 72 | + if pyproject_path: |
| 73 | + pyproject_opts = _parse_pyproject(pyproject_path) |
90 | 74 | else: |
91 | | - # Backwards compatibility, mdformat<0.7.19 |
92 | | - arg_parser = mdformat._cli.make_arg_parser( |
93 | | - mdformat.plugins.PARSER_EXTENSIONS, |
94 | | - mdformat.plugins.CODEFORMATTERS, |
95 | | - ) |
| 75 | + pyproject_opts = {} |
96 | 76 |
|
97 | | - args, unknown = arg_parser.parse_known_args(sys.argv[1:]) |
98 | | - if unknown: |
99 | | - return {} |
100 | | - |
101 | | - return {key: value for key, value in vars(args).items() if value is not None} |
| 77 | + return pyproject_opts, pyproject_path |
102 | 78 |
|
103 | 79 |
|
104 | 80 | def update_mdit(mdit: markdown_it.MarkdownIt) -> None: |
105 | | - """Read the pyproject.toml file and re-create the mdformat options.""" |
106 | | - mdformat_options: _ConfigOptions = mdit.options["mdformat"] |
107 | | - file_path = mdformat_options.get("filename", "-") |
108 | | - pyproject_path = _find_pyproject_toml_path(file_path) |
109 | | - if pyproject_path: |
110 | | - pyproject_opts = _parse_pyproject(pyproject_path) |
111 | | - if pyproject_opts is not None: |
112 | | - cli_opts = _reload_cli_opts() |
113 | | - mdformat_options.update(**pyproject_opts) |
114 | | - mdformat_options.update(**cli_opts) |
| 81 | + """No-op, since this plugin only monkey patches and does not modify mdit.""" |
| 82 | + pass |
115 | 83 |
|
116 | 84 |
|
117 | 85 | RENDERERS: MutableMapping[str, "Render"] = {} |
| 86 | + |
| 87 | +# Monkey patch mdformat._conf to use our own read_toml_opts version |
| 88 | +mdformat._conf.read_toml_opts = read_toml_opts |
0 commit comments