Skip to content

Commit e5c905c

Browse files
committed
Support jinja2-3.0
Since jinja2-3.0, some utility functions like contextfunction and environmentfilter are renamed to new name. This follows the updates to support jinja2-3.0 or above.
1 parent bb2c662 commit e5c905c

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

sphinx/jinja2glue.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from pprint import pformat
1313
from typing import TYPE_CHECKING, Any, Callable, Dict, Iterator, List, Tuple, Union
1414

15-
from jinja2 import BaseLoader, FileSystemLoader, TemplateNotFound, contextfunction
15+
from jinja2 import BaseLoader, FileSystemLoader, TemplateNotFound
1616
from jinja2.environment import Environment
1717
from jinja2.sandbox import SandboxedEnvironment
1818
from jinja2.utils import open_if_exists
@@ -22,6 +22,11 @@
2222
from sphinx.util import logging
2323
from sphinx.util.osutil import mtimes_of_files
2424

25+
try:
26+
from jinja2.utils import pass_context # type: ignore # jinja2-3.0 or above
27+
except ImportError:
28+
from jinja2 import contextfunction as pass_context
29+
2530
if TYPE_CHECKING:
2631
from sphinx.builders import Builder
2732

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

103108

104-
@contextfunction
109+
@pass_context
105110
def warning(context: Dict, message: str, *args: Any, **kwargs: Any) -> str:
106111
if 'pagename' in context:
107112
filename = context.get('pagename') + context.get('file_suffix', '')
@@ -180,9 +185,9 @@ def init(self, builder: "Builder", theme: Theme = None, dirs: List[str] = None)
180185
self.environment.filters['toint'] = _toint
181186
self.environment.filters['todim'] = _todim
182187
self.environment.filters['slice_index'] = _slice_index
183-
self.environment.globals['debug'] = contextfunction(pformat)
188+
self.environment.globals['debug'] = pass_context(pformat)
184189
self.environment.globals['warning'] = warning
185-
self.environment.globals['accesskey'] = contextfunction(accesskey)
190+
self.environment.globals['accesskey'] = pass_context(accesskey)
186191
self.environment.globals['idgen'] = idgen
187192
if use_i18n:
188193
self.environment.install_gettext_translations(builder.app.translator)

sphinx/util/rst.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,17 @@
1818
from docutils.parsers.rst.languages import en as english
1919
from docutils.statemachine import StringList
2020
from docutils.utils import Reporter
21-
from jinja2 import Environment, environmentfilter
21+
from jinja2 import Environment
2222

2323
from sphinx.locale import __
2424
from sphinx.util import docutils, logging
2525

26+
try:
27+
from jinja2.utils import pass_environment # type: ignore # jinja2-3.0 or above
28+
except ImportError:
29+
from jinja2 import environmentfilter as pass_environment
30+
31+
2632
logger = logging.getLogger(__name__)
2733

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

5359

54-
@environmentfilter
60+
@pass_environment
5561
def heading(env: Environment, text: str, level: int = 1) -> str:
5662
"""Create a heading for *level*."""
5763
assert level <= 3

0 commit comments

Comments
 (0)