forked from john-bampton/john-bampton.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender.py
More file actions
169 lines (134 loc) · 5.21 KB
/
Copy pathrender.py
File metadata and controls
169 lines (134 loc) · 5.21 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/env python3
import json
import logging
import os
import re
from typing import Any, Dict, List
from jinja2 import Environment, FileSystemLoader
SITE_DIR = "./docs"
CACHE_FILE = os.path.join(SITE_DIR, "users.json")
LAYOUTS_DIR = "./layouts"
jinja_env = Environment(loader=FileSystemLoader(LAYOUTS_DIR))
def setup_logger() -> logging.Logger:
"""Initialize and configure logger for HTML rendering."""
logger = logging.getLogger("GithubFaces.HTML")
logger.setLevel(logging.INFO)
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
formatter = logging.Formatter(
"[%(asctime)s] %(levelname)s: %(message)s", "%Y-%m-%d %H:%M:%S"
)
ch.setFormatter(formatter)
if not logger.handlers:
logger.addHandler(ch)
return logger
logger = setup_logger()
def ensure_dir(path: str) -> None:
"""Create directory if it doesn't exist."""
if not os.path.exists(path):
os.makedirs(path)
logger.info("Created directory: %s", path)
def format_number(num: Any) -> str:
"""Format number with comma separators or return 'N/A' for invalid values."""
if num == "N/A" or num is None:
return "N/A"
try:
return f"{int(num):,}"
except (ValueError, TypeError):
return str(num)
def prepare_users(users: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
"""Add formatted display fields to user data for template rendering."""
prepared = []
for user in users:
followers = user.get("followers", "N/A")
following = user.get("following", "N/A")
public_repos = user.get("public_repos", "N/A")
public_gists = user.get("public_gists", "N/A")
sponsors_count = user.get("sponsors_count", "N/A")
sponsoring_count = user.get("sponsoring_count", "N/A")
prepared.append(
{
**user,
"followers_display": format_number(followers),
"following_display": format_number(following),
"repos_display": format_number(public_repos),
"gists_display": format_number(public_gists),
"sponsors_display": format_number(sponsors_count),
"sponsoring_display": format_number(sponsoring_count),
}
)
return prepared
def load_cache(cache_file: str = CACHE_FILE) -> List[Dict[str, Any]]:
"""Load user data from JSON cache file."""
if not os.path.exists(cache_file):
logger.error("Cache file not found: %s", cache_file)
logger.error("Please run fetch_users.py first to fetch and cache user data.")
return []
try:
with open(cache_file, "r", encoding="utf-8") as f:
users = json.load(f)
logger.info("Loaded %d users from cache", len(users))
return users
except Exception as e:
logger.error("Failed to load cache: %s", e)
return []
def build_html() -> str:
"""Build the HTML layout using Jinja2 templates."""
layout_template = jinja_env.get_template("layout.html")
layout = layout_template.render()
return layout
def minify_html(html: str) -> str:
"""Aggressive HTML minifier: remove comments, collapse whitespace, minify inline code."""
html = html.replace("\r", "")
html = html.replace("\n", "\n")
html = re.sub(r"\n+", " ", html)
html = re.sub(r"<!--[\s\S]*?-->", "", html)
html = re.sub(r">\s+<", "><", html)
html = re.sub(r"\s{2,}", " ", html)
html = re.sub(
r"(?is)<script\b[^>]*>(.*?)</script\s*>",
lambda m: "<script>" + minify_js(m.group(1)) + "</script>",
html,
)
html = re.sub(
r"(?is)<style\b[^>]*>(.*?)</style\s*>",
lambda m: "<style>" + minify_css(m.group(1)) + "</style>",
html,
)
return html.strip()
def minify_js(code: str) -> str:
"""Minify inline JavaScript: remove comments, unnecessary whitespace."""
code = re.sub(r"//(?!.*:).*?$", "", code, flags=re.MULTILINE)
code = re.sub(r"/\*[\s\S]*?\*/", "", code)
code = re.sub(r"\s+", " ", code)
code = re.sub(r"\s*([{}();,])\s*", r"\1", code)
code = re.sub(r"\s+", " ", code)
return code.strip()
def minify_css(code: str) -> str:
"""Minify inline CSS: remove comments, collapse whitespace, remove unnecessary spaces."""
code = re.sub(r"/\*[\s\S]*?\*/", "", code)
code = re.sub(r"\s+", " ", code)
code = re.sub(r"\s*([{}:;,>+~])\s*", r"\1", code)
return code.strip()
def run() -> None:
"""Main entry point: load cache, export JSON, and generate minified HTML shell."""
ensure_dir(SITE_DIR)
logger.info("Loading user data from cache...")
users = load_cache()
if not users:
logger.error("No users found in cache. Please run fetch_users.py first.")
return
logger.info("Building HTML shell (header + footer + empty grid)...")
html_content = minify_html(build_html())
try:
output_file = os.path.join(SITE_DIR, "index.html")
with open(output_file, "w", encoding="utf-8") as f:
f.write(html_content)
logger.info(
"HTML shell saved successfully. Total users available: %d",
len(users),
)
except Exception as e:
logger.error("Failed to save HTML page: %s", e)
if __name__ == "__main__":
run()