exclude_docs.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. include_set = set()
  9. exclude_set = set()
  10. for tag, docs in config.conditional_include_dict.items():
  11. if not app.tags.has(tag):
  12. exclude_set.update(docs)
  13. else:
  14. include_set.update(docs)
  15. # Do not exclude docs that have been explicitly included, e.g. if a doc is listed in both
  16. # ESP32_DOCS and ESP32S2_DOCS it will be included for those targets.
  17. app.config.exclude_patterns.extend(exclude_set - include_set)
  18. def build_subset(app, config):
  19. # Convert to list of docs to build
  20. docs_to_build = config.docs_to_build.split(',')
  21. # Exclude all documents which were not set as docs_to_build when build_docs were called
  22. exclude_docs = [filename for filename in get_matching_files(app.srcdir, compile_matchers(docs_to_build))]
  23. docs = [filename for filename in get_matching_files(app.srcdir, compile_matchers(exclude_docs))]
  24. app.config.exclude_patterns.extend(exclude_docs)
  25. # Get all docs that will be built
  26. docs = [filename for filename in get_matching_files(app.srcdir, compile_matchers(exclude_docs))]
  27. if not docs:
  28. raise ValueError('No documents to build')
  29. print('Building a subset of the documents: {}'.format(docs))
  30. # Sphinx requires a master document, if there is a document name 'index' then we pick that
  31. index_docs = [doc for doc in docs if 'index' in doc]
  32. if index_docs:
  33. config.master_doc = index_docs[0].replace('.rst', '')
  34. else:
  35. config.master_doc = docs[0].replace('.rst', '')
  36. def setup(app):
  37. # Tags are generated together with defines
  38. app.connect('config-inited', update_exclude_patterns)
  39. return {'parallel_read_safe': True, 'parallel_write_safe': True, 'version': '0.1'}