-
Notifications
You must be signed in to change notification settings - Fork 0
/
copier_extensions.py
56 lines (42 loc) · 1.67 KB
/
copier_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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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 subprocess.getoutput("git config --global user.name").strip()
or default
)
def git_user_email(default: str="") -> str:
return (
subprocess.getoutput("git config user.email").strip()
or subprocess.getoutput("git config --global user.email").strip()
or default
)
def git_to_https(url: str) -> str:
return (
url.replace('.git', '')
.replace(':', '/')
.replace('git@', 'https://')
)
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.globals["git_user_name"] = git_user_name
environment.globals["git_user_email"] = git_user_email
environment.filters["git_to_https"] = git_to_https
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().strftime('%Y')
environment.globals["current_day"] = date.today().strftime('%Y-%m-%d')