Skip to content
Myr

ext.py

This file

Path

src/jinja2/ext.py

844 lines · 30.4 KiB · Python

Last changed by

drop end of life python versions (#2096)David Lordbefore adoption49c48a0

History of this file

Take it

The walk stopped after 200 commits: lines older than c580dd0 point at it.

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

line 1why, one step back
1"""Extension API for adding custom tags and behavior."""

Merge branch '3.1.x'David Lordbefore adoptionde61312

line 2why, one step back
2

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 3–4why, one step back
3import pprint
4import re

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 5why, one step back
5import typing as t

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 6–8why, one step back
6
7from markupsafe import Markup8

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 9why, one step back
9from . import defaults

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 10–13why, one step back
10from . import nodes
11from .environment import Environment12from .exceptions import TemplateAssertionError13from .exceptions import TemplateSyntaxError

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

lines 14–16why, one step back
14from .runtime import concat # type: ignore
15from .runtime import Context16from .runtime import Undefined

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

line 17why, one step back
17from .utils import import_string

Merge pull request #1389 from pallets/unify-decoratorsDavid Lordbefore adoption39846a8

line 18why, one step back
18from .utils import pass_context

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

line 19why, one step back
19

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

lines 20–21why, one step back
20if t.TYPE_CHECKING:
21 import typing_extensions as te

Merge branch '3.1.x'David Lordbefore adoptionde61312

line 22why, one step back
22

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

lines 23–25why, one step back
23 from .lexer import Token
24 from .lexer import TokenStream25 from .parser import Parser

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

line 26why, one step back
26

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 27why, one step back
27 class _TranslationsBasic(te.Protocol):

Merge branch '3.1.x'David Lordbefore adoptionde61312

line 28why, one step back
28 def gettext(self, message: str) -> str: ...

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

line 29why, one step back
29

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

lines 30–31why, one step back
30 def ngettext(self, singular: str, plural: str, n: int) -> str:
31 pass

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

line 32why, one step back
32

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 33why, one step back
33 class _TranslationsContext(_TranslationsBasic):

Merge branch '3.1.x'David Lordbefore adoptionde61312

line 34why, one step back
34 def pgettext(self, context: str, message: str) -> str: ...

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

line 35why, one step back
35

Merge branch '3.1.x'David Lordbefore adoptionde61312

lines 36–38why, one step back
36 def npgettext(
37 self, context: str, singular: str, plural: str, n: int38 ) -> str: ...

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

line 39why, one step back
39

drop end of life python versions (#2096)David Lordbefore adoption49c48a0

line 40why, one step back
40 _SupportedTranslations = _TranslationsBasic | _TranslationsContext

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

lines 41–44why, one step back
41
4243# I18N functions available in Jinja templates. If the I18N library44# provides ugettext, it will be assigned to gettext.

Updated dependency management to uv (#2093)David Lordbefore adoption05f5d74

line 45why, one step back
45GETTEXT_FUNCTIONS: tuple[str, ...] = (

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

lines 46–55why, one step back
46 "_",
47 "gettext",48 "ngettext",49 "pgettext",50 "npgettext",51)52_ws_re = re.compile(r"\s*\n\s*")535455class Extension:

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 56–73why, one step back
56 """Extensions can be used to add extra functionality to the Jinja template
57 system at the parser level. Custom extensions are bound to an environment58 but may not store environment specific data on `self`. The reason for59 this is that an extension can be bound to another environment (for60 overlays) by creating a copy and reassigning the `environment` attribute.6162 As extensions are created by the environment they cannot accept any63 arguments for configuration. One may want to work around that by using64 a factory function, but that is not possible as extensions are identified65 by their import name. The correct way to configure the extension is66 storing the configuration values on the environment. Because this way the67 environment ends up acting as central configuration storage the68 attributes may clash which is why extensions have to ensure that the names69 they choose for configuration are not too generic. ``prefix`` for example70 is a terrible name, ``fragment_cache_prefix`` on the other hand is a good71 name as includes the name of the extension (fragment cache).72 """73

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

lines 74–78why, one step back
74 identifier: t.ClassVar[str]
7576 def __init_subclass__(cls) -> None:77 cls.identifier = f"{cls.__module__}.{cls.__name__}"78

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

line 79why, one step back
79 #: if this extension parses this is the list of tags it's listening to.

Updated dependency management to uv (#2093)David Lordbefore adoption05f5d74

line 80why, one step back
80 tags: set[str] = set()

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 81–88why, one step back
81
82 #: the priority of that extension. This is especially useful for83 #: extensions that preprocess values. A lower value means higher84 #: priority.85 #:86 #: .. versionadded:: 2.487 priority = 10088

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 89why, one step back
89 def __init__(self, environment: Environment) -> None:

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 90–91why, one step back
90 self.environment = environment
91

Merge branch 'stable'David Lordbefore adoption6aeab5d

line 92why, one step back
92 def bind(self, environment: Environment) -> "te.Self":

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

line 93why, one step back
93 """Create a copy of this extension bound to another environment."""

Merge branch '3.0.x'David Lordbefore adoption896a621

line 94why, one step back
94 rv = object.__new__(self.__class__)

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 95–98why, one step back
95 rv.__dict__.update(self.__dict__)
96 rv.environment = environment97 return rv98

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 99why, one step back
99 def preprocess(

drop end of life python versions (#2096)David Lordbefore adoption49c48a0

line 100why, one step back
100 self, source: str, name: str | None, filename: str | None = None

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 101why, one step back
101 ) -> str:

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 102–107why, one step back
102 """This method is called before the actual lexing and can be used to
103 preprocess the source. The `filename` is optional. The return value104 must be the preprocessed source.105 """106 return source107

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

lines 108–110why, one step back
108 def filter_stream(
109 self, stream: "TokenStream"110 ) -> t.Union["TokenStream", t.Iterable["Token"]]:

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 111–117why, one step back
111 """It's passed a :class:`~jinja2.lexer.TokenStream` that can be used
112 to filter tokens returned. This method has to return an iterable of113 :class:`~jinja2.lexer.Token`\\s, but it doesn't have to return a114 :class:`~jinja2.lexer.TokenStream`.115 """116 return stream117

drop end of life python versions (#2096)David Lordbefore adoption49c48a0

line 118why, one step back
118 def parse(self, parser: "Parser") -> nodes.Node | list[nodes.Node]:

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 119–125why, one step back
119 """If any of the :attr:`tags` matched this method is called with the
120 parser as first argument. The token the parser stream is pointing at121 is the name token that matched. This method has to return one or a122 list of multiple nodes.123 """124 raise NotImplementedError()125

drop end of life python versions (#2096)David Lordbefore adoption49c48a0

line 126why, one step back
126 def attr(self, name: str, lineno: int | None = None) -> nodes.ExtensionAttribute:

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 127–136why, one step back
127 """Return an attribute node for the current extension. This is useful
128 to pass constants on extensions to generated template code.129130 ::131132 self.attr('_my_attribute', lineno=lineno)133 """134 return nodes.ExtensionAttribute(self.identifier, name, lineno=lineno)135136 def call_method(

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

lines 137–138why, one step back
137 self,
138 name: str,

drop end of life python versions (#2096)David Lordbefore adoption49c48a0

lines 139–143why, one step back
139 args: list[nodes.Expr] | None = None,
140 kwargs: list[nodes.Keyword] | None = None,141 dyn_args: nodes.Expr | None = None,142 dyn_kwargs: nodes.Expr | None = None,143 lineno: int | None = None,

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 144why, one step back
144 ) -> nodes.Call:

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 145–161why, one step back
145 """Call a method of the extension. This is a shortcut for
146 :meth:`attr` + :class:`jinja2.nodes.Call`.147 """148 if args is None:149 args = []150 if kwargs is None:151 kwargs = []152 return nodes.Call(153 self.attr(name, lineno=lineno),154 args,155 kwargs,156 dyn_args,157 dyn_kwargs,158 lineno=lineno,159 )160161

Merge pull request #1389 from pallets/unify-decoratorsDavid Lordbefore adoption39846a8

line 162why, one step back
162@pass_context

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

lines 163–164why, one step back
163def _gettext_alias(
164 __context: Context, *args: t.Any, **kwargs: t.Any

drop end of life python versions (#2096)David Lordbefore adoption49c48a0

line 165why, one step back
165) -> t.Any | Undefined:

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 166–168why, one step back
166 return __context.call(__context.resolve("gettext"), *args, **kwargs)
167168

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 169why, one step back
169def _make_new_gettext(func: t.Callable[[str], str]) -> t.Callable[..., str]:

Merge pull request #1389 from pallets/unify-decoratorsDavid Lordbefore adoption39846a8

line 170why, one step back
170 @pass_context

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 171why, one step back
171 def gettext(__context: Context, __string: str, **variables: t.Any) -> str:

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 172–177why, one step back
172 rv = __context.call(func, __string)
173 if __context.eval_ctx.autoescape:174 rv = Markup(rv)175 # Always treat as a format string, even if there are no176 # variables. This makes translation strings more consistent177 # and predictable. This requires escaping

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 178why, one step back
178 return rv % variables # type: ignore

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 179–182why, one step back
179
180 return gettext181182

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 183why, one step back
183def _make_new_ngettext(func: t.Callable[[str, str, int], str]) -> t.Callable[..., str]:

Merge pull request #1389 from pallets/unify-decoratorsDavid Lordbefore adoption39846a8

line 184why, one step back
184 @pass_context

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

lines 185–191why, one step back
185 def ngettext(
186 __context: Context,187 __singular: str,188 __plural: str,189 __num: int,190 **variables: t.Any,191 ) -> str:

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 192–196why, one step back
192 variables.setdefault("num", __num)
193 rv = __context.call(func, __singular, __plural, __num)194 if __context.eval_ctx.autoescape:195 rv = Markup(rv)196 # Always treat as a format string, see gettext comment above.

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 197why, one step back
197 return rv % variables # type: ignore

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 198–201why, one step back
198
199 return ngettext200201

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 202why, one step back
202def _make_new_pgettext(func: t.Callable[[str, str], str]) -> t.Callable[..., str]:

Merge pull request #1389 from pallets/unify-decoratorsDavid Lordbefore adoption39846a8

line 203why, one step back
203 @pass_context

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

lines 204–206why, one step back
204 def pgettext(
205 __context: Context, __string_ctx: str, __string: str, **variables: t.Any206 ) -> str:

Merge pull request #1126 from imomaliev/pgettext-supportAdrianbefore adoption43d4228

lines 207–213why, one step back
207 variables.setdefault("context", __string_ctx)
208 rv = __context.call(func, __string_ctx, __string)209210 if __context.eval_ctx.autoescape:211 rv = Markup(rv)212213 # Always treat as a format string, see gettext comment above.

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 214why, one step back
214 return rv % variables # type: ignore

Merge pull request #1126 from imomaliev/pgettext-supportAdrianbefore adoption43d4228

lines 215–218why, one step back
215
216 return pgettext217218

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 219why, one step back
219def _make_new_npgettext(

Merge branch '3.1.x'David Lordbefore adoptionde61312

line 220why, one step back
220 func: t.Callable[[str, str, str, int], str],

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 221why, one step back
221) -> t.Callable[..., str]:

Merge pull request #1389 from pallets/unify-decoratorsDavid Lordbefore adoption39846a8

line 222why, one step back
222 @pass_context

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

lines 223–230why, one step back
223 def npgettext(
224 __context: Context,225 __string_ctx: str,226 __singular: str,227 __plural: str,228 __num: int,229 **variables: t.Any,230 ) -> str:

Merge pull request #1126 from imomaliev/pgettext-supportAdrianbefore adoption43d4228

lines 231–238why, one step back
231 variables.setdefault("context", __string_ctx)
232 variables.setdefault("num", __num)233 rv = __context.call(func, __string_ctx, __singular, __plural, __num)234235 if __context.eval_ctx.autoescape:236 rv = Markup(rv)237238 # Always treat as a format string, see gettext comment above.

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 239why, one step back
239 return rv % variables # type: ignore

Merge pull request #1126 from imomaliev/pgettext-supportAdrianbefore adoption43d4228

lines 240–243why, one step back
240
241 return npgettext242243

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 244–255why, one step back
244class InternationalizationExtension(Extension):
245 """This extension adds gettext support to Jinja."""246247 tags = {"trans"}248249 # TODO: the i18n extension is currently reevaluating values in a few250 # situations. Take this example:251 # {% trans count=something() %}{{ count }} foo{% pluralize252 # %}{{ count }} fooss{% endtrans %}253 # something is called twice here. One time for the gettext value and254 # the other time for the n-parameter of the ngettext function.255

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

lines 256–257why, one step back
256 def __init__(self, environment: Environment) -> None:
257 super().__init__(environment)

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 258–267why, one step back
258 environment.globals["_"] = _gettext_alias
259 environment.extend(260 install_gettext_translations=self._install,261 install_null_translations=self._install_null,262 install_gettext_callables=self._install_callables,263 uninstall_gettext_translations=self._uninstall,264 extract_translations=self._extract,265 newstyle_gettext=False,266 )267

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 268why, one step back
268 def _install(

drop end of life python versions (#2096)David Lordbefore adoption49c48a0

line 269why, one step back
269 self, translations: "_SupportedTranslations", newstyle: bool | None = None

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 270why, one step back
270 ) -> None:

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 271–279why, one step back
271 # ugettext and ungettext are preferred in case the I18N library
272 # is providing compatibility with older Python versions.273 gettext = getattr(translations, "ugettext", None)274 if gettext is None:275 gettext = translations.gettext276 ngettext = getattr(translations, "ungettext", None)277 if ngettext is None:278 ngettext = translations.ngettext279

Merge pull request #1126 from imomaliev/pgettext-supportAdrianbefore adoption43d4228

lines 280–285why, one step back
280 pgettext = getattr(translations, "pgettext", None)
281 npgettext = getattr(translations, "npgettext", None)282 self._install_callables(283 gettext, ngettext, newstyle=newstyle, pgettext=pgettext, npgettext=npgettext284 )285

drop end of life python versions (#2096)David Lordbefore adoption49c48a0

line 286why, one step back
286 def _install_null(self, newstyle: bool | None = None) -> None:

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

lines 287–289why, one step back
287 import gettext
288289 translations = gettext.NullTranslations()

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

line 290why, one step back
290 self._install_callables(

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

lines 291–292why, one step back
291 gettext=translations.gettext,
292 ngettext=translations.ngettext,

Merge pull request #1126 from imomaliev/pgettext-supportAdrianbefore adoption43d4228

line 293why, one step back
293 newstyle=newstyle,

drop support for Python 3.7David Lordbefore adoptionfcd3d3b

lines 294–295why, one step back
294 pgettext=translations.pgettext,
295 npgettext=translations.npgettext,

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 296–297why, one step back
296 )
297

Merge pull request #1126 from imomaliev/pgettext-supportAdrianbefore adoption43d4228

line 298why, one step back
298 def _install_callables(

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

lines 299–301why, one step back
299 self,
300 gettext: t.Callable[[str], str],301 ngettext: t.Callable[[str, str, int], str],

drop end of life python versions (#2096)David Lordbefore adoption49c48a0

lines 302–304why, one step back
302 newstyle: bool | None = None,
303 pgettext: t.Callable[[str, str], str] | None = None,304 npgettext: t.Callable[[str, str, str, int], str] | None = None,

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 305why, one step back
305 ) -> None:

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

line 306why, one step back
306 if newstyle is not None:

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

lines 307–308why, one step back
307 self.environment.newstyle_gettext = newstyle # type: ignore
308 if self.environment.newstyle_gettext: # type: ignore

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 309–311why, one step back
309 gettext = _make_new_gettext(gettext)
310 ngettext = _make_new_ngettext(ngettext)311

Merge pull request #1126 from imomaliev/pgettext-supportAdrianbefore adoption43d4228

lines 312–321why, one step back
312 if pgettext is not None:
313 pgettext = _make_new_pgettext(pgettext)314315 if npgettext is not None:316 npgettext = _make_new_npgettext(npgettext)317318 self.environment.globals.update(319 gettext=gettext, ngettext=ngettext, pgettext=pgettext, npgettext=npgettext320 )321

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 322why, one step back
322 def _uninstall(self, translations: "_SupportedTranslations") -> None:

Merge pull request #1126 from imomaliev/pgettext-supportAdrianbefore adoption43d4228

line 323why, one step back
323 for key in ("gettext", "ngettext", "pgettext", "npgettext"):

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 324–325why, one step back
324 self.environment.globals.pop(key, None)
325

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

lines 326–327why, one step back
326 def _extract(
327 self,

drop end of life python versions (#2096)David Lordbefore adoption49c48a0

line 328why, one step back
328 source: str | nodes.Template,

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 329why, one step back
329 gettext_functions: t.Sequence[str] = GETTEXT_FUNCTIONS,

drop end of life python versions (#2096)David Lordbefore adoption49c48a0

line 330why, one step back
330 ) -> t.Iterator[tuple[int, str, str | None | tuple[str | None, ...]]]:

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 331–334why, one step back
331 if isinstance(source, str):
332 source = self.environment.parse(source)333 return extract_from_ast(source, gettext_functions)334

drop end of life python versions (#2096)David Lordbefore adoption49c48a0

line 335why, one step back
335 def parse(self, parser: "Parser") -> nodes.Node | list[nodes.Node]:

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 336–338why, one step back
336 """Parse a translatable tag."""
337 lineno = next(parser.stream).lineno338

Merge pull request #1559 from pallets/i18n-context-blockDavid Lordbefore adoption4f5c79c

lines 339–344why, one step back
339 context = None
340 context_token = parser.stream.next_if("string")341342 if context_token is not None:343 context = context_token.value344

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 345–347why, one step back
345 # find all the variables referenced. Additionally a variable can be
346 # defined in the body of the trans block too, but this is checked at347 # a later state.

drop end of life python versions (#2096)David Lordbefore adoption49c48a0

lines 348–349why, one step back
348 plural_expr: nodes.Expr | None = None
349 plural_expr_assignment: nodes.Assign | None = None

Merge pull request #1559 from pallets/i18n-context-blockDavid Lordbefore adoption4f5c79c

line 350why, one step back
350 num_called_num = False

Updated dependency management to uv (#2093)David Lordbefore adoption05f5d74

line 351why, one step back
351 variables: dict[str, nodes.Expr] = {}

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 352–360why, one step back
352 trimmed = None
353 while parser.stream.current.type != "block_end":354 if variables:355 parser.stream.expect("comma")356357 # skip colon for python compatibility358 if parser.stream.skip_if("colon"):359 break360

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

lines 361–362why, one step back
361 token = parser.stream.expect("name")
362 if token.value in variables:

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

line 363why, one step back
363 parser.fail(

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

lines 364–365why, one step back
364 f"translatable variable {token.value!r} defined twice.",
365 token.lineno,

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 366–371why, one step back
366 exc=TemplateAssertionError,
367 )368369 # expressions370 if parser.stream.current.type == "assign":371 next(parser.stream)

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

lines 372–374why, one step back
372 variables[token.value] = var = parser.parse_expression()
373 elif trimmed is None and token.value in ("trimmed", "notrimmed"):374 trimmed = token.value == "trimmed"

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 375–376why, one step back
375 continue
376 else:

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 377why, one step back
377 variables[token.value] = var = nodes.Name(token.value, "load")

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 378–381why, one step back
378
379 if plural_expr is None:380 if isinstance(var, nodes.Call):381 plural_expr = nodes.Name("_trans", "load")

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 382why, one step back
382 variables[token.value] = plural_expr

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 383–387why, one step back
383 plural_expr_assignment = nodes.Assign(
384 nodes.Name("_trans", "store"), var385 )386 else:387 plural_expr = var

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 388why, one step back
388 num_called_num = token.value == "num"

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 389–408why, one step back
389
390 parser.stream.expect("block_end")391392 plural = None393 have_plural = False394 referenced = set()395396 # now parse until endtrans or pluralize397 singular_names, singular = self._parse_block(parser, True)398 if singular_names:399 referenced.update(singular_names)400 if plural_expr is None:401 plural_expr = nodes.Name(singular_names[0], "load")402 num_called_num = singular_names[0] == "num"403404 # if we have a pluralize block, we parse that too405 if parser.stream.current.test("name:pluralize"):406 have_plural = True407 next(parser.stream)408 if parser.stream.current.type != "block_end":

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

lines 409–410why, one step back
409 token = parser.stream.expect("name")
410 if token.value not in variables:

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

line 411why, one step back
411 parser.fail(

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

lines 412–413why, one step back
412 f"unknown variable {token.value!r} for pluralization",
413 token.lineno,

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 414–415why, one step back
414 exc=TemplateAssertionError,
415 )

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

lines 416–417why, one step back
416 plural_expr = variables[token.value]
417 num_called_num = token.value == "num"

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 418–425why, one step back
418 parser.stream.expect("block_end")
419 plural_names, plural = self._parse_block(parser, False)420 next(parser.stream)421 referenced.update(plural_names)422 else:423 next(parser.stream)424425 # register free names as simple name expressions

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

lines 426–428why, one step back
426 for name in referenced:
427 if name not in variables:428 variables[name] = nodes.Name(name, "load")

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 429–444why, one step back
429
430 if not have_plural:431 plural_expr = None432 elif plural_expr is None:433 parser.fail("pluralize without variables", lineno)434435 if trimmed is None:436 trimmed = self.environment.policies["ext.i18n.trimmed"]437 if trimmed:438 singular = self._trim_whitespace(singular)439 if plural:440 plural = self._trim_whitespace(plural)441442 node = self._make_node(443 singular,444 plural,

Merge pull request #1559 from pallets/i18n-context-blockDavid Lordbefore adoption4f5c79c

line 445why, one step back
445 context,

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 446–456why, one step back
446 variables,
447 plural_expr,448 bool(referenced),449 num_called_num and have_plural,450 )451 node.set_lineno(lineno)452 if plural_expr_assignment is not None:453 return [plural_expr_assignment, node]454 else:455 return node456

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 457why, one step back
457 def _trim_whitespace(self, string: str, _ws_re: t.Pattern[str] = _ws_re) -> str:

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 458–459why, one step back
458 return _ws_re.sub(" ", string.strip())
459

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

lines 460–461why, one step back
460 def _parse_block(
461 self, parser: "Parser", allow_pluralize: bool

Updated dependency management to uv (#2093)David Lordbefore adoption05f5d74

line 462why, one step back
462 ) -> tuple[list[str], str]:

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 463–465why, one step back
463 """Parse until the next block tag with a given name."""
464 referenced = []465 buf = []

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

lines 466–467why, one step back
466
467 while True:

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 468–478why, one step back
468 if parser.stream.current.type == "data":
469 buf.append(parser.stream.current.value.replace("%", "%%"))470 next(parser.stream)471 elif parser.stream.current.type == "variable_begin":472 next(parser.stream)473 name = parser.stream.expect("name").value474 referenced.append(name)475 buf.append(f"%({name})s")476 parser.stream.expect("variable_end")477 elif parser.stream.current.type == "block_begin":478 next(parser.stream)

Merge branch '3.1.x'David Lordbefore adoption3fd91e4

lines 479–484why, one step back
479 block_name = (
480 parser.stream.current.value481 if parser.stream.current.type == "name"482 else None483 )484 if block_name == "endtrans":

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

line 485why, one step back
485 break

Merge branch '3.1.x'David Lordbefore adoption3fd91e4

line 486why, one step back
486 elif block_name == "pluralize":

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 487–491why, one step back
487 if allow_pluralize:
488 break489 parser.fail(490 "a translatable section can have only one pluralize section"491 )

Merge branch '3.1.x'David Lordbefore adoption3fd91e4

lines 492–495why, one step back
492 elif block_name == "trans":
493 parser.fail(494 "trans blocks can't be nested; did you mean `endtrans`?"495 )

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

line 496why, one step back
496 parser.fail(

Merge branch '3.1.x'David Lordbefore adoption3fd91e4

lines 497–498why, one step back
497 f"control structures in translatable sections are not allowed; "
498 f"saw `{block_name}`"

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 499–507why, one step back
499 )
500 elif parser.stream.eos:501 parser.fail("unclosed translation block")502 else:503 raise RuntimeError("internal parser error")504505 return referenced, concat(buf)506507 def _make_node(

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

lines 508–509why, one step back
508 self,
509 singular: str,

drop end of life python versions (#2096)David Lordbefore adoption49c48a0

lines 510–511why, one step back
510 plural: str | None,
511 context: str | None,

Updated dependency management to uv (#2093)David Lordbefore adoption05f5d74

line 512why, one step back
512 variables: dict[str, nodes.Expr],

drop end of life python versions (#2096)David Lordbefore adoption49c48a0

line 513why, one step back
513 plural_expr: nodes.Expr | None,

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

lines 514–516why, one step back
514 vars_referenced: bool,
515 num_called_num: bool,516 ) -> nodes.Output:

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

line 517why, one step back
517 """Generates a useful node from the data provided."""

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

lines 518–520why, one step back
518 newstyle = self.environment.newstyle_gettext # type: ignore
519 node: nodes.Expr520

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 521–522why, one step back
521 # no variables referenced? no need to escape for old style
522 # gettext invocations only if there are vars.

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 523why, one step back
523 if not vars_referenced and not newstyle:

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 524–527why, one step back
524 singular = singular.replace("%%", "%")
525 if plural:526 plural = plural.replace("%%", "%")527

Merge pull request #1559 from pallets/i18n-context-blockDavid Lordbefore adoption4f5c79c

line 528why, one step back
528 func_name = "gettext"

Updated dependency management to uv (#2093)David Lordbefore adoption05f5d74

line 529why, one step back
529 func_args: list[nodes.Expr] = [nodes.Const(singular)]

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

line 530why, one step back
530

Merge pull request #1559 from pallets/i18n-context-blockDavid Lordbefore adoption4f5c79c

lines 531–539why, one step back
531 if context is not None:
532 func_args.insert(0, nodes.Const(context))533 func_name = f"p{func_name}"534535 if plural_expr is not None:536 func_name = f"n{func_name}"537 func_args.extend((nodes.Const(plural), plural_expr))538539 node = nodes.Call(nodes.Name(func_name, "load"), func_args, [], None, None)

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 540–543why, one step back
540
541 # in case newstyle gettext is used, the method is powerful542 # enough to handle the variable expansion and autoescape543 # handling itself

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 544why, one step back
544 if newstyle:

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 545–576why, one step back
545 for key, value in variables.items():
546 # the function adds that later anyways in case num was547 # called num, so just skip it.548 if num_called_num and key == "num":549 continue550 node.kwargs.append(nodes.Keyword(key, value))551552 # otherwise do that here553 else:554 # mark the return value as safe if we are in an555 # environment with autoescaping turned on556 node = nodes.MarkSafeIfAutoescape(node)557 if variables:558 node = nodes.Mod(559 node,560 nodes.Dict(561 [562 nodes.Pair(nodes.Const(key), value)563 for key, value in variables.items()564 ]565 ),566 )567 return nodes.Output([node])568569570class ExprStmtExtension(Extension):571 """Adds a `do` tag to Jinja that works like the print statement just572 that it doesn't print the return value.573 """574575 tags = {"do"}576

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 577why, one step back
577 def parse(self, parser: "Parser") -> nodes.ExprStmt:

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 578–587why, one step back
578 node = nodes.ExprStmt(lineno=next(parser.stream).lineno)
579 node.node = parser.parse_tuple()580 return node581582583class LoopControlExtension(Extension):584 """Adds break and continue to the template engine."""585586 tags = {"break", "continue"}587

drop end of life python versions (#2096)David Lordbefore adoption49c48a0

line 588why, one step back
588 def parse(self, parser: "Parser") -> nodes.Break | nodes.Continue:

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 589–617why, one step back
589 token = next(parser.stream)
590 if token.value == "break":591 return nodes.Break(lineno=token.lineno)592 return nodes.Continue(lineno=token.lineno)593594595class DebugExtension(Extension):596 """A ``{% debug %}`` tag that dumps the available variables,597 filters, and tests.598599 .. code-block:: html+jinja600601 <pre>{% debug %}</pre>602603 .. code-block:: text604605 {'context': {'cycler': <class 'jinja2.utils.Cycler'>,606 ...,607 'namespace': <class 'jinja2.utils.Namespace'>},608 'filters': ['abs', 'attr', 'batch', 'capitalize', 'center', 'count', 'd',609 ..., 'urlencode', 'urlize', 'wordcount', 'wordwrap', 'xmlattr'],610 'tests': ['!=', '<', '<=', '==', '>', '>=', 'callable', 'defined',611 ..., 'odd', 'sameas', 'sequence', 'string', 'undefined', 'upper']}612613 .. versionadded:: 2.11.0614 """615616 tags = {"debug"}617

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 618why, one step back
618 def parse(self, parser: "Parser") -> nodes.Output:

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

line 619why, one step back
619 lineno = parser.stream.expect("name:debug").lineno

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 620why, one step back
620 context = nodes.ContextReference()

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 621–623why, one step back
621 result = self.call_method("_render", [context], lineno=lineno)
622 return nodes.Output([result], lineno=lineno)623

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 624why, one step back
624 def _render(self, context: Context) -> str:

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 625–631why, one step back
625 result = {
626 "context": context.get_all(),627 "filters": sorted(self.environment.filters.keys()),628 "tests": sorted(self.environment.tests.keys()),629 }630631 # Set the depth since the intent is to show the top few names.

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 632why, one step back
632 return pprint.pformat(result, depth=3, compact=True)

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 633–634why, one step back
633
634

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

lines 635–638why, one step back
635def extract_from_ast(
636 ast: nodes.Template,637 gettext_functions: t.Sequence[str] = GETTEXT_FUNCTIONS,638 babel_style: bool = True,

drop end of life python versions (#2096)David Lordbefore adoption49c48a0

line 639why, one step back
639) -> t.Iterator[tuple[int, str, str | None | tuple[str | None, ...]]]:

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 640–673why, one step back
640 """Extract localizable strings from the given template node. Per
641 default this function returns matches in babel style that means non string642 parameters as well as keyword arguments are returned as `None`. This643 allows Babel to figure out what you really meant if you are using644 gettext functions that allow keyword arguments for placeholder expansion.645 If you don't want that behavior set the `babel_style` parameter to `False`646 which causes only strings to be returned and parameters are always stored647 in tuples. As a consequence invalid gettext calls (calls without a single648 string parameter or string parameters after non-string parameters) are649 skipped.650651 This example explains the behavior:652653 >>> from jinja2 import Environment654 >>> env = Environment()655 >>> node = env.parse('{{ (_("foo"), _(), ngettext("foo", "bar", 42)) }}')656 >>> list(extract_from_ast(node))657 [(1, '_', 'foo'), (1, '_', ()), (1, 'ngettext', ('foo', 'bar', None))]658 >>> list(extract_from_ast(node, babel_style=False))659 [(1, '_', ('foo',)), (1, 'ngettext', ('foo', 'bar'))]660661 For every string found this function yields a ``(lineno, function,662 message)`` tuple, where:663664 * ``lineno`` is the number of the line on which the string was found,665 * ``function`` is the name of the ``gettext`` function used (if the666 string was extracted from embedded Python code), and667 * ``message`` is the string, or a tuple of strings for functions668 with multiple string arguments.669670 This extraction function operates on the AST and is because of that unable671 to extract any comments. For comment support you have to use the babel672 extraction interface or extract comments yourself.673 """

drop end of life python versions (#2096)David Lordbefore adoption49c48a0

line 674why, one step back
674 out: str | None | tuple[str | None, ...]

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

lines 675–676why, one step back
675
676 for node in ast.find_all(nodes.Call):

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 677–682why, one step back
677 if (
678 not isinstance(node.node, nodes.Name)679 or node.node.name not in gettext_functions680 ):681 continue682

drop end of life python versions (#2096)David Lordbefore adoption49c48a0

line 683why, one step back
683 strings: list[str | None] = []

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 684why, one step back
684

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 685–698why, one step back
685 for arg in node.args:
686 if isinstance(arg, nodes.Const) and isinstance(arg.value, str):687 strings.append(arg.value)688 else:689 strings.append(None)690691 for _ in node.kwargs:692 strings.append(None)693 if node.dyn_args is not None:694 strings.append(None)695 if node.dyn_kwargs is not None:696 strings.append(None)697698 if not babel_style:

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

lines 699–701why, one step back
699 out = tuple(x for x in strings if x is not None)
700701 if not out:

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 702–704why, one step back
702 continue
703 else:704 if len(strings) == 1:

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 705why, one step back
705 out = strings[0]

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

line 706why, one step back
706 else:

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 707why, one step back
707 out = tuple(strings)

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

line 708why, one step back
708

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 709why, one step back
709 yield node.lineno, node.node.name, out

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

line 710why, one step back
710

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 711why, one step back
711

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 712–718why, one step back
712class _CommentFinder:
713 """Helper class to find comments in a token stream. Can only714 find comments for gettext calls forwards. Once the comment715 from line 4 is found, a comment for line 1 will not return a716 usable value.717 """718

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 719why, one step back
719 def __init__(

Updated dependency management to uv (#2093)David Lordbefore adoption05f5d74

line 720why, one step back
720 self, tokens: t.Sequence[tuple[int, str, str]], comment_tags: t.Sequence[str]

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 721why, one step back
721 ) -> None:

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 722–726why, one step back
722 self.tokens = tokens
723 self.comment_tags = comment_tags724 self.offset = 0725 self.last_lineno = 0726

Updated dependency management to uv (#2093)David Lordbefore adoption05f5d74

line 727why, one step back
727 def find_backwards(self, offset: int) -> list[str]:

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 728–742why, one step back
728 try:
729 for _, token_type, token_value in reversed(730 self.tokens[self.offset : offset]731 ):732 if token_type in ("comment", "linecomment"):733 try:734 prefix, comment = token_value.split(None, 1)735 except ValueError:736 continue737 if prefix in self.comment_tags:738 return [comment.rstrip()]739 return []740 finally:741 self.offset = offset742

Updated dependency management to uv (#2093)David Lordbefore adoption05f5d74

line 743why, one step back
743 def find_comments(self, lineno: int) -> list[str]:

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 744–751why, one step back
744 if not self.comment_tags or self.last_lineno > lineno:
745 return []746 for idx, (token_lineno, _, _) in enumerate(self.tokens[self.offset :]):747 if token_lineno > lineno:748 return self.find_backwards(self.offset + idx)749 return self.find_backwards(len(self.tokens))750751

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

lines 752–755why, one step back
752def babel_extract(
753 fileobj: t.BinaryIO,754 keywords: t.Sequence[str],755 comment_tags: t.Sequence[str],

Updated dependency management to uv (#2093)David Lordbefore adoption05f5d74

line 756why, one step back
756 options: dict[str, t.Any],

drop end of life python versions (#2096)David Lordbefore adoption49c48a0

line 757why, one step back
757) -> t.Iterator[tuple[int, str, str | None | tuple[str | None, ...], list[str]]]:

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 758–784why, one step back
758 """Babel extraction method for Jinja templates.
759760 .. versionchanged:: 2.3761 Basic support for translation comments was added. If `comment_tags`762 is now set to a list of keywords for extraction, the extractor will763 try to find the best preceding comment that begins with one of the764 keywords. For best results, make sure to not have more than one765 gettext call in one line of code and the matching comment in the766 same line or the line before.767768 .. versionchanged:: 2.5.1769 The `newstyle_gettext` flag can be set to `True` to enable newstyle770 gettext calls.771772 .. versionchanged:: 2.7773 A `silent` option can now be provided. If set to `False` template774 syntax errors are propagated instead of being ignored.775776 :param fileobj: the file-like object the messages should be extracted from777 :param keywords: a list of keywords (i.e. function names) that should be778 recognized as translation functions779 :param comment_tags: a list of translator tags to search for and include780 in the results.781 :param options: a dictionary of additional options (optional)782 :return: an iterator over ``(lineno, funcname, message, comments)`` tuples.783 (comments will be empty currently)784 """

Updated dependency management to uv (#2093)David Lordbefore adoption05f5d74

line 785why, one step back
785 extensions: dict[type[Extension], None] = {}

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

lines 786–790why, one step back
786
787 for extension_name in options.get("extensions", "").split(","):788 extension_name = extension_name.strip()789790 if not extension_name:

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

line 791why, one step back
791 continue

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

lines 792–794why, one step back
792
793 extensions[import_string(extension_name)] = None794

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

line 795why, one step back
795 if InternationalizationExtension not in extensions:

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 796why, one step back
796 extensions[InternationalizationExtension] = None

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

line 797why, one step back
797

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

lines 798–799why, one step back
798 def getbool(options: t.Mapping[str, str], key: str, default: bool = False) -> bool:
799 return options.get(key, str(default)).lower() in {"1", "on", "yes", "true"}

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 800–802why, one step back
800
801 silent = getbool(options, "silent", True)802 environment = Environment(

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

lines 803–815why, one step back
803 options.get("block_start_string", defaults.BLOCK_START_STRING),
804 options.get("block_end_string", defaults.BLOCK_END_STRING),805 options.get("variable_start_string", defaults.VARIABLE_START_STRING),806 options.get("variable_end_string", defaults.VARIABLE_END_STRING),807 options.get("comment_start_string", defaults.COMMENT_START_STRING),808 options.get("comment_end_string", defaults.COMMENT_END_STRING),809 options.get("line_statement_prefix") or defaults.LINE_STATEMENT_PREFIX,810 options.get("line_comment_prefix") or defaults.LINE_COMMENT_PREFIX,811 getbool(options, "trim_blocks", defaults.TRIM_BLOCKS),812 getbool(options, "lstrip_blocks", defaults.LSTRIP_BLOCKS),813 defaults.NEWLINE_SEQUENCE,814 getbool(options, "keep_trailing_newline", defaults.KEEP_TRAILING_NEWLINE),815 tuple(extensions),

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 816–822why, one step back
816 cache_size=0,
817 auto_reload=False,818 )819820 if getbool(options, "trimmed"):821 environment.policies["ext.i18n.trimmed"] = True822 if getbool(options, "newstyle_gettext"):

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 823why, one step back
823 environment.newstyle_gettext = True # type: ignore

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 824–844why, one step back
824
825 source = fileobj.read().decode(options.get("encoding", "utf-8"))826 try:827 node = environment.parse(source)828 tokens = list(environment.lex(environment.preprocess(source)))829 except TemplateSyntaxError:830 if not silent:831 raise832 # skip templates with syntax errors833 return834835 finder = _CommentFinder(tokens, comment_tags)836 for lineno, func, message in extract_from_ast(node, keywords):837 yield lineno, func, message, finder.find_comments(lineno)838839840#: nicer import names841i18n = InternationalizationExtension842do = ExprStmtExtension843loopcontrols = LoopControlExtension844debug = DebugExtension