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

Support jinja2-3.0 #9162

Merged
merged 1 commit into from
May 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Release 4.1.0 (in development)
Dependencies
------------

* Support jinja2-3.0

Incompatible changes
--------------------

Expand Down
3 changes: 1 addition & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
'sphinxcontrib-htmlhelp',
'sphinxcontrib-serializinghtml',
'sphinxcontrib-qthelp',
'Jinja2>=2.3,<3.0',
'MarkupSafe<2.0',
'Jinja2>=2.3',
'Pygments>=2.0',
'docutils>=0.14,<0.18',
'snowballstemmer>=1.1',
Expand Down
13 changes: 9 additions & 4 deletions sphinx/jinja2glue.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from pprint import pformat
from typing import TYPE_CHECKING, Any, Callable, Dict, Iterator, List, Tuple, Union

from jinja2 import BaseLoader, FileSystemLoader, TemplateNotFound, contextfunction
from jinja2 import BaseLoader, FileSystemLoader, TemplateNotFound
from jinja2.environment import Environment
from jinja2.sandbox import SandboxedEnvironment
from jinja2.utils import open_if_exists
Expand All @@ -22,6 +22,11 @@
from sphinx.util import logging
from sphinx.util.osutil import mtimes_of_files

try:
from jinja2.utils import pass_context # type: ignore # jinja2-3.0 or above
except ImportError:
from jinja2 import contextfunction as pass_context

if TYPE_CHECKING:
from sphinx.builders import Builder

Expand Down Expand Up @@ -101,7 +106,7 @@ def __next__(self) -> int:
next = __next__ # Python 2/Jinja compatibility


@contextfunction
@pass_context
def warning(context: Dict, message: str, *args: Any, **kwargs: Any) -> str:
if 'pagename' in context:
filename = context.get('pagename') + context.get('file_suffix', '')
Expand Down Expand Up @@ -180,9 +185,9 @@ def init(self, builder: "Builder", theme: Theme = None, dirs: List[str] = None)
self.environment.filters['toint'] = _toint
self.environment.filters['todim'] = _todim
self.environment.filters['slice_index'] = _slice_index
self.environment.globals['debug'] = contextfunction(pformat)
self.environment.globals['debug'] = pass_context(pformat)
self.environment.globals['warning'] = warning
self.environment.globals['accesskey'] = contextfunction(accesskey)
self.environment.globals['accesskey'] = pass_context(accesskey)
self.environment.globals['idgen'] = idgen
if use_i18n:
self.environment.install_gettext_translations(builder.app.translator)
Expand Down
10 changes: 8 additions & 2 deletions sphinx/util/rst.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,17 @@
from docutils.parsers.rst.languages import en as english
from docutils.statemachine import StringList
from docutils.utils import Reporter
from jinja2 import Environment, environmentfilter
from jinja2 import Environment

from sphinx.locale import __
from sphinx.util import docutils, logging

try:
from jinja2.utils import pass_environment # type: ignore # jinja2-3.0 or above
except ImportError:
from jinja2 import environmentfilter as pass_environment


logger = logging.getLogger(__name__)

docinfo_re = re.compile(':\\w+:.*?')
Expand Down Expand Up @@ -51,7 +57,7 @@ def charwidth(char: str, widechars: str) -> int:
return sum(charwidth(c, widechars) for c in text)


@environmentfilter
@pass_environment
def heading(env: Environment, text: str, level: int = 1) -> str:
"""Create a heading for *level*."""
assert level <= 3
Expand Down