-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathextensions.py
97 lines (77 loc) · 3.27 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
from __future__ import annotations
import re
import subprocess
import unicodedata
from datetime import date
from copier_templates_extensions import ContextHook
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
class GitHubIDsforGiscusExtension(ContextHook):
repo_placeholder = "REPLACE WITH REPOSITORY NODE ID"
category_placeholder = "REPLACE WITH DISCUSSION CATEGORY ID"
update = False
query = """
{
repository(owner: "%(owner)s", name: "%(name)s") {
discussionCategories(first: 100) {
nodes {
id
name
}
}
}
}
"""
repo_id: str | None = None
category_id: str | None = None
def hook(self, context):
repository_namespace = context["repository_namespace"]
repository_name = context["repository_name"]
if self.repo_id is None:
command = f"gh api repos/{repository_namespace}/{repository_name} --jq .node_id"
try:
process = subprocess.run(
command, shell=True, check=True, text=True, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL
)
except subprocess.CalledProcessError:
self.repo_id = self.repo_placeholder
else:
self.repo_id = process.stdout.strip() or self.repo_placeholder
context["giscus_repo_id"] = self.repo_id
if self.category_id is None:
jq_filter = (
"--jq '.data.repository.discussionCategories.nodes[] | select(.name == \"Documentation\") | .id'"
)
command = f"gh api graphql -f query='{self.query}' {jq_filter}" % {
"owner": repository_namespace,
"name": repository_name,
}
try:
process = subprocess.run(
command, shell=True, check=True, text=True, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL
)
except subprocess.CalledProcessError:
self.category_id = self.category_placeholder
else:
self.category_id = process.stdout.strip() or self.category_placeholder
context["giscus_discussion_category_id"] = self.category_id