|
| 1 | +""" |
| 2 | + sphinxcontrib.apidoc.ext |
| 3 | + ~~~~~~~~~~~~~~~~~~~~~~~~ |
| 4 | +
|
| 5 | + A Sphinx extension for running 'sphinx-apidoc' on each build. |
| 6 | +
|
| 7 | + :copyright: Copyright 2018 by Stephen Finucane <[email protected]> |
| 8 | + :license: BSD, see LICENSE for details. |
| 9 | +""" |
| 10 | + |
| 11 | +from os import path |
| 12 | + |
| 13 | +from sphinx.util import logging |
| 14 | + |
| 15 | +try: |
| 16 | + from sphinx.ext import apidoc # Sphinx >= 1.7 |
| 17 | +except ImportError: |
| 18 | + from sphinx import apidoc # Sphinx < 1.7 |
| 19 | + |
| 20 | +if False: |
| 21 | + # For type annotation |
| 22 | + from sphinx.application import Sphinx # noqa |
| 23 | + |
| 24 | +logger = logging.getLogger(__name__) |
| 25 | + |
| 26 | + |
| 27 | +def builder_inited(app): |
| 28 | + # type: (Sphinx) -> None |
| 29 | + module_dir = app.config.apidoc_module_dir |
| 30 | + output_dir = path.join(app.srcdir, app.config.apidoc_output_dir) |
| 31 | + excludes = app.config.apidoc_excluded_modules |
| 32 | + |
| 33 | + if not module_dir: |
| 34 | + logger.warning("No 'apidoc_module_dir' specified; skipping API doc " |
| 35 | + "generation") |
| 36 | + return |
| 37 | + |
| 38 | + # if the path is relative, make it relative to the 'conf.py' directory |
| 39 | + if not path.isabs(module_dir): |
| 40 | + module_dir = path.abspath(path.join(app.srcdir, module_dir)) |
| 41 | + |
| 42 | + excludes = [path.abspath(path.join(module_dir, exc)) for exc in excludes] |
| 43 | + |
| 44 | + # refactor this module so that we can call 'recurse_tree' like a sane |
| 45 | + # person - at present there is way too much passing around of the |
| 46 | + # 'optparse.Value' instance returned by 'optparse.parse_args' |
| 47 | + cmd = ['--force', '-o', output_dir, module_dir] |
| 48 | + if excludes: |
| 49 | + cmd += excludes |
| 50 | + |
| 51 | + apidoc.main(cmd) |
0 commit comments