Skip to content

Commit

Permalink
apidoc: Restore support for legacy '_t'-suffix template files (#12929)
Browse files Browse the repository at this point in the history
Co-authored-by: Adam Turner <[email protected]>
  • Loading branch information
jayaddison and AA-Turner authored Oct 2, 2024
1 parent b16e196 commit 80c4b65
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ Bugs fixed
* #12844: Restore support for ``:noindex:`` for the :rst:dir:`js:module`
and :rst:dir:`py:module` directives.
Patch by Stephen Finucane.
* #12916: Restore support for custom templates named with the legacy ``_t``
suffix during ``apidoc`` RST rendering (regression in 7.4.0).
Patch by James Addison.

Testing
-------
Expand Down
11 changes: 11 additions & 0 deletions sphinx/jinja2glue.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,22 @@ class SphinxFileSystemLoader(FileSystemLoader):
def get_source(
self, environment: Environment, template: str
) -> tuple[str, str, Callable[[], bool]]:
if template.endswith('.jinja'):
legacy_suffix = '_t'
legacy_template = template.removesuffix('.jinja') + legacy_suffix
else:
legacy_template = None

for searchpath in self.searchpath:
filename = path.join(searchpath, template)
f = open_if_exists(filename)
if f is not None:
break
if legacy_template is not None:
filename = path.join(searchpath, legacy_template)
f = open_if_exists(filename)
if f is not None:
break
else:
raise TemplateNotFound(template)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The Jinja module template was found!
------------------------------------
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The legacy module template was found!
-------------------------------------
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The legacy package template was found!
--------------------------------------
Empty file.
Empty file.
35 changes: 35 additions & 0 deletions tests/test_extensions/test_ext_apidoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,41 @@ def test_simple(make_app, apidoc):
print(app._warning.getvalue())


@pytest.mark.apidoc(
coderoot='test-apidoc-custom-templates',
options=[
'--separate',
'--templatedir=tests/roots/test-apidoc-custom-templates/_templates',
],
)
def test_custom_templates(make_app, apidoc):
outdir = apidoc.outdir
assert (outdir / 'conf.py').is_file()
assert (outdir / 'index.rst').is_file()

template_dir = apidoc.coderoot / '_templates'
assert sorted(template_dir.iterdir()) == [
template_dir / 'module.rst.jinja',
template_dir / 'module.rst_t',
template_dir / 'package.rst_t',
]

app = make_app('text', srcdir=outdir)
app.build()

builddir = outdir / '_build' / 'text'

# Assert that the legacy filename is discovered
with open(builddir / 'mypackage.txt', encoding='utf-8') as f:
txt = f.read()
assert 'The legacy package template was found!' in txt

# Assert that the new filename is preferred
with open(builddir / 'mypackage.mymodule.txt', encoding='utf-8') as f:
txt = f.read()
assert 'The Jinja module template was found!' in txt


@pytest.mark.apidoc(
coderoot='test-apidoc-pep420/a',
options=['--implicit-namespaces'],
Expand Down

0 comments on commit 80c4b65

Please sign in to comment.