From 5d8eb872ef3c37337078c0f449c7c21af76cf246 Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Sat, 30 Sep 2023 18:58:28 -0700 Subject: [PATCH] NFC Rearrange directives.py a little (#105) --- sphinx_js/directives.py | 70 ++++++++++++++++++++++------------------- 1 file changed, 38 insertions(+), 32 deletions(-) diff --git a/sphinx_js/directives.py b/sphinx_js/directives.py index ba3ba6b0..c2daa4f0 100644 --- a/sphinx_js/directives.py +++ b/sphinx_js/directives.py @@ -22,7 +22,12 @@ from sphinx.application import Sphinx from sphinx.domains.javascript import JSCallable -from .renderers import AutoAttributeRenderer, AutoClassRenderer, AutoFunctionRenderer +from .renderers import ( + AutoAttributeRenderer, + AutoClassRenderer, + AutoFunctionRenderer, + JsRenderer, +) def unescape(escaped: str) -> str: @@ -35,6 +40,16 @@ def unescape(escaped: str) -> str: return re.sub(r"\\([^ ])", r"\1", escaped) +def _members_to_exclude(arg: str | None) -> set[str]: + """Return a set of members to exclude given a comma-delim list of them. + + Exclude none if none are passed. This differs from autodocs' behavior, + which excludes all. That seemed useless to me. + + """ + return set(a.strip() for a in (arg or "").split(",")) + + def sphinx_js_type_role(role, rawtext, text, lineno, inliner, options=None, content=None): # type: ignore[no-untyped-def] """ The body should be escaped rst. This renders its body as rst and wraps the @@ -59,6 +74,24 @@ class JsDirective(Directive): option_spec = {"short-name": flag} + def _run(self, renderer_class: type[JsRenderer], app: Sphinx) -> list[Node]: + renderer = renderer_class.from_directive(self, app) + note_dependencies(app, renderer.dependencies()) + return renderer.rst_nodes() + + +class JsDirectiveWithChildren(JsDirective): + option_spec = JsDirective.option_spec.copy() + option_spec.update( + { + "members": lambda members: ( + [m.strip() for m in members.split(",")] if members else [] + ), + "exclude-members": _members_to_exclude, + "private-members": flag, + } + ) + def note_dependencies(app: Sphinx, dependencies: Iterable[str]) -> None: """Note dependencies of current document. @@ -86,15 +119,13 @@ class AutoFunctionDirective(JsDirective): """ def run(self) -> list[Node]: - renderer = AutoFunctionRenderer.from_directive(self, app) - note_dependencies(app, renderer.dependencies()) - return renderer.rst_nodes() + return self._run(AutoFunctionRenderer, app) return AutoFunctionDirective def auto_class_directive_bound_to_app(app: Sphinx) -> type[Directive]: - class AutoClassDirective(JsDirective): + class AutoClassDirective(JsDirectiveWithChildren): """js:autoclass directive, which spits out a js:class directive Takes a single argument which is a JS class name combined with an @@ -103,21 +134,8 @@ class AutoClassDirective(JsDirective): """ - option_spec = JsDirective.option_spec.copy() - option_spec.update( - { - "members": lambda members: ( - [m.strip() for m in members.split(",")] if members else [] - ), - "exclude-members": _members_to_exclude, - "private-members": flag, - } - ) - def run(self) -> list[Node]: - renderer = AutoClassRenderer.from_directive(self, app) - note_dependencies(app, renderer.dependencies()) - return renderer.rst_nodes() + return self._run(AutoClassRenderer, app) return AutoClassDirective @@ -131,23 +149,11 @@ class AutoAttributeDirective(JsDirective): """ def run(self) -> list[Node]: - renderer = AutoAttributeRenderer.from_directive(self, app) - note_dependencies(app, renderer.dependencies()) - return renderer.rst_nodes() + return self._run(AutoAttributeRenderer, app) return AutoAttributeDirective -def _members_to_exclude(arg: str | None) -> set[str]: - """Return a set of members to exclude given a comma-delim list them. - - Exclude none if none are passed. This differs from autodocs' behavior, - which excludes all. That seemed useless to me. - - """ - return set(a.strip() for a in (arg or "").split(",")) - - class JSFunction(JSCallable): option_spec = { **JSCallable.option_spec,