feat(html processor): add html processor to process template strings

This commit is contained in:
Halyul
2021-06-01 18:15:36 -04:00
parent 19bf16c719
commit be6dcbfb1a
6 changed files with 76 additions and 32 deletions

View File

@@ -6,8 +6,9 @@ operators:
release_folder: ./release/{name}/ release_folder: ./release/{name}/
source_folder: ./operator/{name}/extracted/ source_folder: ./operator/{name}/extracted/
target_folder: ./operator/{name}/ target_folder: ./operator/{name}/
title: Skadi the Corrupting Heart
use_skel: true use_skel: true
server: server:
operator_folder: /operator/ operator_folder: /operator/
release_folder: ./release/ release_folder: ./release/
template_folder: /template/ template_folder: ./template/

View File

@@ -4,12 +4,14 @@ from lib.skeleton_binary import SkeletonBinary
from lib.alpha_composite import AlphaComposite from lib.alpha_composite import AlphaComposite
from lib.atlas_reader import AtlasReader from lib.atlas_reader import AtlasReader
from lib.base64_util import * from lib.base64_util import *
from lib.html_processor import HtmlProcessor
class Builder: class Builder:
def __init__(self, config, operator_names=list()) -> None: def __init__(self, config, operator_names=list()) -> None:
self.operator_names = operator_names self.operator_names = operator_names
self.config = config self.config = config
self.html_processor = HtmlProcessor(config)
def start(self): def start(self):
if "all" in self.operator_names: if "all" in self.operator_names:
@@ -175,7 +177,7 @@ class Builder:
operator_release_path = pathlib.Path.cwd().joinpath(target_path, operator_name) operator_release_path = pathlib.Path.cwd().joinpath(target_path, operator_name)
release_operator_assets_path = pathlib.Path.cwd().joinpath(operator_release_path, ".{}".format(self.config["server"]["operator_folder"])) release_operator_assets_path = pathlib.Path.cwd().joinpath(operator_release_path, ".{}".format(self.config["server"]["operator_folder"]))
operator_assets_path = pathlib.Path.cwd().joinpath(self.config["operators"][operator_name]["target_folder"].format(name=operator_name)) operator_assets_path = pathlib.Path.cwd().joinpath(self.config["operators"][operator_name]["target_folder"].format(name=operator_name))
template_path = pathlib.Path.cwd().joinpath(".{}".format(self.config["server"]["template_folder"])) template_path = pathlib.Path.cwd().joinpath(self.config["server"]["template_folder"])
if operator_release_path.exists() is True: if operator_release_path.exists() is True:
shutil.rmtree(operator_release_path) shutil.rmtree(operator_release_path)
@@ -200,11 +202,14 @@ class Builder:
if file.is_file() is True: if file.is_file() is True:
filename = file.name filename = file.name
file_path = pathlib.Path.cwd().joinpath(operator_release_path, filename) file_path = pathlib.Path.cwd().joinpath(operator_release_path, filename)
shutil.copyfile( if filename == "index.html":
file, self.html_processor.build(operator_name, file, file_path)
file_path else:
) shutil.copyfile(
file,
file_path
)
elif file.is_dir() is True: elif file.is_dir() is True:
filename = file.name filename = file.name
file_path = pathlib.Path.cwd().joinpath(operator_release_path, filename) file_path = pathlib.Path.cwd().joinpath(operator_release_path, filename)

View File

@@ -22,6 +22,7 @@ class Config:
project_json=str, project_json=str,
preview=str, preview=str,
use_skel=bool, use_skel=bool,
title=str,
) )
) )
self.__read_config() self.__read_config()

25
lib/html_processor.py Normal file
View File

@@ -0,0 +1,25 @@
import pathlib
class HtmlProcessor:
def __init__(self, config):
self.config = config
pass
def process(self, operator, file_path):
with open(pathlib.Path.cwd().joinpath(file_path), "r") as f:
content = f.read()
return content.format(
title=self.config["operators"][operator]["title"],
version=self.__get_version()
)
def build(self, operator, source_path, target_path):
content = self.process(operator, source_path)
with open(pathlib.Path.cwd().joinpath(target_path), "w") as f:
f.write(content)
def __get_version(self):
with open(pathlib.Path.cwd().joinpath("Version"), "r") as f:
version = f.read()
return version

View File

@@ -1,15 +1,16 @@
from http.server import SimpleHTTPRequestHandler
import pathlib import pathlib
from http.server import SimpleHTTPRequestHandler
from socketserver import TCPServer from socketserver import TCPServer
from lib.builder import Builder from lib.builder import Builder
from lib.html_processor import HtmlProcessor
class Server: class Server:
def __init__(self, port, operator, config) -> None: def __init__(self, port, operator, config) -> None:
self.config = config self.config = config
self.operator = operator self.operator = operator
self.port = port self.port = port
self.httpd = TCPServer(("", port), httpd(operator, config["server"], directory=str(pathlib.Path.cwd()))) self.httpd = TCPServer(("", port), httpd(operator, config, directory=str(pathlib.Path.cwd())))
def start(self): def start(self):
# build assets first # build assets first
@@ -25,19 +26,30 @@ class Server:
class httpd(SimpleHTTPRequestHandler): class httpd(SimpleHTTPRequestHandler):
def __init__(self, operator, config, directory): def __init__(self, operator, config, directory):
self.config = config self.config = config["server"]
self.operator = operator self.operator = operator
self.template_path = directory self.template_path = directory
self.html_processor = HtmlProcessor(config)
def __call__(self, *args, **kwds): def __call__(self, *args, **kwds):
super().__init__(*args, directory=self.template_path, **kwds) super().__init__(*args, directory=self.template_path, **kwds)
def do_GET(self): def do_GET(self):
# ignore query string
if "?" in self.path:
self.path = self.path.split("?")[0]
split_path = self.path.split("/") split_path = self.path.split("/")
access_path = "/{}/".format(split_path[1]) access_path = "/{}/".format(split_path[1])
if self.path == "/": if self.path == "/":
self.path = self.config["template_folder"] + "index.html" # self.path = self.config["template_folder"] + "index.html"
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
html = self.html_processor.process(self.operator, self.config["template_folder"] + "index.html")
self.wfile.write(bytes(html, "utf8"))
return
elif access_path == "/assets/": elif access_path == "/assets/":
# assets folder # assets folder
self.path = self.config["template_folder"] + "assets/" + "/".join([i for i in split_path[2:]]) self.path = self.config["template_folder"] + "assets/" + "/".join([i for i in split_path[2:]])

View File

@@ -1,27 +1,27 @@
<!doctype html> <!doctype html>
<html> <html>
<head> <head>
<meta charset="utf-8"> <meta charset="utf-8">
<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1,maximum-scale=1,user-scalable=no"> <meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1,maximum-scale=1,user-scalable=no">
<meta name="apple-mobile-web-app-status-bar-style" content="black"> <meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="renderer" content="webkit"> <meta name="renderer" content="webkit">
<title>Arknights Live 2D Wallpaper</title> <title>{title}</title>
<link rel="stylesheet" href="./assets/style.css"> <link rel="stylesheet" href="./assets/style.css?v={version}">
<link rel="stylesheet" href="./assets/spine-player.css"> <link rel="stylesheet" href="./assets/spine-player.css?v={version}">
</head> </head>
<body> <body>
<div id="dev" style="position: fixed;left: 0;left: 0;background-color: white;"></div> <div id="dev" style="position: fixed;left: 0;left: 0;background-color: white;"></div>
<image src="./operator/operator_logo.png" class="logo" id="logo" alt="operator logo" /> <image src="./operator/operator_logo.png" class="logo" id="logo" alt="operator logo" />
<div id="widget-wrapper" style="width: 100%; height: 100%;"> <div id="widget-wrapper" style="width: 100%; height: 100%;">
<div id="container" style="width: 100%; height: 100%;"></div> <div id="container" style="width: 100%; height: 100%;"></div>
</div> </div>
</body>
</body>
<script type="text/javascript" src="./assets/spine-player.js"></script> <script type="text/javascript" src="./assets/spine-player.js?v={version}"></script>
<script type="text/javascript" src="./operator/operator.js"></script> <script type="text/javascript" src="./operator/operator.js?v={version}"></script>
<script type="text/javascript" src="./operator/settings.js"></script> <script type="text/javascript" src="./operator/settings.js?v={version}"></script>
<script type="text/javascript" src="./assets/runner.js"></script> <script type="text/javascript" src="./assets/runner.js?v={version}"></script>
</html> </html>