Friendiiverse/Build.py

30 lines
867 B
Python
Raw Normal View History

2023-04-19 00:16:35 +02:00
#!/usr/bin/env python3
import os
from pathlib import Path
os.chdir(os.path.dirname(os.path.abspath(__file__)))
2023-04-19 00:16:35 +02:00
os.makedirs('./Dist', exist_ok=True)
2023-04-20 15:17:00 +02:00
os.chdir('./Source')
with open(f'./Friendiiverse.html', 'r') as Base:
2023-04-19 00:16:35 +02:00
Base = Base.read()
2023-04-20 15:17:00 +02:00
def FragReplace(Find, Replace, Pattern='*.*'):
2023-04-19 00:16:35 +02:00
global Base
for File in Path('./').rglob(Pattern):
2023-04-20 15:17:00 +02:00
File = str(File)
2023-04-19 00:16:35 +02:00
with open(File, 'r') as Frag:
2023-04-20 15:17:00 +02:00
Frag = Replace.format(File=File, Frag=Frag.read())
2023-04-19 00:16:35 +02:00
for Prefix in ('', './'):
2023-04-20 15:17:00 +02:00
File = Prefix + File
Base = Base.replace(Find.format(File=File), Frag)
FragReplace('<link rel="stylesheet" href="{File}"/>', '<style data-source="{File}">{Frag}</style>', '*.css')
FragReplace('<script src="{File}"></script>', '<script data-source="{File}">{Frag}</script>', '*.js')
2023-04-19 00:16:35 +02:00
2023-04-20 15:17:00 +02:00
os.chdir('..')
2023-04-19 00:16:35 +02:00
with open('./Dist/Friendiiverse.html', 'w') as Build:
Build.write(Base)