-
Notifications
You must be signed in to change notification settings - Fork 0
/
local_extensions.py
35 lines (27 loc) · 1.07 KB
/
local_extensions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
"""
Copy-pasted from (under MIT license):
https://github.com/dusktreader/cut-out-cookies/tree/main
"""
import jinja2
import jinja2.ext
STENCIL_PATH_PREFIX = "OBSCURATA_LAMINA_INTERRASILIS--"
class Stencil(jinja2.ext.Extension):
counter = 0
def __init__(self, environment, *args, **kwargs):
@jinja2.pass_context
def stencil(ctx, value, pattern):
cookiecutter_config = ctx.get("cookiecutter", {})
included = cookiecutter_config.get(f"__include_{pattern}", None)
if isinstance(included, str):
included = included.lower() == "true"
return value if included else ""
@jinja2.pass_context
def stencil_path(ctx, value, pattern):
rendered_value = stencil(ctx, value, pattern)
if rendered_value == "":
self.counter += 1
return f"{STENCIL_PATH_PREFIX}{value}"
return rendered_value
environment.filters["stencil"] = stencil
environment.filters["stencil_path"] = stencil_path
super().__init__(environment)