Skip to content

Commit

Permalink
Support jinja2-3.0
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tk0miya committed May 19, 2021
1 parent bb2c662 commit e5c905c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
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

0 comments on commit e5c905c

Please sign in to comment.