Merge pull request #7548 from tk0miya/refactor_autosummary4

refactor: AutosummaryRender
This commit is contained in:
Takeshi KOMIYA 2020-04-25 12:42:10 +09:00 committed by GitHub
commit 56ed2991fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 8 deletions

View File

@ -14,6 +14,8 @@ Deprecated
* The first argument for sphinx.ext.autosummary.generate.AutosummaryRenderer has
been changed to Sphinx object
* ``sphinx.ext.autosummary.generate.AutosummaryRenderer`` takes an object type
as an argument
* The ``template_dir`` argument of ``sphinx.ext.autosummary.generate.
AutosummaryRenderer``
* The ``module`` argument of ``sphinx.ext.autosummary.generate.
@ -22,6 +24,7 @@ Deprecated
generate_autosummary_docs()``
* The ``template_dir`` argument of ``sphinx.ext.autosummary.generate.
generate_autosummary_docs()``
* ``sphinx.ext.autosummary.generate.AutosummaryRenderer.exists()``
Features added
--------------

View File

@ -33,6 +33,12 @@ The following is a list of deprecated interfaces.
- 5.0
- N/A
* - ``sphinx.ext.autosummary.generate.AutosummaryRenderer`` takes an object
type as an argument
- 3.1
- 5.0
- N/A
* - The ``template_dir`` argument of
``sphinx.ext.autosummary.generate.AutosummaryRenderer``
- 3.1
@ -57,6 +63,11 @@ The following is a list of deprecated interfaces.
- 5.0
- N/A
* - ``sphinx.ext.autosummary.generate.AutosummaryRenderer.exists()``
- 3.1
- 5.0
- N/A
* - ``desc_signature['first']``
-
- 3.0

View File

@ -147,15 +147,30 @@ class AutosummaryRenderer:
def exists(self, template_name: str) -> bool:
"""Check if template file exists."""
warnings.warn('AutosummaryRenderer.exists() is deprecated.',
RemovedInSphinx50Warning, stacklevel=2)
try:
self.env.get_template(template_name)
return True
except TemplateNotFound:
return False
def render(self, template_name: str, context: Dict) -> str:
def render(self, objtype: str, context: Dict) -> str:
"""Render a template file."""
return self.env.get_template(template_name).render(context)
if objtype.endswith('.rst'):
# old styled: template_name is given
warnings.warn('AutosummaryRenderer.render() takes an object type as an argument.',
RemovedInSphinx50Warning, stacklevel=2)
return self.env.get_template(objtype).render(context)
else:
# objtype is given
try:
template = self.env.get_template('autosummary/%s.rst' % objtype)
except TemplateNotFound:
# fallback to base.rst
template = self.env.get_template('autosummary/base.rst')
return template.render(context)
# -- Generating output ---------------------------------------------------------
@ -167,11 +182,6 @@ def generate_autosummary_content(name: str, obj: Any, parent: Any,
recursive: bool) -> str:
doc = get_documenter(app, obj, parent)
if template_name is None:
template_name = 'autosummary/%s.rst' % doc.objtype
if not template.exists(template_name):
template_name = 'autosummary/base.rst'
def skip_member(obj: Any, name: str, objtype: str) -> bool:
try:
return app.emit_firstresult('autodoc-skip-member', objtype, name,
@ -256,7 +266,7 @@ def generate_autosummary_content(name: str, obj: Any, parent: Any,
ns['objtype'] = doc.objtype
ns['underline'] = len(name) * '='
return template.render(template_name, ns)
return template.render(doc.objtype, ns)
def generate_autosummary_docs(sources: List[str], output_dir: str = None,