Files
dpl/scripts/theme-fuc/vendor-native.py
T

57 lines
3.9 KiB
Python

#!/usr/bin/env python3
"""Import only original FUC static CSS/media dependency closure; never load site config."""
from pathlib import Path
from shutil import copyfile
from urllib.parse import urlparse, unquote
import re, json, posixpath
SOURCE=Path('/Users/hellcat/Documents/GemCodes/fuc-new.loc/public')
OUT=Path(__file__).resolve().parents[2]/'theme/Fuc/assets/native'
css=['theme/cool/assets/vendors/css/vendors.min.css','theme/cool/assets/css/color.css','theme/cool/assets/css/bootstrap.css','theme/cool/assets/css/bootstrap-extended.css','theme/cool/assets/css/cool.css','theme/cool/assets/css/theme/style-purple.css','theme/cool/assets/css/theme/dark-layout.css','theme/cool/assets/css/auth/page-auth.css','theme/cool/assets/css/plugins/chart-apex.css','assets/cool/all.min.css','assets/cool/bootstrap.min.css','assets/cool/swiper.min.css','assets/cool/style.css','assets/cool/bootstrap.min_002.css']
css += ['assets/css/fuc-brand.css','theme/cool/assets/css/theme/style-cyan.css','theme/cool/assets/css/theme/page/shop.css','theme/cool/assets/css/plugins/datatables.bootstrap4.min.css']
styles=json.loads((OUT/'styles.json').read_text())
css=list(dict.fromkeys(css+[urlparse(v).path for values in styles.values() for v in values]))
queue=list(css)+['images/brand/favicon-32.png','images/brand/apple-touch-icon.png','assets/cool/swiper.min.js','theme/cool/assets/vendors/js/charts/apexcharts.min.js'];seen=set();missing=[]
# Original HTML image dependencies, plus CSS transitive dependencies.
for name in [p.name for p in OUT.glob('*.html')]:
p=OUT/name
if p.exists():
for v in re.findall(r'(?:src|poster)=[\'\"]([^\'\"]+)',p.read_text()):
if '__' in v:continue
if v.startswith('/') or v.startswith(('assets/cool/','../theme/')):
queue.append(v.removeprefix('../').lstrip('/'))
for v in re.findall(r'url\((?:[\'\"]?)(/[^)\'\"]+)',p.read_text()):queue.append(v.removeprefix('../').lstrip('/'))
while queue:
rel=unquote(urlparse(queue.pop(0)).path).lstrip('/')
if rel in seen: continue
seen.add(rel);src=(SOURCE/rel).resolve()
if not src.is_relative_to(SOURCE): missing.append(rel);continue
if not src.is_file():
if (OUT/rel).is_file():continue
missing.append(rel);continue
dst=OUT/rel;dst.parent.mkdir(parents=True,exist_ok=True)
if src.suffix=='.css':
s=src.read_text();s=re.sub(r'@import\s+url\([\'\"]?https?[^;]+;', '@import url("local-fonts.css");' if (OUT/'assets/cool/local-fonts.css').exists() and rel=='assets/cool/style.css' else '',s)
# Preserve CSS semantics while keeping root-relative assets inside the theme.
s=re.sub(r"url\(([\'\"]?)(/[^)\'\"]+)\1\)",lambda m: "url("+m[1]+posixpath.relpath(m[2].lstrip('/'),str(Path(rel).parent))+m[1]+")",s)
dst.write_text(s)
for u in re.findall(r'url\([\'\"]?([^\)\'\"]+)',s):
if u.startswith(('data:','http:','https:','//','#')):continue
raw=unquote(u.split('?')[0].split('#')[0]);dep=((SOURCE/raw.lstrip('/')) if raw.startswith('/') else (src.parent/raw)).resolve()
if dep.is_relative_to(SOURCE):queue.append(str(dep.relative_to(SOURCE)))
else:copyfile(src,dst)
manifest={'source':str(SOURCE),'css':css,'files':sorted(seen),'missing':sorted(set(missing)),'remote_imports_removed':True}
(OUT/'manifest.json').write_text(json.dumps(manifest,ensure_ascii=False,indent=2)+'\n')
print(f'{len(seen)-len(missing)} original static dependencies imported; {len(set(missing))} unresolved')
print('\n'.join(sorted(set(missing))[:12]))
# Isolate the unchanged Pace IIFE; do not execute the other bundled plugins.
vendor=(SOURCE/'theme/cool/assets/vendors/js/vendors.min.js').read_text()
a=vendor.index('/*! pace 1.0.2 */');b=vendor.index('}).call(this);',a)+len('}).call(this);')
(OUT/'pace.min.js').write_text(vendor[a:b]+'\n')
styles=(SOURCE/'theme/cool/assets/vendors/css/vendors.min.css').read_text()
pace_rules=[]
for rule in styles.split('}'):
selector=rule.split('{',1)[0]
if '.pace' in selector:pace_rules.append(rule.strip()+'}')
(OUT/'pace.css').write_text('\n'.join(pace_rules)+'\n')