Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename template files in Sphinx to use preferred .jinja suffix. #12364

Merged

Conversation

jayaddison
Copy link
Contributor

Feature or Bugfix

  • Refactoring

Purpose

  • Use the more transparent (as in: easier and less ambiguous to understand) .jinja suffix for templated files within the Sphinx code repository, because they are evaluated as Jinja templates.

Detail

  • Rename files that had a _t suffix (short for 'template') to use .jinja as the file suffix instead.

Relates

@jayaddison
Copy link
Contributor Author

Nope, not as easy as reverting a single-commit; I'll revisit this again later.

@jayaddison jayaddison marked this pull request as draft May 9, 2024 23:32
…plication and test code"

This reverts commit cef1059.

Conflicts:
	sphinx/builders/_epub_base.py
	sphinx/builders/changes.py
	sphinx/ext/imgmath.py
	sphinx/writers/latex.py
… references to '_t'-suffix templates"

This reverts commit f0f22fd.
… template detection and rendering"

This reverts commit 2a5a9a0.

Conflicts:
	sphinx/ext/imgmath.py
	sphinx/writers/latex.py
@jayaddison
Copy link
Contributor Author

Question: will this mess with localization/translation workflows? Various .po files refer to the template file paths:

$ grep -r '_t:' sphinx/locale | sort | head -n 10
sphinx/locale/ar/LC_MESSAGES/sphinx.po:#: sphinx/builders/latex/__init__.py:199 sphinx/templates/latex/latex.tex_t:91
sphinx/locale/ar/LC_MESSAGES/sphinx.po:#: sphinx/domains/std/__init__.py:571 sphinx/templates/latex/latex.tex_t:106
sphinx/locale/ar/LC_MESSAGES/sphinx.po:#: sphinx/templates/latex/longtable.tex_t:52
sphinx/locale/ar/LC_MESSAGES/sphinx.po:#: sphinx/templates/latex/longtable.tex_t:63
sphinx/locale/ar/LC_MESSAGES/sphinx.po:#: sphinx/templates/latex/sphinxmessages.sty_t:10
sphinx/locale/ar/LC_MESSAGES/sphinx.po:#: sphinx/templates/latex/sphinxmessages.sty_t:11
sphinx/locale/ar/LC_MESSAGES/sphinx.po:#: sphinx/templates/latex/sphinxmessages.sty_t:12
sphinx/locale/ar/LC_MESSAGES/sphinx.po:#: sphinx/templates/latex/sphinxmessages.sty_t:13
sphinx/locale/ar/LC_MESSAGES/sphinx.po:#: sphinx/templates/latex/sphinxmessages.sty_t:8
sphinx/locale/ar/LC_MESSAGES/sphinx.po:#: sphinx/templates/latex/sphinxmessages.sty_t:9

@n-peugnet
Copy link
Contributor

n-peugnet commented May 12, 2024

Question: will this mess with localization/translation workflows? Various .po files refer to the template file paths:

No problem, these comments are automatically added and updated by the gettext extractor (here a script based on babel, which should maybe be updated). They are only there as an indication for the translators and are not used when looking up for translation of messages.

…te-files

# Conflicts:
#	sphinx/ext/apidoc.py
@AA-Turner AA-Turner marked this pull request as ready for review July 3, 2024 02:01
Copy link
Member

@AA-Turner AA-Turner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jayaddison are you happy with this?

A

@@ -437,7 +437,7 @@ def astext(self) -> str:
'body': ''.join(self.body),
'indices': self.generate_indices(),
})
return self.render('latex.tex_t', self.elements)
return self.render('latex.tex', self.elements)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review @AA-Turner. On reflection I'm slightly concerned by this filename suffix adjustment to the LaTeX writer's render interface.

Maybe it's OK as-is, but I think I could rewrite this to be more backwards compatible (in case anyone has subclassed the writer, for example, or make calls to that render method directly for some reason).

Comment on lines 524 to 527
elif template_name.endswith('.jinja'):
legacy_template = template[:-len('.jinja')] + '_t'
if path.exists(legacy_template):
return renderer.render(legacy_template, variables)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This legacy loading path logic that I switched in isn't something I'm 100% keen on - it's non-obvious enough to feel a bit ugly behaviour-wise.

I think I'll add a warning message when this legacy path is followed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's also a search precedence question here; do we search all template paths for the .jinja file and then all paths for _t, or do we (as currently implemented) perform a single pass through the template paths and check for both file variants.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically I'm worrying about a security-type concern here: it's that someone could place a _t (legacy) file in a template path that they control, and that this logic would then load then instead of the expected .jinja file.

However: I'm not sure that that particular concern is well-founded, because if an attacker has write access to one of the template paths already, they could equally just write a .jinja file in there, and that would achieve the same effect.

However I do think a warning message would still add some value as it could help people to encourage legacy templates to migrate.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that I'm considering the security concern a non-issue, I think that the file-load-order precedence is less important here, and it is OK to use a logically-simpler single search pass over the template paths.

sphinx/ext/imgmath.py Outdated Show resolved Hide resolved
sphinx/writers/latex.py Outdated Show resolved Hide resolved
@jayaddison
Copy link
Contributor Author

If anyone with a security/sysadmin mindset could double-check and/or challenge my thinking in this thread I'd appreciate it: #12364 (comment)

Aside from anything related to that, I'm now comfortable that the changes here are ready.

@AA-Turner AA-Turner merged commit a6c2bdd into sphinx-doc:master Jul 11, 2024
23 checks passed
@AA-Turner
Copy link
Member

Thanks!

A

@jayaddison jayaddison deleted the pr-11916-followup/rename-template-files branch July 11, 2024 11:54
@jayaddison
Copy link
Contributor Author

Thank you! (including for the removesuffix fixup - a nice py3.9 benefit!)

@AA-Turner AA-Turner added this to the 7.4.0 milestone Jul 13, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 14, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants