Skip to content

Instantly share code, notes, and snippets.

@kakawait
Last active August 4, 2022 09:35
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kakawait/9215487 to your computer and use it in GitHub Desktop.
Save kakawait/9215487 to your computer and use it in GitHub Desktop.
Sphinx only plugin
import sys, os
# The path where you put the only.py file
sys.path.append(os.path.abspath('../sphinx-ext/'))
# Add any Sphinx extension module names here, as strings. They can be extensions
# coming with Sphinx (named 'sphinx.ext.*') or your custom ones.
extensions = ['scope']
import os, re
from sphinx import addnodes
docs_to_remove = []
def setup(app):
app.ignore = []
app.connect('builder-inited', builder_inited)
app.connect('env-get-outdated', env_get_outdated)
app.connect('doctree-read', doctree_read)
def builder_inited(app):
for doc in app.env.found_docs:
first_directive = None
with open(app.env.srcdir + os.sep + doc + app.env.config.source_suffix, 'r') as f:
first_directive = f.readline() + f.readline()
if first_directive:
m = re.match(r'^\.\. meta::\s+:scope: ([a-zA-Z0-9_-]+)', first_directive)
if m and not app.tags.has(m.group(1)):
docs_to_remove.append(doc)
app.env.found_docs.difference_update(docs_to_remove)
def env_get_outdated(app, env, added, changed, removed):
added.difference_update(docs_to_remove)
changed.difference_update(docs_to_remove)
removed.update(docs_to_remove)
return []
def doctree_read(app, doctree):
for toctreenode in doctree.traverse(addnodes.toctree):
for e in toctreenode['entries']:
ref = str(e[1])
if ref in docs_to_remove:
toctreenode['entries'].remove(e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment