Skip to content

Commit

Permalink
NFC Rearrange directives.py a little (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
hoodmane authored Oct 1, 2023
1 parent 673e894 commit 5d8eb87
Showing 1 changed file with 38 additions and 32 deletions.
70 changes: 38 additions & 32 deletions sphinx_js/directives.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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,
Expand Down

0 comments on commit 5d8eb87

Please sign in to comment.