-
Notifications
You must be signed in to change notification settings - Fork 18
/
extensions.py
39 lines (26 loc) · 1.13 KB
/
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
36
37
38
39
import re
import subprocess
import unicodedata
from datetime import date
from jinja2.ext import Extension
def git_user_name(default: str) -> str:
return subprocess.getoutput("git config user.name").strip() or default
def git_user_email(default: str) -> str:
return subprocess.getoutput("git config user.email").strip() or default
def slugify(value, separator="-"):
value = unicodedata.normalize("NFKD", str(value)).encode("ascii", "ignore").decode("ascii")
value = re.sub(r"[^\w\s-]", "", value.lower())
return re.sub(r"[-_\s]+", separator, value).strip("-_")
class GitExtension(Extension):
def __init__(self, environment):
super().__init__(environment)
environment.filters["git_user_name"] = git_user_name
environment.filters["git_user_email"] = git_user_email
class SlugifyExtension(Extension):
def __init__(self, environment):
super().__init__(environment)
environment.filters["slugify"] = slugify
class CurrentYearExtension(Extension):
def __init__(self, environment):
super().__init__(environment)
environment.globals["current_year"] = date.today().year