diff --git a/mdformat_tables/__init__.py b/mdformat_tables/__init__.py index 63f4688..c0482e3 100644 --- a/mdformat_tables/__init__.py +++ b/mdformat_tables/__init__.py @@ -5,6 +5,6 @@ from .plugin import ( # noqa: F401 POSTPROCESSORS, RENDERERS, - add_cli_options, + add_cli_argument_group, update_mdit, ) diff --git a/mdformat_tables/plugin.py b/mdformat_tables/plugin.py index e694775..c8ab115 100644 --- a/mdformat_tables/plugin.py +++ b/mdformat_tables/plugin.py @@ -7,9 +7,10 @@ from wcwidth import wcswidth -def add_cli_options(parser: argparse.ArgumentParser) -> None: - """Add options to the mdformat CLI, to be stored in `mdit.options["mdformat"]`.""" - parser.add_argument( +def add_cli_argument_group(group: argparse._ArgumentGroup) -> None: + """Add options to the mdformat CLI, to be stored in + `mdit.options["mdformat"]["plugin"]["tables"]`.""" + group.add_argument( "--compact-tables", action="store_true", help="If specified, do not add padding to table cells.", @@ -70,7 +71,14 @@ def format_delimiter_cell(index: int, align: str) -> str: def _render_table(node: RenderTreeNode, context: RenderContext) -> str: """Render a `RenderTreeNode` of type "table".""" - compact_tables = context.options["mdformat"].get("compact_tables", False) + compact_tables_from_cli_or_toml = ( + context.options["mdformat"] + .get("plugin", {}) + .get("tables", {}) + .get("compact_tables") + ) + compact_tables_from_api = context.options["mdformat"].get("compact_tables") + compact_tables = compact_tables_from_cli_or_toml or compact_tables_from_api # gather rendered cell content into row * column array rows: List[List[str]] = [] align: List[List[str]] = [] diff --git a/pyproject.toml b/pyproject.toml index d4d2759..916e866 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -17,7 +17,7 @@ classifiers = [ keywords = "mdformat,markdown,markdown-it" requires-python=">=3.9" -requires=["mdformat>=0.7.5,<0.8.0", "wcwidth>=0.2.13"] +requires=["mdformat>=0.7.19,<0.8.0", "wcwidth>=0.2.13"] [tool.flit.metadata.requires-extra] test = [ diff --git a/tests/test_parse.py b/tests/test_parse.py index e379ec5..25fd835 100644 --- a/tests/test_parse.py +++ b/tests/test_parse.py @@ -2,6 +2,7 @@ from markdown_it.utils import read_fixture_file import mdformat +import mdformat._cli import pytest FIXTURE_PATH = Path(__file__).parent / "fixtures.md" @@ -11,12 +12,23 @@ @pytest.mark.parametrize( "line,title,text,expected", fixtures, ids=[f[1] for f in fixtures] ) -def test_fixtures(line, title, text, expected): +def test_fixtures__api(line, title, text, expected): output = mdformat.text(text, extensions={"tables"}) print(output) assert output.rstrip() == expected.rstrip(), output +@pytest.mark.parametrize( + "line,title,text,expected", fixtures, ids=[f[1] for f in fixtures] +) +def test_fixtures__cli(line, title, text, expected, tmp_path): + file_path = tmp_path / "test_markdown.md" + file_path.write_text(text, encoding="utf-8") + assert mdformat._cli.run([str(file_path)]) == 0 + md_new = file_path.read_text(encoding="utf-8") + assert md_new == expected + + FIXTURES_COMPACT_PATH = Path(__file__).parent / "fixtures-compact.md" fixtures_compact = read_fixture_file(FIXTURES_COMPACT_PATH) @@ -24,9 +36,20 @@ def test_fixtures(line, title, text, expected): @pytest.mark.parametrize( "line,title,text,expected", fixtures_compact, ids=[f[1] for f in fixtures_compact] ) -def test_fixtures_compact(line, title, text, expected): +def test_fixtures_compact__api(line, title, text, expected): output = mdformat.text( text, extensions={"tables"}, options={"compact_tables": True} ) print(output) assert output.rstrip() == expected.rstrip(), output + + +@pytest.mark.parametrize( + "line,title,text,expected", fixtures_compact, ids=[f[1] for f in fixtures_compact] +) +def test_fixtures_compact__cli(line, title, text, expected, tmp_path): + file_path = tmp_path / "test_markdown.md" + file_path.write_text(text, encoding="utf-8") + assert mdformat._cli.run([str(file_path), "--compact-tables"]) == 0 + md_new = file_path.read_text(encoding="utf-8") + assert md_new == expected