|
12 | 12 | from pprint import pformat
|
13 | 13 | from typing import TYPE_CHECKING, Any, Callable, Dict, Iterator, List, Tuple, Union
|
14 | 14 |
|
15 |
| -from jinja2 import BaseLoader, FileSystemLoader, TemplateNotFound, contextfunction |
| 15 | +from jinja2 import BaseLoader, FileSystemLoader, TemplateNotFound |
16 | 16 | from jinja2.environment import Environment
|
17 | 17 | from jinja2.sandbox import SandboxedEnvironment
|
18 | 18 | from jinja2.utils import open_if_exists
|
|
22 | 22 | from sphinx.util import logging
|
23 | 23 | from sphinx.util.osutil import mtimes_of_files
|
24 | 24 |
|
| 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 | + |
25 | 30 | if TYPE_CHECKING:
|
26 | 31 | from sphinx.builders import Builder
|
27 | 32 |
|
@@ -101,7 +106,7 @@ def __next__(self) -> int:
|
101 | 106 | next = __next__ # Python 2/Jinja compatibility
|
102 | 107 |
|
103 | 108 |
|
104 |
| -@contextfunction |
| 109 | +@pass_context |
105 | 110 | def warning(context: Dict, message: str, *args: Any, **kwargs: Any) -> str:
|
106 | 111 | if 'pagename' in context:
|
107 | 112 | filename = context.get('pagename') + context.get('file_suffix', '')
|
@@ -180,9 +185,9 @@ def init(self, builder: "Builder", theme: Theme = None, dirs: List[str] = None)
|
180 | 185 | self.environment.filters['toint'] = _toint
|
181 | 186 | self.environment.filters['todim'] = _todim
|
182 | 187 | self.environment.filters['slice_index'] = _slice_index
|
183 |
| - self.environment.globals['debug'] = contextfunction(pformat) |
| 188 | + self.environment.globals['debug'] = pass_context(pformat) |
184 | 189 | self.environment.globals['warning'] = warning
|
185 |
| - self.environment.globals['accesskey'] = contextfunction(accesskey) |
| 190 | + self.environment.globals['accesskey'] = pass_context(accesskey) |
186 | 191 | self.environment.globals['idgen'] = idgen
|
187 | 192 | if use_i18n:
|
188 | 193 | self.environment.install_gettext_translations(builder.app.translator)
|
|
0 commit comments