]>
Commit | Line | Data |
---|---|---|
d1b48898 JB |
1 | From 85f58874ecd9feb391e24f9d0197bef5cc637a87 Mon Sep 17 00:00:00 2001 |
2 | From: Takeshi KOMIYA <i.tkomiya@gmail.com> | |
3 | Date: Mon, 3 May 2021 13:48:17 +0900 | |
4 | Subject: [PATCH] Support jinja2-3.0 | |
5 | ||
6 | Since jinja2-3.0, some utility functions like contextfunction and | |
7 | environmentfilter are renamed to new name. This follows the updates to | |
8 | support jinja2-3.0 or above. | |
9 | --- | |
10 | CHANGES | 2 ++ | |
11 | setup.py | 3 +-- | |
12 | sphinx/jinja2glue.py | 13 +++++++++---- | |
13 | sphinx/util/rst.py | 10 ++++++++-- | |
14 | 4 files changed, 20 insertions(+), 8 deletions(-) | |
15 | ||
16 | #diff --git a/CHANGES b/CHANGES | |
17 | #index ce70aeb83c..c9f0c4556c 100644 | |
18 | #--- a/CHANGES | |
19 | #+++ b/CHANGES | |
20 | #@@ -4,6 +4,8 @@ Release 4.1.0 (in development) | |
21 | # Dependencies | |
22 | # ------------ | |
23 | # | |
24 | #+* Support jinja2-3.0 | |
25 | #+ | |
26 | # Incompatible changes | |
27 | # -------------------- | |
28 | # | |
29 | #diff --git a/setup.py b/setup.py | |
30 | #index 7ce37f9eaf..b669afc00e 100644 | |
31 | #--- a/setup.py | |
32 | #+++ b/setup.py | |
33 | #@@ -21,8 +21,7 @@ | |
34 | # 'sphinxcontrib-htmlhelp', | |
35 | # 'sphinxcontrib-serializinghtml', | |
36 | # 'sphinxcontrib-qthelp', | |
37 | #- 'Jinja2>=2.3,<3.0', | |
38 | #- 'MarkupSafe<2.0', | |
39 | #+ 'Jinja2>=2.3', | |
40 | # 'Pygments>=2.0', | |
41 | # 'docutils>=0.14,<0.18', | |
42 | # 'snowballstemmer>=1.1', | |
43 | diff --git a/sphinx/jinja2glue.py b/sphinx/jinja2glue.py | |
44 | index c239f5a4a1..cd6e8066b9 100644 | |
45 | --- a/sphinx/jinja2glue.py | |
46 | +++ b/sphinx/jinja2glue.py | |
47 | @@ -12,7 +12,7 @@ from os import path | |
48 | from pprint import pformat | |
49 | from typing import Any, Callable, Dict, Iterator, List, Tuple, Union | |
50 | ||
51 | -from jinja2 import BaseLoader, FileSystemLoader, TemplateNotFound, contextfunction | |
52 | +from jinja2 import BaseLoader, FileSystemLoader, TemplateNotFound | |
53 | from jinja2.environment import Environment | |
54 | from jinja2.sandbox import SandboxedEnvironment | |
55 | from jinja2.utils import open_if_exists | |
56 | @@ -22,6 +22,11 @@ from sphinx.theming import Theme | |
57 | from sphinx.util import logging | |
58 | from sphinx.util.osutil import mtimes_of_files | |
59 | ||
60 | +try: | |
61 | + from jinja2.utils import pass_context # type: ignore # jinja2-3.0 or above | |
62 | +except ImportError: | |
63 | + from jinja2 import contextfunction as pass_context | |
64 | + | |
65 | if False: | |
66 | # For type annotation | |
67 | from sphinx.builders import Builder | |
68 | @@ -102,7 +107,7 @@ class idgen: | |
69 | next = __next__ # Python 2/Jinja compatibility | |
70 | ||
71 | ||
72 | -@contextfunction | |
73 | +@pass_context | |
74 | def warning(context: Dict, message: str, *args: Any, **kwargs: Any) -> str: | |
75 | if 'pagename' in context: | |
76 | filename = context.get('pagename') + context.get('file_suffix', '') | |
77 | @@ -181,9 +186,9 @@ class BuiltinTemplateLoader(TemplateBrid | |
78 | self.environment.filters['toint'] = _toint | |
79 | self.environment.filters['todim'] = _todim | |
80 | self.environment.filters['slice_index'] = _slice_index | |
81 | - self.environment.globals['debug'] = contextfunction(pformat) | |
82 | + self.environment.globals['debug'] = pass_context(pformat) | |
83 | self.environment.globals['warning'] = warning | |
84 | - self.environment.globals['accesskey'] = contextfunction(accesskey) | |
85 | + self.environment.globals['accesskey'] = pass_context(accesskey) | |
86 | self.environment.globals['idgen'] = idgen | |
87 | if use_i18n: | |
88 | self.environment.install_gettext_translations(builder.app.translator) | |
89 | diff --git a/sphinx/util/rst.py b/sphinx/util/rst.py | |
90 | index 82b3f6bdaa..8103a8b109 100644 | |
91 | --- a/sphinx/util/rst.py | |
92 | +++ b/sphinx/util/rst.py | |
93 | @@ -18,11 +18,17 @@ | |
94 | from docutils.parsers.rst.languages import en as english | |
95 | from docutils.statemachine import StringList | |
96 | from docutils.utils import Reporter | |
97 | -from jinja2 import Environment, environmentfilter | |
98 | +from jinja2 import Environment | |
99 | ||
100 | from sphinx.locale import __ | |
101 | from sphinx.util import docutils, logging | |
102 | ||
103 | +try: | |
104 | + from jinja2.utils import pass_environment # type: ignore # jinja2-3.0 or above | |
105 | +except ImportError: | |
106 | + from jinja2 import environmentfilter as pass_environment | |
107 | + | |
108 | + | |
109 | logger = logging.getLogger(__name__) | |
110 | ||
111 | docinfo_re = re.compile(':\\w+:.*?') | |
112 | @@ -51,7 +57,7 @@ def charwidth(char: str, widechars: str) -> int: | |
113 | return sum(charwidth(c, widechars) for c in text) | |
114 | ||
115 | ||
116 | -@environmentfilter | |
117 | +@pass_environment | |
118 | def heading(env: Environment, text: str, level: int = 1) -> str: | |
119 | """Create a heading for *level*.""" | |
120 | assert level <= 3 |