Search code examples
pythonconverterspython-sphinxdocstringepydoc

Automated way to switch from epydoc's docstring formatting to sphinx docstring formatting?


I've got a project which I documented using epydoc. Now I'm trying to switch to sphinx. I formatted all my docstrings for epydocs, using B{}, L{} etc for bolding, linking and the like, and using @param, @return, @raise etc to explain input, output, exceptions and the likes.

So now that I'm switching to sphinx it loses all these features. Is there an automated way to convert docstrings formatted for epydocs to docstrings formatted for sphinx?


Solution

  • To expand on Kevin Horn's answer, docstrings can be translated on the fly in an event handler triggered by the autodoc-process-docstring event.

    Below is a small demonstration (try it by adding the code to conf.py). It replaces the @ character in some common Epytext fields with :, which is used in the corresponding Sphinx fields.

    import re
    
    re_field = re.compile('@(param|type|rtype|return)') 
    
    def fix_docstring(app, what, name, obj, options, lines):
        for i in xrange(len(lines)):
            lines[i] = re_field.sub(r':\1', lines[i])
    
    def setup(app):
        app.connect('autodoc-process-docstring', fix_docstring)