Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] 'hidden' option for domain directives #9671

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions sphinx/directives/__init__.py
Expand Up @@ -60,6 +60,7 @@ class ObjectDescription(SphinxDirective, Generic[T]):
final_argument_whitespace = True
option_spec: OptionSpec = {
'noindex': directives.flag,
'hidden': directives.flag,
}

# types of doc fields that this directive handles, see sphinx.util.docfields
Expand Down Expand Up @@ -170,6 +171,7 @@ def run(self) -> List[Node]:
# 'desctype' is a backwards compatible attribute
node['objtype'] = node['desctype'] = self.objtype
node['noindex'] = noindex = ('noindex' in self.options)
node['hidden'] = 'hidden' in self.options
if self.domain:
node['classes'].append(self.domain)
node['classes'].append(node['objtype'])
Expand Down
6 changes: 4 additions & 2 deletions sphinx/domains/c.py
Expand Up @@ -3140,9 +3140,11 @@ class CObject(ObjectDescription[ASTDeclaration]):
names=('rtype',)),
]

option_spec: OptionSpec = {
option_spec: OptionSpec = ObjectDescription.option_spec.copy()
option_spec.update({
'noindexentry': directives.flag,
}
})
del option_spec['noindex'] # is in ObjectDescription but doesn't make sense here

def _add_enumerator_to_parent(self, ast: ASTDeclaration) -> None:
assert ast.objectType == 'enumerator'
Expand Down
6 changes: 4 additions & 2 deletions sphinx/domains/cpp.py
Expand Up @@ -6948,10 +6948,12 @@ class CPPObject(ObjectDescription[ASTDeclaration]):
names=('returns', 'return')),
]

option_spec: OptionSpec = {
option_spec: OptionSpec = ObjectDescription.option_spec.copy()
option_spec.update({
'noindexentry': directives.flag,
'tparam-line-spec': directives.flag,
}
})
del option_spec['noindex'] # is in ObjectDescription but doesn't make sense here

def _add_enumerator_to_parent(self, ast: ASTDeclaration) -> None:
assert ast.objectType == 'enumerator'
Expand Down
5 changes: 3 additions & 2 deletions sphinx/domains/javascript.py
Expand Up @@ -48,10 +48,11 @@ class JSObject(ObjectDescription[Tuple[str, str]]):
#: based on directive nesting
allow_nesting = False

option_spec: OptionSpec = {
option_spec: OptionSpec = ObjectDescription.option_spec.copy()
option_spec.update({
'noindex': directives.flag,
'noindexentry': directives.flag,
}
})

def handle_signature(self, sig: str, signode: desc_signature) -> Tuple[str, str]:
"""Breaks down construct signatures
Expand Down
6 changes: 3 additions & 3 deletions sphinx/domains/python.py
Expand Up @@ -389,13 +389,13 @@ class PyObject(ObjectDescription[Tuple[str, str]]):
:cvar allow_nesting: Class is an object that allows for nested namespaces
:vartype allow_nesting: bool
"""
option_spec: OptionSpec = {
'noindex': directives.flag,
option_spec: OptionSpec = ObjectDescription.option_spec.copy()
option_spec.update({
'noindexentry': directives.flag,
'module': directives.unchanged,
'canonical': directives.unchanged,
'annotation': directives.unchanged,
}
})

doc_field_types = [
PyTypedField('parameter', label=_('Parameters'),
Expand Down
27 changes: 27 additions & 0 deletions sphinx/transforms/post_transforms/__init__.py
Expand Up @@ -267,11 +267,38 @@ def run(self, **kwargs: Any) -> None:
node['classes'].append(node.parent['domain'])


class HideHiddenDesc(SphinxPostTransform):
"""Change all desc nodes to a node just with all ids in the subtree."""
default_priority = 200

def run(self, **kwargs: Any) -> None:
for node in self.document.traverse(addnodes.desc):
if not node.get('hidden'):
continue

def collectIds(node, ids):
if not isinstance(node, nodes.Element):
return
theseIds = node.get('ids')
if theseIds:
ids.extend(theseIds)
for c in node.children:
collectIds(c, ids)

ids: List[str] = []
collectIds(node, ids)
newNode = nodes.inline()
newNode['ids'] = ids
node.replace_self(newNode)
node['classes'] = [] # replace_self seems to copy attributes


def setup(app: Sphinx) -> Dict[str, Any]:
app.add_post_transform(ReferencesResolver)
app.add_post_transform(OnlyNodeTransform)
app.add_post_transform(SigElementFallbackTransform)
app.add_post_transform(PropagateDescDomain)
app.add_post_transform(HideHiddenDesc)

return {
'version': 'builtin',
Expand Down