NiklasRosenstein / pydoc-markdown

Create Python API documentation in Markdown format.
http://niklasrosenstein.github.io/pydoc-markdown/
Other
453 stars 102 forks source link

Space-indented code block loses indentation #259

Open langfingaz opened 2 years ago

langfingaz commented 2 years ago

Environment

I am using the cli interface:

pydoc-markdown -I src -m "${module}" --render-toc > "${dst}".md

Describe the bug

The first line of a code block (that is not surrounded by 3-ticks, but instead indented with 4 spaces) looses its indentation.

Here is a docstring of a python function:

Example:

    >>> url = URL('https://foo.bar')
    >>> print(url)
    https://foo.bar

:param url: Link to a remote file.

And here the generated markdown:

Example:

>>> url = URL('https://foo.bar')
    >>> print(url)
    https://foo.bar

**Arguments**:

- `url`: Link to a remote file.

Expected behavior

All 3 lines of the codeblock should keep their indendations.

Example:

    >>> url = URL('https://foo.bar')
    >>> print(url)
    https://foo.bar

**Arguments**:

- `url`: Link to a remote file.

Note:

The above bug does also occur when one does not use the > sign. So this is not the problem here ;)

NiklasRosenstein commented 2 years ago

Thanks for reporting this issue @langfingaz ! I've experienced this as well but have not found the time to fix it yet :( I might get around to fix it sometime later this week. If you dare to take a look at it yourself, you want to look at the code for the preprocessors (I'm not sure yet if this issue affects all preprocessors).

NiklasRosenstein commented 1 year ago

Hello @langfingaz ,

I did some investigation and it turns out this issue is coming from the docstring_parser module.

https://github.com/NiklasRosenstein/pydoc-markdown/blob/c36422142227072e02af3eb94a41fa8a621b3695/src/pydoc_markdown/contrib/processors/sphinx.py#L151

https://github.com/NiklasRosenstein/pydoc-markdown/blob/c36422142227072e02af3eb94a41fa8a621b3695/src/pydoc_markdown/contrib/processors/sphinx.py#L158-L163

The short_description is Example: and the long_description contains your code block, though unfortunately with the leading whitespace removed.

FranzAtGithub commented 1 year ago

Hey there :)

is this the same problem I get for the following docstring?

def foo(arg1: int, **kwargs: dict):
    """
    Args:
        arg1: The first argument.

            A longer description of the argument.
            There is even a table.

            | value  | description       |
            |--------|-------------------|
            | 1-10   | some text         |
            | 10-100 | some other text   |

        kwargs: Additional options.

            Longer description of possible kwargs.
            Recogniced keywords include:

            * kw_arg1 (bool): description of first keyword

                Longer description the keyword.

            *  kw_arg2 (int): description of the second keyword

                Longer description of the keyword.
                There is even a table:

                | value | description    |
                |-------|----------------|
                | 0     | no output      |
                | 1     | some output    |
                | 2     | lots of output |

            * kw_arg3 (bool): description of third keyword

                Longer description of the keyword.
                This includes a list:

                - nested list item
                - other nested list item
    """

I get the following md-code


**Arguments**:

- `arg1` - The first argument.

  A longer description of the argument.
  There is even a table.

  | value  | description       |
  |--------|-------------------|
  | 1-10   | some text         |
  | 10-100 | some other text   |

- `kwargs` - Additional options.

  Longer description of possible kwargs.
  Recogniced keywords include:

  * kw_arg1 (bool): description of first keyword

  Longer description the keyword.

  *  kw_arg2 (int): description of the second keyword

  Longer description of the keyword.
  There is even a table:

  | value | description    |
  |-------|----------------|
  | 0     | no output      |
  | 1     | some output    |
  | 2     | lots of output |

  * kw_arg3 (bool): description of third keyword

  Longer description of the keyword.
  This includes a list:

  - nested list item
  - other nested list item

The nested structures are not correctly indented. I would appreciate a fix for this.