Skip to content
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

Hook to replace rst parsing in autodoc #8018

Closed
sbliven opened this issue Jul 28, 2020 · 4 comments
Closed

Hook to replace rst parsing in autodoc #8018

sbliven opened this issue Jul 28, 2020 · 4 comments
Labels
extensions:autodoc type:enhancement enhance or introduce a new feature

Comments

@sbliven
Copy link

sbliven commented Jul 28, 2020

Is your feature request related to a problem? Please describe.

I would like to use an alternate markup syntax for my docstrings (specifically MyST). Sphinx plugins that provide alternate syntaxes for documentation files (e.g recommonmark, MyST-parser, etc) seem to produce docutil AST objects, but the autodoc modules store and manipulate rST strings instead.

Describe the solution you'd like

It would be great if there was an interface for replacing rST parsing in autodoc via plugins, analogous to how plugins can provide parsers for specific file extensions.

Describe alternatives you've considered

The closest I could find was the autodoc-process-docstring hook, which does allow rewriting the docstrings prior to parsing. However, it expects rST output and most parsers output AST. I suppose it would be possible to create a AST to rST writer, but this seems backwards since the rST will eventually have to be parsed back to AST.

Additional context

@sbliven sbliven added the type:enhancement enhance or introduce a new feature label Jul 28, 2020
@tk0miya
Copy link
Member

tk0miya commented Jul 29, 2020

It would be great if there was an interface for replacing rST parsing in autodoc via plugins, analogous to how plugins can provide parsers for specific file extensions.

Basically, autodoc does not parse docstrings. It only generates an internal reST document and passes it to upstream's parser.

def foo(x, y):
    """blah blah blah"""

For example, autodoc generates the following internal reST document from above code:

.. py:function:: foo(x, y)

   blah blah blah

I agree that autodoc is strongly coupled with reST. So it would be nice if we can use it from other parsers. But is hard to inject a parser because it does not parse docstring at all.

@sbliven
Copy link
Author

sbliven commented Jul 29, 2020

Thanks, @tk0miya. So in principle could I overwrite the various Documenter classes to emit directives in my syntax of choice, and then switch the parser upstream?

@tk0miya
Copy link
Member

tk0miya commented Jul 29, 2020

Everything is okay if you'll implement it. What you said is rewriting almost of autodoc extension. It's not an easy way. But autodoc is mere python code, not magic. So you can do it.

FYI: Here is a place where autodoc calls upstream parser after generation of internal reST document. You also need to hack here to allow switching parsers.

result = parse_generated_content(self.state, params.result, documenter)

@tk0miya
Copy link
Member

tk0miya commented Dec 25, 2020

Now I'm closing this because it is hard to realize this request (autodoc does not parse at all!).

@tk0miya tk0miya closed this as completed Dec 25, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 16, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
extensions:autodoc type:enhancement enhance or introduce a new feature
Projects
None yet
Development

No branches or pull requests

2 participants