exclude_docs.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. from sphinx.util import get_matching_files
  2. from sphinx.util.matching import compile_matchers
  3. # Updates the excluded documents according to the conditional_include_dict {tag:documents}
  4. def update_exclude_patterns(app, config):
  5. # Default to building all if option not set
  6. if config.docs_to_build:
  7. build_subset(app, config)
  8. for tag, docs in config.conditional_include_dict.items():
  9. if not app.tags.has(tag):
  10. app.config.exclude_patterns.extend(docs)
  11. def build_subset(app, config):
  12. # Convert to list of docs to build
  13. docs_to_build = config.docs_to_build.split(',')
  14. # Exclude all documents which were not set as docs_to_build when build_docs were called
  15. exclude_docs = [filename for filename in get_matching_files(app.srcdir, compile_matchers(docs_to_build))]
  16. docs = [filename for filename in get_matching_files(app.srcdir, compile_matchers(exclude_docs))]
  17. app.config.exclude_patterns.extend(exclude_docs)
  18. # Get all docs that will be built
  19. docs = [filename for filename in get_matching_files(app.srcdir, compile_matchers(exclude_docs))]
  20. if not docs:
  21. raise ValueError("No documents to build")
  22. print("Building a subset of the documents: {}".format(docs))
  23. # Sphinx requires a master document, if there is a document name 'index' then we pick that
  24. index_docs = [doc for doc in docs if 'index' in doc]
  25. if index_docs:
  26. config.master_doc = index_docs[0].replace('.rst', '')
  27. else:
  28. config.master_doc = docs[0].replace('.rst', '')
  29. def setup(app):
  30. # Tags are generated together with defines
  31. app.connect('config-inited', update_exclude_patterns)
  32. return {'parallel_read_safe': True, 'parallel_write_safe': True, 'version': '0.1'}