LilSpazJoekp / docstrfmt

A formatter for reStructuredText
MIT License
23 stars 15 forks source link

Retain literal code block with args #80

Closed jonathf closed 3 months ago

jonathf commented 3 months ago

Solves #79

Assuming literal block has classes attributes (more than the first indicating that it is a code block), format it as .. code:: {args} instead of ::.

LilSpazJoekp commented 3 months ago

Will need some rst code added to the test files for code coverage.

jonathf commented 3 months ago

I made a stab at making a test, but am a bit confused to how it all works. Do you mind taking a look at what I wrote?

Through the CLI I get the response I want where they all identifies as literal_block, but using the same input in the tests .. code:: and .. code-block:: identifies as directive.

Is there something going on in the test harness that is different than in the CLI?

LilSpazJoekp commented 3 months ago

You just need to add some text that would be formatted by the code you added. Added a new test in Python isn't needed.

jonathf commented 3 months ago

You mean like in tests/test_files/test_file.rst? It already has .. code-block:: python directive.

But looking into it close it seems that your code handles :: and .. code-block:: correctly, but struggles with the .. block:: variant. If I add that to test_file.rst it will fail as it is converted to :: or .. code-block:: depending on if it is your version of the code or mine.

Realizing this, I can fix my problem with just renaming all my code to code-block. But for the sake of this PR, I am unsure. What would we say is correct behavior?

LilSpazJoekp commented 3 months ago

It should be converting it to .. code-block:: language. We could add a test similar to the failure cases and just verify that it gets changed.

LilSpazJoekp commented 3 months ago

Is this something that we still want merged in?

jonathf commented 3 months ago

I think so, but I have kind of given up on getting tests to work. I am running a simple test:

def test_code_directive(runner):
    file = ".. code:: python\n\n    import os\n"
    args = ["-r", file]
    result = runner.invoke(main, args=args)
    assert result.output == file
    assert result.exit_code == 0

This does not invokeliteral_block(), but running the CLI on the same text does. Diagnosing why is too much work for me right now.

LilSpazJoekp commented 3 months ago

I'll add a test for it.

LilSpazJoekp commented 3 months ago

Got the test sorted out for you. Basically, just needed to specify the expected result of the block (code-block).

LilSpazJoekp commented 3 months ago

I'm going to go ahead and merge this to get a release out. If you have any issues with it let me know and I'll address them.