Skip to content

Commit

Permalink
use the feature from sphinx-doc/sphinx#8038
Browse files Browse the repository at this point in the history
  • Loading branch information
keewis committed Aug 2, 2020
1 parent c26ebce commit 0f6df5f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 38 deletions.
14 changes: 7 additions & 7 deletions sphinx_autosummary_accessors/__init__.py
Expand Up @@ -15,12 +15,6 @@
AccessorMethodDocumenter,
)

if packaging.version.parse(sphinx.__version__) >= packaging.version.parse("3.1"):
from .autosummary import CustomAutosummary
else:
CustomAutosummary = None


try:
__version__ = version("sphinx-autosummary-accessors")
except Exception:
Expand All @@ -37,5 +31,11 @@ def setup(app):
app.add_autodocumenter(AccessorMethodDocumenter)
app.add_autodocumenter(AccessorCallableDocumenter)

if CustomAutosummary is not None:
if packaging.version.parse(sphinx.__version__) >= packaging.version.parse("3.2"):
from .autosummary import get_documenter_from_template

app.add_autosummary_get_documenter(get_documenter_from_template)
elif packaging.version.parse(sphinx.__version__) >= packaging.version.parse("3.1"):
from .autosummary import CustomAutosummary

app.add_directive("autosummary", CustomAutosummary, override=True)
74 changes: 43 additions & 31 deletions sphinx_autosummary_accessors/autosummary.py
Expand Up @@ -15,35 +15,49 @@ def extract_documenter(content):
return directive_name, "::".join([modname, name])


class CustomAutosummary(Autosummary):
def get_documenter_from_template(self, name, obj, parent, options):
options = options.copy()
options.pop("toctree")
template_name = options.pop("template")
options["imported_members"] = options.get("imported_members", False)
options["recursive"] = options.get("recursive", False)

app = self.env.app

context = {}
context.update(app.config.autosummary_context)

rendered = generate.generate_autosummary_content(
name,
obj,
parent,
template=generate.AutosummaryRenderer(app),
template_name=template_name,
app=app,
context=context,
**options,
)

documenter_name, real_name = extract_documenter(rendered)
documenter = app.registry.documenters.get(documenter_name)

return documenter, real_name
class DocumenterWrapper:
def __init__(self, documenter, name):
self.name = name
self.documenter = documenter

@property
def priority(self):
return self.documenter.priority

def __call__(self, bridge, full_name):
return self.documenter(bridge, self.name)


def get_documenter_from_template(autosummary, obj, parent, name):
options = autosummary.options.copy()
options.pop("toctree")
template_name = options.pop("template")
options["imported_members"] = options.get("imported_members", False)
options["recursive"] = options.get("recursive", False)

app = autosummary.env.app

context = {}
context.update(app.config.autosummary_context)

rendered = generate.generate_autosummary_content(
name,
obj,
parent,
template=generate.AutosummaryRenderer(app),
template_name=template_name,
app=app,
context=context,
**options,
)

documenter_name, real_name = extract_documenter(rendered)
documenter = app.registry.documenters.get(documenter_name)

return DocumenterWrapper(documenter, real_name)


class CustomAutosummary(Autosummary):
def get_items(self, names):
"""Try to import the given names, and return a list of
``[(name, signature, summary_string, real_name), ...]``.
Expand Down Expand Up @@ -84,9 +98,7 @@ def get_items(self, names):
# handle module prefixes slightly differently

if "template" in self.options:
doccls, full_name = self.get_documenter_from_template(
real_name, obj, parent, self.options
)
doccls = get_documenter_from_template(self, obj, parent, real_name)
else:
doccls = autosummary.get_documenter(self.env.app, obj, parent)

Expand Down

0 comments on commit 0f6df5f

Please sign in to comment.