From be6dcbfb1ad846f3e0867457ea150434265aad9b Mon Sep 17 00:00:00 2001 From: Halyul Date: Tue, 1 Jun 2021 18:15:36 -0400 Subject: [PATCH] feat(html processor): add html processor to process template strings --- config.yaml | 3 ++- lib/builder.py | 17 +++++++++++------ lib/config.py | 1 + lib/html_processor.py | 25 +++++++++++++++++++++++++ lib/server.py | 20 ++++++++++++++++---- template/index.html | 42 +++++++++++++++++++++--------------------- 6 files changed, 76 insertions(+), 32 deletions(-) create mode 100644 lib/html_processor.py diff --git a/config.yaml b/config.yaml index b020768..788f37c 100644 --- a/config.yaml +++ b/config.yaml @@ -6,8 +6,9 @@ operators: release_folder: ./release/{name}/ source_folder: ./operator/{name}/extracted/ target_folder: ./operator/{name}/ + title: Skadi the Corrupting Heart use_skel: true server: operator_folder: /operator/ release_folder: ./release/ - template_folder: /template/ + template_folder: ./template/ diff --git a/lib/builder.py b/lib/builder.py index 966063a..ec82f8c 100644 --- a/lib/builder.py +++ b/lib/builder.py @@ -4,12 +4,14 @@ from lib.skeleton_binary import SkeletonBinary from lib.alpha_composite import AlphaComposite from lib.atlas_reader import AtlasReader from lib.base64_util import * +from lib.html_processor import HtmlProcessor class Builder: def __init__(self, config, operator_names=list()) -> None: self.operator_names = operator_names self.config = config + self.html_processor = HtmlProcessor(config) def start(self): if "all" in self.operator_names: @@ -175,7 +177,7 @@ class Builder: 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"])) 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: shutil.rmtree(operator_release_path) @@ -200,11 +202,14 @@ class Builder: if file.is_file() is True: filename = file.name file_path = pathlib.Path.cwd().joinpath(operator_release_path, filename) - - shutil.copyfile( - file, - file_path - ) + + if filename == "index.html": + self.html_processor.build(operator_name, file, file_path) + else: + shutil.copyfile( + file, + file_path + ) elif file.is_dir() is True: filename = file.name file_path = pathlib.Path.cwd().joinpath(operator_release_path, filename) diff --git a/lib/config.py b/lib/config.py index aa055c3..be6684f 100644 --- a/lib/config.py +++ b/lib/config.py @@ -22,6 +22,7 @@ class Config: project_json=str, preview=str, use_skel=bool, + title=str, ) ) self.__read_config() diff --git a/lib/html_processor.py b/lib/html_processor.py new file mode 100644 index 0000000..fd10aef --- /dev/null +++ b/lib/html_processor.py @@ -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 diff --git a/lib/server.py b/lib/server.py index 9aa937d..f53bf26 100644 --- a/lib/server.py +++ b/lib/server.py @@ -1,15 +1,16 @@ -from http.server import SimpleHTTPRequestHandler import pathlib +from http.server import SimpleHTTPRequestHandler from socketserver import TCPServer from lib.builder import Builder +from lib.html_processor import HtmlProcessor class Server: def __init__(self, port, operator, config) -> None: self.config = config self.operator = operator 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): # build assets first @@ -25,19 +26,30 @@ class Server: class httpd(SimpleHTTPRequestHandler): def __init__(self, operator, config, directory): - self.config = config + self.config = config["server"] self.operator = operator self.template_path = directory + self.html_processor = HtmlProcessor(config) def __call__(self, *args, **kwds): super().__init__(*args, directory=self.template_path, **kwds) def do_GET(self): + # ignore query string + if "?" in self.path: + self.path = self.path.split("?")[0] + split_path = self.path.split("/") access_path = "/{}/".format(split_path[1]) 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/": # assets folder self.path = self.config["template_folder"] + "assets/" + "/".join([i for i in split_path[2:]]) diff --git a/template/index.html b/template/index.html index 9beb3ee..a209ca5 100644 --- a/template/index.html +++ b/template/index.html @@ -1,27 +1,27 @@ - - - - - - Arknights Live 2D Wallpaper - - - + + + + + + {title} + + + - -
- -
-
-
- - - - - - + +
+ +
+
+
+ + + + + + \ No newline at end of file