new fonts
This commit is contained in:
@@ -3,7 +3,7 @@ from __future__ import annotations
|
||||
|
||||
import re
|
||||
from pathlib import Path
|
||||
from urllib.parse import urljoin
|
||||
from urllib.parse import urljoin, urlparse
|
||||
from urllib.request import Request, urlopen
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
@@ -14,6 +14,33 @@ FONTAWESOME_VERSION = "6.5.2"
|
||||
FLAG_ICONS_VERSION = "7.2.3"
|
||||
SWAGGER_UI_VERSION = "5"
|
||||
SOCKET_IO_VERSION = "4.7.5"
|
||||
GOOGLE_FONT_FAMILIES = (
|
||||
"DM Sans",
|
||||
"Figtree",
|
||||
"Geist",
|
||||
"IBM Plex Sans",
|
||||
"Inter",
|
||||
"JetBrains Mono",
|
||||
"Lato",
|
||||
"Manrope",
|
||||
"Montserrat",
|
||||
"Nunito Sans",
|
||||
"Open Sans",
|
||||
"Poppins",
|
||||
"Roboto",
|
||||
"Source Sans 3",
|
||||
)
|
||||
GOOGLE_FONT_WEIGHTS = "400;500;600;700;800"
|
||||
|
||||
|
||||
def google_fonts_css_url() -> str:
|
||||
families = "&".join(
|
||||
f"family={name.replace(' ', '+')}:wght@{GOOGLE_FONT_WEIGHTS}"
|
||||
for name in GOOGLE_FONT_FAMILIES
|
||||
)
|
||||
return f"https://fonts.googleapis.com/css2?{families}&display=swap"
|
||||
|
||||
|
||||
BOOTSTRAP_THEMES = (
|
||||
"default",
|
||||
"flatly",
|
||||
@@ -43,6 +70,10 @@ STATIC_ASSETS = {
|
||||
"local": f"{LIBS_STATIC_DIR}/flag-icons/{FLAG_ICONS_VERSION}/css/flag-icons.min.css",
|
||||
"cdn": f"https://cdn.jsdelivr.net/gh/lipis/flag-icons@{FLAG_ICONS_VERSION}/css/flag-icons.min.css",
|
||||
},
|
||||
"font_css": {
|
||||
"local": f"{LIBS_STATIC_DIR}/fonts/google-fonts.css",
|
||||
"cdn": google_fonts_css_url(),
|
||||
},
|
||||
"swagger_css": {
|
||||
"local": f"{LIBS_STATIC_DIR}/swagger-ui/{SWAGGER_UI_VERSION}/swagger-ui.css",
|
||||
"cdn": f"https://cdn.jsdelivr.net/npm/swagger-ui-dist@{SWAGGER_UI_VERSION}/swagger-ui.css",
|
||||
@@ -53,6 +84,7 @@ STATIC_ASSETS = {
|
||||
},
|
||||
}
|
||||
URL_RE = re.compile(r"url\((['\"]?)(?!data:)(?!https?:)([^)'\"]+)\1\)")
|
||||
ANY_URL_RE = re.compile(r"url\((['\"]?)(?!data:)([^)'\"]+)\1\)")
|
||||
|
||||
|
||||
def bootstrap_css_asset(theme: str) -> dict[str, str]:
|
||||
@@ -97,13 +129,49 @@ def download_css_with_assets(url: str, dest: Path) -> None:
|
||||
download(asset_url, asset_dest)
|
||||
|
||||
|
||||
def download_google_fonts_css(url: str, dest: Path) -> None:
|
||||
dest.parent.mkdir(parents=True, exist_ok=True)
|
||||
req = Request(
|
||||
url,
|
||||
headers={
|
||||
"User-Agent": "Mozilla/5.0 pyTorrent installer",
|
||||
"Accept": "text/css,*/*;q=0.1",
|
||||
},
|
||||
)
|
||||
with urlopen(req, timeout=60) as response:
|
||||
css = response.read().decode("utf-8", errors="ignore")
|
||||
if not css.strip():
|
||||
raise RuntimeError(f"Empty response for {url}")
|
||||
|
||||
def replace_url(match: re.Match[str]) -> str:
|
||||
quote = match.group(1) or ""
|
||||
asset_url = match.group(2)
|
||||
parsed = urlparse(asset_url)
|
||||
if parsed.scheme not in {"http", "https"}:
|
||||
return match.group(0)
|
||||
filename = Path(parsed.path).name
|
||||
if not filename:
|
||||
return match.group(0)
|
||||
asset_dest = dest.parent / "files" / filename
|
||||
if not asset_dest.exists():
|
||||
download(asset_url, asset_dest)
|
||||
return f"url({quote}files/{filename}{quote})"
|
||||
|
||||
rewritten = ANY_URL_RE.sub(replace_url, css)
|
||||
tmp = dest.with_suffix(dest.suffix + ".tmp")
|
||||
tmp.write_text(rewritten, encoding="utf-8")
|
||||
tmp.replace(dest)
|
||||
print(f"OK {dest.relative_to(ROOT)}")
|
||||
|
||||
def main() -> None:
|
||||
items = list(STATIC_ASSETS.values())
|
||||
items.extend(bootstrap_css_asset(theme) for theme in BOOTSTRAP_THEMES)
|
||||
for item in items:
|
||||
url = item["cdn"]
|
||||
dest = ROOT / "pytorrent" / "static" / item["local"]
|
||||
if dest.suffix == ".css":
|
||||
if item.get("local") == STATIC_ASSETS["font_css"]["local"]:
|
||||
download_google_fonts_css(url, dest)
|
||||
elif dest.suffix == ".css":
|
||||
download_css_with_assets(url, dest)
|
||||
else:
|
||||
download(url, dest)
|
||||
|
||||
Reference in New Issue
Block a user