modindex_exclude.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #
  2. # This is a Sphinx documentation tool extension which allows to
  3. # exclude some Python modules from the generated indexes. Modules
  4. # are excluded both from "modindex" and "genindex" index tables
  5. # (in the latter case, all members of a module are excluded).
  6. # To control exclusion, set "modindex_exclude" variable in Sphinx
  7. # conf.py to the list of modules to exclude. Note: these should be
  8. # modules (as defined by py:module directive, not just raw filenames).
  9. # This extension works by monkey-patching Sphinx core, so potentially
  10. # may not work with untested Sphinx versions. It tested to work with
  11. # 1.2.2 and 1.4.2
  12. #
  13. # Copyright (c) 2016 Paul Sokolovsky
  14. # Licensed under the terms of BSD license, see LICENSE file.
  15. #
  16. import sphinx
  17. #org_PythonModuleIndex_generate = None
  18. org_PyObject_add_target_and_index = None
  19. org_PyModule_run = None
  20. EXCLUDES = {}
  21. # No longer used, PyModule_run() monkey-patch does all the job
  22. def PythonModuleIndex_generate(self, docnames=None):
  23. docnames = []
  24. excludes = self.domain.env.config['modindex_exclude']
  25. for modname, (docname, synopsis, platforms, deprecated) in self.domain.data['modules'].items():
  26. #print(docname)
  27. if modname not in excludes:
  28. docnames.append(docname)
  29. return org_PythonModuleIndex_generate(self, docnames)
  30. def PyObject_add_target_and_index(self, name_cls, sig, signode):
  31. if hasattr(self.env, "ref_context"):
  32. # Sphinx 1.4
  33. ref_context = self.env.ref_context
  34. else:
  35. # Sphinx 1.2
  36. ref_context = self.env.temp_data
  37. modname = self.options.get(
  38. 'module', ref_context.get('py:module'))
  39. #print("*", modname, name_cls)
  40. if modname in self.env.config['modindex_exclude']:
  41. return None
  42. return org_PyObject_add_target_and_index(self, name_cls, sig, signode)
  43. def PyModule_run(self):
  44. env = self.state.document.settings.env
  45. modname = self.arguments[0].strip()
  46. excl = env.config['modindex_exclude']
  47. if modname in excl:
  48. self.options['noindex'] = True
  49. EXCLUDES.setdefault(modname, []).append(env.docname)
  50. return org_PyModule_run(self)
  51. def setup(app):
  52. app.add_config_value('modindex_exclude', [], 'html')
  53. # global org_PythonModuleIndex_generate
  54. # org_PythonModuleIndex_generate = sphinx.domains.python.PythonModuleIndex.generate
  55. # sphinx.domains.python.PythonModuleIndex.generate = PythonModuleIndex_generate
  56. global org_PyObject_add_target_and_index
  57. org_PyObject_add_target_and_index = sphinx.domains.python.PyObject.add_target_and_index
  58. sphinx.domains.python.PyObject.add_target_and_index = PyObject_add_target_and_index
  59. global org_PyModule_run
  60. org_PyModule_run = sphinx.domains.python.PyModule.run
  61. sphinx.domains.python.PyModule.run = PyModule_run