summaryrefslogtreecommitdiff
path: root/sphinx/domains/javascript.py
diff options
context:
space:
mode:
authorAdam Turner <9087854+AA-Turner@users.noreply.github.com>2022-09-13 20:20:02 +0100
committerGitHub <noreply@github.com>2022-09-13 20:20:02 +0100
commitf57177de89ff1a154c830558d218c3e334b2b437 (patch)
treefd3086cf2e716b9973d8efe4904b3fabdc18ac3a /sphinx/domains/javascript.py
parentb34765780950a04a8a387b1cafa33bf99b704bde (diff)
downloadsphinx-git-f57177de89ff1a154c830558d218c3e334b2b437.tar.gz
Add contents entries for domain objects (#10807)
- Add entries in the table of contents for domain objects (e.g. `py:function`, `rst:role`, etc). Supported domains are Javascript, Python, and reStructuredText. - Support content in `py:module` and `js:module` directives. - Add the `noindexentry` and `noindex` flags to more domains. - Add `toc_object_entries_show_parents` configuration setting - Update documentation and tests
Diffstat (limited to 'sphinx/domains/javascript.py')
-rw-r--r--sphinx/domains/javascript.py45
1 files changed, 41 insertions, 4 deletions
diff --git a/sphinx/domains/javascript.py b/sphinx/domains/javascript.py
index 60ea31e94..b77c8dff0 100644
--- a/sphinx/domains/javascript.py
+++ b/sphinx/domains/javascript.py
@@ -18,8 +18,8 @@ from sphinx.locale import _, __
from sphinx.roles import XRefRole
from sphinx.util import logging
from sphinx.util.docfields import Field, GroupedField, TypedField
-from sphinx.util.docutils import SphinxDirective
-from sphinx.util.nodes import make_id, make_refnode
+from sphinx.util.docutils import SphinxDirective, switch_source_input
+from sphinx.util.nodes import make_id, make_refnode, nested_parse_with_titles
from sphinx.util.typing import OptionSpec
logger = logging.getLogger(__name__)
@@ -108,6 +108,17 @@ class JSObject(ObjectDescription[Tuple[str, str]]):
_pseudo_parse_arglist(signode, arglist)
return fullname, prefix
+ def _object_hierarchy_parts(self, sig_node: desc_signature) -> Tuple[str, ...]:
+ if 'fullname' not in sig_node:
+ return ()
+ modname = sig_node.get('module')
+ fullname = sig_node['fullname']
+
+ if modname:
+ return (modname, *fullname.split('.'))
+ else:
+ return tuple(fullname.split('.'))
+
def add_target_and_index(self, name_obj: Tuple[str, str], sig: str,
signode: desc_signature) -> None:
mod_name = self.env.ref_context.get('js:module')
@@ -201,6 +212,25 @@ class JSObject(ObjectDescription[Tuple[str, str]]):
"""
return fullname.replace('$', '_S_')
+ def _toc_entry_name(self, sig_node: desc_signature) -> str:
+ if not sig_node.get('_toc_parts'):
+ return ''
+
+ config = self.env.app.config
+ objtype = sig_node.parent.get('objtype')
+ if config.add_function_parentheses and objtype in {'function', 'method'}:
+ parens = '()'
+ else:
+ parens = ''
+ *parents, name = sig_node['_toc_parts']
+ if config.toc_object_entries_show_parents == 'domain':
+ return sig_node.get('fullname', name) + parens
+ if config.toc_object_entries_show_parents == 'hide':
+ return name + parens
+ if config.toc_object_entries_show_parents == 'all':
+ return '.'.join(parents + [name + parens])
+ return ''
+
class JSCallable(JSObject):
"""Description of a JavaScript function, method or constructor."""
@@ -249,7 +279,7 @@ class JSModule(SphinxDirective):
:param mod_name: Module name
"""
- has_content = False
+ has_content = True
required_arguments = 1
optional_arguments = 0
final_argument_whitespace = False
@@ -261,7 +291,14 @@ class JSModule(SphinxDirective):
mod_name = self.arguments[0].strip()
self.env.ref_context['js:module'] = mod_name
noindex = 'noindex' in self.options
- ret: List[Node] = []
+
+ content_node: Element = nodes.section()
+ with switch_source_input(self.state, self.content):
+ # necessary so that the child nodes get the right source/line set
+ content_node.document = self.state.document
+ nested_parse_with_titles(self.state, self.content, content_node)
+
+ ret: List[Node] = [*content_node.children]
if not noindex:
domain = cast(JavaScriptDomain, self.env.get_domain('js'))