simplify process templates script
Python provides a high-level path object with `pathlib`[^1], which is included in the standard library[^2]. This makes it simpler to find all html files to process. [^1]: https://docs.python.org/3/library/pathlib.html#module-pathlib [^2]: https://peps.python.org/pep-0428/
This commit is contained in:
parent
9435589e44
commit
968167ad52
@ -1,22 +1,18 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
from jinja2 import FileSystemLoader, Environment
|
from jinja2 import FileSystemLoader, Environment
|
||||||
|
from pathlib import Path
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
ROOT_DIR = sys.argv[1]
|
ROOT_DIR = Path(sys.argv[1])
|
||||||
TEMPLATE_PATH_LIST = [ROOT_DIR, "templates/"]
|
TEMPLATE_PATH_LIST = [ROOT_DIR, "templates/"]
|
||||||
|
|
||||||
loader = FileSystemLoader(searchpath=TEMPLATE_PATH_LIST)
|
loader = FileSystemLoader(searchpath=TEMPLATE_PATH_LIST)
|
||||||
environment = Environment(loader=loader, autoescape=True)
|
environment = Environment(loader=loader, autoescape=True)
|
||||||
|
|
||||||
template_file_list = []
|
file_path_list = ROOT_DIR.glob("**/*.html")
|
||||||
for dirpath, dirnames, filenames in os.walk(ROOT_DIR):
|
template_file_list = [os.fspath(p.relative_to(ROOT_DIR)) for p in file_path_list]
|
||||||
for filename in filenames:
|
|
||||||
if filename.endswith(".html"):
|
|
||||||
template_file_list.append(
|
|
||||||
(os.path.join(dirpath, filename)).split(sep=os.path.sep, maxsplit=1)[1]
|
|
||||||
)
|
|
||||||
|
|
||||||
for template_file in template_file_list:
|
for template_file in template_file_list:
|
||||||
template = environment.get_template(template_file)
|
template = environment.get_template(template_file)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user