Skip to content

Monkey patch to replace read_toml_opts entirely. Resolves #4 #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: 3.11
python-version: 3.13
- name: Installation (deps and package)
run: pip install .
- uses: pre-commit/[email protected]
Expand Down Expand Up @@ -49,7 +49,7 @@ jobs:
pytest --cov=mdformat_pyproject --cov-report=xml --cov-report=term-missing

- name: Store PR number and commit SHA
if: matrix.os == 'ubuntu-latest' && matrix.python-version == 3.11
if: matrix.os == 'ubuntu-latest' && matrix.python-version == 3.13
run: |
echo "Storing PR number ${{ github.event.number }}"
echo "${{ github.event.number }}" > pr_number.txt
Expand All @@ -63,14 +63,14 @@ jobs:
# Triggered sub-workflow is not able to detect the original commit/PR which is available
# in this workflow.
- name: Store PR number
if: matrix.os == 'ubuntu-latest' && matrix.python-version == 3.11
if: matrix.os == 'ubuntu-latest' && matrix.python-version == 3.13
uses: actions/upload-artifact@v4
with:
name: pr_number
path: pr_number.txt

- name: Store commit SHA
if: matrix.os == 'ubuntu-latest' && matrix.python-version == 3.11
if: matrix.os == 'ubuntu-latest' && matrix.python-version == 3.13
uses: actions/upload-artifact@v4
with:
name: commit_sha
Expand All @@ -80,7 +80,7 @@ jobs:
# is executed by a different workflow `coverage-report.yml`. The reason for this
# split is because `on.pull_request` workflows don't have access to secrets.
- name: Store coverage report in artifacts
if: matrix.os == 'ubuntu-latest' && matrix.python-version == 3.11
if: matrix.os == 'ubuntu-latest' && matrix.python-version == 3.13
uses: actions/upload-artifact@v4
with:
name: codecov_report
Expand All @@ -89,7 +89,7 @@ jobs:
- run: |
echo "The coverage report was stored in Github artifacts."
echo "It will be uploaded to Codecov using [codecov.yml] workflow shortly."
if: matrix.os == 'ubuntu-latest' && matrix.python-version == 3.11
if: matrix.os == 'ubuntu-latest' && matrix.python-version == 3.13

pre-commit-hook:
runs-on: ubuntu-latest
Expand All @@ -99,7 +99,7 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: 3.11
python-version: 3.13

- name: Installation (deps and package)
run: |
Expand All @@ -118,10 +118,10 @@ jobs:
steps:
- name: Checkout source
uses: actions/checkout@v3
- name: Set up Python 3.11
- name: Set up Python 3.13
uses: actions/setup-python@v4
with:
python-version: 3.11
python-version: 3.13
- name: install flit
run: |
pip install flit~=3.0
Expand Down
65 changes: 18 additions & 47 deletions mdformat_pyproject/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pathlib
import sys
from typing import TYPE_CHECKING, MutableMapping, Optional, Sequence, Union
from typing import TYPE_CHECKING, MutableMapping, Optional, Sequence, Tuple, Union

import markdown_it
import mdformat
Expand All @@ -27,19 +27,14 @@


@cache
def _find_pyproject_toml_path(search_path: str) -> Optional[pathlib.Path]:
"""Find the pyproject.toml file that corresponds to the search path.
def _find_pyproject_toml_path(search_path: pathlib.Path) -> Optional[pathlib.Path]:
"""Find the pyproject.toml file that applies to the search path.

The search is done ascending through the folders tree until a pyproject.toml
file is found in the same folder. If the root '/' is reached, None is returned.

The special path "-" used for stdin inputs is replaced with the current working
directory.
"""
if search_path == "-":
search_path = pathlib.Path.cwd()
else:
search_path = pathlib.Path(search_path).resolve()
if search_path.is_file():
search_path = search_path.parent

for parent in (search_path, *search_path.parents):
candidate = parent / "pyproject.toml"
Expand Down Expand Up @@ -68,50 +63,26 @@ def _parse_pyproject(pyproject_path: pathlib.Path) -> Optional[_ConfigOptions]:


@cache
def _reload_cli_opts() -> _ConfigOptions:
"""Re-parse the sys.argv array to deduce which arguments were used in the CLI.

If unknown arguments are found, we deduce that mdformat is being used as a
python library and therefore no mdformat command line arguments were passed.
def read_toml_opts(conf_dir: pathlib.Path) -> Tuple[MutableMapping, Optional[pathlib.Path]]:
"""Alternative read_toml_opts that reads from pyproject.toml instead of .mdformat.toml.

Notice that the strategy above does not fully close the door to situations
with colliding arguments with different meanings, but the rarity of the
situation and the complexity of a possible solution makes the risk worth taking.
Notice that if `.mdformat.toml` exists it is ignored.
"""
import mdformat._cli

if hasattr(mdformat.plugins, "_PARSER_EXTENSION_DISTS"):
# New API, mdformat>=0.7.19
arg_parser = mdformat._cli.make_arg_parser(
mdformat.plugins._PARSER_EXTENSION_DISTS,
mdformat.plugins._CODEFORMATTER_DISTS,
mdformat.plugins.PARSER_EXTENSIONS,
)
pyproject_path = _find_pyproject_toml_path(conf_dir)
if pyproject_path:
pyproject_opts = _parse_pyproject(pyproject_path)
else:
# Backwards compatibility, mdformat<0.7.19
arg_parser = mdformat._cli.make_arg_parser(
mdformat.plugins.PARSER_EXTENSIONS,
mdformat.plugins.CODEFORMATTERS,
)
pyproject_opts = {}

args, unknown = arg_parser.parse_known_args(sys.argv[1:])
if unknown:
return {}

return {key: value for key, value in vars(args).items() if value is not None}
return pyproject_opts, pyproject_path


def update_mdit(mdit: markdown_it.MarkdownIt) -> None:
"""Read the pyproject.toml file and re-create the mdformat options."""
mdformat_options: _ConfigOptions = mdit.options["mdformat"]
file_path = mdformat_options.get("filename", "-")
pyproject_path = _find_pyproject_toml_path(file_path)
if pyproject_path:
pyproject_opts = _parse_pyproject(pyproject_path)
if pyproject_opts is not None:
cli_opts = _reload_cli_opts()
mdformat_options.update(**pyproject_opts)
mdformat_options.update(**cli_opts)
"""No-op, since this plugin only monkey patches and does not modify mdit."""
pass


RENDERERS: MutableMapping[str, "Render"] = {}

# Monkey patch mdformat._conf to use our own read_toml_opts version
mdformat._conf.read_toml_opts = read_toml_opts
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ profile = "black"
[tool.mdformat]
wrap = 99
number = true
exclude = [".tox/**", ".venv/**"]

[tool.coverage.report]
exclude_lines = [
Expand Down
Loading