|
31 | 31 | from sphinx import __display_version__, package_dir
|
32 | 32 | from sphinx.cmd.quickstart import EXTENSIONS
|
33 | 33 | from sphinx.locale import __
|
34 |
| -from sphinx.util import rst |
| 34 | +from sphinx.util import logging, rst |
35 | 35 | from sphinx.util.osutil import FileAvoidWrite, ensuredir, walk
|
36 | 36 |
|
37 | 37 | if False:
|
38 | 38 | # For type annotation
|
39 |
| - from typing import Any, List, Tuple # NOQA |
| 39 | + from typing import Any, Dict, List, Tuple # NOQA |
| 40 | + from sphinx.application import Sphinx # NOQA |
| 41 | + |
| 42 | +logger = logging.getLogger(__name__) |
40 | 43 |
|
41 | 44 | # automodule options
|
42 | 45 | if 'SPHINX_APIDOC_OPTIONS' in os.environ:
|
@@ -459,6 +462,45 @@ def main(argv=sys.argv[1:]):
|
459 | 462 | return 0
|
460 | 463 |
|
461 | 464 |
|
462 |
| -# So program can be started with "python -m sphinx.apidoc ..." |
| 465 | +def builder_inited(app): |
| 466 | + # type: (Sphinx) -> None |
| 467 | + module_dir = app.config.apidoc_module_dir |
| 468 | + output_dir = path.join(app.srcdir, app.config.apidoc_output_dir) |
| 469 | + excludes = app.config.apidoc_excluded_modules |
| 470 | + |
| 471 | + if not module_dir: |
| 472 | + logger.warning("No 'apidoc_module_dir' specified; skipping API doc " |
| 473 | + "generation") |
| 474 | + return |
| 475 | + |
| 476 | + # if the path is relative, make it relative to the 'conf.py' directory |
| 477 | + if not path.isabs(module_dir): |
| 478 | + module_dir = path.abspath(path.join(app.srcdir, module_dir)) |
| 479 | + |
| 480 | + excludes = [path.abspath(path.join(module_dir, exc)) for exc in excludes] |
| 481 | + |
| 482 | + # refactor this module so that we can call 'recurse_tree' like a sane |
| 483 | + # person - at present there is way too much passing around of the |
| 484 | + # 'optparse.Value' instance returned by 'optparse.parse_args' |
| 485 | + cmd = [module_dir, '--force', '-o', output_dir] |
| 486 | + if excludes: |
| 487 | + cmd += excludes |
| 488 | + |
| 489 | + main(cmd) |
| 490 | + |
| 491 | + |
| 492 | +def setup(app): |
| 493 | + # type: (Sphinx) -> Dict[unicode, Any] |
| 494 | + app.setup_extension('sphinx.ext.autodoc') # We need autodoc to function |
| 495 | + |
| 496 | + app.connect('builder-inited', builder_inited) |
| 497 | + app.add_config_value('apidoc_module_dir', None, 'env', [str]) |
| 498 | + app.add_config_value('apidoc_output_dir', 'api', 'env', [str]) |
| 499 | + app.add_config_value('apidoc_excluded_modules', [], 'env', |
| 500 | + [[str]]) |
| 501 | + |
| 502 | + return {'version': __display_version__, 'parallel_read_safe': True} |
| 503 | + |
| 504 | + |
463 | 505 | if __name__ == "__main__":
|
464 | 506 | main()
|
0 commit comments