35 lines
1.0 KiB
Python
35 lines
1.0 KiB
Python
import os
|
|
|
|
|
|
TEMPLATE_DIR = os.path.abspath("TempLate")
|
|
BASE_PATH = os.path.join(TEMPLATE_DIR, "_Base")
|
|
FOOTER_PATH = os.path.join(TEMPLATE_DIR, "_Footer")
|
|
|
|
|
|
def openr(filepath):
|
|
try:
|
|
with open(filepath, 'r', encoding='utf-8') as f:
|
|
return f.read()
|
|
except Exception:
|
|
return ''
|
|
|
|
|
|
def ProcessTemplate(HtmlFile, **k):
|
|
html = openr(os.path.join(TEMPLATE_DIR, HtmlFile))
|
|
_Base = openr(BASE_PATH)
|
|
_Footer = openr(FOOTER_PATH)
|
|
h = html
|
|
if not ("DefaultUse" in k and k["DefaultUse"] == False) or html.count("{{ UseBase }}") == 1:
|
|
if html.endswith("{{ NoFooter }}"):
|
|
h = _Base.replace("{{ MAIN }}", html).replace("{{ Footer }}", "").replace("{{ NoFooter }}", "")
|
|
else:
|
|
h = _Base.replace("{{ MAIN }}", html).replace("{{ Footer }}", _Footer)
|
|
for i in k:
|
|
h = h.replace("{{ %s }}" % i, str(k[i]))
|
|
h = h.replace("{{ UseBase }}", "")
|
|
return h
|
|
|
|
|
|
def ProcessTemplateStr(HtmlStr, **k):
|
|
return ProcessTemplate("import(_Nav)\n%s\nimport(_Footer)" % HtmlStr, **k)
|