feat(html processor): add html processor to process template strings
This commit is contained in:
@@ -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/
|
||||
|
||||
@@ -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)
|
||||
@@ -201,6 +203,9 @@ class Builder:
|
||||
filename = file.name
|
||||
file_path = pathlib.Path.cwd().joinpath(operator_release_path, filename)
|
||||
|
||||
if filename == "index.html":
|
||||
self.html_processor.build(operator_name, file, file_path)
|
||||
else:
|
||||
shutil.copyfile(
|
||||
file,
|
||||
file_path
|
||||
|
||||
@@ -22,6 +22,7 @@ class Config:
|
||||
project_json=str,
|
||||
preview=str,
|
||||
use_skel=bool,
|
||||
title=str,
|
||||
)
|
||||
)
|
||||
self.__read_config()
|
||||
|
||||
25
lib/html_processor.py
Normal file
25
lib/html_processor.py
Normal 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
|
||||
@@ -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:]])
|
||||
|
||||
@@ -6,9 +6,9 @@
|
||||
<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="renderer" content="webkit">
|
||||
<title>Arknights Live 2D Wallpaper</title>
|
||||
<link rel="stylesheet" href="./assets/style.css">
|
||||
<link rel="stylesheet" href="./assets/spine-player.css">
|
||||
<title>{title}</title>
|
||||
<link rel="stylesheet" href="./assets/style.css?v={version}">
|
||||
<link rel="stylesheet" href="./assets/spine-player.css?v={version}">
|
||||
</head>
|
||||
|
||||
<body>
|
||||
@@ -17,11 +17,11 @@
|
||||
<div id="widget-wrapper" style="width: 100%; height: 100%;">
|
||||
<div id="container" style="width: 100%; height: 100%;"></div>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
<script type="text/javascript" src="./assets/spine-player.js"></script>
|
||||
<script type="text/javascript" src="./operator/operator.js"></script>
|
||||
<script type="text/javascript" src="./operator/settings.js"></script>
|
||||
<script type="text/javascript" src="./assets/runner.js"></script>
|
||||
|
||||
<script type="text/javascript" src="./assets/spine-player.js?v={version}"></script>
|
||||
<script type="text/javascript" src="./operator/operator.js?v={version}"></script>
|
||||
<script type="text/javascript" src="./operator/settings.js?v={version}"></script>
|
||||
<script type="text/javascript" src="./assets/runner.js?v={version}"></script>
|
||||
|
||||
</html>
|
||||
Reference in New Issue
Block a user