20

I've recently tried using sphinx-apidoc from Sphinx to help generate Sphinx specific reStructuredText from the API of a Python project.

However, the result I'm getting is:

Default look of <code>sphinx-api</code> result

Anyone know if I can customize the template sphinx-api uses for its output? Specifically, I'd like to:

  • Get rid of all the "Submodules", "Subpackages" and "Module contents" headings, and
  • Have the results from docstring in my __init__.py files appear directly under the packages, so that if I click a package name, the first thing I see is the package documentation. At the moment, this documentation is placed under the slightly weird "Module contents" heading at the very end of each package section.

The "Submodules" and "Subpackages" headings are redundant I think, since the normal headings for packages/modules is "xxx.yyy package" and "xxx.yyy.zzz module".

The structure I would like for the above small example is

  • orexplore.components package
    • orexplore.components.mbg120 module
  • orexplore.simulators package
    • orexplore.simulators.test package
      • orexplore.simulators.test.mbg120 module
    • orexplore.simulators.mbg120 module

Where clicking the packages, the first thing I'd see on the page would be the package documentation.

Or maybe even just

  • orexplore.components
    • orexplore.components.mbg120
  • orexplore.simulators
    • orexplore.simulators.test
      • orexplore.simulators.test.mbg120
  • orexplore.simulators.mbg120

if there was some way to visually distinguish packages/modules (color? emblem?) instead of the quite wordy " package" and " module".

estan
  • 1,444
  • 2
  • 18
  • 29

3 Answers3

9

I implemented better-apidoc, a patched version of the sphinx-apidoc script that adds full support for templates.

It adds a -t/--template option, allowing to pass a template directory that must contain template files package.rst and module.rst. See package.rst and module.rst for an example. These render to e.g. http://qnet.readthedocs.io/en/latest/API/qnet.algebra.operator_algebra.html.

Michael Goerz
  • 4,300
  • 3
  • 23
  • 31
  • your tool is much needed in sphinx, which is great for prosaic documentation but incredibly lacking for api reference. I was wondering though if it would be possible to achieve a result such as [this](https://srinikom.github.io/pyside-docs/PySide/QtCore/QAbstractAnimation.html). Pyside uses sphinx for its documentation but as you can see it has been heavily customised. The output is somehow similar to Doxygen: First the class name, followed by its inheritance graph, follow a brief or full description then the list of memebers and methods, finally the detailed section. Much more easy to read – Dan Niero Aug 09 '17 at 09:27
  • Are you aware of a ``better-apidoc`` template which combines the sub-packages and sub-modules listings into one as per the original question? – Peter Cock Sep 11 '17 at 12:22
4

The sphinx-apidoc script uses the apidoc.py module. I am not able to provide detailed instructions, but in order to remove headings or otherwise customize the output, you'll have to write your own version of this module. There is no other "template".

Note that if the API and module structure is stable, there is no need to run sphinx-apidoc repeatedly. You can post-process the generated rst files to your liking once and then place them under version control. See also https://stackoverflow.com/a/28481785/407651.

Update

From Sphinx 2.2.0, sphinx-apidoc supports templates. See https://stackoverflow.com/a/57520238/407651.

Community
  • 1
  • 1
mzjn
  • 48,958
  • 13
  • 128
  • 248
  • 2
    Thanks @mzjn. I kind of suspected that there's was no way to customize it. A bit of a shortcoming of `sphinx-apidoc` that's it's not templated in my opinion. You're absolutely right that once the package structure is stable, I can safely edit the generated `.rst` instead. At the moment it's in a bit of a flux, but I'll live with the strange look until it settles. And after that I can probably avoid having to run `sphinx-apidoc` in the future and do any updates manually. – estan Apr 09 '15 at 07:30
  • 2
    As of Sphinx 2.2, the apidoc script now supports templates – Michael Goerz Oct 25 '19 at 06:14
4

FWIW, here's a complete hack of a script to make your desired changes, which were also my desired changes, in a "filename.rst.new" file next to each "filename.rst":

#!/usr/bin/env python

'''
Rearrange content in sphinx-apidoc generated .rst files.

* Move "Module Contents" section to the top.
* Remove headers for "Module Contents", "Submodules" and "Subpackages",
  including their underlines and the following blank line.
'''


import argparse
import glob
import os


# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def argument_parser():
    '''
    Define command line arguments.
    '''

    parser = argparse.ArgumentParser(
        description='''
        Rearrange content in sphinx-apidoc generated .rst files.
        '''
        )

    parser.add_argument(
        '-v', '--verbose',
        dest='verbose',
        default=False,
        action='store_true',
        help="""
            show more output.
            """
        )

    parser.add_argument(
        'input_file',
        metavar="INPUT_FILE",
        nargs='+',
        help="""
            file.
            """
        )

    return parser


# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def main():
    '''
    Main program entry point.
    '''

    global args
    parser = argument_parser()
    args = parser.parse_args()

    filenames = [glob.glob(x) for x in args.input_file]
    if len(filenames) > 0:
        filenames = reduce(lambda x, y: x + y, filenames)

    for filename in set(filenames):

        # line_num was going to be for some consistency checks, never
        # implemented but left in place.
        found = {
            'Subpackages': {'contents': False, 'line_num': None},
            'Submodules': {'contents': False, 'line_num': None},
            'Module contents': {'contents': True, 'line_num': None},
            }

        in_module_contents = False
        line_num = 0
        reordered = []
        module_contents = []

        new_filename = '.'.join([filename, 'new'])

        with open(filename, 'r') as fptr:

            for line in fptr:
                line = line.rstrip()
                discard = False

                line_num += 1

                if (
                        in_module_contents
                        and len(line) > 0
                        and line[0] not in ['.', '-', ' ']
                        ):  # pylint: disable=bad-continuation
                    in_module_contents = False

                for sought in found:

                    if line.find(sought) == 0:

                        found[sought]['line_num'] = line_num
                        if found[sought]['contents']:
                            in_module_contents = True

                        discard = True
                        # discard the underlines and a blank line too
                        _ = fptr.next()
                        _ = fptr.next()

                if in_module_contents and not discard:
                    module_contents.append(line)

                elif not discard:
                    reordered.append(line)

                # print '{:<6}|{}'.format(len(line), line)

        with open(new_filename, 'w') as fptr:
            fptr.write('\n'.join(reordered[:3]))
            fptr.write('\n')
            if module_contents:
                fptr.write('\n'.join(module_contents))
                fptr.write('\n')
                if len(module_contents[-1]) > 0:
                    fptr.write('\n')
            if reordered[3:]:
                fptr.write('\n'.join(reordered[3:]))
                fptr.write('\n')


if __name__ == "__main__":
    main()
Scott
  • 1,247
  • 3
  • 10
  • 21